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

{{- if .Values.agent.enabled }}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: agent
  name: {{ template "common.names.fullname" . }}-agent
  namespace: {{ .Release.Namespace | quote }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.agent.updateStrategy }}
  updateStrategy: {{- toYaml .Values.agent.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.agent.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: agent
  template:
    metadata:
      {{- if .Values.agent.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.agent.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: agent
    spec:
      {{- include "kiam.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.agent.automountServiceAccountToken }}
      {{- if .Values.agent.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.agent.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ template "kiam.agent.serviceAccountName" . }}
      dnsPolicy: {{ .Values.agent.dnsPolicy }}
      hostNetwork: {{ .Values.agent.useHostNetwork }}
      {{- if .Values.agent.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.agent.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.agent.podAffinityPreset "component" "agent" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.agent.podAntiAffinityPreset "component" "agent" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.agent.nodeAffinityPreset.type "key" .Values.agent.nodeAffinityPreset.key "values" .Values.agent.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.agent.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.agent.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.agent.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.agent.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.agent.priorityClassName }}
      priorityClassName: {{ .Values.agent.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.agent.schedulerName }}
      schedulerName: {{ .Values.agent.schedulerName }}
      {{- end }}
      {{- if .Values.agent.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.agent.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.agent.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.agent.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.agent.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.agent.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: agent
          image: {{ template "kiam.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.agent.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.agent.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.agent.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.agent.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.agent.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.agent.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - kiam
            - agent
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.agent.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.agent.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            {{- if .Values.agent.iptables }}
            - --iptables
            {{- end }}
            {{- if not .Values.agent.iptablesRemoveOnShutdown }}
            - --no-iptables-remove
            {{- end }}
            - --host-interface={{ .Values.agent.hostInterface }}
            {{- if .Values.agent.logJsonOutput }}
            - --json-log
            {{- end }}
            - --level={{ .Values.agent.logLevel }}
            - --port={{ .Values.agent.containerPort }}
            - --cert=/bitnami/kiam/tls/{{ .Values.agent.tlsCerts.certFileName }}
            - --key=/bitnami/kiam/tls/{{ .Values.agent.tlsCerts.keyFileName }}
            - --ca=/bitnami/kiam/tls/{{ .Values.agent.tlsCerts.caFileName }}
            - --server-address={{ template "common.names.fullname" . }}-server:{{ .Values.server.service.port }}
            {{- if .Values.agent.metrics.enabled }}
            - --prometheus-listen-addr=0.0.0.0:{{ .Values.agent.metrics.port }}
            - --prometheus-sync-interval={{ .Values.agent.metrics.syncInterval }}
            {{- end }}
            {{- if .Values.agent.allowRouteRegExp }}
            - --allow-route-regexp={{ .Values.agent.allowRouteRegExp }}
            {{- end }}
            - --gateway-timeout-creation={{ .Values.agent.gatewayTimeoutCreation }}
            {{- if .Values.agent.keepaliveParams.time }}
            - --grpc-keepalive-time-duration={{ .Values.agent.keepaliveParams.time }}
            {{- end }}
            {{- if .Values.agent.keepaliveParams.timeout }}
            - --grpc-keepalive-timeout-duration={{ .Values.agent.keepaliveParams.timeout }}
            {{- end }}
            {{- if .Values.agent.keepaliveParams.permitWithoutStream }}
            - --grpc-keepalive-permit-without-stream
            {{- end }}
            {{- range $key, $value := .Values.agent.extraArgs }}
            {{- if $value }}
            - --{{ $key }}={{ $value }}
            {{- else }}
            - --{{ $key }}
            {{- end }}
            {{- end }}
          {{- end }}
          env:
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            {{- if .Values.agent.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.agent.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.agent.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.agent.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.agent.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.agent.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            {{- if .Values.agent.metrics.enabled }}
            - name: metrics
              containerPort: {{ .Values.agent.metrics.port }}
              protocol: TCP
            {{- end }}
          {{- if .Values.agent.resources }}
          resources: {{- toYaml .Values.agent.resources | nindent 12 }}
          {{- else if ne .Values.agent.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.agent.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.agent.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.agent.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.agent.startupProbe.enabled }}
          startupProbe:
            tcpSocket:
              port: {{ .Values.agent.containerPort }}
            initialDelaySeconds: {{ .Values.agent.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.agent.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.agent.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.agent.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.agent.startupProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.agent.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.agent.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.agent.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              {{- if .Values.agent.enableDeepProbe }}
              path: /health?deep=1
              {{- else }}
              path: /health
              {{- end }}
              port: {{ .Values.agent.containerPort }}
            initialDelaySeconds: {{ .Values.agent.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.agent.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.agent.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.agent.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.agent.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.agent.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.agent.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.agent.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /ping
              port: {{ .Values.agent.containerPort }}
            initialDelaySeconds: {{ .Values.agent.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.agent.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.agent.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.agent.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.agent.readinessProbe.failureThreshold }}
          {{- end }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - mountPath: /bitnami/kiam/tls
              name: tls
            {{- if .Values.server.sslCertHostPath }}
            - mountPath: /etc/ssl/certs
              name: ssl-certs
              readOnly: true
            {{- end }}
            - mountPath: /var/run/xtables.lock
              name: xtables
            {{- if .Values.agent.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.agent.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.agent.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.agent.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: tls
          secret:
            {{- if .Values.agent.tlsSecret }}
            secretName: {{ .Values.agent.tlsSecret }}
            {{ else }}
            secretName: {{ template "common.names.fullname" . }}-agent
            {{- end }}
        {{- if .Values.server.sslCertHostPath }}
        - name: ssl-certs
          hostPath:
            path: {{ .Values.server.sslCertHostPath }}
        {{- end }}
        - name: xtables
          hostPath:
            path: /run/xtables.lock
            type: FileOrCreate
        {{- if .Values.agent.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.agent.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
