Skip to content

fix: Do not mutate a Service kgateway does not own.#14145

Merged
davidjumani merged 8 commits into
kgateway-dev:mainfrom
chandler-solo:chandler/13499fixoverwriteotherservice
Jun 9, 2026
Merged

fix: Do not mutate a Service kgateway does not own.#14145
davidjumani merged 8 commits into
kgateway-dev:mainfrom
chandler-solo:chandler/13499fixoverwriteotherservice

Conversation

@chandler-solo

@chandler-solo chandler-solo commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #13499.

kgateway now refuses to patch an existing Gateway Service unless the Service proves ownership through either:

  • a controller ownerReference for the source Gateway
  • kgateway metadata matching the rendered Gateway Service:
    • app.kubernetes.io/managed-by: kgateway
    • matching gateway.networking.k8s.io/gateway-class-name
    • matching gateway.networking.k8s.io/gateway-name or gateway.kgateway.dev/gateway-full-name

This prevents accidental takeover of unrelated Services with the same name as a Gateway.

Upgrade behavior

  • 2.2.0

    • Communicates ownership with the Gateway controller ownerReference and kgateway labels.
    • Existing Services normally continue through the Gateway ownerRef. The label check is a fallback for orphaned or manually migrated kgateway Services, not the common path.
  • 2.3.0, main

    • See 2.2.0.
    • For long Gateway names, 2.3.0 uses the safe hashed Gateway name label.

Change Type

/kind fix

Changelog

kgateway no longer overwrites an existing Kubernetes Service for a Gateway unless the Service has a matching Gateway ownerReference or kgateway ownership metadata.

Additional Notes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents kgateway from accidentally taking over and mutating an existing Kubernetes Service that happens to share the same name as a reconciled Gateway, unless ownership can be proven via a controller ownerReference or matching kgateway/Gateway metadata.

Changes:

  • Add an ownership validation gate before patching an existing Service during Gateway reconciliation.
  • Render gateway.kgateway.dev/gateway-full-name onto Gateway Service resources as an ownership/identity signal (including for long Gateway names).
  • Update deployer Helm-render golden YAML test outputs and add unit tests covering the new “refuse to patch” behavior.

Reviewed changes

Copilot reviewed 41 out of 41 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
pkg/deployer/deployer.go Refuses to patch existing Services unless ownership is proven (ownerRef or matching kgateway/Gateway metadata).
pkg/deployer/deployer_test.go Adds tests for refusing/allowing Service patches based on ownership signals.
pkg/kgateway/helm/envoy/templates/service.yaml Adds Gateway full-name ownership annotation into rendered Service annotations.
test/deployer/testdata/base-gateway-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/base-gateway-tls-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/both-gwc-and-gw-have-envoy-extra-args-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/both-gwc-and-gw-have-params-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/both-gwc-and-gw-have-params-reversed-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-all-autoscalers-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-both-gwc-and-gw-have-overlays-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-configs-and-overlays-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-dns-resolver-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-dns-resolver-zero-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-hpa-overlay-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-image-digest-erases-tag-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-infrastructure-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-log-json-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-log-text-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-overlay-ordering-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-pdb-overlay-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-readiness-listener-proxy-protocol-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-sds-and-custom-sidecar-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-strategic-merge-patch-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/envoy-vpa-overlay-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/gwc-with-replicas-null-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/gwc-with-replicas-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/gwc-with-replicas-zero-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/istio-enabled-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/istio-enabled-waypoint-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/loadbalancer-class-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/loadbalancer-source-ranges-api-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/loadbalancer-source-ranges-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/loadbalancer-static-ip-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/long-gateway-name-exactly-63-chars-out.yaml Updates expected rendered Service to include gateway-full-name annotation for boundary-length Gateway name.
test/deployer/testdata/long-gateway-name-over-63-chars-out.yaml Updates expected rendered Service to include gateway-full-name annotation for >63 char Gateway name.
test/deployer/testdata/omit-default-security-context-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/omit-default-security-context-via-gw-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/openshift-two-params-security-context-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/priority-class-name-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/stats-matcher-exclusion-out.yaml Updates expected rendered Service to include gateway-full-name annotation.
test/deployer/testdata/stats-matcher-inclusion-out.yaml Updates expected rendered Service to include gateway-full-name annotation.

Comment thread pkg/kgateway/helm/envoy/templates/service.yaml Outdated
@davidxia

davidxia commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Thanks, looks like this will close #13499 based on PR description. Would love to see this in an upcoming 2.3 release.

@davidjumani
davidjumani added this pull request to the merge queue Jun 9, 2026
Merged via the queue into kgateway-dev:main with commit fb8b18f Jun 9, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/fix Categorizes issue or PR as related to a bug. release-note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should controller overwrite existing K8s Service not owned by the Gateway?

5 participants