#!/usr/bin/env bash
# kyverno/kyverno 3.8.1 - base variant: no-crds
# 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/kyverno-kyverno-3-8-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 no-crds base variant into ./kyverno-kyverno-3-8-1-no-crds"
cub installer setup --pull oci://europe-west1-docker.pkg.dev/nth-fort-499605-q5/helm-expt/kyverno-kyverno:3.8.1 --base no-crds --work-dir ./kyverno-kyverno-3-8-1-no-crds --non-interactive --namespace default

say "Read what was rendered; nothing has touched the cluster yet"
ls ./kyverno-kyverno-3-8-1-no-crds/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 default namespace exists"
kubectl create namespace default --dry-run=client -o yaml | kubectl apply -f -

say "Check 22 CRDs included with this package"
missing_crds=0
if ! kubectl get crd/cleanuppolicies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/clustercleanuppolicies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/clusterpolicies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/globalcontextentries.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/policies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/policyexceptions.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/updaterequests.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/clusterephemeralreports.reports.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/ephemeralreports.reports.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/clusterpolicyreports.wgpolicyk8s.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/policyreports.wgpolicyk8s.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/deletingpolicies.policies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/generatingpolicies.policies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/imagevalidatingpolicies.policies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/mutatingpolicies.policies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/namespaceddeletingpolicies.policies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/namespacedgeneratingpolicies.policies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/namespacedimagevalidatingpolicies.policies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/namespacedmutatingpolicies.policies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/namespacedvalidatingpolicies.policies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/policyexceptions.policies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/validatingpolicies.policies.kyverno.io >/dev/null 2>&1; then
  missing_crds=1
fi
if [ "$missing_crds" -eq 1 ]; then
  kubectl apply --server-side -f ./kyverno-kyverno-3-8-1-no-crds/package/prerequisites/target-facts/no-crds-crds.yaml
else
  say "The required CRDs already exist; leave them under their current owner"
fi
wait_for_crd cleanuppolicies.kyverno.io
wait_for_crd clustercleanuppolicies.kyverno.io
wait_for_crd clusterpolicies.kyverno.io
wait_for_crd globalcontextentries.kyverno.io
wait_for_crd policies.kyverno.io
wait_for_crd policyexceptions.kyverno.io
wait_for_crd updaterequests.kyverno.io
wait_for_crd clusterephemeralreports.reports.kyverno.io
wait_for_crd ephemeralreports.reports.kyverno.io
wait_for_crd clusterpolicyreports.wgpolicyk8s.io
wait_for_crd policyreports.wgpolicyk8s.io
wait_for_crd deletingpolicies.policies.kyverno.io
wait_for_crd generatingpolicies.policies.kyverno.io
wait_for_crd imagevalidatingpolicies.policies.kyverno.io
wait_for_crd mutatingpolicies.policies.kyverno.io
wait_for_crd namespaceddeletingpolicies.policies.kyverno.io
wait_for_crd namespacedgeneratingpolicies.policies.kyverno.io
wait_for_crd namespacedimagevalidatingpolicies.policies.kyverno.io
wait_for_crd namespacedmutatingpolicies.policies.kyverno.io
wait_for_crd namespacedvalidatingpolicies.policies.kyverno.io
wait_for_crd policyexceptions.policies.kyverno.io
wait_for_crd validatingpolicies.policies.kyverno.io

if [ -d ./kyverno-kyverno-3-8-1-no-crds/out/secrets ]; then
  say "Apply rendered Secrets first"
  kubectl apply -f ./kyverno-kyverno-3-8-1-no-crds/out/secrets
fi

say "Apply the rendered objects"
kubectl apply -f ./kyverno-kyverno-3-8-1-no-crds/out/manifests

say "Done. The cluster received exactly the files in ./kyverno-kyverno-3-8-1-no-crds/out."
