From f94597d35131759d4f2bb185f71fa1ae7232995c Mon Sep 17 00:00:00 2001 From: Istvan Nagy Date: Wed, 7 Jan 2026 17:39:00 +0100 Subject: [PATCH] handle server side default for TrafficDistribution --- pkg/controller/common/service_control.go | 5 ++++ pkg/controller/common/service_control_test.go | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/pkg/controller/common/service_control.go b/pkg/controller/common/service_control.go index 1c2962ee4f8..c4eea52f6f7 100644 --- a/pkg/controller/common/service_control.go +++ b/pkg/controller/common/service_control.go @@ -162,6 +162,11 @@ func applyServerSideValues(expected, reconciled *corev1.Service) { if expected.Spec.AllocateLoadBalancerNodePorts == nil { expected.Spec.AllocateLoadBalancerNodePorts = reconciled.Spec.AllocateLoadBalancerNodePorts } + + // TrafficDistribution may be defaulted by the API server starting K8s v1.31 + if expected.Spec.TrafficDistribution == nil { + expected.Spec.TrafficDistribution = reconciled.Spec.TrafficDistribution + } } // hasNodePort returns for a given service type, if the service ports have a NodePort or not. diff --git a/pkg/controller/common/service_control_test.go b/pkg/controller/common/service_control_test.go index 6d51b5e17a9..a0790483cee 100644 --- a/pkg/controller/common/service_control_test.go +++ b/pkg/controller/common/service_control_test.go @@ -668,6 +668,30 @@ func Test_applyServerSideValues(t *testing.T) { ClusterIP: "1.2.3.4", }}, }, + { + name: "Reconciled TrafficDistribution is used if the expected one is nil", + args: args{ + expected: corev1.Service{Spec: corev1.ServiceSpec{}}, + reconciled: corev1.Service{Spec: corev1.ServiceSpec{ + TrafficDistribution: ptr.To(corev1.ServiceTrafficDistributionPreferClose), + }}, + }, + want: corev1.Service{Spec: corev1.ServiceSpec{ + TrafficDistribution: ptr.To(corev1.ServiceTrafficDistributionPreferClose), + }}, + }, + { + name: "Expected TrafficDistribution is used if not nil", + args: args{ + expected: corev1.Service{Spec: corev1.ServiceSpec{ + TrafficDistribution: ptr.To(corev1.ServiceTrafficDistributionPreferClose), + }}, + reconciled: corev1.Service{Spec: corev1.ServiceSpec{}}, + }, + want: corev1.Service{Spec: corev1.ServiceSpec{ + TrafficDistribution: ptr.To(corev1.ServiceTrafficDistributionPreferClose), + }}, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {