# helm_charts_gocd-agent-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "gocd.fullname" . }}-agent
  labels:
    app: {{ template "gocd.name" . }}
    chart:  "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
    component: agent
  annotations:
  {{- range $key, $value := .Values.agent.annotations.deployment }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
spec:
  replicas: {{ .Values.agent.replicaCount }}
  {{- if .Values.agent.deployStrategy }}
  strategy:
{{ toYaml .Values.agent.deployStrategy | indent 4 }}
  {{- end }}
  selector:
    matchLabels:
      app: {{ template "gocd.name" . }}
      release: {{ .Release.Name | quote }}
      component: agent
  template:
    metadata:
      labels:
        app: {{ template "gocd.name" . }}
        release: {{ .Release.Name | quote }}
        component: agent
      annotations:
      {{- range $key, $value := .Values.agent.annotations.pod }}
        {{ $key }}: {{ $value | quote }}
      {{- end }}
    spec:
      serviceAccountName: {{ template "gocd.agentServiceAccountName" . }}
      securityContext:
        runAsUser: {{ .Values.agent.securityContext.runAsUser }}
        runAsGroup: {{ .Values.agent.securityContext.runAsGroup }}
        fsGroup: {{ .Values.agent.securityContext.fsGroup }}
      {{- if or .Values.agent.persistence.enabled (or .Values.agent.security.ssh.enabled .Values.agent.persistence.extraVolumes) }}
      volumes:
      {{- end }}
      {{- if .Values.agent.persistence.enabled }}
        - name: goagent-vol
          persistentVolumeClaim:
            claimName: {{ .Values.agent.persistence.existingClaim | default (printf "%s-%s" (include "gocd.fullname" .) "agent")  }}
      {{- end }}
      {{- if ne (len .Values.agent.persistence.extraVolumes) 0 }}
{{ toYaml .Values.agent.persistence.extraVolumes | indent 8 }}
      {{- end }}
      {{- if .Values.agent.security.ssh.enabled }}
        - name: ssh-secrets
          secret:
            secretName: {{ .Values.agent.security.ssh.secretName }}
            defaultMode: {{ .Values.agent.security.ssh.defaultMode | default 256 }}
      {{- end }}
      {{- if .Values.agent.initContainers }}
      initContainers:
{{ toYaml .Values.agent.initContainers | indent 8 }}
      {{- end }}
      containers:
        - name: {{ template "gocd.name" . }}-agent
          {{- if .Values.agent.image.tag }}
          image: "{{ .Values.agent.image.repository }}:{{ .Values.agent.image.tag }}"
          {{- else }}
          image: "{{ .Values.agent.image.repository }}:v{{ .Chart.AppVersion }}"
          {{- end }}
          imagePullPolicy: {{ .Values.agent.image.pullPolicy }}
          resources:
{{ toYaml .Values.agent.resources | indent 12 }}
          env:
            - name: GO_SERVER_URL
            {{- if .Values.agent.env.goServerUrl }}
              value: {{ .Values.agent.env.goServerUrl }}
            {{- else }}
              value: "http://{{ template "gocd.fullname" . }}-server:{{ .Values.server.service.httpPort }}/go"
            {{- end }}

            {{- if .Values.agent.env.agentAutoRegisterKey }}
            - name: AGENT_AUTO_REGISTER_KEY
              value: {{ .Values.agent.env.agentAutoRegisterKey }}
            {{- end }}
            {{- if .Values.agent.env.agentAutoRegisterResources }}
            - name: AGENT_AUTO_REGISTER_RESOURCES
              value: {{ .Values.agent.env.agentAutoRegisterResources }}
            {{- end }}
            {{- if .Values.agent.env.agentAutoRegisterEnvironments }}
            - name: AGENT_AUTO_REGISTER_ENVIRONMENTS
              value: {{ .Values.agent.env.agentAutoRegisterEnvironments }}
            {{- else if .Values.agent.env.agentAutoRegisterEnvironemnts }}
            - name: AGENT_AUTO_REGISTER_ENVIRONMENTS
              value: {{ .Values.agent.env.agentAutoRegisterEnvironemnts }}
            {{- end }}
            {{- if .Values.agent.env.agentAutoRegisterHostname }}
            - name: AGENT_AUTO_REGISTER_HOSTNAME
              value: {{ .Values.agent.env.agentAutoRegisterHostname }}
            {{- end }}
            {{- if .Values.agent.env.goAgentJvmOpts }}
            - name: GOCD_AGENT_JVM_OPTS
              value: {{ .Values.agent.env.goAgentJvmOpts }}
            {{- end }}
            {{- if .Values.agent.env.goAgentBootstrapperJvmArgs }}
            - name: AGENT_BOOTSTRAPPER_JVM_ARGS
              value: {{ .Values.agent.env.goAgentBootstrapperJvmArgs }}
            {{- end }}
            {{- if .Values.agent.env.goAgentBootstrapperArgs }}
            - name: AGENT_BOOTSTRAPPER_ARGS
              value: {{ .Values.agent.env.goAgentBootstrapperArgs }}
            {{- end }}
            {{- if .Values.agent.env.extraEnvVars }}
{{ toYaml .Values.agent.env.extraEnvVars | indent 12 }}
            {{- end }}
          {{- if .Values.agent.healthCheck.enabled }}
          livenessProbe:
            httpGet:
              path: /health/v1/isConnectedToServer
              port: 8152
            initialDelaySeconds: {{ .Values.agent.healthCheck.initialDelaySeconds }}
            failureThreshold: {{ .Values.agent.healthCheck.failureThreshold }}
            periodSeconds: {{ .Values.agent.healthCheck.periodSeconds }}
          readinessProbe:
            httpGet:
              path: /health/v1/isConnectedToServer
              port: 8152
            initialDelaySeconds: {{ .Values.agent.healthCheck.initialDelaySeconds }}
          {{- end }}
          {{- if or .Values.agent.persistence.enabled (or .Values.agent.security.ssh.enabled .Values.agent.persistence.extraVolumeMounts) }}
          volumeMounts:
          {{- end }}
          {{- if .Values.agent.persistence.enabled }}
            - name: goagent-vol
              mountPath: /home/go
              subPath: {{ .Values.agent.persistence.subpath.homego }}
            - name: {{ .Values.agent.persistence.name.dockerEntryPoint }}
              mountPath: /docker-entrypoint.d
              subPath: {{ .Values.agent.persistence.subpath.dockerEntryPoint }}
          {{- end }}
          {{- if ne (len .Values.agent.persistence.extraVolumeMounts) 0 }}
{{ toYaml .Values.agent.persistence.extraVolumeMounts | indent 12 }}
          {{- end }}
          {{- if .Values.agent.security.ssh.enabled }}
            - name: ssh-secrets
              readOnly: true
              mountPath: /home/go/.ssh
          {{- end }}
          {{- if or .Values.agent.preStop .Values.agent.postStart }}
          lifecycle:
            {{- if .Values.agent.preStop }}
            preStop:
              exec:
               command:
{{ toYaml .Values.agent.preStop | indent 18 }}
            {{- end }}
            {{- if .Values.agent.postStart }}
            postStart:
              exec:
               command:
{{ toYaml .Values.agent.postStart | indent 18 }}
            {{- end }}
          {{- end }}
          securityContext:
            privileged: {{ .Values.agent.privileged }}
      {{- if .Values.agent.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.agent.terminationGracePeriodSeconds }}
      {{- end }}
      restartPolicy: {{ .Values.agent.restartPolicy }}
    {{- if .Values.agent.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.agent.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.agent.affinity }}
      affinity:
{{ toYaml .Values.agent.affinity | indent 8 }}
    {{- end }}
    {{- if .Values.agent.tolerations }}
      tolerations:
{{ toYaml .Values.agent.tolerations | indent 8 }}
    {{- end }}
    {{- if .Values.agent.hostAliases }}
      hostAliases:
{{ toYaml .Values.agent.hostAliases | indent 8 }}
    {{- end }}