apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "cluster-agent.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "cluster-agent.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "cluster-agent.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "cluster-agent.selectorLabels" . | nindent 8 }}
    spec:
      serviceAccountName: {{ include "cluster-agent.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.clusterAgent.image.repository }}:{{ .Values.clusterAgent.image.tag }}"
          imagePullPolicy: {{ .Values.clusterAgent.image.pullPolicy }}
          workingDir: /tmp
          volumeMounts:
            - mountPath: /client-ssl
              name: client-ssl
            - mountPath: /opt/other-binary
              name: other-binary
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /healthz
              port: http
          readinessProbe:
            httpGet:
              path: /healthz
              port: http
          command:
            - /cluster-agent
            - --provider
            - {{ .Values.clusterAgent.args.provider }}
            - --tidb-operator-version
            - {{ .Values.clusterAgent.args.tidbOperatorVersion }}
            {{- with .Values.clusterAgent.args.gkePolicyControlProjectID }}
            - --gke-policy-control-project-id
            - {{ . | quote }}
            {{- end }}
            {{- with .Values.clusterAgent.args.gkePolicyControlLocation }}
            - --gke-policy-control-location
            - {{ . | quote }}
            {{- end }}
            {{- with .Values.clusterAgent.args.gkePolicyControlClusterName }}
            - --gke-policy-control-cluster-name
            - {{ . | quote }}
            {{- end }}
            {{- with .Values.clusterAgent.args.gkePolicyControlFleetProjectID }}
            - --gke-policy-control-fleet-project-id
            - {{ . | quote }}
            {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          env:
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          {{- $reservedEnv := dict "POD_NAMESPACE" true }}
          {{- range .Values.clusterAgent.env }}
            {{- $envName := .name | toString | trim }}
            {{- if hasKey $reservedEnv $envName }}
              {{- fail (printf "clusterAgent.env must not set reserved environment variable %s" $envName) }}
            {{- end }}
          {{- end }}
          {{ with .Values.clusterAgent.env }}
            {{- toYaml . | nindent 12 }}
          {{- end }}
      initContainers:
        - command:
            - cp
            - /tikv-ctl
            - /opt/other-binary/
          image: "{{ .Values.tikvCtl.imageStr }}"
          imagePullPolicy: IfNotPresent
          name: copy-tikv-ctl
          volumeMounts:
            - mountPath: /opt/other-binary
              name: other-binary
          workingDir: /opt/other-binary
        - command:
            - cp
            - /pd-ctl
            - /opt/other-binary/
          image: "{{ .Values.pdCtl.imageStr }}"
          imagePullPolicy: IfNotPresent
          name: copy-pd-ctl
          resources: { }
          volumeMounts:
            - mountPath: /opt/other-binary
              name: other-binary
          workingDir: /opt/other-binary
        # The Feat-Gate based on whether imageStr has been formatted by root-config
        {{- if .Values.pdPatrolCtl.imageStr }}
        - command:
            - cp
            - /pd-ctl
            - /opt/other-binary/pd-patrol-ctl
            # Can make pod avoid to stuck in init/failed if patrol-Image is not found.
            # - sh
            # - -c
            # - cp /pd-ctl /opt/other-binary/pd-patrol-ctl || echo "pd-patrol-ctl not copied"
          image: "{{ .Values.pdPatrolCtl.imageStr }}"
          imagePullPolicy: IfNotPresent
          name: copy-pd-patrol-ctl
          volumeMounts:
            - mountPath: /opt/other-binary
              name: other-binary
          workingDir: /opt/other-binary
        {{- end }}
      volumes:
        - name: client-ssl
          secret:
            defaultMode: 420
            secretName: db-cluster-client-secret
        - emptyDir: { }
          name: other-binary
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
