{{- if and .Values.admissionWebhooks.enabled .Values.admissionWebhooks.patch.enabled (not .Values.admissionWebhooks.certManager.enabled) }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ template "kubevela.fullname" . }}-admission-patch
  namespace: {{ .Release.Namespace }}
  annotations:
    "helm.sh/hook": post-install,post-upgrade,post-rollback
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
  labels:
    app: {{ template "kubevela.name" . }}-admission-patch
    {{- include "kubevela.labels" . | nindent 4 }}
spec:
  {{- if .Capabilities.APIVersions.Has "batch/v1alpha1" }}
  # Alpha feature since k8s 1.12
  ttlSecondsAfterFinished: 0
  {{- end }}
  template:
    metadata:
      name: {{ template "kubevela.fullname" . }}-admission-patch
      labels:
        app: {{ template "kubevela.name" . }}-admission-patch
        {{- include "kubevela.labels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: patch
          image: {{ .Values.imageRegistry }}{{ .Values.admissionWebhooks.patch.image.repository }}:{{ .Values.admissionWebhooks.patch.image.tag }}
          imagePullPolicy: {{ .Values.admissionWebhooks.patch.image.pullPolicy }}
          args:
            - patch
            - --webhook-name={{ template "kubevela.fullname" . }}-admission
            - --namespace={{ .Release.Namespace }}
            - --secret-name={{ template "kubevela.fullname" . }}-admission
            - --patch-failure-policy={{ .Values.admissionWebhooks.failurePolicy }}
            {{- if .Values.admissionWebhooks.appConversion.enabled }}
            - --crds={"name":"applications.core.oam.dev","conversion":{"strategy":"Webhook","webhook":{"clientConfig":{"service":{"name":"vela-core-webhook","namespace":"vela-system","path":"/convert","port":443}},"conversionReviewVersions":["v1beta1","v1alpha2"]}}}
            {{- end }}
      restartPolicy: OnFailure
      serviceAccountName: {{ template "kubevela.fullname" . }}-admission
      {{- if .Values.admissionWebhooks.patch.nodeSelector }}
      nodeSelector:
      {{- toYaml .Values.admissionWebhooks.patch.nodeSelector | nindent 8 }}
      {{- else if .Values.nodeSelector }}
      nodeSelector:
      {{- toYaml .Values.nodeSelector | nindent 8 }}
      {{- end }}
      {{- if .Values.admissionWebhooks.patch.affinity }}
      affinity:
      {{- toYaml .Values.admissionWebhooks.patch.affinity | nindent 8 }}
      {{- else if .Values.affinity }}
      affinity:
      {{- toYaml .Values.affinity | nindent 8 }}
      {{- end }}
      {{- if .Values.admissionWebhooks.patch.tolerations }}
      tolerations:
      {{- toYaml .Values.admissionWebhooks.patch.tolerations | nindent 8 }}
      {{- else if .Values.tolerations }}
      tolerations:
      {{- toYaml .Values.tolerations | nindent 8 }}
      {{- end }}
      securityContext:
        runAsGroup: 2000
        runAsNonRoot: true
        runAsUser: 2000
{{- end }}
