mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-01 04:35:40 +09:00
A kubeadm installation can finish at its old requested version while
core moves to the intermediate release. Ignoring its initial empty
installedVersion leaves the final bootstrap chart outside its supported
upgrade window.
Fixes: 4d251f4122 ("fix(capi-providers-core): let a never-installed provider take the fresh path")
Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
155 lines
6.5 KiB
Python
155 lines
6.5 KiB
Python
#!/usr/bin/env python3
|
|
import copy
|
|
import json
|
|
import os
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
state_path = Path(os.environ["CAPI_TEST_STATE"])
|
|
log_path = Path(os.environ["CAPI_TEST_LOG"])
|
|
state = json.loads(state_path.read_text())
|
|
args = sys.argv[1:]
|
|
expected = ["--context", "fixture", "--namespace", "test-capi", "--request-timeout=10s"]
|
|
if args[:len(expected)] != expected:
|
|
sys.exit("unexpected kubectl context or namespace")
|
|
args = args[len(expected):]
|
|
verb, resource, name = args[:3]
|
|
resources = {
|
|
"coreproviders.operator.cluster.x-k8s.io": "providers",
|
|
"bootstrapproviders.operator.cluster.x-k8s.io": "providers",
|
|
"deployments.apps": "deployments",
|
|
}
|
|
if resource not in resources or name not in (
|
|
"cluster-api", "kubeadm", "capi-controller-manager", "capi-kubeadm-bootstrap-controller-manager"
|
|
):
|
|
sys.exit("unexpected resource")
|
|
entry = {"verb": verb, "resource": resource, "name": name}
|
|
|
|
|
|
def selected(selector):
|
|
return [cm for cm in state.get("configmaps", []) if all(cm["labels"].get(k) == v for k, v in selector.items())]
|
|
|
|
|
|
def repository_ready(name, obj):
|
|
own = selected(obj["spec"].get("fetchConfig", {}).get("selector", {}).get("matchLabels", {}))
|
|
if len(own) != 1 or own[0]["labels"].get("provider.cluster.x-k8s.io/version", own[0]["name"]) != obj["spec"]["version"]:
|
|
return False
|
|
# A version change makes cluster-api-operator resolve the repository of every
|
|
# other installed provider, refusing a selector that matches more than one ConfigMap.
|
|
return obj["status"].get("installedVersion") == obj["spec"]["version"] or all(
|
|
len(selected(other["spec"].get("fetchConfig", {}).get("selector", {}).get("matchLabels", {}))) == 1
|
|
for other_name, other in state["providers"].items()
|
|
if other_name != name and other["status"].get("installedVersion")
|
|
)
|
|
|
|
|
|
def save():
|
|
state_path.write_text(json.dumps(state))
|
|
|
|
|
|
def log():
|
|
with log_path.open("a") as stream:
|
|
stream.write(json.dumps(entry) + "\n")
|
|
|
|
|
|
if verb == "get":
|
|
log()
|
|
time.sleep(state.get("readDelay", 0))
|
|
if state.get("readDelay"):
|
|
state["delayedReadCompleted"] = True
|
|
save()
|
|
if state.get("failGet") == name:
|
|
sys.exit("API unavailable")
|
|
obj = state[resources[resource]].get(name)
|
|
if obj is None:
|
|
if "--ignore-not-found" not in args:
|
|
sys.exit("NotFound")
|
|
sys.exit(0)
|
|
if resource != "deployments.apps" and name in state.get("pending", {}):
|
|
pending = state["pending"][name]
|
|
if pending > 0:
|
|
state["pending"][name] -= 1
|
|
elif not state.get("stalled") and repository_ready(name, obj):
|
|
obj["status"]["installedVersion"] = obj["spec"]["version"]
|
|
obj["status"]["observedGeneration"] = obj["metadata"]["generation"]
|
|
obj["status"]["conditions"] = [{"type": "Ready", "status": "True"}]
|
|
deployment_name = "capi-controller-manager" if name == "cluster-api" else "capi-kubeadm-bootstrap-controller-manager"
|
|
deployment = state["deployments"][deployment_name]
|
|
deployment["metadata"]["generation"] += 1
|
|
deployment["status"]["observedGeneration"] = deployment["metadata"]["generation"]
|
|
deployment["status"]["updatedReplicas"] = 1
|
|
deployment["status"]["replicas"] = 1
|
|
deployment["status"]["readyReplicas"] = 0 if state.get("lagRollout") else 1
|
|
deployment["status"]["availableReplicas"] = deployment["status"]["readyReplicas"]
|
|
if state.get("lagRollout"):
|
|
state.setdefault("rolloutReads", {})[deployment_name] = 1
|
|
del state["pending"][name]
|
|
if resource == "deployments.apps" and name in state.get("rolloutReads", {}):
|
|
if state["rolloutReads"][name] > 0:
|
|
state["rolloutReads"][name] -= 1
|
|
else:
|
|
obj["status"]["readyReplicas"] = 1
|
|
obj["status"]["availableReplicas"] = 1
|
|
del state["rolloutReads"][name]
|
|
if name == "capi-controller-manager" and "bootstrapAfterCore" in state and obj["status"]["readyReplicas"] == 1:
|
|
state["providers"]["kubeadm"] = state.pop("bootstrapAfterCore")
|
|
save()
|
|
print(json.dumps(obj))
|
|
elif verb == "patch":
|
|
if resource == "deployments.apps" or "--type=json" not in args:
|
|
sys.exit("unexpected write")
|
|
patch = json.loads(args[args.index("--patch") + 1])
|
|
entry["patch"] = patch
|
|
if state.get("failPatch") == name:
|
|
log()
|
|
sys.exit("patch refused")
|
|
obj = state["providers"][name]
|
|
if state.get("replaceUIDOnPatch") == name:
|
|
obj["metadata"]["uid"] = "replacement"
|
|
save()
|
|
if state.get("replaceVersionOnPatch") == name:
|
|
obj["spec"]["version"] = "v1.12.4-core"
|
|
save()
|
|
if state.get("replaceFetchConfigOnPatch") == name:
|
|
obj["spec"]["fetchConfig"] = {"selector": {"matchLabels": {"competing": "repository"}}}
|
|
save()
|
|
if state.get("statusOnlyOnPatch") == name:
|
|
obj["metadata"]["resourceVersion"] = "2"
|
|
obj["status"]["conditions"] = [{"type": "Ready", "status": "False"}]
|
|
save()
|
|
candidate = copy.deepcopy(obj)
|
|
for operation in patch:
|
|
keys = [key.replace("~1", "/").replace("~0", "~") for key in operation["path"].strip("/").split("/")]
|
|
parent = candidate
|
|
for key in keys[:-1]:
|
|
parent = parent[key]
|
|
key = keys[-1]
|
|
if operation["op"] == "test":
|
|
if key not in parent or parent[key] != operation["value"]:
|
|
log()
|
|
sys.exit("JSON patch test failed")
|
|
elif operation["op"] in ("replace", "add") and operation["path"] in ("/spec/version", "/spec/fetchConfig"):
|
|
if operation["op"] == "replace" and key not in parent:
|
|
sys.exit("JSON patch replace target missing")
|
|
if name == "kubeadm":
|
|
core = state["providers"]["cluster-api"]
|
|
deployment = state["deployments"]["capi-controller-manager"]
|
|
if core["status"]["installedVersion"] == "v1.10.1-core" or deployment["status"]["readyReplicas"] != 1:
|
|
log()
|
|
sys.exit("bootstrap upgrade started before core was ready")
|
|
parent[key] = operation["value"]
|
|
else:
|
|
sys.exit("unexpected patch operation")
|
|
obj = candidate
|
|
state["providers"][name] = obj
|
|
obj["metadata"]["generation"] += 1
|
|
obj["status"]["conditions"] = [{"type": "Ready", "status": "False"}]
|
|
state.setdefault("pending", {})[name] = 1
|
|
entry["accepted"] = True
|
|
save()
|
|
log()
|
|
print(json.dumps(obj))
|
|
else:
|
|
sys.exit("unexpected kubectl operation")
|