{{- if .Values.operator.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "kube-prometheus.operator.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "kube-prometheus.operator.labels" . | 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: 1
  selector:
    matchLabels: {{- include "kube-prometheus.operator.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels: {{- include "kube-prometheus.operator.labels" . | 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:
      serviceAccountName: {{ template "kube-prometheus.operator.serviceAccountName" . }}
      {{- include "kube-prometheus.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.operator.schedulerName }}
      schedulerName: {{ .Values.operator.schedulerName | quote }}
      {{- end }}
      {{- if .Values.operator.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.operator.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.priorityClassName }}
      priorityClassName: {{ .Values.operator.priorityClassName }}
      {{- end }}
      {{- if .Values.operator.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.operator.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.operator.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.operator.podAffinityPreset "component" "operator" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.operator.podAntiAffinityPreset "component" "operator" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.operator.nodeAffinityPreset.type "key" .Values.operator.nodeAffinityPreset.key "values" .Values.operator.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.operator.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.operator.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.operator.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.operator.topologySpreadConstraints "context" .) | 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: prometheus-operator
          image: {{ template "kube-prometheus.image" . }}
          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 }}
          {{- end }}
          {{- if .Values.operator.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.operator.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            {{- if .Values.operator.namespaces }}
            - --namespaces={{ .Values.operator.namespaces }}
            {{- end }}
            {{- if .Values.operator.kubeletService.enabled }}
            - --kubelet-service={{ .Values.operator.kubeletService.namespace }}/{{ template "kube-prometheus.fullname" . }}-kubelet
            {{- end }}
            {{- if .Values.operator.logFormat }}
            - --log-format={{ .Values.operator.logFormat }}
            {{- end }}
            {{- if .Values.operator.logLevel }}
            - --log-level={{ .Values.operator.logLevel }}
            {{- end }}
            - --localhost=127.0.0.1
            - --prometheus-config-reloader=$(PROMETHEUS_CONFIG_RELOADER)
            {{- if .Values.operator.configReloaderResources.requests }}
              {{- if .Values.operator.configReloaderResources.requests.cpu }}
            - --config-reloader-cpu-request={{ .Values.operator.configReloaderResources.requests.cpu }}
              {{- end }}
              {{- if .Values.operator.configReloaderResources.requests.memory }}
            - --config-reloader-memory-request={{ .Values.operator.configReloaderResources.requests.memory }}
              {{- end }}
            {{- end }}
            {{- if .Values.operator.configReloaderResources.limits }}
              {{- if .Values.operator.configReloaderResources.limits.cpu }}
            - --config-reloader-cpu-limit={{ .Values.operator.configReloaderResources.limits.cpu }}
              {{- end }}
              {{- if .Values.operator.configReloaderResources.limits.memory }}
            - --config-reloader-memory-limit={{ .Values.operator.configReloaderResources.limits.memory }}
              {{- end }}
            {{- 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: PROMETHEUS_CONFIG_RELOADER
              valueFrom:
                configMapKeyRef:
                  name: {{ template "kube-prometheus.operator.fullname" . }}
                  key: prometheus-config-reloader
            {{- 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 }}
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          {{- if .Values.operator.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.operator.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.operator.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.operator.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /metrics
              port: http
              scheme: HTTP
          {{- end }}
          {{- if .Values.operator.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.operator.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.operator.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.operator.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /metrics
              port: http
              scheme: HTTP
          {{- end }}
          {{- if .Values.operator.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.operator.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.operator.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.operator.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /metrics
              port: http
              scheme: HTTP
          {{- end }}
          {{- if .Values.operator.resources }}
          resources: {{- toYaml .Values.operator.resources | 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 }}
