#!/usr/bin/env bash
# k8s-ephemeral-storage-metrics/k8s-ephemeral-storage-metrics 1.19.2 - 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/k8s-ephemeral-storage-metrics-k8s-ephemeral-storage-metrics-1-19-2.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 ./k8s-ephemeral-storage-metrics-k8s-ephemeral-storage-metrics-1-19-2-default"
cub installer setup --pull oci://europe-west1-docker.pkg.dev/nth-fort-499605-q5/helm-expt/k8s-ephemeral-storage-metrics-k8s-ephemeral-storage-metrics:1.19.2@sha256:d869a5ff9e363ef5624db8145215f4b369d41f3a8f9d20849258af39f4adb21d --base default --work-dir ./k8s-ephemeral-storage-metrics-k8s-ephemeral-storage-metrics-1-19-2-default --non-interactive --namespace monitoring

say "Read what was rendered; nothing has touched the cluster yet"
ls ./k8s-ephemeral-storage-metrics-k8s-ephemeral-storage-metrics-1-19-2-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 monitoring namespace exists"
kubectl create namespace monitoring --dry-run=client -o yaml | kubectl apply -f -

say "Check the servicemonitors.monitoring.coreos.com CRD included with this package"
missing_crds=0
if ! kubectl get crd/servicemonitors.monitoring.coreos.com >/dev/null 2>&1; then
  missing_crds=1
fi
if [ "$missing_crds" -eq 1 ]; then
  kubectl apply --server-side -f ./k8s-ephemeral-storage-metrics-k8s-ephemeral-storage-metrics-1-19-2-default/package/prerequisites/target-facts/default-crds.yaml
elif kubectl diff --server-side -f ./k8s-ephemeral-storage-metrics-k8s-ephemeral-storage-metrics-1-19-2-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 ./k8s-ephemeral-storage-metrics-k8s-ephemeral-storage-metrics-1-19-2-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 servicemonitors.monitoring.coreos.com

if [ -d ./k8s-ephemeral-storage-metrics-k8s-ephemeral-storage-metrics-1-19-2-default/out/secrets ]; then
  say "Apply rendered Secrets first"
  kubectl apply -f ./k8s-ephemeral-storage-metrics-k8s-ephemeral-storage-metrics-1-19-2-default/out/secrets
fi

say "Apply the rendered objects, except the CRDs already settled above"
settled_crds=" servicemonitors.monitoring.coreos.com "
manifest_args=()
for manifest in ./k8s-ephemeral-storage-metrics-k8s-ephemeral-storage-metrics-1-19-2-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 ./k8s-ephemeral-storage-metrics-k8s-ephemeral-storage-metrics-1-19-2-default/out. Its CRDs were settled first, from the package."
