diff --git a/bootstrap/platform/README.md b/bootstrap/platform/README.md index 6e09bd4..0aaeb3b 100644 --- a/bootstrap/platform/README.md +++ b/bootstrap/platform/README.md @@ -17,9 +17,14 @@ LAB_CLUSTER_PROFILE=pimox ./jeannie platform all Available components are `calico`, `metrics`, `dns`, `metallb`, `ingress`, `storage`, `argocd`, `security`, `logging`, `monitoring`, and `tools`. -Targeted applies skip the legacy existing-resource adoption pass, which avoids -touching unrelated platform state during a small change. Use `platform all` -for the full reconciliation and adoption path. +Targeted applies skip the legacy platform-wide adoption pass. The monitoring +target performs its own scoped adoption after OpenTofu initialization: existing +Helm releases (`prometheus-stack` and Mimir when enabled), the monitoring +namespace, scheduling priority, and repo-managed dashboard ConfigMaps are +imported only when missing from state. It checks the selected control-plane +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 during reduced CPU capacity diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index 9360183..432f086 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -438,6 +438,91 @@ adopt_tofu_kubernetes_manifest() { tofu -chdir="${REPO_ROOT}/${stack}" import -input=false "${resource_address}" "${import_id}" } +adopt_monitoring_resource_if_missing() { + local address="$1" namespace="$2" kind="$3" name="$4" import_id="$5" + local existing + + if grep -Fxq -- "${address}" <<<"${monitoring_state_addresses}"; then + return 0 + fi + if [[ -n "${namespace}" ]]; then + existing="$(kubectl --kubeconfig "${KUBECONFIG_PATH}" --request-timeout=10s \ + -n "${namespace}" get "${kind}" "${name}" --ignore-not-found -o name)" || return 1 + else + existing="$(kubectl --kubeconfig "${KUBECONFIG_PATH}" --request-timeout=10s \ + get "${kind}" "${name}" --ignore-not-found -o name)" || return 1 + fi + [[ -n "${existing}" ]] || return 0 + echo "Adopting existing monitoring resource ${address}: ${import_id}" + tofu -chdir="${REPO_ROOT}/bootstrap/platform" import -input=false "${address}" "${import_id}" || return 1 +} + +adopt_platform_monitoring_resources() { + local monitoring_state_addresses resolved namespace mimir_enabled namespace_exists + local expected_node="${LAB_CLUSTER_CONTROL_PLANE_NODE_NAME:-}" + local observed_node release address revisions dashboard_file dashboard_key dashboard_name + + # Do not silently treat an API, authorization, or state read failure as an + # empty cluster. Only namespace/name matches in the selected cluster count. + if [[ -n "${expected_node}" ]]; then + observed_node="$(kubectl --kubeconfig "${KUBECONFIG_PATH}" --request-timeout=10s \ + get node "${expected_node}" --ignore-not-found -o name)" || return 1 + if [[ -z "${observed_node}" ]]; then + echo "Expected control plane ${expected_node} was not found; refusing monitoring adoption in this cluster." >&2 + return 1 + fi + fi + resolved="$(printf '%s\n' 'jsonencode({namespace = var.observability.namespace, mimir_enabled = var.observability.mimir.enabled})' | + tofu -chdir="${REPO_ROOT}/bootstrap/platform" console | + 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="$(tofu -chdir="${REPO_ROOT}/bootstrap/platform" show -json | + python3 -c ' +import json, sys +def walk(module): + for resource in module.get("resources", []): + print(resource["address"]) + for child in module.get("child_modules", []): + walk(child) +walk((json.load(sys.stdin).get("values") or {}).get("root_module") or {}) +')" || return 1 + + adopt_monitoring_resource_if_missing kubernetes_priority_class_v1.monitoring \ + "" priorityclass homelab-monitoring homelab-monitoring || 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 + adopt_monitoring_resource_if_missing kubernetes_namespace_v1.monitoring \ + "" namespace "${namespace}" "${namespace}" || return 1 + + for release in prometheus-stack mimir; do + if [[ "${release}" == mimir ]]; then + [[ "${mimir_enabled}" == true ]] || continue + address='helm_release.mimir[0]' + else + address=helm_release.prometheus_stack + fi + if grep -Fxq -- "${address}" <<<"${monitoring_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 monitoring Helm release ${namespace}/${release} into ${address}" + tofu -chdir="${REPO_ROOT}/bootstrap/platform" import -input=false \ + "${address}" "${namespace}/${release}" || return 1 + done + + for dashboard_file in "${REPO_ROOT}/bootstrap/platform/grafana-dashboards/"*.json; do + [[ -e "${dashboard_file}" ]] || continue + dashboard_key="$(basename "${dashboard_file}")" + dashboard_name="homelab-grafana-${dashboard_key%.json}" + adopt_monitoring_resource_if_missing "kubernetes_config_map.grafana_dashboards[\"${dashboard_key}\"]" \ + "${namespace}" configmap "${dashboard_name}" "${namespace}/${dashboard_name}" || return 1 + done +} + adopt_platform_existing_resources() { local stack="bootstrap/platform" local dashboard_file @@ -748,6 +833,13 @@ run_tofu_stack() { ensure_kubernetes_api_ready_for_tofu_stack "${stack}" if [[ "${stack}" == "bootstrap/platform" ]] && ! tofu_apply_has_targets "${apply_args[@]}"; then adopt_platform_existing_resources + elif [[ "${stack}" == "bootstrap/platform" ]]; then + for target in "${apply_args[@]}"; do + if [[ "${target}" == "-target=helm_release.prometheus_stack" ]]; then + adopt_platform_monitoring_resources || return 1 + break + fi + done fi if [[ "${stack}" == "bootstrap/apps" ]] && ! tofu_apply_has_targets "${apply_args[@]}"; then adopt_apps_existing_resources