From 149a51afb119bc91d24957fb237585f8a0f6c58c Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Fri, 30 Oct 2026 20:11:19 -0600 Subject: [PATCH] Adopt existing Calico releases before targeted platform reconciliation --- bootstrap/platform/README.md | 10 +++++- bootstrap/platform/main.tf | 23 ++++++++++--- lib/jeannie/pipeline.sh | 67 +++++++++++++++++++++++++++++++----- 3 files changed, 85 insertions(+), 15 deletions(-) diff --git a/bootstrap/platform/README.md b/bootstrap/platform/README.md index 17f3319..5efa96a 100644 --- a/bootstrap/platform/README.md +++ b/bootstrap/platform/README.md @@ -9,6 +9,7 @@ Run these on the Debian homelab host with the active cluster profile: ```bash LAB_CLUSTER_PROFILE=pimox ./jeannie platform monitoring +LAB_CLUSTER_PROFILE=pimox ./jeannie platform calico LAB_CLUSTER_PROFILE=pimox ./jeannie platform logging LAB_CLUSTER_PROFILE=pimox ./jeannie platform argocd LAB_CLUSTER_PROFILE=pimox ./jeannie platform all @@ -26,7 +27,14 @@ node before adoption, preserves existing state entries, and stops on discovery errors rather than treating them as absent resources. No release uninstall or PVC deletion is involved. Use `platform all` for platform-wide adoption. -Monitoring imports retry recognized Kubernetes transport failures (including +The Calico target similarly adopts the existing `calico-crds` and `calico` +Helm releases in the configured Calico namespace, without uninstalling either +release or deleting CRDs. It verifies the selected control plane and the desired +operator node exist before changing state or applying the rollout. A stale +`debian` selector in the Pimox profile therefore produces a placement error +instead of creating another unschedulable operator pod. + +Scoped platform imports retry recognized Kubernetes transport failures (including TLS bad-record-MAC responses) up to three times, with five seconds between attempts. Each attempt rereads state so an import already recorded by a prior attempt is not repeated. Authentication, authorization, configuration, and diff --git a/bootstrap/platform/main.tf b/bootstrap/platform/main.tf index f799ba9..98c4a89 100644 --- a/bootstrap/platform/main.tf +++ b/bootstrap/platform/main.tf @@ -299,16 +299,29 @@ resource "null_resource" "calico_helm_recovery" { command = </dev/null | +kubeconfig="${self.triggers.kubeconfig_path}" +namespace="${self.triggers.namespace}" +release="${self.triggers.release_name}" +echo "Checking Helm release $namespace/$release using $kubeconfig..." + +if ! revisions="$(kubectl --kubeconfig "$kubeconfig" --request-timeout=10s -n "$namespace" get secrets \ + -l "owner=helm,name=$release" \ + -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.metadata.labels.status}{"\n"}{end}')"; then + echo "Cannot read Calico Helm metadata; refusing recovery or upgrade. Kubernetes error is shown above." >&2 + exit 1 +fi + while IFS=$'\t' read -r secret status; do case "$status" in pending-install|pending-upgrade|pending-rollback|failed|uninstalling) - kubectl --kubeconfig "${self.triggers.kubeconfig_path}" -n "${self.triggers.namespace}" delete secret "$secret" + echo "Removing Helm revision $secret with status $status..." + kubectl --kubeconfig "$kubeconfig" --request-timeout=10s -n "$namespace" \ + delete secret "$secret" --ignore-not-found=true --timeout=20s ;; esac -done +done <<< "$revisions" + +echo "Calico Helm metadata recovery completed." EOT } } diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index 5a219a9..cd4fd8a 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -438,7 +438,7 @@ adopt_tofu_kubernetes_manifest() { tofu -chdir="${REPO_ROOT}/${stack}" import -input=false "${resource_address}" "${import_id}" } -monitoring_read_state_addresses() { +platform_read_state_addresses() { tofu -chdir="${REPO_ROOT}/bootstrap/platform" show -json | python3 -c ' import json, sys @@ -451,7 +451,7 @@ walk((json.load(sys.stdin).get("values") or {}).get("root_module") or {}) ' } -monitoring_import_with_retry() { +platform_import_with_retry() { local address="$1" import_id="$2" local max_attempts="${LAB_TOFU_K8S_IMPORT_RETRIES:-3}" local attempt=1 status output_file current_addresses @@ -464,12 +464,12 @@ monitoring_import_with_retry() { while ((attempt <= max_attempts)); do # A failed response can follow a committed state write. Recheck before # retrying rather than deleting state or importing an already-owned object. - current_addresses="$(monitoring_read_state_addresses)" || return 1 + current_addresses="$(platform_read_state_addresses)" || return 1 if grep -Fxq -- "${address}" <<<"${current_addresses}"; then - echo "Monitoring resource ${address} is already recorded; import skipped." + echo "Platform resource ${address} is already recorded; import skipped." return 0 fi - output_file="$(mktemp "${TMPDIR:-/tmp}/homelab-monitoring-import.XXXXXX")" || return 1 + output_file="$(mktemp "${TMPDIR:-/tmp}/homelab-platform-import.XXXXXX")" || return 1 if tofu -chdir="${REPO_ROOT}/bootstrap/platform" import -input=false \ "${address}" "${import_id}" >"${output_file}" 2>&1; then cat "${output_file}" @@ -485,7 +485,7 @@ monitoring_import_with_retry() { fi rm -f "${output_file}" attempt=$((attempt + 1)) - echo "Monitoring import hit a Kubernetes transport error; retrying ${address} (${attempt}/${max_attempts}) in 5s..." >&2 + echo "Platform import hit a Kubernetes transport error; retrying ${address} (${attempt}/${max_attempts}) in 5s..." >&2 sleep 5 done } @@ -506,7 +506,7 @@ adopt_monitoring_resource_if_missing() { fi [[ -n "${existing}" ]] || return 0 echo "Adopting existing monitoring resource ${address}: ${import_id}" - monitoring_import_with_retry "${address}" "${import_id}" || return 1 + platform_import_with_retry "${address}" "${import_id}" || return 1 } adopt_platform_monitoring_resources() { @@ -529,7 +529,7 @@ adopt_platform_monitoring_resources() { python3 -c 'import json,sys; v=json.loads(json.loads(sys.stdin.read())); print(v["namespace"]); print(str(v["mimir_enabled"]).lower())')" || return 1 namespace="${resolved%%$'\n'*}" mimir_enabled="${resolved##*$'\n'}" - monitoring_state_addresses="$(monitoring_read_state_addresses)" || return 1 + monitoring_state_addresses="$(platform_read_state_addresses)" || return 1 adopt_monitoring_resource_if_missing kubernetes_priority_class_v1.monitoring \ "" priorityclass homelab-monitoring homelab-monitoring || return 1 @@ -553,7 +553,7 @@ adopt_platform_monitoring_resources() { -n "${namespace}" get secrets -l "owner=helm,name=${release}" -o name)" || return 1 [[ -n "${revisions}" ]] || continue echo "Adopting existing monitoring Helm release ${namespace}/${release} into ${address}" - monitoring_import_with_retry "${address}" "${namespace}/${release}" || return 1 + platform_import_with_retry "${address}" "${namespace}/${release}" || return 1 done for dashboard_file in "${REPO_ROOT}/bootstrap/platform/grafana-dashboards/"*.json; do @@ -565,6 +565,52 @@ adopt_platform_monitoring_resources() { done } +adopt_platform_calico_resources() { + local resolved namespace operator_node node_exists namespace_exists state_addresses + local expected_node="${LAB_CLUSTER_CONTROL_PLANE_NODE_NAME:-}" + local release address revisions + + if [[ -n "${expected_node}" ]]; then + node_exists="$(kubectl --kubeconfig "${KUBECONFIG_PATH}" --request-timeout=10s \ + get node "${expected_node}" --ignore-not-found -o name)" || return 1 + if [[ -z "${node_exists}" ]]; then + echo "Expected control plane ${expected_node} was not found; refusing Calico adoption in this cluster." >&2 + return 1 + fi + fi + resolved="$(printf '%s\n' 'jsonencode({namespace = var.calico.namespace, operator_node = var.calico_operator_node_name})' | + tofu -chdir="${REPO_ROOT}/bootstrap/platform" console | + python3 -c 'import json,sys; v=json.loads(json.loads(sys.stdin.read())); print(v["namespace"]); print(v["operator_node"])')" || return 1 + namespace="${resolved%%$'\n'*}" + operator_node="${resolved##*$'\n'}" + node_exists="$(kubectl --kubeconfig "${KUBECONFIG_PATH}" --request-timeout=10s \ + get node "${operator_node}" --ignore-not-found -o name)" || return 1 + if [[ -z "${node_exists}" ]]; then + echo "Configured Calico operator node ${operator_node} does not exist; refusing an unschedulable rollout." >&2 + return 1 + fi + state_addresses="$(platform_read_state_addresses)" || return 1 + namespace_exists="$(kubectl --kubeconfig "${KUBECONFIG_PATH}" --request-timeout=10s \ + get namespace "${namespace}" --ignore-not-found -o name)" || return 1 + [[ -n "${namespace_exists}" ]] || return 0 + + for release in calico-crds calico; do + if [[ "${release}" == calico-crds ]]; then + address=helm_release.calico_crds + else + address=helm_release.calico + fi + if grep -Fxq -- "${address}" <<<"${state_addresses}"; then + continue + fi + revisions="$(kubectl --kubeconfig "${KUBECONFIG_PATH}" --request-timeout=10s \ + -n "${namespace}" get secrets -l "owner=helm,name=${release}" -o name)" || return 1 + [[ -n "${revisions}" ]] || continue + echo "Adopting existing Calico Helm release ${namespace}/${release} into ${address}" + platform_import_with_retry "${address}" "${namespace}/${release}" || return 1 + done +} + adopt_platform_existing_resources() { local stack="bootstrap/platform" local dashboard_file @@ -880,6 +926,9 @@ run_tofu_stack() { if [[ "${target}" == "-target=helm_release.prometheus_stack" ]]; then adopt_platform_monitoring_resources || return 1 break + elif [[ "${target}" == "-target=helm_release.calico_crds" || "${target}" == "-target=helm_release.calico" ]]; then + adopt_platform_calico_resources || return 1 + break fi done fi