{{- if .Values.plugin.jobs.patchConsoles.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "openshift-console-plugin.patcherName" . }}
  labels:
    {{- include "openshift-console-plugin.labels" . | nindent 4 }}
  annotations:
    "helm.sh/hook": post-install,post-upgrade
    "helm.sh/hook-weight": "1"
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
  backoffLimit: {{ .Values.plugin.jobs.patchConsoles.backoffLimit }}
  activeDeadlineSeconds: {{ .Values.plugin.jobs.patchConsoles.activeDeadlineSeconds }}
  ttlSecondsAfterFinished: {{ .Values.plugin.jobs.patchConsoles.ttlSecondsAfterFinished }}
  template:
    metadata:
      name: {{ include "openshift-console-plugin.patcherName" . }}
      labels:
        {{- include "openshift-console-plugin.labels" . | nindent 8 }}
    spec:
      serviceAccountName: {{ include "openshift-console-plugin.patcherName" . }}
      restartPolicy: OnFailure
      containers:
        - name: patcher
          image: {{ .Values.plugin.image }}
          imagePullPolicy: Always
          command:
            - /bin/bash
            - -c
            - |
              echo "Patching OpenShift Console..."
              for console in $(oc get console.operator.openshift.io cluster -o name); do
                echo "Patching $console..."
                oc patch $console --type=json -p '[{"op": "add", "path": "/spec/plugins/-", "value": "{{ include "openshift-console-plugin.fullname" . }}"}]'
              done
{{- end }}
