diff --git a/bootstrap/cluster/main.tf b/bootstrap/cluster/main.tf index 30d0b6f..b0a31f9 100644 --- a/bootstrap/cluster/main.tf +++ b/bootstrap/cluster/main.tf @@ -1638,7 +1638,9 @@ EOT locals { control_plane_node_label_pairs = [ - for label, value in var.control_plane_node_labels : { + for label, value in merge(var.control_plane_node_labels, + length(var.worker_nodes) == 0 ? { "homelab.dev/workload-class" = "platform" } : {} + ) : { node_name = var.control_plane_node_name label = label value = value diff --git a/bootstrap/platform/README.md b/bootstrap/platform/README.md index eddb67c..77fc669 100644 --- a/bootstrap/platform/README.md +++ b/bootstrap/platform/README.md @@ -62,6 +62,31 @@ step waits for the Argo CD rollouts. This does not change worker CPU topology. ## Scheduling feasibility before deployment +Single-node clusters use their selected control plane for platform workloads, +regardless of profile or runtime. The platform apply reconciles +`homelab.dev/workload-class=platform` after checking the pinned cluster identity. +Multi-node clusters retain their existing worker placement. Missing selector +labels are scheduling failures; the estimator does not assume they will appear. + +Initial CNI bootstrapping is inferred only when the plan contains a Calico +Installation, no live calico-node DaemonSet exists, and the selected node reports +an uninitialized network plugin. Existing CNI failures remain blocking. Planned +OpenTofu StorageClasses are included in the estimate; Loki, Mimir and the +Prometheus stack depend on creation of the retained OpenEBS class. + +Before deploying applications on a different topology, review the current +registry manifests: their retained PV/PVC and required node affinity explicitly +target `pimox-worker-01`. That data must be migrated deliberately or an alternate +registry must be configured; relabeling a different host cannot move local data. +The Gitea runner also requires an app-node selector and a host Docker socket. +These application requirements are not satisfied merely by installing k3s. + +The k3s installer currently also enables its bundled metrics-server and +ServiceLB, while the platform manages metrics-server and MetalLB. Check live +ownership before consolidating these services on an existing installation. +Recovery and repository-scan helpers still contain timestamp triggers, so a +successful second plan is not necessarily a zero-change plan. + Every platform and application OpenTofu apply now runs `scripts/scheduling-preflight.py` first, including retry attempts. The pipeline creates a private temporary plan, renders the selected Helm releases using their diff --git a/bootstrap/platform/main.tf b/bootstrap/platform/main.tf index 057507c..20b3d31 100644 --- a/bootstrap/platform/main.tf +++ b/bootstrap/platform/main.tf @@ -1594,7 +1594,7 @@ resource "helm_release" "loki" { create_namespace = true timeout = 900 wait = true - depends_on = [null_resource.calico_ready] + depends_on = [null_resource.calico_ready, kubernetes_storage_class_v1.openebs_hostpath_retain] values = [ yamlencode({ @@ -1703,7 +1703,7 @@ resource "helm_release" "mimir" { create_namespace = true timeout = 1200 wait = true - depends_on = [null_resource.calico_ready] + depends_on = [null_resource.calico_ready, kubernetes_storage_class_v1.openebs_hostpath_retain] values = [ yamlencode({ @@ -1909,7 +1909,7 @@ resource "kubernetes_priority_class_v1" "monitoring" { } resource "helm_release" "prometheus_stack" { - depends_on = [null_resource.prometheus_stack_helm_recovery, null_resource.calico_ready] + depends_on = [null_resource.prometheus_stack_helm_recovery, null_resource.calico_ready, kubernetes_storage_class_v1.openebs_hostpath_retain] name = "prometheus-stack" repository = var.observability.prometheus.repository chart = var.observability.prometheus.chart diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index 19a7d21..89ac0c7 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -835,20 +835,30 @@ delete_prometheus_stack_storage() { fi } +ensure_platform_scheduling_labels() { + local nodes control_plane_node + control_plane_node="${LAB_CLUSTER_CONTROL_PLANE_NODE_NAME:-}" + cluster_context_require || return 1 + nodes="$(kubectl --kubeconfig "${KUBECONFIG_PATH}" --request-timeout=15s get nodes \ + -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}')" || return 1 + # A single-node cluster must run platform services on its control plane, + # regardless of runtime/profile. Multi-node placement keeps its labels. + if [[ -n "${control_plane_node}" && "${nodes}" == "${control_plane_node}" ]]; then + kubectl --kubeconfig "${KUBECONFIG_PATH}" --request-timeout=15s \ + label node "${control_plane_node}" homelab.dev/workload-class=platform --overwrite || return 1 + fi +} + kubernetes_scheduling_preflight() { local stack="$1" shift local directory status arg plan_output retry_pattern local -a plan_args=() - local -a preflight_args=() [[ "${stack}" == "bootstrap/platform" || "${stack}" == "bootstrap/apps" ]] || return 0 if [[ -z "${LAB_CLUSTER_CONTROL_PLANE_NODE_NAME:-}" ]]; then echo "Scheduling preflight requires the selected control-plane node name." >&2 return 1 fi - if [[ "${stack}" == "bootstrap/platform" && "${LAB_CLUSTER_PROFILE:-}" == "k3s" ]]; then - preflight_args+=(--bootstrap-node "${LAB_CLUSTER_CONTROL_PLANE_NODE_NAME}") - fi for arg in "$@"; do [[ "${arg}" == "-auto-approve" ]] || plan_args+=("${arg}") done @@ -878,8 +888,7 @@ kubernetes_scheduling_preflight() { status=0 python3 "${REPO_ROOT}/scripts/scheduling-preflight.py" \ --plan "${directory}/plan.json" --kubeconfig "${KUBECONFIG_PATH}" \ - --repo-root "${REPO_ROOT}" --expected-node "${LAB_CLUSTER_CONTROL_PLANE_NODE_NAME}" \ - "${preflight_args[@]}" || status=$? + --repo-root "${REPO_ROOT}" --expected-node "${LAB_CLUSTER_CONTROL_PLANE_NODE_NAME}" || status=$? # The private directory can contain sensitive planned chart values. rm -rf -- "${directory}" return "${status}" @@ -1020,6 +1029,7 @@ run_tofu_stack() { if [[ "${stack}" == "bootstrap/platform" ]]; then configure_platform_grafana_sso + ensure_platform_scheduling_labels || return 1 fi tofu -chdir="${REPO_ROOT}/${stack}" init diff --git a/scripts/scheduling-preflight.py b/scripts/scheduling-preflight.py index 13c420c..45a18ae 100644 --- a/scripts/scheduling-preflight.py +++ b/scripts/scheduling-preflight.py @@ -290,13 +290,6 @@ class Estimate: if not node_ready and not bootstrap: reasons.append("NotReady") selector_mismatch = any(labels.get(k) != v for k, v in spec.get("nodeSelector", {}).items()) - if bootstrap and spec.get("nodeSelector", {}).get("homelab.dev/workload-class") == "platform": - # The k3s bootstrap node receives this label from the cluster - # stack, but platform is intentionally checked before that apply. - selector_mismatch = any( - k != "homelab.dev/workload-class" and labels.get(k) != v - for k, v in spec.get("nodeSelector", {}).items() - ) if selector_mismatch: reasons.append("nodeSelector mismatch") affinity = spec.get("affinity", {}) @@ -323,9 +316,6 @@ class Estimate: defaults = [s for s in self.storage_classes.values() if s["metadata"].get("annotations", {}).get("storageclass.kubernetes.io/is-default-class") == "true"] storage_class = defaults[0] if len(defaults) == 1 else None if not storage_class or storage_class.get("provisioner") == "kubernetes.io/no-provisioner": - if self.bootstrap_node: - self.notice(f"{namespace}/{claim}: dynamic storage will be provided by the platform bootstrap") - continue raise CannotCheck(f"{namespace}/{claim}: unbound claim has no known dynamic provisioner") selected_node = pvc["metadata"].get("annotations", {}).get("volume.kubernetes.io/selected-node") if selected_node and selected_node != name: @@ -525,6 +515,23 @@ def manifests_from_plan(args, plan, kubectl): metadata["namespace"] = metadata.get("namespace") or value["namespace"] item["_preflight_noop"] = change.get("actions") == ["no-op"] documents.append(item) + elif resource_type == "kubernetes_storage_class_v1": + metadata = value.get("metadata") or [] + if not metadata or not metadata[0].get("name") or not value.get("storage_provisioner"): + raise CannotCheck(f"{resource['address']}: planned StorageClass identity/provisioner is unknown") + documents.append({ + "kind": "StorageClass", + "metadata": {"name": metadata[0]["name"], "annotations": metadata[0].get("annotations") or {}}, + "provisioner": value["storage_provisioner"], + "volumeBindingMode": value.get("volume_binding_mode"), + "allowedTopologies": [ + {"matchLabelExpressions": [ + {"key": expression["key"], "values": expression["values"]} + for expression in term.get("match_label_expressions") or [] + ]} + for term in value.get("allowed_topologies") or [] + ], + }) elif resource_type == "kubernetes_manifest": item = value.get("manifest") if not item: @@ -637,7 +644,6 @@ def main(): parser.add_argument("--kubeconfig", required=True) parser.add_argument("--repo-root", required=True) parser.add_argument("--expected-node", required=True) - parser.add_argument("--bootstrap-node", help="Allow the selected node to be NotReady while the initial CNI/storage platform is installed") args = parser.parse_args() kubectl = ["kubectl", "--kubeconfig", args.kubeconfig, "--request-timeout=15s"] nodes = json.loads(run(kubectl + ["get", "nodes", "-o", "json"]))["items"] @@ -649,9 +655,24 @@ def main(): namespaces = json.loads(run(kubectl + ["get", "namespaces", "-o", "json"]))["items"] with open(args.plan, encoding="utf-8") as handle: documents = manifests_from_plan(args, json.load(handle), kubectl) + # Infer the initial CNI bootstrap from live state and the plan, not the + # selected profile. Never waive readiness for an existing broken CNI. + bootstrap_node = None + installing_cni = any(d.get("kind") == "Installation" for d in documents) + cni_exists = any(o.get("kind") == "DaemonSet" and o["metadata"]["name"] == "calico-node" for o in objects) + if installing_cni and not cni_exists: + for node in nodes: + if node["metadata"]["name"] != args.expected_node: + continue + for condition in node.get("status", {}).get("conditions") or []: + message = condition.get("message", "").lower() + if (condition.get("type") == "Ready" and condition.get("status") == "False" + and ("networkpluginnotready" in message or "cni plugin not initialized" in message)): + bootstrap_node = args.expected_node + print(f" NOTICE {bootstrap_node}: initial planned Calico installation must establish node readiness") engine = Estimate(nodes, [p for p in objects if p["kind"] == "Pod"], objects, [p for p in objects if p["kind"] == "PersistentVolumeClaim"], pvs, storage_classes, namespaces, - bootstrap_node=args.bootstrap_node) + bootstrap_node=bootstrap_node) for item in documents: if item["kind"] == "PersistentVolumeClaim": key = item["metadata"]["namespace"], item["metadata"]["name"]