#!/usr/bin/env bash
# nvidia/gpu-operator v26.7.1 - base variant: default
# Path: pull the package, render this base variant locally, read the objects,
# then apply them with kubectl. No ConfigHub account is needed.
# Generated by scripts/generate-public-site.mjs from the committed package
# data for this base variant. Chart page: https://confighub.github.io/helm-expt/site/charts/nvidia-gpu-operator-v26-7-1.html
set -euo pipefail

say() { printf '\n>> %s\n' "$*"; }

if ! command -v cub >/dev/null 2>&1; then
  printf 'cub is not installed. Install the cub CLI with:\n  curl -fsSL https://hub.confighub.com/cub/install.sh | bash\nthen add ~/.confighub/bin to your PATH and re-run this script.\n' >&2
  exit 1
fi

# Installer is a cub plugin. Check it before this script does any work.
if ! cub installer --help >/dev/null 2>&1; then
  printf 'The cub installer plugin is missing. Follow https://confighub.github.io/helm-expt/site/try.html#install-cub, then re-run this script.\n' >&2
  exit 1
fi

if ! command -v kustomize >/dev/null 2>&1; then
  printf 'kustomize is missing. Follow https://confighub.github.io/helm-expt/site/try.html#install-cub, then re-run this script.\n' >&2
  exit 1
fi

if ! command -v kubectl >/dev/null 2>&1; then
  printf 'kubectl is required for the apply step.\n' >&2
  exit 1
fi

say "Pull the package and render the default base variant into ./nvidia-gpu-operator-v26-7-1-default"
cub installer setup --pull oci://europe-west1-docker.pkg.dev/nth-fort-499605-q5/helm-expt/nvidia-gpu-operator:v26.7.1@sha256:03fa8aec9221b64cc5c25f3334775338a9f98c0529bad66f12340d124c238b71 --base default --work-dir ./nvidia-gpu-operator-v26-7-1-default --non-interactive --namespace gpu-operator

say "Read what was rendered; nothing has touched the cluster yet"
ls ./nvidia-gpu-operator-v26-7-1-default/out/manifests

wait_for_crd() {
  crd_name="$1"
  deadline=$(( $(date +%s) + 180 ))
  until kubectl get "crd/${crd_name}" >/dev/null 2>&1; do
    if [ "$(date +%s)" -ge "$deadline" ]; then
      printf "CRD %s did not appear within 180 seconds.\n" "$crd_name" >&2
      return 1
    fi
    sleep 2
  done
  kubectl wait --for=condition=Established --timeout=120s "crd/${crd_name}"
}

say "Ensure the gpu-operator namespace exists"
kubectl create namespace gpu-operator --dry-run=client -o yaml | kubectl apply -f -

say "Check 8 CRDs included with this package"
missing_crds=0
if ! kubectl get crd/clusterpolicies.nvidia.com >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/computedomaincliques.resource.nvidia.com >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/computedomains.resource.nvidia.com >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/gpuclusters.nvidia.com >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/nodefeaturegroups.nfd.k8s-sigs.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/nodefeaturerules.nfd.k8s-sigs.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/nodefeatures.nfd.k8s-sigs.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/nvidiadrivers.nvidia.com >/dev/null 2>&1; then
  missing_crds=1
fi
if [ "$missing_crds" -eq 1 ]; then
  kubectl apply --server-side -f ./nvidia-gpu-operator-v26-7-1-default/package/prerequisites/target-facts/default-crds.yaml
elif kubectl diff --server-side -f ./nvidia-gpu-operator-v26-7-1-default/package/prerequisites/target-facts/default-crds.yaml >/dev/null 2>&1; then
  say "The required CRDs already exist and match this package; leave them under their current owner"
elif [ "${APPLY_CRDS:-0}" = "1" ]; then
  say "The existing CRDs differ from this package. APPLY_CRDS=1 is set, so apply the package's CRDs"
  kubectl apply --server-side -f ./nvidia-gpu-operator-v26-7-1-default/package/prerequisites/target-facts/default-crds.yaml
else
  say "The CRDs already exist and differ from this package, or another tool owns them"
  cat <<'EOF_CRDS'
This script does not change CRDs it did not just create.
If this is an upgrade, the objects that follow may need the newer CRDs.
To let this script apply the package's CRDs, re-run with:
  APPLY_CRDS=1 bash try.sh
EOF_CRDS
  exit 1
fi
wait_for_crd clusterpolicies.nvidia.com
wait_for_crd computedomaincliques.resource.nvidia.com
wait_for_crd computedomains.resource.nvidia.com
wait_for_crd gpuclusters.nvidia.com
wait_for_crd nodefeaturegroups.nfd.k8s-sigs.io
wait_for_crd nodefeaturerules.nfd.k8s-sigs.io
wait_for_crd nodefeatures.nfd.k8s-sigs.io
wait_for_crd nvidiadrivers.nvidia.com

if [ -d ./nvidia-gpu-operator-v26-7-1-default/out/secrets ]; then
  say "Apply rendered Secrets first"
  kubectl apply -f ./nvidia-gpu-operator-v26-7-1-default/out/secrets
fi

say "Apply the rendered objects, except the CRDs already settled above"
settled_crds=" clusterpolicies.nvidia.com computedomaincliques.resource.nvidia.com computedomains.resource.nvidia.com gpuclusters.nvidia.com nodefeaturegroups.nfd.k8s-sigs.io nodefeaturerules.nfd.k8s-sigs.io nodefeatures.nfd.k8s-sigs.io nvidiadrivers.nvidia.com "
manifest_args=()
for manifest in ./nvidia-gpu-operator-v26-7-1-default/out/manifests/*.yaml; do
  if grep -q '^kind: CustomResourceDefinition$' "$manifest"; then
    crd_name="$(awk '/^metadata:/{m=1; next} m && /^  name:/{print $2; exit}' "$manifest")"
    case "$settled_crds" in
      *" $crd_name "*) continue ;;
    esac
  fi
  manifest_args+=(-f "$manifest")
done
if [ "${#manifest_args[@]}" -gt 0 ]; then
  kubectl apply "${manifest_args[@]}"
fi

say "Done. The cluster received the files in ./nvidia-gpu-operator-v26-7-1-default/out. Its CRDs were settled first, from the package."
say "Not run by this script: the pre-upgrade, pre-delete and post-delete steps of this base's plan. The entry page says who runs each one."
