Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /ThalesGroup_fred /scripts /generate_chart_schema.py
| # /// script | |
| # dependencies = [] | |
| # /// | |
| """Generate values.schema.json for the Fred Helm chart. | |
| Inlines the three backend configuration JSON schemas (already generated by | |
| generate_config_schema.py) into the correct positions of a hand-written | |
| chart-level schema. The result is a single self-contained JSON Schema | |
| (draft-07, no $ref) that Helm and IDE plugins can use for validation and | |
| autocompletion. | |
| Usage (called automatically by python-config-schema.mk): | |
| python scripts/generate_chart_schema.py \\ | |
| --fred-agents apps/fred-agents/config/schema/configuration.schema.json \\ | |
| --knowledge-flow apps/knowledge-flow-backend/config/schema/configuration.schema.json \\ | |
| --control-plane apps/control-plane-backend/config/schema/configuration.schema.json \\ | |
| --output deploy/charts/fred/values.schema.json | |
| """ | |
| import argparse | |
| import copy | |
| import json | |
| import sys | |
| from pathlib import Path | |
| # --------------------------------------------------------------------------- | |
| # Helpers | |
| # --------------------------------------------------------------------------- | |
| def _load(path: Path) -> dict: | |
| with open(path) as f: | |
| return json.load(f) | |
| def _inline_refs(schema: dict) -> dict: | |
| """Resolve all $ref pointers within a self-contained schema that uses $defs. | |
| Uses a `seen` set to break cycles: if a definition is currently being | |
| expanded, replace the back-reference with an empty object rather than | |
| looping indefinitely. | |
| """ | |
| schema = copy.deepcopy(schema) | |
| defs = schema.get("$defs", {}) | |
| def _resolve(node: object, seen: frozenset[str] = frozenset()) -> object: | |
| if isinstance(node, dict): | |
| if "$ref" in node: | |
| ref = node["$ref"] | |
| if ref.startswith("#/$defs/"): | |
| name = ref[len("#/$defs/"):] | |
| if name in seen: | |
| return {} # cycle guard: emit empty object | |
| if name not in defs: | |
| raise ValueError( | |
| f"Invalid schema: $ref '{ref}' points to missing definition '{name}' in $defs" | |
| ) | |
| return _resolve(copy.deepcopy(defs[name]), seen | {name}) | |
| return node | |
| return {k: _resolve(v, seen) for k, v in node.items() if k != "$defs"} | |
| if isinstance(node, list): | |
| return [_resolve(i, seen) for i in node] | |
| return node | |
| resolved = _resolve(schema) | |
| resolved.pop("$defs", None) | |
| return resolved | |
| # --------------------------------------------------------------------------- | |
| # Shared Kubernetes / Helm sub-schemas | |
| # --------------------------------------------------------------------------- | |
| _STRING = {"type": "string"} | |
| _BOOL = {"type": "boolean"} | |
| _INT = {"type": "integer"} | |
| _OBJECT_FREE = {"type": "object"} # used only where the shape is truly open (annotations, labels) | |
| _ANY = {} | |
| def _obj(props: dict, required: list[str] | None = None, additional: bool = False) -> dict: | |
| s: dict = {"type": "object", "properties": props, "additionalProperties": additional} | |
| if required: | |
| s["required"] = required | |
| return s | |
| def _arr(items: dict) -> dict: | |
| return {"type": "array", "items": items} | |
| # ── Kubernetes primitives ──────────────────────────────────────────────────── | |
| _IMAGE = _obj({ | |
| "repository": _STRING, | |
| "tag": _STRING, | |
| "pullPolicy": {"type": "string", "enum": ["Always", "IfNotPresent", "Never"]}, | |
| }) | |
| _ROLLING_UPDATE = _obj({ | |
| "maxSurge": {"anyOf": [_INT, _STRING]}, | |
| "maxUnavailable": {"anyOf": [_INT, _STRING]}, | |
| }) | |
| _PORT_DEF = _obj({ | |
| "name": _STRING, | |
| "containerPort": _INT, | |
| "protocol": _STRING, | |
| }, additional=True) | |
| _SERVICE_PORT = _obj({ | |
| "name": _STRING, | |
| "port": _INT, | |
| "targetPort": {"anyOf": [_INT, _STRING]}, | |
| "protocol": _STRING, | |
| }, additional=True) | |
| _SERVICE_MONITOR = _obj({ | |
| "enabled": _BOOL, | |
| "annotations": _OBJECT_FREE, | |
| "labels": _OBJECT_FREE, | |
| "interval": _STRING, | |
| "scrapeTimeout": _STRING, | |
| "path": _STRING, | |
| "scheme": _STRING, | |
| "honorLabels": _BOOL, | |
| "relabelings": _arr(_ANY), | |
| "metricRelabelings": _arr(_ANY), | |
| "namespaceSelector": _OBJECT_FREE, | |
| }) | |
| _SERVICE = _obj({ | |
| "enabled": _BOOL, | |
| "annotations": _OBJECT_FREE, | |
| "extraLabels": _OBJECT_FREE, | |
| "type": _STRING, | |
| "ports": _arr(_SERVICE_PORT), | |
| }) | |
| _METRICS_SERVICE = _obj({ | |
| "enabled": _BOOL, | |
| "annotations": _OBJECT_FREE, | |
| "extraLabels": _OBJECT_FREE, | |
| "type": _STRING, | |
| "port": _INT, | |
| "targetPort": {"anyOf": [_INT, _STRING]}, | |
| "serviceMonitor": _SERVICE_MONITOR, | |
| }) | |
| _INGRESS_HOST_PATH = _obj({ | |
| "path": _STRING, | |
| "pathType": _STRING, | |
| "service": _obj({ | |
| "name": _STRING, | |
| "port": {"anyOf": [_INT, _STRING]}, | |
| }, additional=True), | |
| }, additional=True) | |
| _INGRESS_HOST = _obj({ | |
| "host": _STRING, | |
| "paths": _arr(_INGRESS_HOST_PATH), | |
| }, additional=True) | |
| _INGRESS_TLS = _obj({ | |
| "secretName": _STRING, | |
| "hosts": _arr(_STRING), | |
| }, additional=True) | |
| _INGRESS = _obj({ | |
| "enabled": _BOOL, | |
| "className": _STRING, | |
| "hosts": _arr(_INGRESS_HOST), | |
| "tls": _arr(_INGRESS_TLS), | |
| }, additional=True) | |
| # ── Gateway API HTTPRoute ──────────────────────────────────────────────────── | |
| # parentRefs and rules are passed through to the HTTPRoute spec verbatim | |
| # (templates/http-route.yaml uses `toYaml`), so their items stay free-form to | |
| # accept the full Gateway API shape without re-modelling it here. | |
| _HTTP_ROUTE_PARENT_REF = _obj({ | |
| "group": _STRING, | |
| "kind": _STRING, | |
| "name": _STRING, | |
| "namespace": _STRING, | |
| "sectionName": _STRING, | |
| "port": _INT, | |
| }, additional=True) | |
| _HTTP_ROUTE = _obj({ | |
| "enabled": _BOOL, | |
| "annotations": _OBJECT_FREE, | |
| "parentRefs": _arr(_HTTP_ROUTE_PARENT_REF), | |
| "hostnames": _arr(_STRING), | |
| "rules": _arr(_ANY), | |
| }, additional=True) | |
| _VOLUME_MOUNT = _obj({ | |
| "name": _STRING, | |
| "mountPath": _STRING, | |
| "subPath": _STRING, | |
| "readOnly": _BOOL, | |
| }, additional=True) | |
| _VOLUME = _obj({ | |
| "name": _STRING, | |
| }, additional=True) | |
| _PROBE_DATA = _obj({}, additional=True) | |
| _PROBE_ENTRY = _obj({ | |
| "enabled": _BOOL, | |
| "data": _PROBE_DATA, | |
| }) | |
| _PROBES = _obj({ | |
| "lifecycle": _obj({"enabled": _BOOL}, additional=True), | |
| "livenessProbe": _PROBE_ENTRY, | |
| "readinessProbe": _PROBE_ENTRY, | |
| "startupProbe": _PROBE_ENTRY, | |
| }) | |
| _SECURITY_CONTEXT = _obj({ | |
| "allowPrivilegeEscalation": _BOOL, | |
| "capabilities": _obj({ | |
| "drop": _arr(_STRING), | |
| "add": _arr(_STRING), | |
| }), | |
| "runAsUser": _INT, | |
| "runAsNonRoot": _BOOL, | |
| "readOnlyRootFilesystem": _BOOL, | |
| "seccompProfile": _obj({"type": _STRING}, additional=True), | |
| }) | |
| _RBAC_RULE = _obj({ | |
| "apiGroups": _arr(_STRING), | |
| "resources": _arr(_STRING), | |
| "verbs": _arr(_STRING), | |
| }, additional=True) | |
| _RBAC = _obj({ | |
| "enabled": _BOOL, | |
| "permissions": _obj({ | |
| "namespaced": _arr(_RBAC_RULE), | |
| "cluster": _arr(_RBAC_RULE), | |
| }, additional=True), | |
| }) | |
| _SERVICE_ACCOUNT = _obj({ | |
| "enabled": _BOOL, | |
| "name": _STRING, | |
| "annotations": _OBJECT_FREE, | |
| "labels": _OBJECT_FREE, | |
| "automountServiceAccountToken": _BOOL, | |
| "rbac": _RBAC, | |
| }) | |
| _ENV_VAR = _obj({ | |
| "name": _STRING, | |
| "value": _STRING, | |
| "valueFrom": _obj({}, additional=True), | |
| }, additional=True) | |
| _MIGRATION = _obj({ | |
| "enabled": _BOOL, | |
| "scaleDown": _BOOL, | |
| "backoffLimit": _INT, | |
| "command": _arr(_STRING), | |
| "args": _arr(_STRING), | |
| "resources": _OBJECT_FREE, | |
| # Annotations for the migration Job pod only. Distinct from the app-level | |
| # podAnnotations, which are shared with the Deployment: a service-mesh | |
| # sidecar that is desirable on a long-running Deployment keeps a hook Job | |
| # from ever completing, so the opt-out must target the Job alone. | |
| "podAnnotations": _OBJECT_FREE, | |
| }) | |
| _COMMAND = _obj({ | |
| "enabled": _BOOL, | |
| "data": _arr(_STRING), | |
| }) | |
| _CONFIGURATION_TYPE = _obj({ | |
| "backend": _BOOL, | |
| "frontend": _BOOL, | |
| }) | |
| _KUBECONFIG = _obj({ | |
| "enabled": _BOOL, | |
| }) | |
| def _base_app_props(extra: dict | None = None) -> dict: | |
| """Properties common to every application entry.""" | |
| props = { | |
| "enabled": _BOOL, | |
| "applicationName": _STRING, | |
| "configurationFileName": _STRING, | |
| "deployment": _obj({"enabled": _BOOL}), | |
| "replicaCount": _INT, | |
| "statefulset": _obj({"enabled": _BOOL}), | |
| "job": _obj({"enabled": _BOOL}, additional=True), | |
| "migration": _MIGRATION, | |
| "rollingUpdate": _ROLLING_UPDATE, | |
| "image": _IMAGE, | |
| "command": _COMMAND, | |
| "env": _arr(_ENV_VAR), | |
| "extraEnvVars": _arr(_ENV_VAR), | |
| "ports": _arr(_PORT_DEF), | |
| "service": _SERVICE, | |
| "metricsService": _METRICS_SERVICE, | |
| "ingress": _INGRESS, | |
| "httpRoute": _HTTP_ROUTE, | |
| "volumeMounts": _arr(_VOLUME_MOUNT), | |
| "volumes": _arr(_VOLUME), | |
| # Pod- and container-level keys below are read by templates/deployment.yaml, | |
| # job.yaml and hook-migration.yaml, but were absent from the schema, so | |
| # values files legitimately using them were rejected by `helm template`. | |
| "extraVolumeMounts": _arr(_VOLUME_MOUNT), | |
| "extraVolumes": _arr(_VOLUME), | |
| "resources": _obj({"limits": _OBJECT_FREE, "requests": _OBJECT_FREE}), | |
| "imagePullSecrets": _arr(_obj({"name": _STRING}, additional=True)), | |
| "podAnnotations": _OBJECT_FREE, | |
| "podSecurityContext": _OBJECT_FREE, | |
| "envFrom": _arr(_obj({}, additional=True)), | |
| "nodeSelector": _OBJECT_FREE, | |
| "tolerations": _arr(_obj({}, additional=True)), | |
| "probes": _PROBES, | |
| "securityContext": _SECURITY_CONTEXT, | |
| "serviceAccount": _SERVICE_ACCOUNT, | |
| "kubeconfig": _KUBECONFIG, | |
| "configuration_type": _CONFIGURATION_TYPE, | |
| } | |
| if extra: | |
| props.update(extra) | |
| return props | |
| # --------------------------------------------------------------------------- | |
| # Application-specific schemas | |
| # --------------------------------------------------------------------------- | |
| def _fred_agents_app(fa_config: dict) -> dict: | |
| # models_catalog: free-form (large catalog structure, fully user-defined) | |
| # mcp_catalog: free-form | |
| # dotenv: key/value env secrets | |
| return _obj(_base_app_props({ | |
| "configuration": fa_config, | |
| "dotenv": _obj({}, additional=True), | |
| "models_catalog": _obj({}, additional=True), | |
| "mcp_catalog": _obj({}, additional=True), | |
| })) | |
| def _frontend_app() -> dict: | |
| _frontend_config = _obj({ | |
| "config_json": _obj({ | |
| "frontend_basename": _STRING, | |
| }, additional=True), | |
| }, additional=True) | |
| return _obj(_base_app_props({ | |
| "configuration": _frontend_config, | |
| })) | |
| def _kf_app(kf_config: dict) -> dict: | |
| return _obj(_base_app_props({ | |
| "configuration": kf_config, | |
| "dotenv": _obj({}, additional=True), | |
| })) | |
| def _kf_worker_app(kf_config: dict) -> dict: | |
| return _obj(_base_app_props({ | |
| "configuration": kf_config, | |
| "dotenv": _obj({}, additional=True), | |
| })) | |
| def _cp_app(cp_config: dict) -> dict: | |
| _policy_entry = _obj({ | |
| "mode": _STRING, | |
| "retention": _STRING, | |
| "cancel_on_rejoin": _BOOL, | |
| }, additional=True) | |
| _policy_rule = _obj({ | |
| "rule_id": _STRING, | |
| "match": _obj({ | |
| "team_id": _STRING, | |
| "trigger": _STRING, | |
| }, additional=True), | |
| "action": _obj({ | |
| "retention": _STRING, | |
| }, additional=True), | |
| }, additional=True) | |
| _purge_policy = _obj({ | |
| "default": _policy_entry, | |
| "rules": _arr(_policy_rule), | |
| }) | |
| _conversation_policy_catalog = _obj({ | |
| "version": _STRING, | |
| "conversation_policies": _obj({ | |
| "purge": _purge_policy, | |
| }, additional=True), | |
| }) | |
| return _obj(_base_app_props({ | |
| "configuration": cp_config, | |
| "conversation_policy_catalog": _conversation_policy_catalog, | |
| "dotenv": _obj({}, additional=True), | |
| })) | |
| def _cp_worker_app(cp_config: dict) -> dict: | |
| return _obj(_base_app_props({ | |
| "configuration": cp_config, | |
| "dotenv": _obj({}, additional=True), | |
| })) | |
| # --------------------------------------------------------------------------- | |
| # Global schema | |
| # --------------------------------------------------------------------------- | |
| _GLOBAL = _obj({ | |
| "kubeconfig": _STRING, | |
| "persistence": _obj({ | |
| "enabled": _BOOL, | |
| "dynamic": _BOOL, | |
| "accessModes": _STRING, | |
| "reclaimPolicy": _STRING, | |
| "storageClass": _STRING, | |
| "hostPath": _STRING, | |
| }), | |
| "whitelist": _obj({ | |
| "enabled": _BOOL, | |
| "configMapName": _STRING, | |
| "filePath": _STRING, | |
| "user_list_inline": _STRING, | |
| "mountPath": _STRING, | |
| "subPath": _STRING, | |
| }), | |
| }) | |
| # --------------------------------------------------------------------------- | |
| # Assembly | |
| # --------------------------------------------------------------------------- | |
| def build(fa_schema_path: Path, kf_schema_path: Path, cp_schema_path: Path) -> dict: | |
| fa_config = _inline_refs(_load(fa_schema_path)) | |
| kf_config = _inline_refs(_load(kf_schema_path)) | |
| cp_config = _inline_refs(_load(cp_schema_path)) | |
| return { | |
| "$schema": "https://json-schema.org/draft-07/schema#", | |
| "title": "Fred Helm Chart Values", | |
| "type": "object", | |
| "additionalProperties": False, | |
| # x-* keys are YAML anchors used for DRY value reuse. Helm resolves | |
| # them before template rendering but they remain visible to schema | |
| # validators, so we allow any x-* key with any value at the root. | |
| "patternProperties": {"^x-": {}}, | |
| "properties": { | |
| "global": _GLOBAL, | |
| "applications": _obj({ | |
| "fred-agents": _fred_agents_app(fa_config), | |
| "frontend": _frontend_app(), | |
| "knowledge-flow-backend": _kf_app(kf_config), | |
| "knowledge-flow-worker": _kf_worker_app(kf_config), | |
| "control-plane-backend": _cp_app(cp_config), | |
| "control-plane-worker": _cp_worker_app(cp_config), | |
| }), | |
| }, | |
| } | |
| # --------------------------------------------------------------------------- | |
| # CLI | |
| # --------------------------------------------------------------------------- | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--fred-agents", required=True, type=Path, metavar="SCHEMA") | |
| parser.add_argument("--knowledge-flow", required=True, type=Path, metavar="SCHEMA") | |
| parser.add_argument("--control-plane", required=True, type=Path, metavar="SCHEMA") | |
| parser.add_argument("--output", required=True, type=Path, metavar="OUTPUT") | |
| args = parser.parse_args() | |
| for p in (args.fred_agents, args.knowledge_flow, args.control_plane): | |
| if not p.exists(): | |
| print(f"ERROR: schema not found: {p}", file=sys.stderr) | |
| sys.exit(1) | |
| schema = build(args.fred_agents, args.knowledge_flow, args.control_plane) | |
| args.output.parent.mkdir(parents=True, exist_ok=True) | |
| with open(args.output, "w") as f: | |
| json.dump(schema, f, indent=2) | |
| f.write("\n") | |
| print(f"Chart values schema written to {args.output}") | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 15.6 kB
- Xet hash:
- cf21b0d520ee29225ee6f795340ccf90909cd7cc5266dc991001864311d6aeeb
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.