apiVersion: batch/v1
kind: Job
metadata:
  name: {{ template "application-gateway-kubernetes-ingress.fullname" . }}-cleanup
  labels:
    app: {{ template "application-gateway-kubernetes-ingress.name" . }}-cleanup
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  annotations:
    "helm.sh/hook": pre-delete
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
  template:
    spec:
      serviceAccountName: {{ template "application-gateway-kubernetes-ingress.serviceaccountname" . }}
      restartPolicy: OnFailure
      containers:
      - name: cleanup
        image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        env:
        - name: AGIC_POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        command:
          - "/appgw-ingress"
          - "--cleanup-oec"
{{- if .Values.addon }}
          - "app.kubernetes.io/managed-by=ingress-appgw-addon"
{{- else }}
          - "app.kubernetes.io/managed-by=ingress-appgw-helm"
{{- end }}
        securityContext:
          capabilities:
            drop:
              - ALL
      {{- if .Values.kubernetes.nodeSelector }}
      {{- with .Values.kubernetes.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
      {{- end}}
      {{- else}}
      {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
      {{- end}}
      {{- end}}

      {{- with .Values.kubernetes.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- with .Values.kubernetes.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
        - name: {{ .Values.image.pullSecrets }}
      {{- end }}
