---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: lifecycle-operator
  namespace: {{ .Release.Namespace | quote }}
  labels:
{{- include "common.labels.standard" ( dict "context" . ) | nindent 4 }}
{{- include "common.annotations" ( dict "context" . ) }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: lifecycle-operator
  namespace: {{ .Release.Namespace | quote }}
  labels:
    app.kubernetes.io/part-of: keptn
    control-plane: lifecycle-operator
    keptn.sh/inject-cert: "true"
{{- include "common.labels.standard" ( dict "context" . ) | nindent 4 }}
{{- include "common.annotations" ( dict "context" . ) }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      control-plane: lifecycle-operator
    {{- include "common.selectorLabels"  ( dict "context" . ) | nindent 6 }}
  template:
    metadata:
      labels:
        control-plane: lifecycle-operator
      {{- include "common.selectorLabels"  ( dict "context" . )  | nindent 8 }}
      annotations:
        kubectl.kubernetes.io/default-container: lifecycle-operator
        metrics.dynatrace.com/port: "2222"
        metrics.dynatrace.com/scrape: "true"
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      containers:
      - args:
        - --leader-elect
        command:
        - /manager
        env:
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: FUNCTION_RUNNER_IMAGE
          value: {{ .Values.env.functionRunnerImage | quote }}
        - name: PYTHON_RUNNER_IMAGE
          value: {{ .Values.env.pythonRunnerImage | quote }}
        - name: KEPTN_APP_CONTROLLER_LOG_LEVEL
          value: {{ .Values.env.keptnAppControllerLogLevel | quote
            }}
        - name: KEPTN_APP_CREATION_REQUEST_CONTROLLER_LOG_LEVEL
          value: {{ .Values.env.keptnAppCreationRequestControllerLogLevel
            | quote }}
        - name: KEPTN_APP_VERSION_CONTROLLER_LOG_LEVEL
          value: {{ .Values.env.keptnAppVersionControllerLogLevel
            | quote }}
        - name: KEPTN_EVALUATION_CONTROLLER_LOG_LEVEL
          value: {{ .Values.env.keptnEvaluationControllerLogLevel
            | quote }}
        - name: KEPTN_TASK_CONTROLLER_LOG_LEVEL
          value: {{ .Values.env.keptnTaskControllerLogLevel | quote
            }}
        - name: KEPTN_TASK_DEFINITION_CONTROLLER_LOG_LEVEL
          value: {{ .Values.env.keptnTaskDefinitionControllerLogLevel
            | quote }}
        - name: KEPTN_WORKLOAD_CONTROLLER_LOG_LEVEL
          value: {{ .Values.env.keptnWorkloadControllerLogLevel
            | quote }}
        - name: KEPTN_WORKLOAD_VERSION_CONTROLLER_LOG_LEVEL
          value: {{ .Values.env.keptnWorkloadVersionControllerLogLevel
            | quote }}
        - name: KEPTN_DORA_METRICS_PORT
          value: {{ .Values.env.keptnDoraMetricsPort | quote }}
        - name: OPTIONS_CONTROLLER_LOG_LEVEL
          value: {{ .Values.env.optionsControllerLogLevel | quote
            }}
        - name: PROMOTION_TASKS_ENABLED
          value: {{ .Values.promotionTasksEnabled | quote
            }}
        - name: KUBERNETES_CLUSTER_DOMAIN
          value: {{ .Values.kubernetesClusterDomain }}
        - name: CERT_MANAGER_ENABLED
          value: {{ .Values.global.certManagerEnabled | quote }}
        image: {{ include "common.images.image" ( dict "imageRoot" .Values.image "global" .Values.global ) }}
        imagePullPolicy: {{ .Values.image.imagePullPolicy | default (.Values.global.imagePullPolicy | default "IfNotPresent") }}
        name: lifecycle-operator
        ports:
        - containerPort: 9443
          name: webhook-server
          protocol: TCP
        - containerPort: 2222
          name: metrics
          protocol: TCP
        resources: {{- toYaml .Values.resources | nindent 10 }}
        securityContext:
          allowPrivilegeEscalation: {{ .Values.containerSecurityContext.allowPrivilegeEscalation
            }}
          capabilities: {{- include "common.tplvalues.render" (dict "value" .Values.containerSecurityContext.capabilities
            "context" $) | nindent 12 }}
          privileged: {{ .Values.containerSecurityContext.privileged
            }}
          {{- if not .Values.global.openShift.enabled }}
          runAsGroup: {{ .Values.containerSecurityContext.runAsGroup
            }}
          runAsUser: {{ .Values.containerSecurityContext.runAsUser
            }}
          {{- end }}
          runAsNonRoot: {{ .Values.containerSecurityContext.runAsNonRoot
            }}
          seccompProfile: {{- include "common.tplvalues.render" (dict "value" .Values.containerSecurityContext.seccompProfile
            "context" $) | nindent 12 }}
        volumeMounts:
        - name: keptn-certs
          mountPath: /tmp/webhook/certs/
        {{- if .Values.livenessProbe }}
        livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.livenessProbe "context" $) | nindent 10 }}
         {{- else }}
        livenessProbe:
         httpGet:
           path: /healthz
           port: 8081
         initialDelaySeconds: 15
         periodSeconds: 20
        {{- end }}
        {{- if .Values.readinessProbe }}
        readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.readinessProbe "context" $) | nindent 10 }}
         {{- else }}
        readinessProbe:
         httpGet:
           path: /readyz
           port: 8081
         initialDelaySeconds: 5
         periodSeconds: 10
        {{- end }}
      {{- include "imagePullSecrets" . | nindent 6 }}
      securityContext:
        runAsNonRoot: true
      serviceAccountName: lifecycle-operator
      terminationGracePeriodSeconds: 10
      hostNetwork: {{ .Values.hostNetwork }}
      volumes:
      - name: keptn-certs
        secret:
          secretName: keptn-certs
{{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" $) | nindent 8 }}
{{- end }}
{{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
{{- end }}
{{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" .) | nindent 8 }}
{{- end }}
