{{- if and .Values.prometheusOperator.admissionWebhooks.enabled .Values.prometheusOperator.admissionWebhooks.patch.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
  name:  {{ template "prometheus-operator.fullname" . }}-admission-create
  namespace: {{ template "prometheus-operator.namespace" . }}
  annotations:
    "helm.sh/hook": pre-install,pre-upgrade
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
  labels:
    app: {{ template "prometheus-operator.name" $ }}-admission-create
{{- include "prometheus-operator.labels" $ | indent 4 }}
spec:
  {{- if .Capabilities.APIVersions.Has "batch/v1alpha1" }}
  # Alpha feature since k8s 1.12
  ttlSecondsAfterFinished: 0
  {{- end }}
  template:
    metadata:
      name:  {{ template "prometheus-operator.fullname" . }}-admission-create
{{- with .Values.prometheusOperator.admissionWebhooks.patch.podAnnotations }}
      annotations:
{{ toYaml .  | indent 8 }}
{{- end }}
      labels:
        app: {{ template "prometheus-operator.name" $ }}-admission-create
{{- include "prometheus-operator.labels" $ | indent 8 }}
    spec:
      {{- if .Values.prometheusOperator.admissionWebhooks.patch.priorityClassName }}
      priorityClassName: {{ .Values.prometheusOperator.admissionWebhooks.patch.priorityClassName }}
      {{- end }}
      containers:
        - name: create
          image: {{ .Values.prometheusOperator.admissionWebhooks.patch.image.repository }}:{{ .Values.prometheusOperator.admissionWebhooks.patch.image.tag }}
          imagePullPolicy: {{ .Values.prometheusOperator.admissionWebhooks.patch.image.pullPolicy }}
          args:
            - create
            - --host={{ template "prometheus-operator.operator.fullname" . }},{{ template "prometheus-operator.operator.fullname" . }}.{{ template "prometheus-operator.namespace" . }}.svc
            - --namespace={{ template "prometheus-operator.namespace" . }}
            - --secret-name={{ template "prometheus-operator.fullname" . }}-admission
          resources:
{{ toYaml .Values.prometheusOperator.admissionWebhooks.patch.resources | indent 12 }}
      restartPolicy: OnFailure
      serviceAccountName: {{ template "prometheus-operator.fullname" . }}-admission
      {{- with .Values.prometheusOperator.admissionWebhooks.patch.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- with .Values.prometheusOperator.admissionWebhooks.patch.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- with .Values.prometheusOperator.admissionWebhooks.patch.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
      {{- end }}
      securityContext:
        runAsGroup: 2000
        runAsNonRoot: true
        runAsUser: 2000
{{- end }}
