#!/usr/bin/env bash
# karpenter/karpenter 1.14.0 - base variant: crds-managed
# 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/karpenter-karpenter-1-14-0.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 crds-managed base variant into ./karpenter-karpenter-1-14-0-crds-managed"
cub installer setup --pull oci://europe-west1-docker.pkg.dev/nth-fort-499605-q5/helm-expt/karpenter-karpenter:1.14.0@sha256:9df71774cdd0dec1b03d8f417d28176833a14f1bb3d094e01502f46101e777cc --base crds-managed --work-dir ./karpenter-karpenter-1-14-0-crds-managed --non-interactive --namespace kube-system

say "Read what was rendered; nothing has touched the cluster yet"
ls ./karpenter-karpenter-1-14-0-crds-managed/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 kube-system namespace exists"
kubectl create namespace kube-system --dry-run=client -o yaml | kubectl apply -f -

say "Check 5 CRDs included with this package"
missing_crds=0
if ! kubectl get crd/capacitybuffers.autoscaling.x-k8s.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/ec2nodeclasses.karpenter.k8s.aws >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/nodeclaims.karpenter.sh >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/nodeoverlays.karpenter.sh >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/nodepools.karpenter.sh >/dev/null 2>&1; then
  missing_crds=1
fi
if [ "$missing_crds" -eq 1 ]; then
  kubectl apply --server-side -f ./karpenter-karpenter-1-14-0-crds-managed/package/prerequisites/target-facts/crds-managed-crds.yaml
else
  say "The required CRDs already exist; leave them under their current owner"
fi
wait_for_crd capacitybuffers.autoscaling.x-k8s.io
wait_for_crd ec2nodeclasses.karpenter.k8s.aws
wait_for_crd nodeclaims.karpenter.sh
wait_for_crd nodeoverlays.karpenter.sh
wait_for_crd nodepools.karpenter.sh

if [ "${REQUIREMENTS_READY:-0}" != "1" ]; then
  cat >&2 <<'EOF_REQUIREMENTS'
This base variant needs resources you must create with your own values first:
  - minimum schedulable nodes 1
    use a target with at least 1 schedulable nodes before applying this base
Complete these prerequisites before applying the rendered objects.
Replace any <...> placeholders with values or files for your environment.
When the resources exist, re-run with:
  REQUIREMENTS_READY=1 bash try.sh
EOF_REQUIREMENTS
  exit 1
fi

if [ -d ./karpenter-karpenter-1-14-0-crds-managed/out/secrets ]; then
  say "Apply rendered Secrets first"
  kubectl apply -f ./karpenter-karpenter-1-14-0-crds-managed/out/secrets
fi

say "Apply the rendered objects"
kubectl apply -f ./karpenter-karpenter-1-14-0-crds-managed/out/manifests

say "Done. The cluster received exactly the files in ./karpenter-karpenter-1-14-0-crds-managed/out."
