apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ include "metallb.fullname" . }}-speaker
  labels: {{- include "metallb.labels" . | nindent 4 }}
    app.kubernetes.io/component: speaker
spec:
  selector:
    matchLabels: {{- include "metallb.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: speaker
  template:
    metadata:
      labels: {{- include "metallb.labels" . | nindent 8 }}
        app.kubernetes.io/component: speaker
      {{- if .Values.speaker.podAnnotations }}
      annotations: {{- toYaml .Values.speaker.podAnnotations | nindent 8 }}
      {{- end }}
    spec:
      {{- include "metallb.speaker.imagePullSecrets" . | nindent 6 }}
      tolerations:
      - key: node-role.kubernetes.io/master
        effect: NoSchedule
      serviceAccountName: {{ include "metallb.speakerServiceAccountName" . }}
      terminationGracePeriodSeconds: {{ .Values.speaker.daemonset.terminationGracePeriodSeconds }}
      hostNetwork: true
      containers:
      - name: metallb-speaker
        image: {{ include "metallb.speaker.image" . }}
        imagePullPolicy: {{ .Values.speaker.image.pullPolicy }}
        args:
        - --port={{ .Values.speaker.daemonset.hostPorts.metrics }}
        - --config={{ include "metallb.configMapName" . }}
        env:
        - name: METALLB_NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - 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=metallb,app.kubernetes.io/component=speaker"
        - name: METALLB_ML_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: METALLB_ML_SECRET_KEY
          valueFrom:
            secretKeyRef:
              name: {{ include "metallb.secretName" . }}
              key: {{ include "metallb.secretKey" . }}
        {{- if .Values.speaker.extraEnvVars }}
        {{- include "metallb.tplValue" ( dict "value" .Values.speaker.extraEnvVars "context" $ ) | nindent 8 }}
        {{- end }}
        envFrom:
        {{- if .Values.extraEnvVarsSecret }}
        - secretRef:
            name: {{ include "metallb.tplValue" ( dict "value" .Values.speaker.extraEnvVarsSecret "context" $ ) }}
        {{- end }}
        ports:
        - name: metrics
          containerPort: {{ .Values.speaker.daemonset.hostPorts.metrics }}
      {{- if .Values.speaker.livenessProbe.enabled }}
        livenessProbe:
          httpGet:
            path: /metrics
            port: metrics
          initialDelaySeconds: {{ .Values.speaker.livenessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.speaker.livenessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.speaker.livenessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.speaker.livenessProbe.successThreshold }}
          failureThreshold: {{ .Values.speaker.livenessProbe.failureThreshold }}
      {{- end }}
      {{- if .Values.speaker.readinessProbe.enabled }}
        readinessProbe:
          httpGet:
            path: /metrics
            port: metrics
          initialDelaySeconds: {{ .Values.speaker.readinessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.speaker.readinessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.speaker.readinessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.speaker.readinessProbe.successThreshold }}
          failureThreshold: {{ .Values.speaker.readinessProbe.failureThreshold }}
      {{- end }}
      {{- if .Values.speaker.resources }}
        resources: {{- toYaml .Values.speaker.resources | nindent 10 }}
      {{- end }}
      {{- if .Values.speaker.securityContext.enabled }}
        securityContext:
          runAsUser: {{ .Values.speaker.securityContext.runAsUser }}
          allowPrivilegeEscalation: {{ .Values.speaker.securityContext.allowPrivilegeEscalation }}
          readOnlyRootFilesystem: {{ .Values.speaker.securityContext.readOnlyRootFilesystem }}
          capabilities:
            drop: {{- toYaml .Values.speaker.securityContext.capabilities.drop | nindent 12 }}
            add: {{- toYaml .Values.speaker.securityContext.capabilities.add | nindent 12 }}
      {{- end }}
      nodeSelector:
      {{- if .Values.speaker.nodeSelector }} {{- include "metallb.tplValue" (dict "value" .Values.speaker.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
        "kubernetes.io/os": linux
      {{- if .Values.speaker.affinity }}
      affinity: {{- include "metallb.tplValue" (dict "value" .Values.speaker.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.speaker.tolerations}}
      tolerations: {{- include "metallb.tplValue" (dict "value" .Values.speaker.tolerations "context" $) | nindent 8 }}
      {{- end }}
