{{/* vim: set filetype=mustache: */}}

{{/*
Create the name of the pod name to use in prometheus queries
*/}}
{{- define "defaultTemplates.podname" -}}
{{ .Values.rolloutStrategy.deploymentName }}
{{- end -}}

{{/*
Define the address of the prometheus server to be used in the default AnalysisTemplates
*/}}
{{- define "defaultTemplates.basicTemplates.promServerAdress" -}}
{{ .Values.rolloutAnalysisTemplates.defaultTemplates.basicTemplates.promServerAddress }}
{{- end -}}

{{- define "promAuthSecretName" -}}
{{- include "defaultTemplates.podname" . }}-prom-auth
{{- end -}}

{{- define "defaultTemplates.basicTemplates.prometheusProvider" -}}
prometheus:
{{- if .Values.rolloutAnalysisTemplates.authToken }}
  headers:
  - key: Authorization
    value: {{ "Bearer {{" }} {{ "args.prom-auth-token" }} {{ "}}" }}
{{- end }}
  address: {{ include "defaultTemplates.basicTemplates.promServerAdress" . }}
{{- end -}}

{{- define "memoryUsageTemplate.name" -}}jfrog-memory-usage-analysis{{- end -}}

{{- define "memoryUsageTemplate.provider" -}}
{{ include "defaultTemplates.basicTemplates.prometheusProvider" . }}
  query: {{ include "memoryUsageTemplate.query" . | quote }}
{{- end -}}

{{- define "cpuUsageTemplate.name" -}}jfrog-cpu-usage-analysis{{- end -}}

{{- define "cpuUsageTemplate.provider" -}}
{{ include "defaultTemplates.basicTemplates.prometheusProvider" . }}
  query: {{ include "cpuUsageTemplate.query" . | quote }}
{{- end -}}

{{/*
Define the prometheus queries to be used in the default AnalysisTemplates
*/}}
{{- define "memoryUsageTemplate.query" -}}
avg( sum by (namespace, pod) (container_memory_working_set_bytes{container!="POD",container!="",pod=~"{{ include "defaultTemplates.podname" . }}.*"}) and on(namespace, pod) max by (namespace, pod) (kube_pod_labels{label_role="canary"}) ) / avg( sum by (namespace, pod) (container_memory_working_set_bytes{container!="POD",container!="",pod=~"{{ include "defaultTemplates.podname" . }}.*"}) and on(namespace, pod) max by (namespace, pod) (kube_pod_labels{label_role="stable"}) )
{{- end -}}
{{- define "cpuUsageTemplate.query" -}}
avg( sum by (namespace, pod) (node_namespace_pod_container:container_cpu_usage_seconds_total:sum_irate{container!="POD",container!="",pod=~"{{ include "defaultTemplates.podname" . }}.*"}) and on(namespace, pod) max by (namespace, pod) (kube_pod_labels{label_role="canary"}) ) / avg( sum by (namespace, pod) (node_namespace_pod_container:container_cpu_usage_seconds_total:sum_irate{container!="POD",container!="",pod=~"{{ include "defaultTemplates.podname" . }}.*"}) and on(namespace, pod) max by (namespace, pod) (kube_pod_labels{label_role="stable"}) )
{{- end -}}

{{- define "canaryJobTemplateName" -}}
{{ .Release.Name }}-canary-job
{{- end -}}

{{- define "canarySecretName" -}}
{{ .Release.Name }}-canary-secret
{{- end -}}

{{- define "renderCanarySteps" -}}
{{- $root := . -}}
{{- $steps := .Values.rolloutStrategy.canary.steps -}}
{{- $canary_region := .Values.canaryRegion -}}
{{- $canary_service_name := .Values.canaryServiceName -}}
{{- range $i, $step := $steps }}
  {{- if $step.setAccountWeight }}
    {{- $account_type := $step.setAccountWeight.type -}}
    {{- $weight := $step.setAccountWeight.weight -}}
    {{- $interval := (or $step.setAccountWeight.interval 10) -}}
    {{- $timeout := (or $step.setAccountWeight.timeout 7200) -}}
    {{- if not (and $account_type $weight) }}
      {{- fail (printf "Missing required fields in setAccountWeight step at index %d, required fields: [account_type, weight]" $i) }}
    {{- end }}
    {{- if not (or (eq (int $weight) 20) (eq (int $weight) 40) (eq (int $weight) 100)) }}
      {{- fail (printf "Invalid weight value '%v' in canary-request step at index %d, allowed values: [20, 40, 100]" $weight $i) }}
    {{- end }}
    {{- if not (and $canary_service_name $canary_region) }}
      {{- fail "Missing required values: canary_service_name and/or canary_region" }}
    {{- end }}
- analysis:
    args:
      - name: service
        value: {{ $canary_service_name | quote }}
      - name: account_type
        value: {{ $account_type | quote }}
      - name: weight
        value: {{ $weight | quote }}
      - name: region
        value: {{ $canary_region | quote }}
      - name: interval
        value: {{ $interval | quote }}
      - name: timeout
        value: {{ $timeout | quote }}
    templates:
      - templateName: {{ include "canaryJobTemplateName" $root }}
  {{- else }}
- {{ toYaml $step | nindent 2 | trim }}
  {{- end }}
{{- end }}
{{- end -}}

{{/*
Generate image pull secret name from registry URL
Extracts registry from image URL (e.g., "x.jfrog.io/path:tag" -> "x.jfrog.io")
Converts to pull secret format (e.g., "x.jfrog.io" -> "x-jfrog-io-pull-secret")
*/}}
{{- define "jfrog-argo-rollouts.registryPullSecretName" -}}
{{- $image := .Values.canaryRequestJobImage -}}
{{- $parts := splitList "/" $image -}}
{{- $registry := (index $parts 0) -}}
{{- $secretName := ($registry | replace "." "-") -}}
{{- printf "%s-pull-secret" $secretName -}}
{{- end -}}
