{{- if and (.Values.rolloutStrategy) (.Values.rolloutStrategy.enabled) }}
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
  name: {{ .Values.rolloutStrategy.deploymentName }}
{{- if and (.Values.rolloutStrategy.notifications.enabled) (.Values.rolloutStrategy.notifications.slackChannels) }}
  annotations:
{{- range $trigger := .Values.rolloutStrategy.notifications.triggers }}
    notifications.argoproj.io/subscribe.{{ $trigger }}.slack: {{ $.Values.rolloutStrategy.notifications.slackChannels }}
{{- end }}
{{- end }}
spec:
  replicas: {{ .Values.rolloutStrategy.replicaCount }}
  selector:
    matchLabels: {{- toYaml .Values.rolloutStrategy.deploymentMatchLabels | nindent 6 }}
  workloadRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ .Values.rolloutStrategy.deploymentName }}
  strategy:
    canary:
      stableMetadata:
        labels:
          role: stable
        annotations:
          role: stable
      canaryMetadata:
        labels:
          role: canary
        annotations:
          role: canary
      canaryService: {{ .Values.rolloutStrategy.canary.stableService }}-canary
      stableService: {{ .Values.rolloutStrategy.canary.stableService }}
      trafficRouting:
        nginx:
          stableIngresses: 
          {{ toYaml .Values.rolloutStrategy.canary.stableIngresses }}
          additionalIngressAnnotations:
            canary-by-header: X-JFrog-Canary
            canary-by-header-value: "always"
      steps:
      {{- if .Values.rolloutAnalysisTemplates.enabled }}
      {{- range $defaultTemplateType, $defaultTemplateConf := .Values.rolloutAnalysisTemplates.defaultTemplates }}
      {{- if $defaultTemplateConf.defaultStepsEnabled }}
      - setCanaryScale:
          weight: 25
      - pause:
          duration: 10m
      - analysis:
          args:
          - name: service-name
          templates:
          {{- range $templateName := .templates }}
          - templateName: {{ $templateName.name }}
          {{- end -}}
      {{- end -}}
      {{- end -}}
      {{- end -}}
      {{- if .Values.rolloutStrategy.canary.steps }}{{- toYaml .Values.rolloutStrategy.canary.steps | nindent 6 }}{{- end }}
{{- end -}}
