Skip to content

Conversation

@ymesika
Copy link
Contributor

@ymesika ymesika commented Dec 1, 2025

Description

The BackendObjectIR.Equals() method was missing comparison of the TrafficDistribution field, so backends with different traffic distribution values (like "Any" vs "PreferNetwork") were incorrectly considered equal. This could lead to stale configurations being reused when traffic distribution policies changed. The fix adds c.TrafficDistribution == in.TrafficDistribution to the equality check.

Also changed PreferSameNetwork to PreferNetwork as this is the expected value from Istio upstream. There is no constant for that there.

Fixes #13004

Change Type

/kind fix

Changelog

Fixed issue with stale configuration when changing a service traffic distribution.

Additional Notes

Copilot AI review requested due to automatic review settings December 1, 2025 12:56
@gateway-bot gateway-bot added kind/fix Categorizes issue or PR as related to a bug. release-note labels Dec 1, 2025
Copy link
Contributor

Copilot AI left a comment

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 fixes a critical bug where the TrafficDistribution field was not being compared in the Equals() methods of BackendObjectIR and EndpointsForBackend. This caused backends with different traffic distribution policies (e.g., "Any" vs "PreferNetwork") to be incorrectly treated as equal, leading to stale configurations when traffic distribution changed. Additionally, the PR renames the traffic distribution value from PreferSameNetwork to PreferNetwork to align with Istio's upstream naming convention.

Key changes:

  • Added TrafficDistribution comparison to BackendObjectIR.Equals() and EndpointsForBackend.Equals() methods
  • Added TrafficDistribution field to EndpointsForBackend.EmptyCopy() method
  • Renamed TrafficDistributionPreferSameNetwork constant to TrafficDistributionPreferNetwork
  • Updated all references throughout the codebase to use PreferNetwork instead of PreferSameNetwork

Reviewed changes

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

Show a summary per file
File Description
pkg/pluginsdk/ir/model.go Added TrafficDistribution to EmptyCopy() and Equals() methods for EndpointsForBackend; removed TODO comment
pkg/pluginsdk/ir/backend.go Added TrafficDistribution comparison to BackendObjectIR.Equals() method; removed TODO comment
pkg/pluginsdk/ir/backend_test.go Added comprehensive unit tests for BackendObjectIR.Equals() covering various traffic distribution scenarios
internal/kgateway/wellknown/traffic_dist.go Renamed constant and updated parsing logic from PreferSameNetwork to PreferNetwork
internal/kgateway/setup/testdata/traffic_distribution/svc-prefer-same-network.yaml Updated annotation value from PreferSameNetwork to PreferNetwork
internal/kgateway/setup/testdata/traffic_distribution/se-prefer-same-network.yaml Updated annotation values and comments from PreferSameNetwork to PreferNetwork
internal/kgateway/krtcollections/endpoints_test.go Updated test to use renamed TrafficDistributionPreferNetwork constant
internal/kgateway/endpoints/prioritize.go Updated case statement to use renamed TrafficDistributionPreferNetwork constant

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

}
}

func createTestBackendObjectIR(trafficDist wellknown.TrafficDistribution) BackendObjectIR {
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

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

The new test TestBackendObjectIREquals doesn't test scenarios where other fields differ (e.g., different Port, Namespace, or Name). While testing TrafficDistribution is good, consider adding test cases that verify the method correctly handles differences in other key fields to ensure comprehensive coverage of the equality logic.

Copilot uses AI. Check for mistakes.

func (c EndpointsForBackend) Equals(in EndpointsForBackend) bool {
return c.UpstreamResourceName == in.UpstreamResourceName && c.ClusterName == in.ClusterName && c.Port == in.Port && c.LbEpsEqualityHash == in.LbEpsEqualityHash && c.Hostname == in.Hostname
return c.UpstreamResourceName == in.UpstreamResourceName && c.ClusterName == in.ClusterName && c.Port == in.Port && c.LbEpsEqualityHash == in.LbEpsEqualityHash && c.Hostname == in.Hostname && c.TrafficDistribution == in.TrafficDistribution
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

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

While EndpointsForBackend.Equals() now correctly compares TrafficDistribution, there are no unit tests that directly verify the Equals() method behavior with different TrafficDistribution values. The existing test TestEndpointsForUpstreamWithDifferentTrafficDistributionButSameEndpoints only verifies hash differences. Consider adding a test similar to TestBackendObjectIREquals that directly calls EndpointsForBackend.Equals() with different traffic distributions to ensure the equality logic is properly validated.

Copilot uses AI. Check for mistakes.
@ymesika ymesika added this pull request to the merge queue Dec 1, 2025
Merged via the queue into kgateway-dev:main with commit 3a47825 Dec 1, 2025
30 checks passed
@ymesika ymesika deleted the fix-traffic-distribution branch December 1, 2025 15:59
yuval-k pushed a commit that referenced this pull request Dec 1, 2025
ymesika added a commit to ymesika/kgateway that referenced this pull request Dec 11, 2025
github-merge-queue bot pushed a commit that referenced this pull request Dec 11, 2025
github-merge-queue bot pushed a commit that referenced this pull request Dec 12, 2025
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.

BackendObjectIR.Equals() missing TrafficDistribution comparison

3 participants