{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{ if and .Values.notifications.enabled .Values.notifications.bots.slack.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "argocd.notifications" . }}-slack-bot
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: notifications-slack-bot
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: 1
  strategy:
    type: Recreate
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.notifications.bots.slack.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: notifications-slack-bot
  template:
    metadata:
      {{- if .Values.notifications.bots.slack.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.notifications.bots.slack.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: notifications-slack-bot
    spec:
      serviceAccountName: {{ include "argocd.notifications.bots.slack.serviceAccountName" . }}
      {{- include "argocd.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.notifications.bots.slack.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.notifications.bots.slack.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.notifications.bots.slack.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.notifications.bots.slack.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.notifications.bots.slack.podAffinityPreset "component" "notifications" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.notifications.bots.slack.podAntiAffinityPreset "component" "notifications" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.notifications.bots.slack.nodeAffinityPreset.type "key" .Values.notifications.bots.slack.nodeAffinityPreset.key "values" .Values.notifications.bots.slack.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.notifications.bots.slack.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.notifications.bots.slack.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.notifications.bots.slack.priorityClassName }}
      priorityClassName: {{ .Values.notifications.bots.slack.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.notifications.bots.slack.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.notifications.bots.slack.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: notifications-slack-bot
          image: {{ include "argocd.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.notifications.bots.slack.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.notifications.bots.slack.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.notifications.bots.slack.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.notifications.bots.slack.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - argocd-notifications
            - bot
            - --metrics-port
            - {{ .Values.notifications.bots.slack.containerPorts.metrics | quote }}
          {{- if .Values.notifications.bots.slack.extraArgs }}
          {{- include "common.tplvalues.render" (dict "value" .Values.notifications.bots.slack.extraArgs "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          env:
            {{- if .Values.notifications.bots.slack.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.notifications.bots.slack.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.notifications.bots.slack.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.notifications.bots.slack.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.notifications.bots.slack.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.notifications.bots.slack.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
            - name: metrics
              containerPort: {{ .Values.notifications.bots.slack.containerPorts.metrics }} 
              protocol: TCP
          {{- if .Values.notifications.bots.slack.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.notifications.bots.slack.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.notifications.bots.slack.livenessProbe.enabled }}
          livenessProbe:
            tcpSocket:
              port: metrics
            initialDelaySeconds: {{ .Values.notifications.bots.slack.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.notifications.bots.slack.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.notifications.bots.slack.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.notifications.bots.slack.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.notifications.bots.slack.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.notifications.bots.slack.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.notifications.bots.slack.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.notifications.bots.slack.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              port: metrics
              path: /metrics
            initialDelaySeconds: {{ .Values.notifications.bots.slack.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.notifications.bots.slack.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.notifications.bots.slack.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.notifications.bots.slack.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.notifications.bots.slack.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.notifications.bots.slack.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.notifications.bots.slack.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.notifications.bots.slack.startupProbe.enabled }}
          startupProbe:
            httpGet:
              port: metrics
              path: /metrics
            initialDelaySeconds: {{ .Values.notifications.bots.slack.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.notifications.bots.slack.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.notifications.bots.slack.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.notifications.bots.slack.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.notifications.bots.slack.startupProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.notifications.bots.slack.resources }}
          resources: {{- toYaml .Values.notifications.bots.slack.resources | nindent 12 }}
          {{- else if ne .Values.notifications.bots.slack.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.notifications.bots.slack.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if .Values.notifications.bots.slack.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.notifications.bots.slack.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
          {{- if .Values.notifications.bots.slack.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.notifications.bots.slack.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.notifications.bots.slack.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.notifications.bots.slack.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{ end }}
