apiVersion: v1
kind: ConfigMap
metadata:
  namespace: {{ .Values.namespace }}
  name: pre-hook-cfg
  annotations:
    "helm.sh/hook": pre-install
    "helm.sh/hook-weight": "100"
    "helm.sh/hook-delete-policy": hook-succeeded
data:
  metadata.json: |
    {
      "metadata": {
        "annotations": {
        "meta.helm.sh/release-name": "eks-anywhere-packages",
        "meta.helm.sh/release-namespace": "eksa-packages"
        },
        "labels": {
          "app.kubernetes.io/managed-by": "Helm"
        }
      }
    }

  metadatacrds.json: |
    {
      "metadata": {
        "annotations": {
        "meta.helm.sh/release-name": "eks-anywhere-packages-crds",
        "meta.helm.sh/release-namespace": "eksa-packages"
        },
        "labels": {
          "app.kubernetes.io/managed-by": "Helm"
        }
      }
    }
---
apiVersion: batch/v1
kind: Job
metadata:
  name: pre-install-hook
  namespace: {{ .Values.namespace }}
  labels:
    app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
    app.kubernetes.io/instance: {{ .Release.Name | quote }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
  annotations:
    "helm.sh/hook": pre-install
    "helm.sh/hook-weight": "100"
    "helm.sh/hook-delete-policy": hook-succeeded
spec:
  template:
    metadata:
      name: "{{ .Release.Name }}"
      namespace: {{ .Values.namespace }}
      labels:
        app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
        app.kubernetes.io/instance: {{ .Release.Name | quote }}
    spec:
      restartPolicy: Never
      serviceAccountName: {{ .Values.serviceAccount.name }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
      - name: patch-awssecret
        image: {{.Values.sourceRegistry}}{{ template "template.image" .Values.controller }}
        volumeMounts:
        - name: config
          mountPath: /opt/packages-migrations
        command: ["kubectl", "patch", "secret", "-n", "eksa-packages", "aws-secret", "--patch-file", "/opt/packages-migrations/metadata.json"]

      - name: patch-crds-1
        image: {{.Values.sourceRegistry}}{{ template "template.image" .Values.controller }}
        volumeMounts:
        - name: config
          mountPath: /opt/packages-migrations
        command: ["kubectl", "patch", "crd", "packages.packages.eks.amazonaws.com", "--patch-file", "/opt/packages-migrations/metadatacrds.json"]

      - name: patch-crds-2
        image: {{.Values.sourceRegistry}}{{ template "template.image" .Values.controller }}
        volumeMounts:
        - name: config
          mountPath: /opt/packages-migrations
        command: ["kubectl", "patch", "crd", "packagebundlecontrollers.packages.eks.amazonaws.com", "--patch-file", "/opt/packages-migrations/metadatacrds.json"]

      - name: patch-crds-3
        image: {{.Values.sourceRegistry}}{{ template "template.image" .Values.controller }}
        volumeMounts:
        - name: config
          mountPath: /opt/packages-migrations
        command: ["kubectl", "patch", "crd", "packagebundles.packages.eks.amazonaws.com", "--patch-file", "/opt/packages-migrations/metadatacrds.json"]

      volumes:
      - name: config
        configMap:
          name: pre-hook-cfg
