diff --git a/pkg/registry/core/service/storage/storage.go b/pkg/registry/core/service/storage/storage.go index 363cdc8f1ea..d9d77ae6cf4 100644 --- a/pkg/registry/core/service/storage/storage.go +++ b/pkg/registry/core/service/storage/storage.go @@ -242,6 +242,17 @@ func (r *REST) defaultOnReadService(service *api.Service) { // Set ipFamilies and ipFamilyPolicy if needed. r.defaultOnReadIPFamilies(service) + + // We unintentionally defaulted internalTrafficPolicy when it's not needed + // for the ExternalName type. It's too late to change the field in storage, + // but we can drop the field when read. + defaultOnReadInternalTrafficPolicy(service) +} + +func defaultOnReadInternalTrafficPolicy(service *api.Service) { + if service.Spec.Type == api.ServiceTypeExternalName { + service.Spec.InternalTrafficPolicy = nil + } } func (r *REST) defaultOnReadIPFamilies(service *api.Service) { diff --git a/pkg/registry/core/service/storage/storage_test.go b/pkg/registry/core/service/storage/storage_test.go index 444b5be00d2..1a0fc0a694f 100644 --- a/pkg/registry/core/service/storage/storage_test.go +++ b/pkg/registry/core/service/storage/storage_test.go @@ -597,9 +597,12 @@ func TestServiceDefaultOnRead(t *testing.T) { svctest.SetIPFamilyPolicy(api.IPFamilyPolicySingleStack), svctest.SetIPFamilies(api.IPv4Protocol)), }, { - name: "external name", - input: makeServiceList(svctest.SetTypeExternalName), - expect: makeServiceList(svctest.SetTypeExternalName), + name: "external name", + input: makeServiceList(svctest.SetTypeExternalName), + expect: makeServiceList(svctest.SetTypeExternalName, func(svc *api.Service) { + // we now drop internalTrafficPolicy on read when Type=ExternalName + svc.Spec.InternalTrafficPolicy = nil + }), }, { name: "dual v4v6", input: svctest.MakeService("foo", svctest.SetClusterIPs("10.0.0.1", "2000::1")), @@ -679,6 +682,9 @@ func TestServiceDefaultOnRead(t *testing.T) { if want, got := exp.Spec.IPFamilies, svc.Spec.IPFamilies; !reflect.DeepEqual(want, got) { t.Errorf("ipFamilies: expected %v, got %v", want, got) } + if want, got := fmtInternalTrafficPolicy(exp.Spec.InternalTrafficPolicy), fmtInternalTrafficPolicy(svc.Spec.InternalTrafficPolicy); want != got { + t.Errorf("internalTrafficPolicy: expected %v, got %v", want, got) + } }) } } @@ -1243,6 +1249,13 @@ func fmtIPFamilyPolicy(pol *api.IPFamilyPolicyType) string { return string(*pol) } +func fmtInternalTrafficPolicy(pol *api.ServiceInternalTrafficPolicyType) string { + if pol == nil { + return "" + } + return string(*pol) +} + func fmtIPFamilies(fams []api.IPFamily) string { if fams == nil { return "[]"