apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "devops-cli.fullname" . }}
  labels:
    {{- include "devops-cli.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "devops-cli.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "devops-cli.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "devops-cli.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: DOCKER_HOST
              value: {{ .Values.docker.host }}
            - name: KUBECONFIG
              value: /config/.kube/config
            - name: AWS_REGION
              value: {{ .Values.cloud.region }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /health
              port: http
          readinessProbe:
            httpGet:
              path: /ready
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - name: docker-socket
              mountPath: /var/run/docker.sock
            - name: config
              mountPath: /config
              readOnly: true
            {{- if .Values.kubernetes.enabled }}
            - name: kubeconfig
              mountPath: /config/.kube
              readOnly: true
            {{- end }}
      volumes:
        - name: docker-socket
          hostPath:
            path: /var/run/docker.sock
            type: Socket
        - name: config
          configMap:
            name: {{ include "devops-cli.fullname" . }}-config
        {{- if .Values.kubernetes.enabled }}
        - name: kubeconfig
          secret:
            secretName: {{ .Values.kubernetes.kubeconfig }}
            optional: true
        {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
