kind: CronJob
apiVersion: batch/v1
metadata:
  annotations:
    kfctl.kubeflow.io/kfdef-instance: rhods-anaconda.redhat-ods-applications
  resourceVersion: '5438800'
  name: anaconda-ce-periodic-validator
  uid: bf7c5641-d5d4-4359-96db-6a5e05292614
  creationTimestamp: '2023-01-11T09:48:24Z'
  generation: 3
  managedFields:
    - manager: application/apply-patch+yaml
      operation: Apply
      apiVersion: batch/v1
      time: '2023-01-12T10:08:14Z'
      fieldsType: FieldsV1
      fieldsV1:
        'f:metadata':
          'f:annotations':
            'f:kfctl.kubeflow.io/kfdef-instance': {}
          'f:labels':
            'f:component.opendatahub.io/name': {}
            'f:opendatahub.io/component': {}
            'f:opendatahub.io/modified': {}
        'f:spec':
          'f:concurrencyPolicy': {}
          'f:failedJobsHistoryLimit': {}
          'f:jobTemplate':
            'f:metadata':
              'f:labels':
                'f:component.opendatahub.io/name': {}
                'f:opendatahub.io/component': {}
            'f:spec':
              'f:template':
                'f:metadata':
                  'f:labels':
                    'f:component.opendatahub.io/name': {}
                    'f:opendatahub.io/component': {}
                    'f:parent': {}
                'f:spec':
                  'f:containers':
                    'k:{"name":"anaconda-ce-validator"}':
                      .: {}
                      'f:command': {}
                      'f:image': {}
                      'f:name': {}
                      'f:resources':
                        'f:limits':
                          'f:cpu': {}
                          'f:memory': {}
                        'f:requests':
                          'f:cpu': {}
                          'f:memory': {}
                      'f:volumeMounts':
                        'k:{"mountPath":"/etc/secret-volume"}':
                          .: {}
                          'f:mountPath': {}
                          'f:name': {}
                          'f:readOnly': {}
                  'f:imagePullSecrets':
                    'k:{"name":"addon-managed-odh-pullsecret"}': {}
                  'f:restartPolicy': {}
                  'f:serviceAccount': {}
                  'f:volumes':
                    'k:{"name":"secret-volume"}':
                      .: {}
                      'f:name': {}
                      'f:secret':
                        'f:secretName': {}
          'f:schedule': {}
          'f:startingDeadlineSeconds': {}
          'f:successfulJobsHistoryLimit': {}
          'f:suspend': {}
  namespace: redhat-ods-applications
  labels:
    component.opendatahub.io/name: anaconda-ce
    opendatahub.io/component: 'true'
    opendatahub.io/modified: 'false'
