apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "metallb.fullname" . }}-controller
  labels: {{- include "metallb.labels" . | nindent 4 }}
    app.kubernetes.io/component: controller
spec:
  revisionHistoryLimit: {{ .Values.controller.revisionHistoryLimit }}
  selector:
    matchLabels: {{- include "metallb.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: controller
  template:
    metadata:
      labels: {{- include "metallb.labels" . | nindent 8 }}
        app.kubernetes.io/component: controller
      {{- if .Values.controller.podAnnotations }}
      annotations: {{- toYaml .Values.controller.podAnnotations | nindent 8 }}
      {{- end }}
    spec:
      {{- include "metallb.controller.imagePullSecrets" . | nindent 6 }}
      serviceAccountName: {{ include "metallb.controllerServiceAccountName" . }}
      terminationGracePeriodSeconds: {{ .Values.controller.terminationGracePeriodSeconds }}
      nodeSelector:
      {{- if .Values.controller.nodeSelector }} {{- include "metallb.tplValue" (dict "value" .Values.controller.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
        "kubernetes.io/os": linux
      {{- if .Values.controller.affinity }}
      affinity: {{- include "metallb.tplValue" (dict "value" .Values.controller.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.controller.tolerations}}
      tolerations: {{- include "metallb.tplValue" (dict "value" .Values.controller.tolerations "context" $) | nindent 8 }}
      {{- end }}
      containers:
      - name: metallb-controller
        image: {{ include "metallb.controller.image" . }}
        imagePullPolicy: {{ .Values.controller.image.pullPolicy }}
        args:
        - --port={{ .Values.controller.containerPort.metrics }}
        - --config={{ include "metallb.configMapName" . }}
        ports:
        - name: metrics
          containerPort: {{ .Values.controller.containerPort.metrics }}
      {{- if .Values.controller.livenessProbe.enabled }}
        livenessProbe:
          httpGet:
            path: /metrics
            port: metrics
          initialDelaySeconds: {{ .Values.controller.livenessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.controller.livenessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.controller.livenessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.controller.livenessProbe.successThreshold }}
          failureThreshold: {{ .Values.controller.livenessProbe.failureThreshold }}
      {{- end }}
      {{- if .Values.controller.readinessProbe.enabled }}
        readinessProbe:
          httpGet:
            path: /metrics
            port: metrics
          initialDelaySeconds: {{ .Values.controller.readinessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.controller.readinessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.controller.readinessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.controller.readinessProbe.successThreshold }}
          failureThreshold: {{ .Values.controller.readinessProbe.failureThreshold }}
      {{- end }}
      {{- if .Values.controller.securityContext.enabled }}
        securityContext:
          allowPrivilegeEscalation: {{ .Values.controller.securityContext.allowPrivilegeEscalation }}
          readOnlyRootFilesystem: {{ .Values.controller.securityContext.readOnlyRootFilesystem }}
          capabilities:
            drop: {{- toYaml .Values.controller.securityContext.capabilities.drop | nindent 12 }}
      {{- end }}
      {{- if .Values.controller.resources }}
        resources: {{- toYaml .Values.controller.resources | nindent 10 }}
      {{- end }}
      {{- if .Values.controller.securityContext.enabled }}
      securityContext:
        runAsUser: {{ .Values.controller.securityContext.runAsUser }}
        fsGroup: {{ .Values.controller.securityContext.fsGroup }}
        runAsNonRoot: {{ .Values.controller.securityContext.runAsNonRoot }}
      {{- end }}
