- build and push v1.1 images from the service main worktrees - switch manifests to :v1.1; add web control Secret refs - configure API BOOTSTRAP_REFRESH=60s and remove unused DATA_DIR - add k6 presenter scenario with a mid-run control POST - require control credentials in make load; update runbook and drills
267 lines
12 KiB
Python
Executable File
267 lines
12 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Static, cluster-free guardrails for deploy/k8s.
|
|
|
|
Usage: check_manifests.py <dir> <namespace> <tag> <registry> <owner>
|
|
Exit 0 when the manifests satisfy the namespace-only and pod-security rules.
|
|
"""
|
|
import glob
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
root, ns, tag, registry, owner = sys.argv[1:6]
|
|
errors = []
|
|
warns = []
|
|
docs = []
|
|
|
|
for path in sorted(glob.glob(os.path.join(root, "*.yaml"))):
|
|
with open(path, encoding="utf-8") as handle:
|
|
try:
|
|
loaded = [d for d in __import__("yaml").safe_load_all(handle) if d]
|
|
except Exception as exc: # noqa: BLE001 - report the parse error verbatim
|
|
errors.append(f"{os.path.basename(path)}: YAML parse error: {exc}")
|
|
continue
|
|
if not loaded:
|
|
errors.append(f"{os.path.basename(path)}: no YAML documents")
|
|
for doc in loaded:
|
|
doc["__file__"] = os.path.basename(path)
|
|
docs.append(doc)
|
|
|
|
if not docs:
|
|
errors.append("deploy/k8s contains no manifests")
|
|
|
|
allowed = {"ConfigMap", "Deployment", "Service"}
|
|
forbidden = {
|
|
"Ingress", "Job", "CronJob", "NetworkPolicy", "Role", "RoleBinding",
|
|
"ClusterRole", "ClusterRoleBinding", "CustomResourceDefinition",
|
|
"Namespace", "PersistentVolume", "PersistentVolumeClaim", "StorageClass",
|
|
"PodSecurityPolicy", "PriorityClass", "APIService",
|
|
"MutatingWebhookConfiguration", "ValidatingWebhookConfiguration",
|
|
}
|
|
|
|
objects = {}
|
|
configmaps = {}
|
|
services = {}
|
|
deployments = {}
|
|
|
|
for doc in docs:
|
|
where = f"{doc['__file__']}:{doc.get('kind', '?')}/{doc.get('metadata', {}).get('name', '?')}"
|
|
kind = doc.get("kind")
|
|
meta = doc.get("metadata") or {}
|
|
name = meta.get("name")
|
|
if not doc.get("apiVersion") or not kind or not name:
|
|
errors.append(f"{where}: missing apiVersion, kind, or metadata.name")
|
|
continue
|
|
if kind not in allowed:
|
|
errors.append(f"{where}: kind {kind} is not allowed (namespace-only: ConfigMap, Deployment, Service)")
|
|
continue
|
|
if kind in forbidden:
|
|
errors.append(f"{where}: forbidden kind {kind}")
|
|
if meta.get("namespace") != ns:
|
|
errors.append(f"{where}: metadata.namespace is {meta.get('namespace')!r}, expected {ns!r}")
|
|
key = (kind, name)
|
|
if key in objects:
|
|
errors.append(f"{where}: duplicate object")
|
|
objects[key] = doc
|
|
if kind == "ConfigMap":
|
|
configmaps[name] = doc
|
|
elif kind == "Service":
|
|
services[name] = doc
|
|
elif kind == "Deployment":
|
|
deployments[name] = doc
|
|
|
|
for name, cm in configmaps.items():
|
|
data = cm.get("data") or {}
|
|
for key in data:
|
|
if re.search(r"(?i)(token|password|secret|private[_-]?key|credential)", key):
|
|
errors.append(f"ConfigMap/{name}: key {key!r} looks like a secret; use a Secret created by the human")
|
|
|
|
required_config = {
|
|
"pulsecast-shared": {"PUBLIC_WEB_URL", "PUBLIC_API_URL", "API_INTERNAL_URL", "ML_INTERNAL_URL"},
|
|
"web-config": {"PORT", "NUXT_PUBLIC_API_BASE", "NUXT_API_INTERNAL_BASE"},
|
|
"api-config": {"PORT", "FASTAPI_URL", "BOOTSTRAP_REFRESH", "ML_TIMEOUT", "SSE_HEARTBEAT"},
|
|
"ml-config": {
|
|
"PORT", "PULSECAST_ROOT", "PULSECAST_ARTIFACTS", "PULSECAST_WEATHER",
|
|
"PULSECAST_MODELS", "PULSECAST_UPLOADS",
|
|
},
|
|
}
|
|
for name, keys in required_config.items():
|
|
cm = configmaps.get(name)
|
|
if cm is None:
|
|
errors.append(f"missing ConfigMap/{name}")
|
|
continue
|
|
have = set((cm.get("data") or {}).keys())
|
|
for key in sorted(keys - have):
|
|
errors.append(f"ConfigMap/{name}: missing key {key}")
|
|
if "DATA_DIR" in ((configmaps.get("api-config") or {}).get("data") or {}):
|
|
errors.append("ConfigMap/api-config: DATA_DIR is no longer used by the v1.1 API")
|
|
|
|
expected_ports = {"web": 3000, "api": 8080, "ml": 8000, "cloudflared": 2000}
|
|
expected_uids = {"web": 10001, "api": 65532, "ml": 10001, "cloudflared": 65532}
|
|
expected_images = {
|
|
"web": f"{registry}/{owner}/pulsecast-web:{tag}",
|
|
"api": f"{registry}/{owner}/pulsecast-api:{tag}",
|
|
"ml": f"{registry}/{owner}/pulsecast-ml:{tag}",
|
|
}
|
|
gitea_images = {"web", "api", "ml"}
|
|
|
|
for name in ("web", "api", "ml", "cloudflared"):
|
|
dep = deployments.get(name)
|
|
if dep is None:
|
|
errors.append(f"missing Deployment/{name}")
|
|
continue
|
|
spec = dep.get("spec") or {}
|
|
if spec.get("replicas") != 1:
|
|
errors.append(f"Deployment/{name}: replicas should be 1")
|
|
template = (spec.get("template") or {})
|
|
pod = template.get("spec") or {}
|
|
if pod.get("automountServiceAccountToken") is not False:
|
|
errors.append(f"Deployment/{name}: automountServiceAccountToken must be false")
|
|
pod_security = pod.get("securityContext") or {}
|
|
if (pod_security.get("seccompProfile") or {}).get("type") != "RuntimeDefault":
|
|
errors.append(f"Deployment/{name}: seccompProfile must be RuntimeDefault")
|
|
for field in ("runAsUser", "runAsGroup", "fsGroup"):
|
|
value = pod_security.get(field)
|
|
if not isinstance(value, int) or value <= 0:
|
|
errors.append(f"Deployment/{name}: {field} must be a positive numeric id (SKE runAsNonRoot caveat)")
|
|
elif value != expected_uids[name]:
|
|
errors.append(f"Deployment/{name}: {field} is {value}, expected {expected_uids[name]}")
|
|
if pod_security.get("runAsNonRoot") is True and pod_security.get("runAsUser") == 0:
|
|
errors.append(f"Deployment/{name}: runAsUser 0 conflicts with runAsNonRoot")
|
|
for field in ("hostNetwork", "hostPID", "hostIPC"):
|
|
if pod.get(field):
|
|
errors.append(f"Deployment/{name}: {field} is not allowed")
|
|
if name in gitea_images:
|
|
pulls = pod.get("imagePullSecrets") or []
|
|
if not any(p.get("name") == "gitea-regcred" for p in pulls):
|
|
errors.append(f"Deployment/{name}: imagePullSecrets must include gitea-regcred")
|
|
containers = pod.get("containers") or []
|
|
if len(containers) != 1:
|
|
errors.append(f"Deployment/{name}: expected exactly one container")
|
|
continue
|
|
container = containers[0]
|
|
if container.get("name") != name:
|
|
errors.append(f"Deployment/{name}: container name is {container.get('name')!r}")
|
|
image = container.get("image") or ""
|
|
if image.endswith(":latest"):
|
|
errors.append(f"Deployment/{name}: image tag must be pinned, not :latest")
|
|
if name in gitea_images and not image.startswith(f"{registry}/{owner}/"):
|
|
errors.append(f"Deployment/{name}: image {image!r} is not under {registry}/{owner}/")
|
|
if name in expected_images and image != expected_images[name]:
|
|
warns.append(f"Deployment/{name}: image {image!r} does not match TAG={tag}")
|
|
if name == "cloudflared" and not image.startswith("cloudflare/cloudflared:"):
|
|
errors.append(f"Deployment/{name}: unexpected image {image!r}")
|
|
if name in expected_ports:
|
|
ports = container.get("ports") or []
|
|
if not any(int(p.get("containerPort", -1)) == expected_ports[name] for p in ports):
|
|
errors.append(f"Deployment/{name}: containerPort {expected_ports[name]} not found")
|
|
for port in ports:
|
|
if port.get("hostPort"):
|
|
errors.append(f"Deployment/{name}: hostPort is not allowed")
|
|
security = container.get("securityContext") or {}
|
|
if security.get("runAsNonRoot") is not True:
|
|
errors.append(f"Deployment/{name}: runAsNonRoot must be true")
|
|
if security.get("allowPrivilegeEscalation") is not False:
|
|
errors.append(f"Deployment/{name}: allowPrivilegeEscalation must be false")
|
|
if security.get("readOnlyRootFilesystem") is not True:
|
|
errors.append(f"Deployment/{name}: readOnlyRootFilesystem must be true")
|
|
if security.get("privileged") is True:
|
|
errors.append(f"Deployment/{name}: privileged is not allowed")
|
|
if security.get("runAsUser") == 0:
|
|
errors.append(f"Deployment/{name}: runAsUser 0 is not allowed")
|
|
drops = ((security.get("capabilities") or {}).get("drop") or [])
|
|
if "ALL" not in drops:
|
|
errors.append(f"Deployment/{name}: capabilities.drop must include ALL")
|
|
resources = container.get("resources") or {}
|
|
requests = resources.get("requests") or {}
|
|
limits = resources.get("limits") or {}
|
|
for group_name, group in (("requests", requests), ("limits", limits)):
|
|
for key in ("cpu", "memory"):
|
|
if not group.get(key):
|
|
errors.append(f"Deployment/{name}: resources.{group_name}.{key} is missing")
|
|
mounts = container.get("volumeMounts") or []
|
|
tmp_mounts = [m for m in mounts if m.get("mountPath") == "/tmp"]
|
|
if not tmp_mounts:
|
|
errors.append(f"Deployment/{name}: /tmp volumeMount is missing")
|
|
else:
|
|
volume_name = tmp_mounts[0].get("name")
|
|
volumes = pod.get("volumes") or []
|
|
if not any(v.get("name") == volume_name and "emptyDir" in v for v in volumes):
|
|
errors.append(f"Deployment/{name}: /tmp must be backed by an emptyDir")
|
|
for probe in ("startupProbe", "livenessProbe", "readinessProbe"):
|
|
probe_spec = container.get(probe) or {}
|
|
http = probe_spec.get("httpGet") or {}
|
|
if not http.get("path") or not http.get("port"):
|
|
errors.append(f"Deployment/{name}: {probe}.httpGet is missing")
|
|
refs = []
|
|
for source in container.get("envFrom") or []:
|
|
if source.get("configMapRef"):
|
|
refs.append(source["configMapRef"].get("name"))
|
|
for env in container.get("env") or []:
|
|
if env.get("valueFrom", {}).get("configMapKeyRef"):
|
|
refs.append(env["valueFrom"]["configMapKeyRef"].get("name"))
|
|
for ref in refs:
|
|
if ref not in configmaps:
|
|
errors.append(f"Deployment/{name}: references missing ConfigMap/{ref}")
|
|
expected_secret_refs = {
|
|
"web": {
|
|
("control-auth", "CONTROL_USER"),
|
|
("control-auth", "CONTROL_PASS"),
|
|
("control-auth", "CONTROL_TOKEN"),
|
|
},
|
|
"api": {
|
|
("control-auth", "CONTROL_USER"),
|
|
("control-auth", "CONTROL_PASS"),
|
|
("control-auth", "CONTROL_TOKEN"),
|
|
},
|
|
"ml": {("control-auth", "CONTROL_TOKEN")},
|
|
"cloudflared": {("cloudflared", "TUNNEL_TOKEN")},
|
|
}
|
|
seen_secret_refs = set()
|
|
for env in container.get("env") or []:
|
|
secret_ref = env.get("valueFrom", {}).get("secretKeyRef")
|
|
if not secret_ref:
|
|
continue
|
|
secret_name = secret_ref.get("name")
|
|
secret_key = secret_ref.get("key")
|
|
if secret_name not in ("gitea-regcred", "cloudflared", "control-auth"):
|
|
errors.append(f"Deployment/{name}: unexpected Secret reference {secret_name!r}")
|
|
seen_secret_refs.add((secret_name, secret_key))
|
|
for required in expected_secret_refs.get(name, set()):
|
|
if required not in seen_secret_refs:
|
|
errors.append(f"Deployment/{name}: missing Secret reference {required[0]}/{required[1]}")
|
|
|
|
if "cloudflared" in deployments:
|
|
args = ((deployments["cloudflared"].get("spec") or {}).get("template") or {}).get("spec", {}).get("containers", [{}])[0].get("args") or []
|
|
for required in ("tunnel", "run", "--token", "$(TUNNEL_TOKEN)", "--no-autoupdate", "--protocol", "http2"):
|
|
if required not in args:
|
|
errors.append(f"Deployment/cloudflared: args must include {required!r}")
|
|
|
|
for name in ("web", "api", "ml"):
|
|
svc = services.get(name)
|
|
if svc is None:
|
|
errors.append(f"missing Service/{name}")
|
|
continue
|
|
spec = svc.get("spec") or {}
|
|
if spec.get("type") != "ClusterIP":
|
|
errors.append(f"Service/{name}: type must be ClusterIP")
|
|
for field in ("externalIPs", "loadBalancerIP", "loadBalancerSourceRanges"):
|
|
if spec.get(field):
|
|
errors.append(f"Service/{name}: {field} is not allowed")
|
|
ports = spec.get("ports") or []
|
|
if not any(int(p.get("port", -1)) == expected_ports[name] for p in ports):
|
|
errors.append(f"Service/{name}: port {expected_ports[name]} not found")
|
|
if any(p.get("nodePort") for p in ports):
|
|
errors.append(f"Service/{name}: nodePort is not allowed")
|
|
|
|
kinds = {}
|
|
for kind, _ in objects:
|
|
kinds[kind] = kinds.get(kind, 0) + 1
|
|
summary = ", ".join(f"{kind}={count}" for kind, count in sorted(kinds.items()))
|
|
print(f"parsed {len(objects)} objects ({summary})")
|
|
for message in warns:
|
|
print(f"WARN: {message}")
|
|
for message in errors:
|
|
print(f"FAIL: {message}")
|
|
sys.exit(1 if errors else 0)
|