{{- if .Values.operator.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.operator.replicaCount }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  {{- if .Values.operator.updateStrategy }}
  strategy: {{ toYaml .Values.operator.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        {{- if .Values.operator.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.operator.podLabels "context" $) | nindent 8 }}
        {{- end }}
      {{- if .Values.operator.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.operator.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
    spec:
      {{- include "common.images.pullSecrets" (dict "images" (list .Values.operator.image .Values.grafana.image .Values.grafana.pluginsInitContainerImage) "global" .Values.global) | nindent 6 }}
      {{- if .Values.operator.priorityClassName }}
      priorityClassName: {{ .Values.operator.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.operator.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.operator.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.schedulerName }}
      schedulerName: {{ .Values.operator.schedulerName | quote }}
      {{- end }}
      {{- if .Values.operator.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.operator.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.operator.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.operator.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        {{- if not (empty .Values.operator.podAffinityPreset) }}
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.operator.podAffinityPreset "component" "grafana-operator" "context" $) | nindent 10 }}
        {{- end }}
        {{- if not (empty .Values.operator.podAntiAffinityPreset) }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.operator.podAntiAffinityPreset "component" "grafana-operator" "context" $) | nindent 10 }}
        {{- end }}
        {{- if not (empty .Values.operator.nodeAffinityPreset.type) }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.operator.nodeAffinityPreset.type "key" .Values.operator.nodeAffinityPreset.key "values" .Values.operator.nodeAffinityPreset.values) | nindent 10 }}
        {{- end }}
      {{- end }}
      {{- if .Values.operator.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.operator.tolerations "context" $) | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "grafana-operator.serviceAccountName" . }}
      {{- if .Values.operator.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.operator.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.operator.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.operator.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.operator.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: grafana-operator
          image: {{ include "common.images.image" (dict "imageRoot" .Values.operator.image "global" .Values.global) }}
          imagePullPolicy: {{ .Values.operator.image.pullPolicy }}
          {{- if .Values.operator.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.operator.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.operator.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.operator.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - grafana-operator
          {{- end }}
          {{- if .Values.operator.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.operator.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --grafana-image={{ include "grafana-operator.grafana.baseImage" . }}
            - --grafana-image-tag={{ .Values.grafana.image.tag | trim }}
            - --grafana-plugins-init-container-image={{ include "grafana-operator.grafana.pluginsInitContainerImage" . }}
            - --grafana-plugins-init-container-tag={{ default .Values.grafana.image.tag .Values.grafana.pluginsInitContainerImage.tag | trim }}
            {{- if .Values.operator.leaderElect }}
            - --leader-elect=True
            {{- end }}
            {{- if (and .Values.operator.scanAllNamespaces (not .Values.operator.scanNamespaces)) }}
            - --scan-all=True
            {{- else if (and .Values.operator.scanNamespaces (not .Values.operator.scanAllNamespaces )) }}
            - --namespaces={{ include "grafana-operator.joinListWithComma" .Values.operator.scanNamespaces }}
            {{- end }}
            {{- if .Values.operator.zapDevel }}
            - --zap-devel={{ .Values.operator.zapDevel }}
            {{- end }}
            {{- if .Values.operator.zapEncoder }}
            - --zap-encoder={{ .Values.operator.zapEncoder }}
            {{- end }}
            {{- if .Values.operator.zapLevel }}
            - --zap-level=True
            {{- end }}
            {{- if .Values.operator.zapSample }}
            - --zap-sample={{ .Values.operator.zapSample }}
            {{- end }}
            {{- if .Values.operator.zapStacktraceLevel }}
            - --zap-stacktrace-level={{ .Values.operator.zapStacktraceLevel }}
            {{- end }}
            {{- if .Values.operator.zapTimeEncoding }}
            - --zap-time-encoding={{ .Values.operator.zapTimeEncoding }}
            {{- end }}
            {{- if .Values.operator.extraArgs }}
            {{- include "common.tplvalues.render" (dict "value" .Values.operator.extraArgs "context" $) | nindent 12 }}
            {{- end }}
          {{- end }}
          {{- if .Values.operator.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.operator.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: TEMPLATE_PATH
              value: /usr/local/bin/templates
            - name: WATCH_NAMESPACE
              {{- if .Values.operator.watchNamespace }}
              value: {{ .Values.operator.watchNamespace }}
              {{- else }}
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
              {{- end }}
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: OPERATOR_NAME
              value: {{ include "common.names.fullname" . }}
            {{- if .Values.operator.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.operator.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.operator.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.operator.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.operator.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.operator.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.operator.resources }}
          resources: {{- toYaml .Values.operator.resources | nindent 12 }}
          {{- end }}
          ports:
            - containerPort: {{ .Values.operator.containerPorts.metrics }}
              name: metrics
              protocol: TCP
          {{- if .Values.operator.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.operator.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: 8081
          {{- else if .Values.operator.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.operator.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.operator.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.operator.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /metrics
              port: metrics
          {{- else if .Values.operator.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.operator.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.operator.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.operator.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /metrics
              port: metrics
          {{- else if .Values.operator.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.operator.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.operator.extraVolumeMounts }}
          volumeMounts: {{- include "common.tplvalues.render" (dict "value" .Values.operator.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.operator.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.operator.sidecars "context" $) | nindent 8 }}
        {{- end }}
      {{- if .Values.operator.extraVolumes }}
      volumes: {{- include "common.tplvalues.render" (dict "value" .Values.operator.extraVolumes "context" $) | nindent 8 }}
      {{- end }}
{{- end }}
