forked from longtao.wu/droboticflow
454 lines
24 KiB
Python
454 lines
24 KiB
Python
"""Pure, offline Kubernetes renderer for one internal test batch.
|
|
|
|
The result is a review artifact, not an apply command. Credentials, source
|
|
captures, and cluster state never enter this module.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ipaddress
|
|
import re
|
|
from urllib.parse import urlsplit
|
|
|
|
|
|
NAMESPACE = "droboticflow-internal-test"
|
|
REQUIRED_SECRET_KEYS = (
|
|
"MONGO_ROOT_USERNAME",
|
|
"MONGO_ROOT_PASSWORD",
|
|
"MONGO_APP_USERNAME",
|
|
"MONGO_APP_PASSWORD",
|
|
"MONGO_URI",
|
|
"DROBOTICFLOW_API_TOKEN",
|
|
)
|
|
_PRODUCTION_HOSTS = {"120.48.180.80"}
|
|
_CONFIG_KEYS = {
|
|
"schemaVersion", "namespace", "batchId", "hostname", "productionOrigins",
|
|
"image", "mongoImage", "buildCommit", "workspaceGi", "mongoGi",
|
|
"resources", "storageClass", "tlsSecretName", "imagePullSecretName",
|
|
"ingressNamespace", "ingressClassName",
|
|
}
|
|
_DNS_LABEL = re.compile(r"^[a-z](?:[a-z0-9-]*[a-z0-9])?$")
|
|
_BATCH = re.compile(r"^[a-z](?:[a-z0-9-]{0,30}[a-z0-9])?$")
|
|
_IMAGE = re.compile(r"^[^\s@]+@sha256:[0-9a-f]{64}$")
|
|
_CPU = re.compile(r"^(?:[1-9][0-9]*m|[1-9][0-9]*)$")
|
|
_MEM = re.compile(r"^([1-9][0-9]*)(Mi|Gi)$")
|
|
|
|
|
|
def _required_string(value: object, field: str) -> str:
|
|
if not isinstance(value, str) or not value or value != value.strip():
|
|
raise ValueError(f"{field} must be a nonblank string without surrounding whitespace")
|
|
return value
|
|
|
|
|
|
def _dns_label(value: object, field: str) -> str:
|
|
value = _required_string(value, field)
|
|
if len(value) > 63 or not _DNS_LABEL.fullmatch(value):
|
|
raise ValueError(f"{field} must be a lowercase DNS label")
|
|
return value
|
|
|
|
|
|
def _quantity(value: object, field: str, *, cpu: bool) -> int:
|
|
value = _required_string(value, field)
|
|
if cpu:
|
|
if not _CPU.fullmatch(value):
|
|
raise ValueError(f"{field} must be an integer core or positive millicore quantity")
|
|
return int(value[:-1]) if value.endswith("m") else 1000 * int(value)
|
|
match = _MEM.fullmatch(value)
|
|
if not match:
|
|
raise ValueError(f"{field} must be a positive Mi or Gi quantity")
|
|
return int(match[1]) * (1024 if match[2] == "Gi" else 1)
|
|
|
|
|
|
def _origin_host(value: object) -> str:
|
|
value = _required_string(value, "productionOrigins entry")
|
|
parsed = urlsplit(value)
|
|
if parsed.scheme not in ("http", "https") or not parsed.hostname or parsed.username or parsed.password:
|
|
raise ValueError("productionOrigins must contain absolute HTTP(S) origins")
|
|
if parsed.path not in ("", "/") or parsed.query or parsed.fragment:
|
|
raise ValueError("productionOrigins entries must be origins without paths or queries")
|
|
try:
|
|
_ = parsed.port
|
|
except ValueError as exc:
|
|
raise ValueError("productionOrigins contains an invalid port") from exc
|
|
return parsed.hostname.lower().rstrip(".")
|
|
|
|
|
|
def validate_config(raw: dict) -> dict:
|
|
"""Reject ambiguous or unsafe deployment input and return derived names."""
|
|
if not isinstance(raw, dict):
|
|
raise ValueError("config must be a JSON object")
|
|
unknown = set(raw) - _CONFIG_KEYS - {
|
|
"databaseName", "resourcePrefix", "credentialsSecretName", "mongoServiceName",
|
|
"workspacePvcName", "mongoPvcName",
|
|
}
|
|
if unknown:
|
|
raise ValueError(f"unknown config fields: {', '.join(sorted(unknown))}")
|
|
if type(raw.get("schemaVersion")) is not int or raw["schemaVersion"] != 1:
|
|
raise ValueError("schemaVersion must be 1")
|
|
if raw.get("namespace", NAMESPACE) != NAMESPACE:
|
|
raise ValueError(f"namespace must be {NAMESPACE}")
|
|
batch = _required_string(raw.get("batchId"), "batchId")
|
|
if not _BATCH.fullmatch(batch):
|
|
raise ValueError("batchId must be 1-32 lowercase letters, digits, or hyphens")
|
|
hostname = _required_string(raw.get("hostname"), "hostname").lower()
|
|
if hostname != raw["hostname"] or hostname.endswith(".") or len(hostname) > 253:
|
|
raise ValueError("hostname must be a lowercase DNS name")
|
|
if len(hostname.split(".")) < 3 or any(not _DNS_LABEL.fullmatch(part) or len(part) > 63 for part in hostname.split(".")):
|
|
raise ValueError("hostname must be a dedicated fully qualified DNS name")
|
|
try:
|
|
ipaddress.ip_address(hostname)
|
|
except ValueError:
|
|
pass
|
|
else:
|
|
raise ValueError("hostname cannot be an IP address")
|
|
if hostname == "localhost" or hostname.endswith(".localhost"):
|
|
raise ValueError("hostname cannot be localhost")
|
|
origins = raw.get("productionOrigins")
|
|
if not isinstance(origins, list) or not origins:
|
|
raise ValueError("productionOrigins must be a nonempty list")
|
|
origin_hosts = {_origin_host(origin) for origin in origins}
|
|
if hostname in origin_hosts or hostname in _PRODUCTION_HOSTS:
|
|
raise ValueError("hostname must not be a production origin")
|
|
for field in ("image", "mongoImage"):
|
|
if not _IMAGE.fullmatch(_required_string(raw.get(field), field)):
|
|
raise ValueError(f"{field} must be pinned by a sha256 digest")
|
|
commit = _required_string(raw.get("buildCommit"), "buildCommit")
|
|
if not re.fullmatch(r"[0-9a-f]{40}", commit):
|
|
raise ValueError("buildCommit must be a full 40-character lowercase Git SHA")
|
|
for field, maximum in (("workspaceGi", 500), ("mongoGi", 100)):
|
|
value = raw.get(field)
|
|
if type(value) is not int or not 1 <= value <= maximum:
|
|
raise ValueError(f"{field} must be an integer between 1 and {maximum}")
|
|
resources = raw.get("resources")
|
|
if not isinstance(resources, dict) or set(resources) != {"api", "processor", "mongo"}:
|
|
raise ValueError("resources must specify api, processor, and mongo")
|
|
for component, value in resources.items():
|
|
if not isinstance(value, dict) or set(value) != {"requests", "limits"}:
|
|
raise ValueError(f"resources.{component} needs requests and limits")
|
|
for category in ("requests", "limits"):
|
|
quantities = value[category]
|
|
if not isinstance(quantities, dict) or set(quantities) != {"cpu", "memory"}:
|
|
raise ValueError(f"resources.{component}.{category} needs cpu and memory")
|
|
for key, cpu in (("cpu", True), ("memory", False)):
|
|
request = _quantity(value["requests"][key], f"resources.{component}.requests.{key}", cpu=cpu)
|
|
limit = _quantity(value["limits"][key], f"resources.{component}.limits.{key}", cpu=cpu)
|
|
maximum = (4000 if cpu else 8192) if component == "processor" else (2000 if cpu else 4096)
|
|
if request > limit or limit > maximum:
|
|
raise ValueError(f"resources.{component}.{key} request/limit exceeds the bounded lane")
|
|
for field in ("tlsSecretName", "imagePullSecretName", "ingressNamespace"):
|
|
_dns_label(raw.get(field), field)
|
|
if "storageClass" in raw:
|
|
_dns_label(raw["storageClass"], "storageClass")
|
|
if "ingressClassName" in raw:
|
|
_dns_label(raw["ingressClassName"], "ingressClassName")
|
|
prefix = f"internal-test-{batch}"
|
|
derived = {
|
|
"namespace": NAMESPACE,
|
|
"databaseName": "droboticflow_internal_test_" + batch.replace("-", "_"),
|
|
"resourcePrefix": prefix,
|
|
"credentialsSecretName": prefix + "-credentials",
|
|
"mongoServiceName": prefix + "-mongo",
|
|
"workspacePvcName": prefix + "-workspace",
|
|
"mongoPvcName": prefix + "-mongo-data",
|
|
}
|
|
for field, expected in derived.items():
|
|
if field in raw and raw[field] != expected:
|
|
raise ValueError(f"{field} must equal the batch-scoped derived value")
|
|
result = dict(raw)
|
|
result.update(derived)
|
|
return result
|
|
|
|
|
|
def render_environment(config: dict) -> dict:
|
|
"""Render one batch as a Kubernetes List; never talks to Kubernetes."""
|
|
c = validate_config(config)
|
|
ns, prefix, db = c["namespace"], c["resourcePrefix"], c["databaseName"]
|
|
secret, mongo = c["credentialsSecretName"], c["mongoServiceName"]
|
|
labels = {"app.kubernetes.io/part-of": "droboticflow-internal-test", "internal-test/batch": c["batchId"]}
|
|
|
|
def obj(api_version: str, kind: str, name: str, spec: dict | None = None, *, data: dict | None = None) -> dict:
|
|
item = {"apiVersion": api_version, "kind": kind, "metadata": {"name": name, "namespace": ns, "labels": labels}}
|
|
if spec is not None:
|
|
item["spec"] = spec
|
|
if data is not None:
|
|
item["data"] = data
|
|
return item
|
|
|
|
def env(name: str, *, key: str | None = None, value: str | None = None) -> dict:
|
|
if key is not None:
|
|
return {"name": name, "valueFrom": {"secretKeyRef": {"name": secret, "key": key}}}
|
|
return {"name": name, "value": value}
|
|
|
|
def pod_spec(component: str, container: dict, *, workspace: bool = False, init: list | None = None) -> dict:
|
|
spec = {
|
|
"serviceAccountName": prefix + "-" + component,
|
|
"automountServiceAccountToken": False,
|
|
"imagePullSecrets": [{"name": c["imagePullSecretName"]}],
|
|
"securityContext": {"runAsNonRoot": True, "runAsUser": 10001, "runAsGroup": 10001, "fsGroup": 10001, "seccompProfile": {"type": "RuntimeDefault"}},
|
|
"containers": [container],
|
|
}
|
|
if init:
|
|
spec["initContainers"] = init
|
|
if workspace:
|
|
spec["volumes"] = [{"name": "workspace", "persistentVolumeClaim": {"claimName": c["workspacePvcName"]}}]
|
|
return spec
|
|
|
|
def deployment(component: str, container: dict, *, workspace: bool = False, init: list | None = None) -> dict:
|
|
name = prefix + "-" + component
|
|
selector = {"app": name}
|
|
spec = {
|
|
"replicas": 0,
|
|
"strategy": {"type": "Recreate"},
|
|
"selector": {"matchLabels": selector},
|
|
"template": {"metadata": {"labels": {**labels, **selector}}, "spec": pod_spec(component, container, workspace=workspace, init=init)},
|
|
}
|
|
return obj("apps/v1", "Deployment", name, spec)
|
|
|
|
items = [{"apiVersion": "v1", "kind": "Namespace", "metadata": {"name": ns, "labels": {"app.kubernetes.io/part-of": "droboticflow-internal-test"}}}]
|
|
for component in ("api", "processor", "mongo", "initializer"):
|
|
items.append(obj("v1", "ServiceAccount", prefix + "-" + component))
|
|
items.append(obj("v1", "ConfigMap", prefix + "-config", data={
|
|
"MONGO_DB": db,
|
|
"PORT": "3001",
|
|
"WORKSPACE_MOUNT_PATH": "/workspace",
|
|
"ANNOTATION_PROCESSOR_MODE": "external",
|
|
"DROBOTICFLOW_REQUIRE_SESSION_AUTH": "true",
|
|
"DROBOTICFLOW_SESSION_COOKIE_SECURE": "true",
|
|
"DROBOTICFLOW_OBJECT_SYNC_ENABLED": "false",
|
|
"K8S_NAMESPACE": ns,
|
|
"WORKSPACE_PVC_NAME": c["workspacePvcName"],
|
|
"DROBOTICFLOW_BUILD_COMMIT": c["buildCommit"],
|
|
"INTERNAL_TEST_BATCH_ID": c["batchId"],
|
|
"INTERNAL_TEST_ENVIRONMENT": "true",
|
|
}))
|
|
for kind, size in (("workspace", c["workspaceGi"]), ("mongo-data", c["mongoGi"])):
|
|
name = c["workspacePvcName"] if kind == "workspace" else c["mongoPvcName"]
|
|
pvc = obj("v1", "PersistentVolumeClaim", name, {
|
|
"accessModes": ["ReadWriteMany" if kind == "workspace" else "ReadWriteOnce"],
|
|
"resources": {"requests": {"storage": f"{size}Gi"}},
|
|
})
|
|
pvc["metadata"]["annotations"] = {"helm.sh/resource-policy": "keep", "internal-test/retention": "manual-review-only"}
|
|
if "storageClass" in c:
|
|
pvc["spec"]["storageClassName"] = c["storageClass"]
|
|
items.append(pvc)
|
|
mongo_bootstrap = (
|
|
"const name = process.env.MONGO_INITDB_DATABASE;\n"
|
|
"if (!/^droboticflow_internal_test_[a-z0-9_]+$/.test(name)) throw Error('unsafe database name');\n"
|
|
"db.getSiblingDB(name).createUser({user: process.env.MONGO_APP_USERNAME,"
|
|
" pwd: process.env.MONGO_APP_PASSWORD, roles: [{role: 'readWrite', db: name}]});\n"
|
|
)
|
|
items.append(obj("v1", "ConfigMap", prefix + "-mongo-init", data={"01-create-app-user.js": mongo_bootstrap}))
|
|
items.append(obj("v1", "Service", mongo, {
|
|
"type": "ClusterIP", "selector": {"app": mongo},
|
|
"ports": [{"name": "mongo", "port": 27017, "targetPort": 27017}],
|
|
}))
|
|
mongo_container = {
|
|
"name": "mongo", "image": c["mongoImage"], "imagePullPolicy": "IfNotPresent",
|
|
"ports": [{"name": "mongo", "containerPort": 27017}],
|
|
"env": [
|
|
env("MONGO_INITDB_DATABASE", value=db),
|
|
env("MONGO_INITDB_ROOT_USERNAME", key="MONGO_ROOT_USERNAME"),
|
|
env("MONGO_INITDB_ROOT_PASSWORD", key="MONGO_ROOT_PASSWORD"),
|
|
env("MONGO_APP_USERNAME", key="MONGO_APP_USERNAME"),
|
|
env("MONGO_APP_PASSWORD", key="MONGO_APP_PASSWORD"),
|
|
],
|
|
"volumeMounts": [{"name": "data", "mountPath": "/data/db"}, {"name": "mongo-init", "mountPath": "/docker-entrypoint-initdb.d", "readOnly": True}],
|
|
"resources": c["resources"]["mongo"],
|
|
"securityContext": {"allowPrivilegeEscalation": False, "capabilities": {"drop": ["ALL"]}},
|
|
"readinessProbe": {"tcpSocket": {"port": "mongo"}, "periodSeconds": 10},
|
|
}
|
|
mongo_pod = pod_spec("mongo", mongo_container)
|
|
mongo_pod["volumes"] = [
|
|
{"name": "data", "persistentVolumeClaim": {"claimName": c["mongoPvcName"]}},
|
|
{"name": "mongo-init", "configMap": {"name": prefix + "-mongo-init"}},
|
|
]
|
|
# The official Mongo entrypoint can initialize as its pre-created uid 999.
|
|
# Kubernetes fsGroup makes a fresh PVC writable without root chown/gosu.
|
|
mongo_pod["securityContext"] = {
|
|
"runAsNonRoot": True, "runAsUser": 999, "runAsGroup": 999,
|
|
"fsGroup": 999, "seccompProfile": {"type": "RuntimeDefault"},
|
|
}
|
|
items.append(obj("apps/v1", "Deployment", mongo, {
|
|
"replicas": 1, "strategy": {"type": "Recreate"},
|
|
"selector": {"matchLabels": {"app": mongo}},
|
|
"template": {"metadata": {"labels": {**labels, "app": mongo}}, "spec": mongo_pod},
|
|
}))
|
|
app_mongo_env = [env("MONGO_DB", value=db), env("MONGO_URI", key="MONGO_URI")]
|
|
expected_uri_suffix = f"@{mongo}.{ns}.svc.cluster.local:27017/{db}?authSource={db}"
|
|
guard = {
|
|
"name": "validate-mongo-target", "image": c["image"], "imagePullPolicy": "IfNotPresent",
|
|
"command": ["/bin/sh", "-ec", """
|
|
case "$MONGO_URI" in mongodb://*"$EXPECTED_URI_SUFFIX") ;; *)
|
|
echo 'Mongo target must be the isolated test database' >&2; exit 1;; esac
|
|
uri_body=${MONGO_URI#mongodb://}
|
|
userinfo=${uri_body%%@*}
|
|
case "$userinfo" in *:*) ;; *) exit 1;; esac
|
|
case "${uri_body#*@}" in *'@'*) exit 1;; esac
|
|
"""],
|
|
"env": [env("MONGO_URI", key="MONGO_URI"), env("EXPECTED_URI_SUFFIX", value=expected_uri_suffix)],
|
|
"resources": {"requests": {"cpu": "10m", "memory": "16Mi"}, "limits": {"cpu": "100m", "memory": "64Mi"}},
|
|
"securityContext": {"allowPrivilegeEscalation": False, "capabilities": {"drop": ["ALL"]}},
|
|
}
|
|
seed_gate = {
|
|
"name": "wait-for-seed", "image": c["mongoImage"], "imagePullPolicy": "IfNotPresent",
|
|
"command": ["/bin/sh", "-ec", """
|
|
i=0
|
|
while [ "$i" -lt 120 ]; do
|
|
if mongosh "$MONGO_URI" --quiet --eval '
|
|
const ready = db.getCollection("_internalTestSeed").findOne({
|
|
_id: "environment", kind: "droboticflow-internal-test-lane",
|
|
batchId: process.env.EXPECTED_BATCH, state: 'complete'
|
|
});
|
|
quit(ready ? 0 : 1);
|
|
' >/dev/null 2>&1; then exit 0; fi
|
|
i=$((i + 1))
|
|
sleep 5
|
|
done
|
|
echo 'Internal test seed is not complete' >&2
|
|
exit 1
|
|
"""],
|
|
"env": [env("MONGO_URI", key="MONGO_URI"), env("EXPECTED_BATCH", value=c["batchId"])],
|
|
"resources": {"requests": {"cpu": "10m", "memory": "32Mi"}, "limits": {"cpu": "100m", "memory": "128Mi"}},
|
|
"securityContext": {"allowPrivilegeEscalation": False, "capabilities": {"drop": ["ALL"]}},
|
|
}
|
|
api_container = {
|
|
"name": "api", "image": c["image"], "imagePullPolicy": "IfNotPresent",
|
|
"ports": [{"name": "http", "containerPort": 3001}],
|
|
"env": app_mongo_env + [
|
|
env("PORT", value="3001"), env("DIST_DIR", value="/app/dist"),
|
|
env("WORKSPACE_MOUNT_PATH", value="/workspace"),
|
|
env("ANNOTATION_PROCESSOR_MODE", value="external"),
|
|
env("DROBOTICFLOW_REQUIRE_SESSION_AUTH", value="true"),
|
|
env("DROBOTICFLOW_SESSION_COOKIE_SECURE", value="true"),
|
|
env("DROBOTICFLOW_OBJECT_SYNC_ENABLED", value="false"),
|
|
env("K8S_NAMESPACE", value=ns), env("WORKSPACE_PVC_NAME", value=c["workspacePvcName"]),
|
|
env("DROBOTICFLOW_BUILD_COMMIT", value=c["buildCommit"]),
|
|
env("DROBOTICFLOW_API_TOKEN", key="DROBOTICFLOW_API_TOKEN"),
|
|
],
|
|
"volumeMounts": [{"name": "workspace", "mountPath": "/workspace"}],
|
|
"resources": c["resources"]["api"],
|
|
"securityContext": {"allowPrivilegeEscalation": False, "capabilities": {"drop": ["ALL"]}},
|
|
"readinessProbe": {"httpGet": {"path": "/api/ready", "port": "http"}, "periodSeconds": 10},
|
|
}
|
|
items.append(deployment("api", api_container, workspace=True, init=[guard, seed_gate]))
|
|
processor_container = {
|
|
"name": "processor", "image": c["image"], "imagePullPolicy": "IfNotPresent",
|
|
"command": ["/usr/local/bin/droboticflow-api"],
|
|
"ports": [{"name": "health", "containerPort": 3003}],
|
|
"env": app_mongo_env + [env("ANNOTATION_PROCESSOR_MODE", value="processor"),
|
|
env("ANNOTATION_PROCESSOR_CONCURRENCY", value="1"),
|
|
env("ANNOTATION_INDEX_BACKFILL", value="0"),
|
|
env("WORKSPACE_MOUNT_PATH", value="/workspace")],
|
|
"volumeMounts": [{"name": "workspace", "mountPath": "/workspace"}],
|
|
"resources": c["resources"]["processor"],
|
|
"securityContext": {"allowPrivilegeEscalation": False, "capabilities": {"drop": ["ALL"]}},
|
|
"readinessProbe": {"httpGet": {"path": "/ready", "port": "health"}, "periodSeconds": 10},
|
|
}
|
|
items.append(deployment("processor", processor_container, workspace=True, init=[guard, seed_gate]))
|
|
items.append(obj("v1", "Service", prefix + "-api", {
|
|
"type": "ClusterIP", "selector": {"app": prefix + "-api"},
|
|
"ports": [{"name": "http", "port": 80, "targetPort": 3001}],
|
|
}))
|
|
ingress = obj("networking.k8s.io/v1", "Ingress", prefix + "-api", {
|
|
"tls": [{"hosts": [c["hostname"]], "secretName": c["tlsSecretName"]}],
|
|
"rules": [{"host": c["hostname"], "http": {"paths": [{"path": "/", "pathType": "Prefix", "backend": {"service": {"name": prefix + "-api", "port": {"number": 80}}}}]}}],
|
|
})
|
|
ingress["metadata"]["annotations"] = {"nginx.ingress.kubernetes.io/ssl-redirect": "true"}
|
|
if "ingressClassName" in c:
|
|
ingress["spec"]["ingressClassName"] = c["ingressClassName"]
|
|
items.append(ingress)
|
|
items.append(obj("v1", "ResourceQuota", "internal-test-quota", {"hard": {
|
|
"pods": "3", "persistentvolumeclaims": "8",
|
|
"requests.storage": f"{4 * (c['workspaceGi'] + c['mongoGi'])}Gi",
|
|
"requests.cpu": _millicores(sum(_quantity(c["resources"][component]["requests"]["cpu"], "cpu", cpu=True) for component in ("api", "processor", "mongo"))),
|
|
"limits.cpu": _millicores(sum(_quantity(c["resources"][component]["limits"]["cpu"], "cpu", cpu=True) for component in ("api", "processor", "mongo"))),
|
|
"requests.memory": f"{sum(_quantity(c['resources'][component]['requests']['memory'], 'memory', cpu=False) for component in ('api', 'processor', 'mongo'))}Mi",
|
|
"limits.memory": f"{sum(_quantity(c['resources'][component]['limits']['memory'], 'memory', cpu=False) for component in ('api', 'processor', 'mongo'))}Mi",
|
|
}}))
|
|
items.append(obj("networking.k8s.io/v1", "NetworkPolicy", prefix + "-default-deny", {
|
|
"podSelector": {}, "policyTypes": ["Ingress", "Egress"],
|
|
}))
|
|
items.append(obj("networking.k8s.io/v1", "NetworkPolicy", prefix + "-allow-mongo", {
|
|
"podSelector": {"matchLabels": {"app": mongo}}, "policyTypes": ["Ingress"],
|
|
"ingress": [{"from": [{"podSelector": {"matchLabels": {"app": prefix + "-api"}}},
|
|
{"podSelector": {"matchLabels": {"app": prefix + "-processor"}}},
|
|
{"podSelector": {"matchLabels": {"app": prefix + "-initializer"}}}],
|
|
"ports": [{"protocol": "TCP", "port": 27017}]}],
|
|
}))
|
|
items.append(obj("networking.k8s.io/v1", "NetworkPolicy", prefix + "-allow-app-egress", {
|
|
"podSelector": {"matchExpressions": [{"key": "app", "operator": "In", "values": [prefix + "-api", prefix + "-processor", prefix + "-initializer"]}]},
|
|
"policyTypes": ["Egress"],
|
|
"egress": [
|
|
{"to": [{"podSelector": {"matchLabels": {"app": mongo}}}], "ports": [{"protocol": "TCP", "port": 27017}]},
|
|
{"to": [{"namespaceSelector": {"matchLabels": {"kubernetes.io/metadata.name": "kube-system"}}}], "ports": [{"protocol": "UDP", "port": 53}, {"protocol": "TCP", "port": 53}]},
|
|
],
|
|
}))
|
|
items.append(obj("networking.k8s.io/v1", "NetworkPolicy", prefix + "-allow-ingress", {
|
|
"podSelector": {"matchLabels": {"app": prefix + "-api"}}, "policyTypes": ["Ingress"],
|
|
"ingress": [{"from": [{"namespaceSelector": {"matchLabels": {"kubernetes.io/metadata.name": c["ingressNamespace"]}}}],
|
|
"ports": [{"protocol": "TCP", "port": 3001}]}],
|
|
}))
|
|
# Apply policies and quotas before the first workload can become runnable.
|
|
priority = {
|
|
"Namespace": 0, "ResourceQuota": 1, "NetworkPolicy": 2,
|
|
"ServiceAccount": 3, "ConfigMap": 4, "PersistentVolumeClaim": 5,
|
|
"Service": 6, "Deployment": 7, "Ingress": 8,
|
|
}
|
|
items.sort(key=lambda item: priority[item["kind"]])
|
|
return {"apiVersion": "v1", "kind": "List", "items": items}
|
|
|
|
|
|
def render_initializer(config: dict) -> dict:
|
|
"""Render a temporary pod for an approved seed operation.
|
|
|
|
The pod is deliberately absent from render_environment. An operator
|
|
creates it only after review, then removes it before enabling the API and
|
|
processor. The application image supplies the same Debian runtime as the
|
|
seed executable, but this manifest does not embed assets or credentials.
|
|
"""
|
|
c = validate_config(config)
|
|
prefix = c["resourcePrefix"]
|
|
suffix = f"@{c['mongoServiceName']}.{c['namespace']}.svc.cluster.local:27017/{c['databaseName']}?authSource={c['databaseName']}"
|
|
return {
|
|
"apiVersion": "v1", "kind": "Pod",
|
|
"metadata": {
|
|
"name": prefix + "-initializer", "namespace": c["namespace"],
|
|
"labels": {"app.kubernetes.io/part-of": "droboticflow-internal-test", "internal-test/batch": c["batchId"], "app": prefix + "-initializer"},
|
|
"annotations": {"internal-test/purpose": "temporary-approved-seed"},
|
|
},
|
|
"spec": {
|
|
"serviceAccountName": prefix + "-initializer", "automountServiceAccountToken": False,
|
|
"restartPolicy": "Never", "activeDeadlineSeconds": 28800,
|
|
"imagePullSecrets": [{"name": c["imagePullSecretName"]}],
|
|
"securityContext": {"runAsNonRoot": True, "runAsUser": 10001, "runAsGroup": 10001, "fsGroup": 10001, "seccompProfile": {"type": "RuntimeDefault"}},
|
|
"containers": [{
|
|
"name": "initializer", "image": c["image"], "imagePullPolicy": "IfNotPresent",
|
|
"command": ["/bin/sh", "-ec", """
|
|
case "$MONGO_URI" in mongodb://*"$EXPECTED_URI_SUFFIX") ;; *)
|
|
echo 'Mongo target must be the isolated test database' >&2; exit 1;; esac
|
|
uri_body=${MONGO_URI#mongodb://}
|
|
case "${uri_body#*@}" in *'@'*) exit 1;; esac
|
|
sleep 28800
|
|
"""],
|
|
"env": [
|
|
{"name": "MONGO_URI", "valueFrom": {"secretKeyRef": {"name": c["credentialsSecretName"], "key": "MONGO_URI"}}},
|
|
{"name": "EXPECTED_URI_SUFFIX", "value": suffix},
|
|
{"name": "MONGO_DB", "value": c["databaseName"]},
|
|
{"name": "WORKSPACE_MOUNT_PATH", "value": "/workspace"},
|
|
{"name": "INTERNAL_TEST_BATCH_ID", "value": c["batchId"]},
|
|
],
|
|
"resources": c["resources"]["api"],
|
|
"securityContext": {"allowPrivilegeEscalation": False, "capabilities": {"drop": ["ALL"]}},
|
|
"volumeMounts": [{"name": "workspace", "mountPath": "/workspace"}, {"name": "scratch", "mountPath": "/tmp"}],
|
|
}],
|
|
"volumes": [
|
|
{"name": "workspace", "persistentVolumeClaim": {"claimName": c["workspacePvcName"]}},
|
|
{"name": "scratch", "emptyDir": {"sizeLimit": "2Gi"}},
|
|
],
|
|
},
|
|
}
|
|
|
|
|
|
def _millicores(value: int) -> str:
|
|
return f"{value}m"
|