apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "keel.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ template "keel.name" . }}
    chart: {{ template "keel.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ template "keel.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "keel.name" . }}
        release: {{ .Release.Name }}
{{- with .Values.podAnnotations }}
      annotations:
{{ toYaml . | indent 8 }}
{{- end }}
    spec:
      serviceAccountName: {{ template "serviceAccount.name" . }}
      containers:
        - name: keel
          # Note that we use appVersion to get images tag.
          image: "{{ .Values.image.repository }}:{{ default .Chart.AppVersion .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["/bin/keel"]
          volumeMounts:
{{- if .Values.persistance.enable}}
            - name: storage-logs
              mountPath: /data
{{- end}}
{{- if .Values.googleApplicationCredentials }}
            - name: secret
              mountPath: "/secret"
              readOnly: true
{{- end }}
          env:
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
{{- if .Values.googleApplicationCredentials }}
            - name: GOOGLE_APPLICATION_CREDENTIALS
              value: /secret/google-application-credentials.json
{{- end }}
{{- if .Values.polling.enabled }}
            # Enable polling
            - name: POLL
              value: "1"
{{- else }}
            # Disable polling
            - name: POLL
              value: "0"
{{- end }}
{{- if .Values.helmProvider.enabled }}
            # Enable/disable Helm provider
            - name: HELM_PROVIDER
              value: "1"
            - name: TILLER_NAMESPACE
              value: "{{ .Values.helmProvider.tillerNamespace }}"
            - name: TILLER_ADDRESS
              value: "{{ .Values.helmProvider.tillerAddress }}"
{{- end }}
{{- if .Values.gcr.enabled }}
            # Enable GCR with pub/sub support
            - name: PROJECT_ID
              value: "{{ .Values.gcr.projectId }}"
            - name: PUBSUB
              value: "1"
  {{- if .Values.gcr.clusterName }}
            # Customize the cluster name, mainly useful when outside of GKE
            - name: CLUSTER_NAME
              value: "{{ .Values.gcr.clusterName }}"
  {{- end }}
{{- end }}
{{- if .Values.ecr.enabled }}
            # Enable AWS ECR
            - name: AWS_ACCESS_KEY_ID
              value: "{{ .Values.ecr.accessKeyId }}"
            - name: AWS_REGION
              value: "{{ .Values.ecr.region }}"
{{- end }}
{{- if .Values.dockerRegistry.enabled }}
            - name: DOCKER_REGISTRY_CFG
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.dockerRegistry.name }}
                  key: {{ .Values.dockerRegistry.key }}
{{- end }}
{{- if .Values.webhook.enabled }}
            # Enable webhook endpoint
            - name: WEBHOOK_ENDPOINT
              value: "{{ .Values.webhook.endpoint }}"
{{- end }}
{{- if .Values.mattermost.enabled }}
            # Enable mattermost endpoint
            - name: MATTERMOST_ENDPOINT
              value: "{{ .Values.mattermost.endpoint }}"
{{- end }}
{{- if .Values.basicauth.enabled }}
            # Enable basic auth
            - name: BASIC_AUTH_USER
              value: "{{ .Values.basicauth.user }}"
{{- end }}
{{- if .Values.slack.enabled }}
            - name: SLACK_CHANNELS
              value: "{{ .Values.slack.channel }}"
            - name: SLACK_APPROVALS_CHANNEL
              value: "{{ .Values.slack.approvalsChannel }}"
  {{- if .Values.slack.botName }}
            - name: SLACK_BOT_NAME
              value: "{{ .Values.slack.botName }}"
  {{- end }}
{{- end }}
{{- if .Values.hipchat.enabled }}
            # Enable hipchat approvials and notification
            - name: HIPCHAT_CHANNELS
              value: "{{ .Values.hipchat.channel }}"
            - name: HIPCHAT_APPROVALS_CHANNEL
              value: "{{ .Values.hipchat.approvalsChannel }}"
            - name: HIPCHAT_APPROVALS_BOT_NAME
              value: "{{ .Values.hipchat.botName }}"
            - name: HIPCHAT_APPROVALS_USER_NAME
              value: "{{ .Values.hipchat.userName }}"
{{- end }}
            - name: NOTIFICATION_LEVEL
              value: "{{ .Values.notificationLevel }}"
{{- if .Values.debug }}
            # Enable debug logging
            - name: DEBUG
              value: "true"
{{- end }}
{{- if .Values.insecureRegistry }}
            # Enable insecure registries
            - name: INSECURE_REGISTRY
              value: "{{ .Values.insecureRegistry }}"
 {{- end }}
{{- if .Values.aws.region }}
            - name: AWS_REGION
              value: "{{ .Values.aws.region }}"
{{- end }}
          envFrom:
            - secretRef:
                name: {{ .Values.secret.name | default (include "keel.fullname" .) }}
          ports:
            - containerPort: 9300
          livenessProbe:
            httpGet:
              path: /healthz
              port: 9300
            initialDelaySeconds: 30
            timeoutSeconds: 10
          readinessProbe:
            httpGet:
              path: /healthz
              port: 9300
            initialDelaySeconds: 30
            timeoutSeconds: 10
          resources:
{{ toYaml .Values.resources | indent 12 }}
{{- if .Values.webhookRelay.enabled }}
        - name: webhookrelayd
          image: "{{ .Values.webhookRelay.image.repository }}:{{ .Values.webhookRelay.image.tag }}"
          imagePullPolicy: {{ .Values.webhookRelay.image.pullPolicy }}
          command: ["/relayd"]
          env:
            - name: KEY
              valueFrom:
                secretKeyRef:
                  name: {{ template "keel.name" . }}-webhookrelay
                  key: key
            - name: SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ template "keel.name" . }}-webhookrelay
                  key: secret
            - name: BUCKET
              value: "{{ .Values.webhookRelay.bucket }}"
          resources:
{{ toYaml .Values.resources | indent 12 }}
{{- end }}
{{- if .Values.persistance.enable }}
      volumes:
        - name: storage-logs
          persistentVolumeClaim:
            claimName: {{ template "keel.fullname" . }}
{{- end}}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
{{- if .Values.googleApplicationCredentials }}
      volumes:
        - name: secret
          secret:
            secretName: {{ .Values.secret.name | default (include "keel.fullname" .) }}
{{- end }}
