Skip to content

[2.2] fix: Do not mutate a Service kgateway does not own.#14350

Merged
davidjumani merged 2 commits into
kgateway-dev:v2.2.xfrom
chandler-solo:chandler/backport-14145-v2.2.x
Jul 10, 2026
Merged

[2.2] fix: Do not mutate a Service kgateway does not own.#14350
davidjumani merged 2 commits into
kgateway-dev:v2.2.xfrom
chandler-solo:chandler/backport-14145-v2.2.x

Conversation

@chandler-solo

Copy link
Copy Markdown
Contributor

Backport of #14145 to v2.2.x.


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

Signed-off-by: David L. Chandler <[email protected]>
(cherry picked from commit fb8b18f)
Copilot AI review requested due to automatic review settings July 3, 2026 04:04
@gateway-bot gateway-bot added kind/fix Categorizes issue or PR as related to a bug. release-note labels Jul 3, 2026

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

Prevents kgateway’s deployer from overwriting an existing Kubernetes Service for a Gateway unless the Service demonstrates ownership (via controller ownerReference or matching kgateway/Gateway labels), avoiding accidental takeover when an unrelated Service shares the same name.

Changes:

  • Added ownership validation before patching an existing Service during DeployObjsWithSource.
  • Added deployer unit tests covering “refuse patch”, “allow via labels”, and “allow via controller ownerRef”.
  • Stabilized a multiinstall e2e policy test by using non-fatal assertions within Eventually and increasing the wait timeout.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.

File Description
test/e2e/features/multiinstall/suite.go Makes the log-verification Eventually block non-fatal per-iteration and increases timeout for stability.
pkg/kgateway/helm/envoy/templates/service.yaml Minor Helm templating formatting adjustment (whitespace).
pkg/deployer/deployer.go Introduces Service ownership validation gate before patching existing Services.
pkg/deployer/deployer_test.go Adds tests to ensure Services are only patched when owned via ownerRef or labels.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +2869 to +2874
APIVersion: wellknown.GatewayGVK.GroupVersion().String(),
Kind: wellknown.GatewayGVK.Kind,
Name: gw.GetName(),
UID: gw.GetUID(),
Controller: new(true),
}}
Comment thread pkg/deployer/deployer.go
Comment on lines +449 to +465
func hasMatchingGatewayServiceMetadata(existingObj, desiredObj client.Object) bool {
existingLabels := existingObj.GetLabels()
desiredLabels := desiredObj.GetLabels()
if existingLabels[appKubernetesManagedByLabel] != kgatewayManagedByValue ||
desiredLabels[appKubernetesManagedByLabel] != kgatewayManagedByValue {
return false
}

if desiredClassName, ok := desiredLabels[wellknown.GatewayClassNameLabel]; ok {
if existingLabels[wellknown.GatewayClassNameLabel] != desiredClassName {
return false
}
}

desiredGatewayLabel, desiredHasGatewayLabel := desiredLabels[wellknown.GatewayNameLabel]
return desiredHasGatewayLabel && existingLabels[wellknown.GatewayNameLabel] == desiredGatewayLabel
}
@davidjumani
davidjumani added this pull request to the merge queue Jul 10, 2026
Merged via the queue into kgateway-dev:v2.2.x with commit 92ea35d Jul 10, 2026
37 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.

4 participants