text: {{/*
  Copyright 2020 The Magma Authors.
  
  This source code is licensed under the BSD-style license found in the
  LICENSE file in the root directory of this source tree.
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  */}}
  {{- $serviceName := print .Release.Name "-controller" -}}
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: {{ $serviceName }}
    labels:
      app.kubernetes.io/component: controller
  {{ include "labels" . | indent 4 }}
  spec:
    replicas: {{ .Values.controller.replicas }}
    selector:
      matchLabels:
        app.kubernetes.io/component: controller
  {{ include "selector-labels" . | indent 6 }}
    template:
      metadata:
        labels:
          app.kubernetes.io/component: controller
  {{ include "selector-labels" . | indent 8 }}
        annotations:
          {{- with .Values.controller.podAnnotations }}
  {{ toYaml . | indent 8 }}
          {{- end }}
      spec:
        {{- with .Values.controller.nodeSelector }}
        nodeSelector:
  {{ toYaml . | indent 8 }}
        {{- end }}
        {{- with .Values.controller.tolerations }}
        tolerations:
  {{ toYaml . | indent 8 }}
        {{- end }}
        {{- with .Values.controller.affinity }}
        affinity:
  {{ toYaml . | indent 8 }}
        {{- end }}
        {{- with .Values.imagePullSecrets }}
        imagePullSecrets:
  {{ toYaml . | trimSuffix "\n" | indent 8 }}
        {{- end }}
        volumes:
          - name: certs
            secret:
              secretName: {{ required "secret.certs must be provided" .Values.secret.certs }}
          - name: envdir
            secret:
              secretName: {{ required "secret.envdir must be provided" .Values.secret.envdir }}
          {{- if .Values.secret.configs }}
          {{- range $module, $secretName := .Values.secret.configs }}
          - name: {{ $secretName }}-{{ $module }}
            secret:
              secretName: {{ $secretName }}
          {{- end }}
          {{- else }}
          - name: "empty-configs"
            emptyDir: {}
          {{- end }}
        containers:
          - name: {{ $serviceName }}
            image: {{ required "controller.image.repository must be provided" .Values.controller.image.repository }}:{{ .Values.controller.image.tag }}
            imagePullPolicy: {{ .Values.controller.image.pullPolicy }}
            volumeMounts:
              {{- range tuple "certs" "envdir" }}
              - name: {{ . }}
                mountPath: /var/opt/magma/{{ . }}
                readOnly: true
              {{- end }}
              {{- if .Values.secret.configs }}
              {{- range $module, $secretName := .Values.secret.configs }}
              - name: {{ $secretName }}-{{ $module }}
                mountPath: {{ print "/var/opt/magma/configs/" $module }}
                readOnly: true
              {{- end }}
              {{- else }}
              - name: "empty-configs"
                mountPath: /var/opt/magma/configs
                readOnly: true
              {{- end }}
            ports:
              {{- with .Values.controller.service }}
              - containerPort: {{ .targetPort }}
              {{- range $_, $port := untilStep (.portStart | int) (.portEnd | add1 | int) 1 }}
              - containerPort: {{ $port }}
              {{- end }}
              {{- end }}
            env:
              - name: DATABASE_SOURCE
                valueFrom:
                  secretKeyRef:
                    name: {{ $serviceName }}
                    key: {{ .Values.controller.spec.database.driver }}.connstr
              - name: SQL_DRIVER
                value: {{ .Values.controller.spec.database.driver }}
              - name: SQL_DIALECT
                value: {{ .Values.controller.spec.database.sql_dialect }}
              # Hostname override for dispatcher
              - name: SERVICE_HOST_NAME
                valueFrom:
                  fieldRef:
                    fieldPath: status.podIP
              # Hostname override for metricsd
              - name: HOST_NAME
                valueFrom:
                  fieldRef:
                    fieldPath: status.podIP
            livenessProbe:
              tcpSocket:
                port: 9081
              initialDelaySeconds: 10
              periodSeconds: 30
            readinessProbe:
              tcpSocket:
                port: 9081
              initialDelaySeconds: 5
              periodSeconds: 10
            resources:
  {{ toYaml .Values.controller.resources | indent 12 }}
  