Validate and atomically replace control plane manifests
Homelab Main / validate (push) Waiting to run Details
Homelab Main / running-update (push) Blocked by required conditions Details
Homelab Main / debian-ops (push) Blocked by required conditions Details

This commit is contained in:
juvenal.diaz 2026-10-29 19:26:57 -06:00
parent ea718d482f
commit 0fa3c1007c
1 changed files with 49 additions and 10 deletions

View File

@ -531,7 +531,7 @@ resource "null_resource" "kube_system_resource_policy" {
user = var.control_plane_user
ssh_key_path = var.control_plane_ssh_key_path
kubeconfig_path = var.kubeconfig_path
resource_policy_version = "3"
resource_policy_version = "4"
}
connection {
@ -549,24 +549,63 @@ resource "null_resource" "kube_system_resource_policy" {
set -euo pipefail
patch_static_pod_resources() {
name="$1"
request_cpu="$2"
request_memory="$3"
limit_cpu="$4"
limit_memory="$5"
manifest="/etc/kubernetes/manifests/$${name}.yaml"
local name="$1"
local request_cpu="$2"
local request_memory="$3"
local limit_cpu="$4"
local limit_memory="$5"
local manifest="/etc/kubernetes/manifests/$${name}.yaml"
local tmp backup_dir staged
if ! sudo test -f "$manifest"; then
echo "Static pod manifest not found, skipping: $manifest"
return 0
fi
if ! sudo test -s "$manifest"; then
echo "Refusing to patch empty static pod manifest: $manifest. Restore it before retrying." >&2
return 1
fi
command -v python3 >/dev/null
tmp="$(mktemp)"
sudo kubectl set resources -f "$manifest" --local -o yaml \
if ! sudo kubectl set resources -f "$manifest" --local -o json \
--requests="cpu=$${request_cpu},memory=$${request_memory}" \
--limits="cpu=$${limit_cpu},memory=$${limit_memory}" >"$tmp"
sudo install -m 0644 "$tmp" "$manifest"
--limits="cpu=$${limit_cpu},memory=$${limit_memory}" >"$tmp"; then
rm -f "$tmp"
echo "Failed to render resources for $name; original manifest preserved." >&2
return 1
fi
if ! python3 - "$tmp" "$name" <<'PY'
import json
import sys
with open(sys.argv[1]) as stream:
pod = json.load(stream)
if (pod.get("apiVersion") != "v1" or pod.get("kind") != "Pod"
or pod.get("metadata", {}).get("name") != sys.argv[2]
or not pod.get("spec", {}).get("containers")):
raise SystemExit("Refusing invalid static pod output; original manifest preserved")
PY
then
rm -f "$tmp"
return 1
fi
if sudo cmp -s "$tmp" "$manifest"; then
rm -f "$tmp"
return 0
fi
sudo install -d -m 0700 /etc/kubernetes/manifest-backups
backup_dir="$(sudo mktemp -d /etc/kubernetes/manifest-backups/resource-policy.XXXXXXXX)"
sudo cp -p "$manifest" "$backup_dir/$name.yaml"
# Kubelet ignores dotfiles; stage on the same filesystem for atomic rename.
staged="$(sudo mktemp /etc/kubernetes/manifests/.resource-policy.XXXXXXXX)"
if ! sudo install -m 0644 "$tmp" "$staged" || ! sudo mv -f "$staged" "$manifest"; then
sudo rm -f "$staged"
rm -f "$tmp"
return 1
fi
rm -f "$tmp"
echo "Updated $name resources; original saved in $backup_dir/$name.yaml"
}
patch_cluster_workload_resources() {