apiVersion: batch/v1
kind: Job
metadata:
  labels:
    job-name: icpa-installer
  name: icpa-installer
  namespace: ${ icpa_namespace }
spec:
  backoffLimit: 1
  template:
    metadata:
      labels:
        job-name: icpa-installer
    spec:
      containers:
      - name: icpa-installer
        image: "${ entitled_registry }/${ entitled_registry_user }/icpa/${ icpa_installer_image }"
        command:
        - /bin/sh
        args:
        - -c
        - /bin/bash /installer/scripts/extra/patch.sh && /installer/entrypoint.sh ${ installer_command }
        # FIX: There is a bug in the icpa-installer container, this bug and its fix is documented at:
        # https://github.ibm.com/IBMCloudPak4Apps/icpa-install/issues/902
        # To fix it, the above 'patch.sh` script is mounted from a ConfigMap, then it is executed.
        # When the bug is fixed, remove the volume and volume_mount parameters for 'patch-volume',
        # the command parameter and replace the 'args' parameter for:
        #    args  = [ var.cp4app_installer_command ]

        # DEBUG: You can debug this job either (1) Viewing the logs or (2) Login into the container:
        # 1) Execute:
        #      pod=$(kubectl get pods --selector=job-name=icpa-installer -n icpa-installer --output=jsonpath='{.items[*].metadata.name}')
        #      kubectl logs -f -n icpa-installer $pod
        # 2) Change the command and args to:
        #      command = ["/bin/sh"]
        #      args = ["-c", "while true; do date; sleep 120;done"]
        #    Then, execute:
        #      pod=$(kubectl get pods --selector=job-name=icpa-installer -n icpa-installer --output=jsonpath='{.items[*].metadata.name}')
        #      kubectl exec --stdin --tty -n icpa-installer $pod -- /bin/bash
        env:
        - name: LICENSE
          value: accept
        - name: KUBECONFIG
          value: /installer/.kube/config
        - name: ENTITLED_REGISTRY
          value: ${ entitled_registry }
        - name: ENTITLED_REGISTRY_USER
          value: ${ entitled_registry_user }
        - name: ENTITLED_REGISTRY_KEY
          value: ${ entitled_registry_key }
        imagePullPolicy: IfNotPresent
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /installer/scripts/extra
          mountPropagation: None
          name: patch-volume
        - mountPath: /data_from_terraform
          mountPropagation: None
          name: config-data-volume
        - mountPath: /installer/.kube
          mountPropagation: None
          name: kubeconfig-volume
      dnsPolicy: ClusterFirst
      enableServiceLinks: true
      imagePullSecrets:
      - name: icpa-installer-pull-secret
      restartPolicy: Never
      schedulerName: default-scheduler
      shareProcessNamespace: false
      terminationGracePeriodSeconds: 30
      volumes:
      - configMap:
          defaultMode: 420
          name: icpa-patch
          optional: false
        name: patch-volume
      - configMap:
          defaultMode: 420
          name: icpa-config-data
          optional: false
        name: config-data-volume
      - configMap:
          defaultMode: 420
          name: icpa-kubeconfig
          optional: false
        name: kubeconfig-volume
