Skip to content

fix(controller): derive the model-gateway endpoint from config, drop the aperture default - #276

Merged
dzungtr merged 1 commit into
mainfrom
fix/no-hardcoded-model-gateway
Oct 22, 2026
Merged

dzungtr merged 1 commit into
mainfrom
fix/no-hardcoded-model-gateway

Conversation

@dzungtr

@dzungtr dzungtr commented Oct 22, 2026

Copy link
Copy Markdown
Owner

Problem

KAPE_MODEL_GATEWAY_URL was consumed five ways but honoured by one. The
models.json patch used the configured URL; the sandbox egress rule
(policy.go) and the startup probe (gateway.go) used a compiled-in
model-gateway-http.aperture.svc.cluster.local, and DefaultConfig() shipped
that host as the default.

Consequence: point the controller at any other gateway and it patches
models.json at the new endpoint while the OPA proxy still denies egress to
it — fail-closed, so sandboxes silently lose model traffic — and the probe
reports on a host the sandbox was never configured to reach.

How the gateway is consumed (after)

chart values agentServer.modelGateway.url   (required, no default)
  → KapeAgentServer spec.modelGateway.url   (CRD: minLength 1, ^https?://.+)
  → operator render.go: KAPE_MODEL_GATEWAY_URL   (contract env, cannot be shadowed)
  → controller Config.ModelGatewayURL       (no compiled default; Validate names it)
  → { piWrapperCommand → models.json baseUrl,
      modelGatewayRule → egress allowlist,
      ProbeModelGateway → GET <url>/models }

Changes

  • controller/policy.go — modelGatewayRule(endpoint) + endpointHostPort():
    host and port derive from the configured URL (explicit port > https 443 >
    http 80); empty / no-host / non-http / out-of-range port returns an error
    instead of a silent fallback; ApplyModelGatewayPolicy fails closed before
    any UpdateConfig.
  • controller/gateway.go — ProbeModelGateway targets
    <configured-url>/models.
  • controller/config.go — no gateway default in DefaultConfig(); Validate()
    reports the missing variable by name.
  • charts/kape — agentServer.modelGateway.url becomes required with no
    default; render test supplies it and asserts that omitting it fails.
  • openshell/agents/sandbox-policy.yaml — the published image policy no longer
    pins one namespace's gateway (controller sandboxes get the rule at runtime).
  • Docs/fixtures — controller/README.md documents "required"; operator test
    fixtures and goldens use neutral hosts.

Verification

  • controller: go vet ./..., go test ./... → ok (54s)
  • operator: go vet ./..., go test ./domain/... ./infra/{k8s,openshell,toml}/... ./controller/ → ok
  • charts/kape/tests/render-test.sh → all assertions pass (incl. new case 0:
    render fails without the URL)
  • New tests: controller/policy_test.go (derivation matrix, rejection cases,
    fail-closed unusable URL) and TestProbeModelGateway_UsesConfiguredEndpoint

Pre-existing failures in operator/infra/qdrant and
operator/controller/reconcile reproduce on main (sandbox-blocked
127.0.0.1 dials) and are unrelated.

Breaking change

  • kape-controller refuses to start without KAPE_MODEL_GATEWAY_URL.
  • helm template/install fails when agentServer.modelGateway.url is empty.

Both values come from the KapeAgentServer CR (spec.modelGateway.url is
already required in the CRD), so an operator-managed install is unaffected.
Standalone controller runs and default chart renders must pass the value.

Not in scope (follow-ups)

  • PI_PROVIDER=openrouter and providers.openrouter in the models.json patch
    (gateway.go:365,390) ignore the configurable DefaultProvider; fixing needs
    a provider-key contract, not a string swap.
  • kape-controller.openshell.svc.cluster.local:8081 /
    SandboxNamespace: "openshell" (config.go:266).
  • Monitor baseUrl: https://openrouter.ai/api/v1 (vendor default, env-overridable).

…the aperture default

The model-gateway URL was configurable in the models.json patch path but
compiled into two other consumers, so a deployment that pointed
KAPE_MODEL_GATEWAY_URL somewhere else patched one endpoint while the OPA
policy allowed another (fail-closed: sandboxes lost model traffic) and the
startup probe reported on a host the sandbox was never configured to reach.

Consumers now share one value:

- policy.go: modelGatewayRule(endpoint) derives host/port via
  endpointHostPort (explicit port > https 443 > http 80), rejects
  empty / no-host / non-http / out-of-range-port with an error instead of a
  silent fallback, and ApplyModelGatewayPolicy fails closed before any
  UpdateConfig call.
- gateway.go: ProbeModelGateway GETs <configured-url>/models instead of a
  literal host.
- config.go: DefaultConfig no longer carries a gateway default; Validate
  names the missing variable. The KapeAgentServer CR already owns the value
  and the operator already renders it as contract env.

Remove the deployment-specific host from every other carrier too: the chart
value becomes required (no system-specific default), the published
pi-openshell sandbox policy stops pinning one namespace's gateway, and the
controller README documents "required" instead of a compiled default.
Test fixtures use neutral hosts.

Tests: controller (go vet + go test) and operator unit tests pass; the chart
render test supplies the URL and asserts that omitting it fails. New:
policy_test.go pins endpoint derivation + rejection + fail-closed, and
TestProbeModelGateway_UsesConfiguredEndpoint pins the probe target.

BREAKING CHANGE: kape-controller now refuses to start without
KAPE_MODEL_GATEWAY_URL, and `helm template/install` fails when
agentServer.modelGateway.url is empty. Both are supplied by the existing
KapeAgentServer CR (spec.modelGateway.url is required in the CRD), so an
operator-managed install is unaffected; standalone controller runs and
default chart renders must pass the value.
@dzungtr

dzungtr commented Oct 22, 2026

Copy link
Copy Markdown
Owner Author

CI note: the single failing check (Operator envtest suite) fails for two unrelated reasons:

  1. TestAgentServerReconcileCredentialsEnvtest — "controller with name kapeagentserver already exists" — controller-registration collision in the envtest suite, not exercised by this diff.
  2. ToolReconciler memory tool [It] creates a Qdrant StatefulSet… — 10s timeout.

Both reproduce on every recent branch run (e.g. fix/agentserver-patch-loop, the #263/#265 workers): the workflow has not passed on any branch since Oct 9. This PR touches no qdrant/reconciler code; controller tests, operator unit tests, and the chart render test pass locally. The envtest suite needs a separate fix.

@dzungtr
dzungtr merged commit 52fd42c into main Oct 22, 2026
0 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant