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

{{- if .Values.forwarder.enabled }}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: forwarder
    ## Istio Labels: https://istio.io/docs/ops/deployment/requirements/
    app: forwarder
  {{- if or .Values.forwarder.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.forwarder.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.forwarder.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: forwarder
  updateStrategy: {{- toYaml .Values.forwarder.updateStrategy | nindent 4 }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: forwarder
        app: forwarder
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/forwarder-configmap.yaml") . | sha256sum }}
        {{- if .Values.forwarder.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.forwarder.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "fluentd.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.forwarder.automountServiceAccountToken }}
      {{- if .Values.forwarder.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.forwarder.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ template "fluentd.forwarder.serviceAccountName" . }}
      priorityClassName: {{ .Values.forwarder.priorityClassName | quote }}
      {{- if .Values.forwarder.schedulerName }}
      schedulerName: {{ .Values.forwarder.schedulerName }}
      {{- end }}
      {{- if .Values.forwarder.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.forwarder.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.forwarder.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.forwarder.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.forwarder.podAffinityPreset "component" "forwarder" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.forwarder.podAntiAffinityPreset "component" "forwarder" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.forwarder.nodeAffinityPreset.type "key" .Values.forwarder.nodeAffinityPreset.key "values" .Values.forwarder.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.forwarder.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.forwarder.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.forwarder.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.forwarder.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.forwarder.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.forwarder.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.forwarder.extraGems }}
        - name: install-extra-gems
          image: {{ include "fluentd.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          command:
            - sh
            - -c
          args:
            - |-
              set -e
              cd /tmp

              # install extra gems
              {{- range $gem := .Values.forwarder.extraGems }}
              {{- if kindIs "string" $gem }}
              fluent-gem install {{ $gem }} --no-document
              {{- else }}
              fluent-gem install {{ $gem.name }}{{ if $gem.version }} -v {{ $gem.version }}{{ end }} --no-document
              {{- end }}
              {{- end }}

              # copy installed gems to shared dir
              cp -r /opt/bitnami/fluentd/extensions/* /extensions/
              cp -r /opt/bitnami/fluentd/gems/* /gems/
              cp -r /opt/bitnami/fluentd/specifications/* /specifications/
          {{- if .Values.forwarder.initResources }}
          resources: {{- toYaml .Values.forwarder.initResources | nindent 12 }}
          {{- else if ne .Values.forwarder.initResourcePresets "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.forwarder.initResourcePresets) | nindent 12 }}
          {{- end }}
          securityContext:
            allowPrivilegeEscalation: false
            runAsUser: 0
          volumeMounts:
            - name: empty-dir
              mountPath: /extensions
              subPath: app-extensions-dir
            - name: empty-dir
              mountPath: /gems
              subPath: app-gems-dir
            - name: empty-dir
              mountPath: /specifications
              subPath: app-specifications-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
        {{- end }}
        # Source: https://github.com/fluent/fluentd-kubernetes-daemonset/issues/1393
        - name: tmp-dir-permissions
          image: {{ include "fluentd.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          command:
            - sh
            - -c
          args:
            - |-
              #!/bin/sh
              chmod o-rwx /tmp
          {{- if .Values.forwarder.initResources }}
          resources: {{- toYaml .Values.forwarder.initResources | nindent 12 }}
          {{- else if ne .Values.forwarder.initResourcePresets "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.forwarder.initResourcePresets) | nindent 12 }}
          {{- end }}
          securityContext:
            allowPrivilegeEscalation: false
            runAsUser: 0
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
        {{- if .Values.forwarder.initContainers }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.forwarder.initContainers "context" $ ) | nindent 8 }}
        {{- end }}
      terminationGracePeriodSeconds: {{ .Values.forwarder.terminationGracePeriodSeconds }}
      hostNetwork: {{ .Values.forwarder.hostNetwork }}
      {{- if .Values.forwarder.dnsPolicy }}
      dnsPolicy: {{ .Values.forwarder.dnsPolicy }}
      {{- end }}
      containers:
        - name: fluentd
          image: {{ include "fluentd.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.forwarder.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.forwarder.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.forwarder.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" ( dict "value" .Values.forwarder.lifecycleHooks "context" $ ) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else }}
          {{- if .Values.forwarder.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.forwarder.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.forwarder.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.forwarder.args "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: FLUENTD_CONF
              value: {{ .Values.forwarder.configFile }}
            - name: FLUENTD_OPT
              value: {{ .Values.forwarder.extraArgs | quote }}
            - name: FLUENTD_DAEMON_USER
              value: {{ .Values.forwarder.daemonUser | quote }}
            - name: FLUENTD_DAEMON_GROUP
              value: {{ .Values.forwarder.daemonGroup | quote }}
            {{- if .Values.forwarder.extraEnvVars }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.forwarder.extraEnvVars "context" $ ) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.forwarder.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.forwarder.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.forwarder.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.forwarder.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.forwarder.resources }}
          resources: {{- toYaml .Values.forwarder.resources | nindent 12 }}
          {{- else if ne .Values.forwarder.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.forwarder.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            {{- if .Values.forwarder.containerPorts }}
            {{- toYaml .Values.forwarder.containerPorts | nindent 12 }}
            {{- end }}
            {{- if .Values.metrics.enabled }}
            - name: tcp-metrics
              containerPort: {{ .Values.metrics.service.port }}
              protocol: TCP
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.forwarder.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.forwarder.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.forwarder.startupProbe.enabled }}
          startupProbe:
            httpGet:
              path: {{ .Values.forwarder.startupProbe.httpGet.path }}
              port: {{ .Values.forwarder.startupProbe.httpGet.port }}
            initialDelaySeconds: {{ .Values.forwarder.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.forwarder.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.forwarder.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.forwarder.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.forwarder.startupProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.forwarder.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.forwarder.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.forwarder.livenessProbe.enabled }}
          livenessProbe:
            tcpSocket:
              port: {{ .Values.forwarder.livenessProbe.tcpSocket.port }}
            initialDelaySeconds: {{ .Values.forwarder.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.forwarder.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.forwarder.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.forwarder.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.forwarder.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.forwarder.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.forwarder.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.forwarder.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: {{ .Values.forwarder.readinessProbe.httpGet.path }}
              port: {{ .Values.forwarder.readinessProbe.httpGet.port }}
            initialDelaySeconds: {{ .Values.forwarder.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.forwarder.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.forwarder.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.forwarder.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.forwarder.readinessProbe.failureThreshold }}
          {{- end }}
          {{- end }}
          volumeMounts:
            - name: fluentd-config
              mountPath: /opt/bitnami/fluentd/conf
            {{- if .Values.forwarder.persistence.enabled }}
            - name: fluentd-buffer
              mountPath: /opt/bitnami/fluentd/logs/buffers
            {{- else }}
            - name: buffer
              mountPath: /opt/bitnami/fluentd/logs/buffers
            {{- end }}
            {{- if .Values.forwarder.extraGems }}
            - name: empty-dir
              mountPath: /opt/bitnami/fluentd/extensions/
              subPath: app-extensions-dir
            - name: empty-dir
              mountPath: /opt/bitnami/fluentd/gems/
              subPath: app-gems-dir
            - name: empty-dir
              mountPath: /opt/bitnami/fluentd/specifications/
              subPath: app-specifications-dir
            {{- end }}
            {{- if .Values.tls.enabled }}
            - name: fluentd-forwarder-certificates
              mountPath: /opt/bitnami/fluentd/certs/out_forward
              readOnly: true
            {{- end }}
            {{- if .Values.forwarder.initScripts }}
            - name: custom-init-scripts
              mountPath: /docker-entrypoint-initdb.d/init-scripts
            {{- end }}
            {{- if .Values.forwarder.initScriptsCM }}
            - name: custom-init-scripts-cm
              mountPath: /docker-entrypoint-initdb.d/init-scripts-cm
            {{- end }}
            {{- if .Values.forwarder.initScriptsSecret }}
            - name: custom-init-scripts-secret
              mountPath: /docker-entrypoint-initdb.d/init-scripts-secret
            {{- end }}
            - name: varlog
              mountPath: /var/log
              readOnly: {{ .Values.varlog.readonly }}
            - name: varlibdockercontainers
              mountPath: /var/lib/docker/containers
              readOnly: true
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            {{- if .Values.forwarder.extraVolumeMounts }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.forwarder.extraVolumeMounts "context" $ ) | nindent 12 }}
            {{- end }}
        {{- if .Values.forwarder.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.forwarder.sidecars "context" $ ) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.tls.enabled }}
        - name: fluentd-forwarder-certificates
          secret:
            secretName: {{ include "fluentd.forwarder.tlsSecretName" . }}
            defaultMode: 256
        {{- end }}
        {{- if .Values.forwarder.initScripts }}
        - name: custom-init-scripts
          configMap:
            name: {{ template "fluentd.forwarder.initScripts" . }}
        {{- end }}
        {{- if .Values.forwarder.initScriptsCM }}
        - name: custom-init-scripts-cm
          configMap:
            name: {{ template "fluentd.forwarder.initScriptsCM" . }}
        {{- end }}
        {{- if .Values.forwarder.initScriptsSecret }}
        - name: custom-init-scripts-secret
          secret:
            secretName: {{ template "fluentd.forwarder.initScriptsSecret" . }}
            defaultMode: 0755
        {{- end }}
        - name: fluentd-config
          configMap:
            name: {{ template "fluentd.forwarder.configMap" . }}
        {{- if .Values.forwarder.persistence.enabled }}
        - name: fluentd-buffer
          hostPath:
            path: {{ .Values.forwarder.persistence.hostPath.path }}
            type: DirectoryOrCreate
        {{- else }}
        - name: buffer
          emptyDir: {}
        {{- end }}
        - name: varlog
          hostPath:
            path: /var/log
        - name: varlibdockercontainers
          hostPath:
            path: /var/lib/docker/containers
        {{- if .Values.forwarder.extraVolumes }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.forwarder.extraVolumes "context" $ ) | nindent 8 }}
        {{- end }}
{{- end }}
