{{- if and .Values.admissionWebhooks.enabled .Values.admissionWebhooks.patch.enabled (not .Values.admissionWebhooks.certManager.enabled) }}
apiVersion: batch/v1
kind: Job
metadata:
  name:  {{ template "oam-runtime.fullname" . }}-admission-patch
  namespace: {{ .Release.Namespace }}
  annotations:
    "helm.sh/hook": post-install,post-upgrade
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
  labels:
    app: {{ template "oam-runtime.name" . }}-admission-patch
    {{- include "oam-runtime.labels" . | nindent 4 }}
spec:
  {{- if .Capabilities.APIVersions.Has "batch/v1alpha1" }}
  # Alpha feature since k8s 1.12
  ttlSecondsAfterFinished: 0
  {{- end }}
  template:
    metadata:
      name:  {{ template "oam-runtime.fullname" . }}-admission-patch
      labels:
        app: {{ template "oam-runtime.name" . }}-admission-patch
        {{- include "oam-runtime.labels" . | nindent 8 }}
    spec:
      containers:
        - name: patch
          image: {{ .Values.admissionWebhooks.patch.image.repository }}:{{ .Values.admissionWebhooks.patch.image.tag }}
          imagePullPolicy: {{ .Values.admissionWebhooks.patch.image.pullPolicy }}
          args:
            - patch
            - --webhook-name={{ template "oam-runtime.fullname" . }}-admission
            - --namespace={{ .Release.Namespace }}
            - --secret-name={{ template "oam-runtime.fullname" . }}-admission
            - --patch-failure-policy={{ .Values.admissionWebhooks.failurePolicy }}
      restartPolicy: OnFailure
      serviceAccountName: {{ template "oam-runtime.fullname" . }}-admission
      {{- with .Values.admissionWebhooks.patch.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- with .Values.admissionWebhooks.patch.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
      {{- end }}
      securityContext:
        runAsGroup: 2000
        runAsNonRoot: true
        runAsUser: 2000
{{- end }}
