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

{{- if .Values.speaker.enabled }}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ printf "%s-speaker" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.speaker.image "chart" .Chart ) ) }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: speaker
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.speaker.updateStrategy }}
  updateStrategy: {{- toYaml .Values.speaker.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.speaker.podLabels .Values.commonLabels $versionLabel ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: speaker
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: speaker
      {{- if .Values.speaker.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.speaker.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
    spec:
      {{- include "common.images.renderPullSecrets" (dict "images" (list .Values.speaker.image .Values.controller.image) "context" $) | nindent 6 }}
      automountServiceAccountToken: {{ .Values.speaker.automountServiceAccountToken }}
      {{- if .Values.speaker.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.speaker.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "metallb.speaker.serviceAccountName" . }}
      {{- if .Values.speaker.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.speaker.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.speaker.podAffinityPreset "component" "speaker" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.speaker.podAntiAffinityPreset "component" "speaker" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.speaker.nodeAffinityPreset.type "key" .Values.speaker.nodeAffinityPreset.key "values" .Values.speaker.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      nodeSelector:
        {{- if .Values.speaker.nodeSelector }}
        {{- include "common.tplvalues.render" (dict "value" .Values.speaker.nodeSelector "context" $) | nindent 8 }}
        {{- end }}
        "kubernetes.io/os": linux
      {{- if .Values.speaker.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.speaker.tolerations "context" $) | nindent 8 }}
      {{- end }}
      hostNetwork: true
      {{- if .Values.controller.runtimeClassName }}
      runtimeClassName: {{ .Values.controller.runtimeClassName | quote }}
      {{- end }}
      {{- if .Values.speaker.priorityClassName }}
      priorityClassName: {{ .Values.speaker.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.speaker.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.speaker.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.speaker.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.speaker.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if or .Values.speaker.initContainers .Values.speaker.frr.enabled }}
      initContainers:
        {{- if .Values.speaker.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.speaker.initContainers "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.speaker.frr.enabled }}
          # Copies the initial config files with the right permissions to the shared volume.
          - name: cp-frr-files
            image: {{ include "common.images.image" (dict "imageRoot" .Values.speaker.frr.image "global" .Values.global) }}
            {{- if .Values.speaker.frr.image.pullPolicy }}
            imagePullPolicy: {{ .Values.speaker.frr.image.pullPolicy }}
            {{- end }}
            command: ["/bin/sh", "-c", "cp -rLf /tmp/frr/* /etc/frr/"]
            volumeMounts:
              - name: frr-startup
                mountPath: /tmp/frr
              - name: frr-conf
                mountPath: /etc/frr
          # Copies the reloader to the shared volume between the speaker and reloader.
          - name: cp-reloader
            image: {{ include "common.images.image" (dict "imageRoot" .Values.speaker.frr.image "global" .Values.global) }}
            {{- if .Values.speaker.frr.image.pullPolicy }}
            imagePullPolicy: {{ .Values.speaker.frr.image.pullPolicy }}
            {{- end }}
            command: ["/bin/sh", "-c", "cp -f /opt/bitnami/metallb-speaker/bin/frr-reloader.sh /etc/frr_reloader/"]
            volumeMounts:
              - name: reloader
                mountPath: /etc/frr_reloader
          # Copies the metrics exporter
          - name: cp-metrics
            image: {{ include "common.images.image" (dict "imageRoot" .Values.speaker.frr.image "global" .Values.global) }}
            {{- if .Values.speaker.frr.image.pullPolicy }}
            imagePullPolicy: {{ .Values.speaker.frr.image.pullPolicy }}
            {{- end }}
            command: ["/bin/sh", "-c", "cp -f /opt/bitnami/metallb-speaker/bin/frr-metrics /etc/frr_metrics/"]
            volumeMounts:
              - name: metrics
                mountPath: /etc/frr_metrics
      shareProcessNamespace: true
        {{- end }}
      {{- end }}
      containers:
        - name: metallb-speaker
          image: {{ include "common.images.image" (dict "imageRoot" .Values.speaker.image "global" .Values.global) }}
          imagePullPolicy: {{ .Values.speaker.image.pullPolicy }}
          {{- if .Values.speaker.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.speaker.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.speaker.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.speaker.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.speaker.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.speaker.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --port={{ .Values.speaker.containerPorts.metrics }}
            - --log-level={{ .Values.speaker.logLevel }}
          {{- end }}
          {{- if .Values.speaker.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.speaker.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: METALLB_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: METALLB_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: METALLB_HOST
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: METALLB_ML_BIND_ADDR
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: METALLB_ML_LABELS
              value: "app.kubernetes.io/name={{ include "common.names.name" . }},app.kubernetes.io/instance={{ .Release.Name }},app.kubernetes.io/component=speaker"
            - name: METALLB_ML_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: METALLB_ML_SECRET_KEY_PATH
              value: /etc/memberlist
            {{- if .Values.speaker.frr.enabled }}
            - name: FRR_CONFIG_FILE
              value: /etc/frr_reloader/frr.conf
            - name: FRR_RELOADER_PID_FILE
              value: /etc/frr_reloader/reloader.pid
            - name: METALLB_BGP_TYPE
              value: frr
            {{- end }}
            {{- if .Values.speaker.extraEnvVars }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.speaker.extraEnvVars "context" $ ) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.speaker.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.speaker.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.speaker.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.speaker.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - name: metrics
              containerPort: {{ .Values.speaker.containerPorts.metrics }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.speaker.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.speaker.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.speaker.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.speaker.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: metrics
          {{- end }}
          {{- if .Values.speaker.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.speaker.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.speaker.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.speaker.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /metrics
              port: metrics
          {{- end }}
          {{- if .Values.speaker.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.speaker.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.speaker.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.speaker.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: metrics
          {{- end }}
          {{- end }}
          {{- if .Values.speaker.resources }}
          resources: {{- toYaml .Values.speaker.resources | nindent 12 }}
          {{- else if ne .Values.speaker.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.speaker.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: memberlist-secret
              mountPath: /etc/memberlist
              readOnly: true
            {{- if .Values.speaker.frr.enabled }}
            - name: reloader
              mountPath: /etc/frr_reloader
            {{- end }}
            {{- if .Values.speaker.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.speaker.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.speaker.frr.enabled }}
        - name: frr
          image: {{ include "common.images.image" (dict "imageRoot" .Values.speaker.frr.image "global" .Values.global) }}
          imagePullPolicy: {{ .Values.speaker.frr.image.pullPolicy }}
          {{- if .Values.speaker.frr.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.speaker.frr.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: frr-sockets
              mountPath: /var/run/frr
            - name: frr-conf
              mountPath: /etc/frr
          # The command is FRR's default entrypoint & waiting for the log file to appear and tailing it.
          # If the log file isn't created in 60 seconds the tail fails and the container is restarted.
          # This workaround is needed to have the frr logs as part of kubectl logs -c frr < speaker_pod_name >.
          command:
            - /bin/bash
            - -c
            - |
              . /usr/lib/frr/frrcommon.sh
              /usr/lib/frr/watchfrr $(daemon_list) &
              attempts=0
              until [[ -f /etc/frr/frr.log || $attempts -eq 60 ]]; do
                sleep 1
                attempts=$(( $attempts + 1 ))
              done
              tail -f /etc/frr/frr.log
          {{- if .Values.speaker.frr.resources }}
          resources: {{- toYaml .Values.speaker.frr.resources | nindent 12 }}
          {{- else if ne .Values.speaker.frr.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.speaker.frr.resourcesPreset) | nindent 12 }}
          {{- end }}
        - name: reloader
          image: {{ include "common.images.image" (dict "imageRoot" .Values.speaker.frr.image "global" .Values.global) }}
          imagePullPolicy: {{ .Values.speaker.frr.image.pullPolicy }}
          {{- if .Values.speaker.frr.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.speaker.frr.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          command: ["/etc/frr_reloader/frr-reloader.sh"]
          volumeMounts:
            - name: frr-sockets
              mountPath: /var/run/frr
            - name: frr-conf
              mountPath: /etc/frr
            - name: reloader
              mountPath: /etc/frr_reloader
          {{- if .Values.speaker.frr.resources }}
          resources: {{- toYaml .Values.speaker.frr.resources | nindent 12 }}
          {{- else if ne .Values.speaker.frr.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.speaker.frr.resourcesPreset) | nindent 12 }}
          {{- end }}
        - name: frr-metrics
          image: {{ include "common.images.image" (dict "imageRoot" .Values.speaker.frr.image "global" .Values.global) }}
          imagePullPolicy: {{ .Values.speaker.frr.image.pullPolicy }}
          {{- if .Values.speaker.frr.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.speaker.frr.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          command: ["/etc/frr_metrics/frr-metrics"]
          args:
            - --metrics-port={{ .Values.speaker.frr.containerPorts.metrics }}
          ports:
            - containerPort: {{ .Values.speaker.frr.containerPorts.metrics }}
              name: monitoring
          volumeMounts:
            - name: frr-sockets
              mountPath: /var/run/frr
            - name: frr-conf
              mountPath: /etc/frr
            - name: metrics
              mountPath: /etc/frr_metrics
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.speaker.frr.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.speaker.frr.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.speaker.frr.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.speaker.frr.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /livez
              port: {{ .Values.speaker.frr.containerPorts.metrics }}
          {{- end }}
          {{- if .Values.speaker.frr.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.speaker.frr.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.speaker.frr.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.speaker.frr.readinessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: {{ .Values.speaker.frr.containerPorts.metrics }}
          {{- end }}
          {{- if .Values.speaker.frr.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.speaker.frr.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.speaker.frr.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.speaker.frr.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /livez
              port: {{ .Values.speaker.frr.containerPorts.metrics }}
          {{- end }}
          {{- end }}

          {{- if .Values.speaker.frr.resources }}
          resources: {{- toYaml .Values.speaker.frr.resources | nindent 12 }}
          {{- else if ne .Values.speaker.frr.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.speaker.frr.resourcesPreset) | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- if .Values.speaker.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.speaker.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: memberlist-secret
          secret:
            secretName: {{ include "metallb.speaker.secretName" . }}
            defaultMode: 0444
            items:
              - key: {{ include "metallb.speaker.secretKey" . }}
                path: secretkey
        {{- if .Values.speaker.frr.enabled }}
        - name: frr-sockets
          emptyDir: {}
        - name: frr-startup
          configMap:
            name: {{ printf "%s-speaker-frr-startup" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
        - name: frr-conf
          emptyDir: {}
        - name: reloader
          emptyDir: {}
        - name: metrics
          emptyDir: {}
        {{- end }}
        {{- if .Values.speaker.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.speaker.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
