Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(hcc): close review gaps on the orphan-sweep cap (#478)
Alfredo Lopezb7218bd41d0e
diff --git a/host-context-controller/README.md b/host-context-controller/README.mdindex edc083460..063b6dceb 100644--- a/host-context-controller/README.md+++ b/host-context-controller/README.md@@ -439,16 +439,16 @@ Read by `src/gfsConfig.ts`. Invalid values **throw at startup** rather than sile### NetworkPolicy-| Variable | Default | Description |-| ------------------------------------------------- | ----------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |-| `CONTEXT_MAPPER_RUNTIME_NAMESPACES` | `mcp-server,mcp-host,sandbox-recipes,rpc-proxy` | Namespaces that get L0 deny-all + L1 infrastructure policies. Manifest overrides to `mcp-server,sandbox-recipes,rpc-proxy,sandbox-ui` — note it **drops `mcp-host`**, whose policies are static (`deploy/base/mcp-host/networkpolicies.yaml`), because an HCC deny-all would block mcp-host from reaching LLM APIs. |-| `CONTEXT_MAPPER_MINIMAL_INFRA_NAMESPACES` | `` (empty) | Subset of runtime namespaces that get **only** deny-all + DNS egress — no HCC-API or K8s-API egress. Manifest sets `sandbox-ui`. |-| `CONTEXT_MAPPER_K8S_API_CIDRS` | _(unset → `[]`)_ | K8s API-server CIDRs for `allow-k8s-api-egress-*`. Empty falls back to `KUBERNETES_SERVICE_HOST`. **Validated fail-closed at module load**: a malformed entry, or one broader than `/24` (IPv4) or `/120` (IPv6), **crashes startup** rather than programming a permissive policy. |-| `CONTEXT_MAPPER_NODELOCAL_DNS_CIDR` | `` (empty) | DNS infrastructure CIDR for NodeLocal DNSCache / kube-dns. Must be exactly one IPv4 `/32`; anything else throws at startup. |-| `CONTEXT_MAPPER_NETPOL_RESYNC_SEC` | `0` | Periodic NetworkPolicy coordinated convergence (seconds). Re-enters the single-flight pass with `ensureDefaults: true` so a tick covers orphan sweep (class A) and L0/L1 defaults (class D). A tick while a pass is in flight is **skipped** (`clerum_hcc_netpol_resync_ticks_skipped_total{reason="pass-in-flight"}`) and does not coalesce; event-driven trailing is unchanged. `0` disables the interval (merge default). Startup still converges once; `start()` already applied defaults as a bootstrap barrier. Default `0` logs a `console.warn` on every boot (same pattern as Host/SFS/GFS/LlmHook) — expected; it names the self-heal gap. **Enable is a separate ops decision** (issue #478, not this merge): with the skip guard the chosen value is **`1500`**; without the guard the floor is **`3600`**. `300` is not a valid enable while passes last minutes. Treadmill ⇔ pass duration ≥ period. Enable preconditions live on #478: image deployed, census read, histogram p99/max ≪ T, then infra **dev first**. |-| `CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP` | `10` | Absolute orphan-delete cap for a namespace-wide NetworkPolicy sweep. Candidates **strictly above** this count refuse **deletes** and increment `clerum_hcc_netpol_orphan_sweep_capped_total`; the pass **still certifies** (#478: alert and do not delete). Governs every `fullReconcile` (startup, event-driven, timer). Absolute is checked first, so at a fleet ≥ 50 with these defaults the percent rule never binds. `0` refuses every orphan delete; a negative value fails loud at load. Census: `scripts/ops/hcc-netpol-orphan-census.sh` — `live_cap_would_trip` is Deployment env only; `controller_cap_would_trip` applies compiled defaults when unset. When the Deployment omits `CONTEXT_MAPPER_RPC_PROXY_NAMESPACE`, the census assumes the compiled `rpc-proxy` default. |-| `CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP_PERCENT` | `20` | Percent orphan-delete cap against the listed managed fleet. Strict `>`. Inert on a tiny inventory where `percent * listed < 1`. Same every-`fullReconcile` scope as the absolute cap; only binds when the fleet is small enough that 20% is below the absolute cap of 10. |-| `KUBERNETES_SERVICE_HOST` | _(injected by Kubernetes)_ | Not set by the operator — the `/32` fallback source when `CONTEXT_MAPPER_K8S_API_CIDRS` is empty. |+| Variable | Default | Description |+| ------------------------------------------------- | ----------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |+| `CONTEXT_MAPPER_RUNTIME_NAMESPACES` | `mcp-server,mcp-host,sandbox-recipes,rpc-proxy` | Namespaces that get L0 deny-all + L1 infrastructure policies. Manifest overrides to `mcp-server,sandbox-recipes,rpc-proxy,sandbox-ui` — note it **drops `mcp-host`**, whose policies are static (`deploy/base/mcp-host/networkpolicies.yaml`), because an HCC deny-all would block mcp-host from reaching LLM APIs. |+| `CONTEXT_MAPPER_MINIMAL_INFRA_NAMESPACES` | `` (empty) | Subset of runtime namespaces that get **only** deny-all + DNS egress — no HCC-API or K8s-API egress. Manifest sets `sandbox-ui`. |+| `CONTEXT_MAPPER_K8S_API_CIDRS` | _(unset → `[]`)_ | K8s API-server CIDRs for `allow-k8s-api-egress-*`. Empty falls back to `KUBERNETES_SERVICE_HOST`. **Validated fail-closed at module load**: a malformed entry, or one broader than `/24` (IPv4) or `/120` (IPv6), **crashes startup** rather than programming a permissive policy. |+| `CONTEXT_MAPPER_NODELOCAL_DNS_CIDR` | `` (empty) | DNS infrastructure CIDR for NodeLocal DNSCache / kube-dns. Must be exactly one IPv4 `/32`; anything else throws at startup. |+| `CONTEXT_MAPPER_NETPOL_RESYNC_SEC` | `0` | Periodic NetworkPolicy coordinated convergence (seconds). Re-enters the single-flight pass with `ensureDefaults: true` so a tick covers orphan sweep (class A) and L0/L1 defaults (class D). A tick while a pass is in flight is **skipped** (`clerum_hcc_netpol_resync_ticks_skipped_total{reason="pass-in-flight"}`) and does not coalesce; event-driven trailing is unchanged. `0` disables the interval (merge default). Startup still converges once; `start()` already applied defaults as a bootstrap barrier. Default `0` logs a `console.warn` on every boot (same pattern as Host/SFS/GFS/LlmHook) — expected; it names the self-heal gap. **Enable is a separate ops decision** (issue #478, not this merge): with the skip guard the chosen value is **`1500`**; without the guard the floor is **`3600`**. `300` is not a valid enable while passes last minutes. Treadmill ⇔ pass duration ≥ period. Enable preconditions live on #478: image deployed, census read, histogram p99/max ≪ T, then infra **dev first**. |+| `CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP` | `10` | Absolute orphan-delete cap for a namespace-wide NetworkPolicy sweep. Candidates **strictly above** this count refuse **deletes** and increment `clerum_hcc_netpol_orphan_sweep_capped_total`; the pass **still certifies** (#478: alert and do not delete). Governs every `fullReconcile` (startup, event-driven, timer). Absolute is checked first, so at a fleet ≥ 50 with these defaults the percent rule never binds. One cap over the **sum of all lanes** (not per-lane): a trip leaves those orphans in place until an operator raises the env; the signal is that counter. `0` refuses every orphan delete; a negative or non-canonical value (`2e1`, `10.9`) fails loud at load. Census: `scripts/ops/hcc-netpol-orphan-census.sh` — `live_cap_would_trip` is Deployment env only; `controller_cap_would_trip` applies compiled defaults when unset. Census listed/orphan sets include the controller `repairable` class (managed-by + reserved name, policy-type absent). When the Deployment omits `CONTEXT_MAPPER_RPC_PROXY_NAMESPACE`, the census assumes the compiled `rpc-proxy` default. |+| `CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP_PERCENT` | `20` | Percent orphan-delete cap against the listed managed fleet. Strict `>`. Inert on a tiny inventory where `percent * listed < 1`. Values above 100 never fire. Same every-`fullReconcile` scope as the absolute cap; only binds when the fleet is small enough that 20% is below the absolute cap of 10. Non-canonical values fail loud at load. |+| `KUBERNETES_SERVICE_HOST` | _(injected by Kubernetes)_ | Not set by the operator — the `/32` fallback source when `CONTEXT_MAPPER_K8S_API_CIDRS` is empty. |### Plugin image allowlistdiff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.jsonindex 97d84521c..85f6b0842 100644--- a/host-context-controller/package-lock.json+++ b/host-context-controller/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-host-context-controller",- "version": "0.1.253",+ "version": "0.1.254","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-host-context-controller",- "version": "0.1.253",+ "version": "0.1.254","license": "SEE LICENSE IN LICENSE","dependencies": {"@clerum/image-policy": "file:../packages/image-policy",diff --git a/host-context-controller/package.json b/host-context-controller/package.jsonindex 80a149d81..bb14ab1fd 100644--- a/host-context-controller/package.json+++ b/host-context-controller/package.json@@ -1,6 +1,6 @@{"name": "clerum-host-context-controller",- "version": "0.1.253",+ "version": "0.1.254","description": "Host Context Controller - provides McpServer CRDs to mcp-host via REST API","main": "dist/main.js","engines": {diff --git a/host-context-controller/src/config.netPolResync.test.ts b/host-context-controller/src/config.netPolResync.test.tsindex adac25644..f6586f9d9 100644--- a/host-context-controller/src/config.netPolResync.test.ts+++ b/host-context-controller/src/config.netPolResync.test.ts@@ -61,4 +61,22 @@ describe('NetworkPolicy resync and orphan-sweep config (#478)', () => {loadConfig({ CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP_PERCENT: '-1' })).rejects.toThrow(/CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP_PERCENT/)})++ it.each(['2e1', '10.9', '12abc', '0x1F'] as const)(+ 'fails loud when the absolute orphan-delete cap is non-canonical (%s)',+ async raw => {+ await expect(loadConfig({ CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP: raw })).rejects.toThrow(+ /CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP/+ )+ }+ )++ it.each(['2e1', '10.9', '12abc'] as const)(+ 'fails loud when the percent orphan-delete cap is non-canonical (%s)',+ async raw => {+ await expect(+ loadConfig({ CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP_PERCENT: raw })+ ).rejects.toThrow(/CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP_PERCENT/)+ }+ )})diff --git a/host-context-controller/src/config.ts b/host-context-controller/src/config.tsindex 4b756f2ac..c720a4857 100644--- a/host-context-controller/src/config.ts+++ b/host-context-controller/src/config.ts@@ -184,9 +184,9 @@ export interface Config {// CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP.netPolOrphanDeleteCap: number- // Percent orphan-delete cap (0-100) against the listed managed fleet.- // Candidates strictly above this fraction are refused. Inert on a tiny- // inventory where percent*fleet < 1. Set via+ // Percent orphan-delete cap against the listed managed fleet. Candidates+ // strictly above this fraction are refused. Inert on a tiny inventory+ // where percent*fleet < 1. Values above 100 never fire. Set via// CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP_PERCENT.netPolOrphanDeleteCapPercent: number@@ -291,6 +291,20 @@ function requireNonNegativeEnvInt(key: string, value: number): number {return value}+/**+ * Unsigned base-10 integer, including 0. Rejects the parseInt coercions+ * (`2e1`→2, `10.9`→10, `12abc`→12) that getEnvInt would swallow.+ */+function requireCanonicalNonNegativeEnvInt(key: string, defaultValue: number): number {+ const raw = process.env[key]+ if (raw === undefined || raw === '') return defaultValue+ if (!/^\d+$/.test(raw)) {+ throw new Error(`${key} must be an unsigned base-10 integer, got '${raw}'`)+ }+ const parsed = Number(raw)+ return requireNonNegativeEnvInt(key, parsed)+}+export function parseExternalEgressDnsTimeoutMs(raw: string | undefined): number {if (raw === undefined || raw.trim() === '') return 5_000const parsed = Number(raw)@@ -773,13 +787,13 @@ export const config: Config = {// Mass-delete guard for the namespace-wide orphan sweep. Thresholds are// strict `>`: exactly N candidates still delete; N+1 refuses deletes and// still certifies.- netPolOrphanDeleteCap: requireNonNegativeEnvInt(+ netPolOrphanDeleteCap: requireCanonicalNonNegativeEnvInt('CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP',- getEnvInt('CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP', 10)+ 10),- netPolOrphanDeleteCapPercent: requireNonNegativeEnvInt(+ netPolOrphanDeleteCapPercent: requireCanonicalNonNegativeEnvInt('CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP_PERCENT',- getEnvInt('CONTEXT_MAPPER_NETPOL_ORPHAN_DELETE_CAP_PERCENT', 20)+ 20),// Every finite Kubernetes request that can hold Host convergence gets itsdiff --git a/host-context-controller/src/k8sClient.test.ts b/host-context-controller/src/k8sClient.test.tsindex d3c6cab7f..8a4681e9d 100644--- a/host-context-controller/src/k8sClient.test.ts+++ b/host-context-controller/src/k8sClient.test.ts@@ -4893,8 +4893,9 @@ describe('McpServerWatcher startup', () => {{ lane: 'NetworkPolicy', result: 'deferred-unsynced' })- await (watcher as any).runInitialNetworkPolicyConvergence()+ await (watcher as any).runInitialNetworkPolicyConvergence({ ensureDefaults: true })+ expect((watcher as any).netPolConvergenceEnsureDefaults).toBe(false)expect(mocks.netPolFullReconcile).not.toHaveBeenCalled()expect(warnSpy.mock.calls.some(call => String(call[0]).includes('caches unsynced'))).toBe(true)expect((watcher as any).initialConvergenceRetryAttempts.get('NetworkPolicy')).toBe(3)@@ -11437,6 +11438,8 @@ describe('McpServerWatcher NetworkPolicy periodic resync (#478)', () => {expect(mocks.netPolFullReconcile).not.toHaveBeenCalled()expect(warnSpy.mock.calls.some(call => String(call[0]).includes('caches unsynced'))).toBe(true)expect((watcher as any).initialConvergenceRetryTimers.has('NetworkPolicy')).toBe(true)+ // Tick requested defaults; the unsynced path never reached fullReconcile.+ expect((watcher as any).netPolConvergenceEnsureDefaults).toBe(false)warnSpy.mockRestore()await watcher.stop()})diff --git a/host-context-controller/src/k8sClient.ts b/host-context-controller/src/k8sClient.tsindex 44c395d42..8d2c2256d 100644--- a/host-context-controller/src/k8sClient.ts+++ b/host-context-controller/src/k8sClient.ts@@ -3115,6 +3115,9 @@ export class McpServerWatcher implements McpServerProvider {initialConvergenceSwallowedTotal.inc({ lane: 'NetworkPolicy', sink: 'unsynced' })observeInitialNetworkPolicyPass(startedAtMs, 'deferred-unsynced')this.scheduleInitialConvergenceRetry('NetworkPolicy')+ // A timer tick may have requested defaults. This path never reached+ // fullReconcile, so drop the flag: event/retry entry stays false.+ this.netPolConvergenceEnsureDefaults = falsereturn}let authoritativeRevocationCompleted = falsediff --git a/host-context-controller/src/networkPolicyReconciler.orphanSweepCap.test.ts b/host-context-controller/src/networkPolicyReconciler.orphanSweepCap.test.tsnew file mode 100644index 000000000..8b79827f3--- /dev/null+++ b/host-context-controller/src/networkPolicyReconciler.orphanSweepCap.test.ts@@ -0,0 +1,73 @@+import { describe, expect, it } from 'vitest'+import { evaluateNetPolOrphanSweepCap } from './networkPolicyReconciler'++describe('evaluateNetPolOrphanSweepCap', () => {+ it.each([+ {+ name: 'exact absolute cap still deletes',+ orphan: 10,+ listed: 100,+ abs: 10,+ pct: 20,+ want: null,+ },+ {+ name: 'absolute wins when both rules would fire',+ orphan: 11,+ listed: 40,+ abs: 10,+ pct: 20,+ want: 'absolute' as const,+ },+ {+ name: 'percent fires when under the absolute cap',+ orphan: 5,+ listed: 20,+ abs: 10,+ pct: 20,+ want: 'percent' as const,+ },+ {+ name: 'percent is inert when percent*listed < 1',+ orphan: 1,+ listed: 2,+ abs: 10,+ pct: 20,+ want: null,+ },+ {+ name: 'absolute 0 refuses a single orphan',+ orphan: 1,+ listed: 100,+ abs: 0,+ pct: 20,+ want: 'absolute' as const,+ },+ {+ name: 'absolute 0 with zero orphans does not trip',+ orphan: 0,+ listed: 100,+ abs: 0,+ pct: 20,+ want: null,+ },+ {+ name: 'negative absolute trips even at zero orphans',+ orphan: 0,+ listed: 10,+ abs: -1,+ pct: 20,+ want: 'absolute' as const,+ },+ {+ name: 'percent above 100 never fires',+ orphan: 11,+ listed: 10,+ abs: 100,+ pct: 150,+ want: null,+ },+ ])('$name', ({ orphan, listed, abs, pct, want }) => {+ expect(evaluateNetPolOrphanSweepCap(orphan, listed, abs, pct)).toBe(want)+ })+})diff --git a/host-context-controller/src/networkPolicyReconciler.ts b/host-context-controller/src/networkPolicyReconciler.tsindex 624f54570..140e92bff 100644--- a/host-context-controller/src/networkPolicyReconciler.ts+++ b/host-context-controller/src/networkPolicyReconciler.ts@@ -93,6 +93,9 @@ export type NetPolOrphanSweepCapReason = 'absolute' | 'percent'* timer), not only a periodic tick. A trip refuses the orphan deletes and* increments the cap metric; the pass still certifies the rest of the* inventory (#478: alert and do not delete — not "and do not certify").+ * One cap over the sum of all lanes (not per-lane): a trip leaves those+ * orphans in place until an operator raises the env. The operator signal+ * is `clerum_hcc_netpol_orphan_sweep_capped_total`.*/export function evaluateNetPolOrphanSweepCap(orphanCount: number,@@ -1462,19 +1465,21 @@ export class NetworkPolicyReconciler {const serverName = policy.metadata?.labels?.[MCPSERVER_LABEL]return !serverName || !desiredServerNames.has(serverName)}+ const countExternalLane =+ options.serverInventoryComplete !== false && serverCleanupAuthoritative()const orphanCandidates = [...allContextPolicies.filter(isContextLaneOrphan),...allContextEgressPolicies.filter(isContextLaneOrphan),...allRpcProxyEgressPolicies.filter(isContextLaneOrphan),- ...(options.serverInventoryComplete !== false && serverCleanupAuthoritative()- ? allExternalPolicies.filter(isExternalLaneOrphan)- : []),+ ...(countExternalLane ? allExternalPolicies.filter(isExternalLaneOrphan) : []),]+ // Count the external fleet with the same gate as orphanCandidates so a+ // mid-pass authority flip cannot inflate the percent denominator.const listedManagedFleet =allContextPolicies.length +allContextEgressPolicies.length +allRpcProxyEgressPolicies.length +- allExternalPolicies.length+ (countExternalLane ? allExternalPolicies.length : 0)const capReason = evaluateNetPolOrphanSweepCap(orphanCandidates.length, listedManagedFleet)if (capReason) {netPolOrphanSweepCappedTotal.inc({ reason: capReason })diff --git a/scripts/ops/hcc-netpol-orphan-census.sh b/scripts/ops/hcc-netpol-orphan-census.shindex 5daa5d9c8..572183556 100755--- a/scripts/ops/hcc-netpol-orphan-census.sh+++ b/scripts/ops/hcc-netpol-orphan-census.sh@@ -3,7 +3,12 @@## get/list only — never create, patch, replace, or delete.# Classifies by clerum.io/policy-type in {context-allow, rpc-proxy-egress,-# external-egress}. Do not treat managed-by alone as membership.+# external-egress}, plus the controller's `repairable` class: managed-by+# present, policy-type absent, reserved name (ctx-/rpc-egress-/ext-egress-).+# That last set is what a policy-type-strip corruption looks like; omitting+# it can print VERDICT=CLEAN while the controller counts those objects and+# may trip the cap. This is not the full four-lane classifier (reserved+# names without owner labels can over-count vs the controller).## CONTEXT_MAPPER_* knobs are read from the live host-context-controller# Deployment. Code defaults are never printed as cluster facts: an absent@@ -22,8 +27,9 @@# two-tier shape as live_cap vs controller_cap. Sampling then uses that# compiled default. Do not declare the key in deploy/ to unblock the job.#-# This census reflects on-cluster orphans by policy-type, not the-# controller's current pass authority gate.+# This census reflects on-cluster orphans by policy-type plus the+# reserved-name repairable approximation, not the controller's current+# pass authority gate.## Double-samples 90s apart. Adjudicates only when the desired Context +# McpServer identity set is identical across samples; otherwise@@ -93,8 +99,30 @@ if [[ "${RPC_NS}" == "UNSET" ]]; thenRPC_NS="${COMPILED_RPC_NS_DEFAULT}"fi+# Typed policy-type membership, plus the reserved-name repairable class+# (managed-by present, policy-type absent). Shared by listed + orphan filters.+CENSUS_MANAGED_JQ='+ def policy_type: .metadata.labels["clerum.io/policy-type"];+ def is_typed:+ policy_type == "context-allow"+ or policy_type == "rpc-proxy-egress"+ or policy_type == "external-egress";+ def is_untyped_repairable:+ .metadata.labels["clerum.io/managed-by"] == "host-context-controller"+ and (policy_type == null)+ and (+ (.metadata.name // "" | startswith("ctx-"))+ or (.metadata.name // "" | startswith("rpc-egress-"))+ or (.metadata.name // "" | startswith("ext-egress-"))+ );+ def is_census_managed: is_typed or is_untyped_repairable;+ def census_lane:+ if policy_type == "external-egress"+ or (policy_type == null and (.metadata.name // "" | startswith("ext-egress-")))+ then "external" else "context" end;+'+# One read-only snapshot of desired identities + orphan candidates.-# Filters on policy-type, not managed-by.sample_state() {local contexts_json servers_json np_jsoncontexts_json="$(kc get contexts.clerum.io -n "${MAPPER_NS}" -o json)"@@ -107,27 +135,27 @@ sample_state() {} | jq -s "{items: [.[].items[]?]}")"- local desired_contexts desired_servers listed orphans orphan_count+ local desired_contexts desired_servers listed untyped orphans orphan_countdesired_contexts="$(printf "%s" "${contexts_json}" | jq -r '[.items[]?.spec.contextId // empty] | unique | sort | join(",")')"desired_servers="$(printf "%s" "${servers_json}" | jq -r '[.items[]?.metadata.name // empty] | unique | sort | join(",")')"- listed="$(printf "%s" "${np_json}" | jq -r '[.items[] | select((.metadata.labels["clerum.io/policy-type"] == "context-allow") or (.metadata.labels["clerum.io/policy-type"] == "rpc-proxy-egress") or (.metadata.labels["clerum.io/policy-type"] == "external-egress"))] | length')"+ listed="$(printf "%s" "${np_json}" | jq -r "${CENSUS_MANAGED_JQ} [.items[] | select(is_census_managed)] | length")"+ untyped="$(printf "%s" "${np_json}" | jq -r "${CENSUS_MANAGED_JQ} [.items[] | select(is_untyped_repairable)] | length")"- orphans="$(printf "%s" "${np_json}" | jq -r --arg ctxs "${desired_contexts}" --arg srvs "${desired_servers}" '+ orphans="$(printf "%s" "${np_json}" | jq -r --arg ctxs "${desired_contexts}" --arg srvs "${desired_servers}" "${CENSUS_MANAGED_JQ}"'($ctxs | split(",") | map(select(length>0))) as $desiredCtx| ($srvs | split(",") | map(select(length>0))) as $desiredSrv| .items[]- | .metadata.labels["clerum.io/policy-type"] as $t- | select($t == "context-allow" or $t == "rpc-proxy-egress" or $t == "external-egress")+ | select(is_census_managed)| . as $p| (- if $t == "external-egress" then+ if census_lane == "external" then($p.metadata.labels["clerum.io/mcpserver"] as $s | ($s == null) or (($desiredSrv | index($s)) == null))else($p.metadata.labels["clerum.io/context"] as $c | ($c == null) or (($desiredCtx | index($c)) == null))end)| select(.)- | [$p.metadata.namespace, $p.metadata.name, $t]+ | [$p.metadata.namespace, $p.metadata.name, ($p.metadata.labels["clerum.io/policy-type"] // "repairable-untyped")]| @tsv')"@@ -140,6 +168,7 @@ sample_state() {printf "DESIRED_CONTEXTS=%s\n" "${desired_contexts}"printf "DESIRED_SERVERS=%s\n" "${desired_servers}"printf "LISTED_MANAGED=%s\n" "${listed}"+ printf "REPAIRABLE_UNTYPED=%s\n" "${untyped}"printf "ORPHAN_COUNT=%s\n" "${orphan_count}"printf "ORPHANS<<EOF\n"printf "%s\n" "${orphans}"@@ -179,8 +208,9 @@ fiORPHAN_COUNT="$(parse_field "${SAMPLE2}" ORPHAN_COUNT)"LISTED="$(parse_field "${SAMPLE2}" LISTED_MANAGED)"+REPAIRABLE_UNTYPED="$(parse_field "${SAMPLE2}" REPAIRABLE_UNTYPED)"echo "[census] desired set identical across samples"-echo "[census] listed_managed=${LISTED} orphan_count=${ORPHAN_COUNT}"+echo "[census] listed_managed=${LISTED} repairable_untyped=${REPAIRABLE_UNTYPED} orphan_count=${ORPHAN_COUNT}"echo "[census] orphans (namespace name policy-type):"printf "%s" "${SAMPLE2}" | sed -n "/^ORPHANS<<EOF/,/^EOF/{ /^ORPHANS<<EOF/d; /^EOF/d; p; }"@@ -189,8 +219,16 @@ printf "%s" "${SAMPLE2}" | sed -n "/^ORPHANS<<EOF/,/^EOF/{ /^ORPHANS<<EOF/d; /^ECOMPILED_ABS_DEFAULT=10COMPILED_PCT_DEFAULT=20+is_uint() {+ [[ "$1" =~ ^[0-9]+$ ]]+}+cap_would_trip() {local orphan_count="$1" listed="$2" abs="$3" pct="$4"+ if ! is_uint "${orphan_count}" || ! is_uint "${listed}" || ! is_uint "${abs}" || ! is_uint "${pct}"; then+ printf "malformed"+ return+ fiif [[ "${orphan_count}" -gt "${abs}" ]]; thenprintf "absolute"elif [[ "${listed}" -gt 0 && $((listed * pct)) -ge 100 && $((orphan_count * 100)) -gt $((listed * pct)) ]]; then@@ -201,12 +239,21 @@ cap_would_trip() {}CAP_REASON="none"-if [[ "${ORPHAN_CAP}" != "UNSET" && "${ORPHAN_COUNT}" -gt "${ORPHAN_CAP}" ]]; then- CAP_REASON="absolute"-elif [[ "${ORPHAN_CAP_PCT}" != "UNSET" && "${LISTED}" -gt 0 ]]; then- # Integer compare of orphan*100 > listed*percent, inert when percent*listed < 100.- if [[ $((LISTED * ORPHAN_CAP_PCT)) -ge 100 && $((ORPHAN_COUNT * 100)) -gt $((LISTED * ORPHAN_CAP_PCT)) ]]; then- CAP_REASON="percent"+if [[ "${ORPHAN_CAP}" != "UNSET" ]]; then+ if ! is_uint "${ORPHAN_CAP}"; then+ CAP_REASON="malformed"+ elif [[ "${ORPHAN_COUNT}" -gt "${ORPHAN_CAP}" ]]; then+ CAP_REASON="absolute"+ fi+fi+if [[ "${CAP_REASON}" == "none" && "${ORPHAN_CAP_PCT}" != "UNSET" ]]; then+ if ! is_uint "${ORPHAN_CAP_PCT}"; then+ CAP_REASON="malformed"+ elif [[ "${LISTED}" -gt 0 ]]; then+ # Integer compare of orphan*100 > listed*percent, inert when percent*listed < 100.+ if [[ $((LISTED * ORPHAN_CAP_PCT)) -ge 100 && $((ORPHAN_COUNT * 100)) -gt $((LISTED * ORPHAN_CAP_PCT)) ]]; then+ CAP_REASON="percent"+ fififi@@ -214,9 +261,13 @@ CONTROLLER_ABS="${ORPHAN_CAP}"CONTROLLER_PCT="${ORPHAN_CAP_PCT}"if [[ "${CONTROLLER_ABS}" == "UNSET" ]]; thenCONTROLLER_ABS="${COMPILED_ABS_DEFAULT}"+elif ! is_uint "${CONTROLLER_ABS}"; then+ CONTROLLER_ABS="malformed"fiif [[ "${CONTROLLER_PCT}" == "UNSET" ]]; thenCONTROLLER_PCT="${COMPILED_PCT_DEFAULT}"+elif ! is_uint "${CONTROLLER_PCT}"; then+ CONTROLLER_PCT="malformed"fiCONTROLLER_CAP_REASON="$(cap_would_trip "${ORPHAN_COUNT}" "${LISTED}" "${CONTROLLER_ABS}" "${CONTROLLER_PCT}")"