---
apiVersion: batch/v1
kind: Job
metadata:
  name: airm-configure
  namespace: airm
spec:
  backoffLimit: 3
  template:
    metadata:
      labels:
        app: airm-configure
    spec:
      containers:
        - env:
            - name: DEBIAN_FRONTEND
              value: noninteractive
            - name: ORG_NAME
              value: demo
            - name: NEW_DOMAIN_NAME
              value: aiplatform.combient.com
            - name: KEYCLOAK_CLIENT_ID
              value: 354a0fa1-35ac-4a6d-9c4d-d661129c2cd0
            - name: KEYCLOAK_CLIENT_SECRET
              valueFrom:
                secretKeyRef:
                  key: KEYCLOAK_SECRET
                  name: airm-keycloak-ui-creds
            - name: USER_EMAIL
              value: devuser@aiplatform.combient.com
            - name: KEYCLOAK_URL
              value: http://keycloak.keycloak.svc.cluster.local:8080
            - name: AIRM_API_URL
              value: http://airm-api.airm.svc.cluster.local
            - name: CLUSTER_BASE_URL
              value: https://workspaces.aiplatform.combient.com/
          image: ghcr.io/silogen/airm-configure:v2025.09.001
          imagePullPolicy: IfNotPresent
          name: configure
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              add:
                - SETUID
                - SETGID
                - CHOWN
                - DAC_OVERRIDE
                - FOWNER
                - FSETID
              drop:
                - ALL
            runAsNonRoot: false
            runAsUser: 0
            seccompProfile:
              type: RuntimeDefault
      initContainers:
        - args:
            - |
              apt-get update -y 1> /dev/null 2>&1
              apt-get install curl -y 1> /dev/null 2>&1
              echo "Checking dependencies..."

              echo "Checking keycloak..."
              while true; do
                if curl -s --max-time 5 http://keycloak.keycloak.svc.cluster.local:8080/realms/airm/.well-known/openid-configuration > /dev/null 2>&1; then
                  echo "Keycloak is ready!"
                  break
                else
                  echo "Waiting for Keycloak..."
                  sleep 10
                fi
              done

              echo "Checking airm-api..."
              while true; do
                if curl -s --max-time 5 http://airm-api.airm.svc.cluster.local/v1/health > /dev/null 2>&1; then
                  echo "AIRM API is ready!"
                  break
                else
                  echo "Waiting for AIRM API..."
                  sleep 10
                fi
              done

              echo "Checking rabbitmq..."
              while true; do
                if curl -s --max-time 5 http://airm-rabbitmq.airm.svc.cluster.local:15672/api/overview > /dev/null 2>&1; then
                  echo "RabbitMQ is ready!"
                  break
                else
                  echo "Waiting for RabbitMQ..."
                  sleep 10
                fi
              done

              echo "All dependencies are ready!"
          command:
            - /bin/bash
            - -c
          image: ubuntu@sha256:09506232a8004baa32c47d68f1e5c307d648fdd59f5e7eaa42aaf87914100db3 # Original tag: 22.04
          imagePullPolicy: IfNotPresent
          name: wait-for-dependencies
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              add:
                - SETUID
                - SETGID
                - CHOWN
                - DAC_OVERRIDE
                - FOWNER
                - FSETID
              drop:
                - ALL
            runAsNonRoot: false
            runAsUser: 0
            seccompProfile:
              type: RuntimeDefault
      restartPolicy: Never
      serviceAccountName: airm-configure-sa