spec:
  schedule: 0 0 * * *
  startingDeadlineSeconds: 200
  concurrencyPolicy: Replace
  suspend: true
  jobTemplate:
    metadata:
      creationTimestamp: null
      labels:
        component.opendatahub.io/name: anaconda-ce
        opendatahub.io/component: 'true'
    spec:
      template:
        metadata:
          creationTimestamp: null
          labels:
            component.opendatahub.io/name: anaconda-ce
            opendatahub.io/component: 'true'
            parent: anaconda-ce-periodic-validator
        spec:
          restartPolicy: Never
          serviceAccountName: rhods-dashboard
          imagePullSecrets:
            - name: addon-managed-odh-pullsecret
          schedulerName: default-scheduler
          terminationGracePeriodSeconds: 30
          securityContext: {}
          containers:
            - name: anaconda-ce-validator
              image: >-
                quay.io/openshift/origin-deployer@sha256:baa1a8672911cd3dd71a83f91f534ba865b74027f7c1f53196100c8779d930b7
              command:
                - /bin/sh
                - '-c'
                - >
                  #!/bin/sh

                  IMAGESTREAM_NAME='s2i-minimal-notebook-anaconda'
                  CONFIGMAP_NAME='anaconda-ce-validation-result'
                  BUILDCONFIG_NAME='s2i-minimal-notebook-anaconda'
                  ANACONDA_VERSION='v0.2.2-anaconda'

                  function generate_imagestream() {
                    echo '{"apiVersion":"image.openshift.io/v1","kind":"ImageStream","metadata":{"annotations":{"opendatahub.io/notebook-image-desc":"Notebook with Anaconda tools instead of pip.","opendatahub.io/notebook-image-name":"Anaconda Professional","opendatahub.io/notebook-image-url":"https://github.com/red-hat-data-services/s2i-minimal-notebook-anaconda"},"labels":{"component.opendatahub.io/name":"jupyterhub","opendatahub.io/modified":"false","opendatahub.io/notebook-image":"true"},"name":"s2i-minimal-notebook-anaconda"},"spec":{"lookupPolicy":{"local":true},"tags":[{"annotations":{"opendatahub.io/notebook-python-dependencies":"[{\"name\":\"conda\",\"version\":\"4.13.0\"}]","opendatahub.io/notebook-software":"[{\"name\":\"Anaconda-Python\",\"version\":\"v3.8\"}]"},"name":"v0.2.2-anaconda","referencePolicy":{"type":"Source"}}]}}'
                  }

                  function generate_buildconfig() {
                    echo '{"kind":"BuildConfig","apiVersion":"build.openshift.io/v1","metadata":{"name":"s2i-minimal-notebook-anaconda","namespace":"redhat-ods-applications","labels":{"opendatahub.io/build_type":"notebook_image"}},"spec":{"output":{"to":{"kind":"ImageStreamTag","name":"s2i-minimal-notebook-anaconda:v0.2.2-anaconda"}},"strategy":{"type":"Docker","dockerStrategy":{"dockerfilePath":"Dockerfile.AIO"}},"source":{"type":"Git","git":{"uri":"https://github.com/red-hat-data-services/s2i-minimal-notebook-anaconda","ref":"v0.2.2-anaconda"}},"runPolicy":"Serial"}}'
                  }

                  function create_imagestream() {
                    generate_imagestream | oc apply -f-
                  }

                  function delete_imagestream() {
                    generate_imagestream | oc delete -f-
                  }

                  function create_buildconfig() {
                    generate_buildconfig | oc apply -f-
                  }

                  function delete_buildconfig() {
                    generate_buildconfig | oc delete -f-
                  }

                  function start_build() {
                    if ! oc get imagestreamtags "${BUILDCONFIG_NAME}:${ANACONDA_VERSION}" 2>/dev/null; then
                      oc start-build "${BUILDCONFIG_NAME}"
                    fi
                  }

                  function get_variable() {
                    cat "/etc/secret-volume/${1}"
                  }

                  function verify_configmap_exists() {
                    if ! oc get configmap "${CONFIGMAP_NAME}" &>/dev/null; then
                      echo "Result ConfigMap doesn't exist, creating"
                      oc create configmap "${CONFIGMAP_NAME}" --from-literal validation_result="false"
                    fi
                  }

                  function write_configmap_value() {
                    oc patch configmap "${CONFIGMAP_NAME}" -p '"data": { "validation_result": "'${1}'" }'
                  }

                  function write_last_valid_time() {
                    oc patch configmap "${CONFIGMAP_NAME}" -p '"data": { "last_valid_time": "'$(date -Is)'" }'
                  }

                  function success() {
                    echo "Validation succeeded, enabling image"
                    create_imagestream
                    create_buildconfig
                    start_build
                    verify_configmap_exists
                    write_configmap_value true
                    write_last_valid_time
                  }

                  function failure() {
                    echo "Validation failed, disabling image"
                    delete_imagestream
                    delete_buildconfig
                    verify_configmap_exists
                    write_configmap_value false
                  }

                  CURL_RESULT=$(curl -w 'RESP_CODE:%{response_code}' -IHEAD
                  "https://repo.anaconda.cloud/repo/t/$(get_variable
                  Anaconda_ce_key)/main/noarch/repodata.json" 2>/dev/null)
                  CURL_CODE=$(echo "${CURL_RESULT}" | grep -o
                  'RESP_CODE:[1-5][0-9][0-9]'| cut -d':' -f2)

                  echo "Validation result: ${CURL_CODE}"

                  if [ "${CURL_CODE}" == 200 ]; then
                    success
                  elif [ "${CURL_CODE}" == 403 ]; then
                    failure
                  else
                    echo "Return code ${CURL_CODE} from validation check, possibly upstream error. Exiting."
                    echo "Result from curl:"
                    echo "${CURL_RESULT}"
                  fi

                  exit 0
              resources:
                limits:
                  cpu: 100m
                  memory: 256Mi
                requests:
                  cpu: 100m
                  memory: 256Mi
              volumeMounts:
                - name: secret-volume
                  readOnly: true
                  mountPath: /etc/secret-volume
              terminationMessagePath: /dev/termination-log
              terminationMessagePolicy: File
              imagePullPolicy: IfNotPresent
          serviceAccount: rhods-dashboard
          volumes:
            - name: secret-volume
              secret:
                secretName: anaconda-ce-access
                defaultMode: 420
          dnsPolicy: ClusterFirst
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 1
status: {}
