From 249f763c7ee6033ebfe7c398879bac439017d30b Mon Sep 17 00:00:00 2001 From: Katarzyna Lach Date: Fri, 23 Dec 2022 12:36:04 +0000 Subject: [PATCH] Generate warning for EndpointSlice AddressType FQDN Change-Id: Ibc213acdffa741e630821f371ea25e2b5187a011 --- pkg/apis/discovery/types.go | 2 +- .../discovery/endpointslice/strategy.go | 17 ++++++++- .../discovery/endpointslice/strategy_test.go | 36 +++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/pkg/apis/discovery/types.go b/pkg/apis/discovery/types.go index 8b4dc265a60..efc2d331ec9 100644 --- a/pkg/apis/discovery/types.go +++ b/pkg/apis/discovery/types.go @@ -37,7 +37,7 @@ type EndpointSlice struct { // supported: // * IPv4: Represents an IPv4 Address. // * IPv6: Represents an IPv6 Address. - // * FQDN: Represents a Fully Qualified Domain Name. + // * FQDN: Represents a Fully Qualified Domain Name. [DEPRECATED] AddressType AddressType // endpoints is a list of unique endpoints in this slice. Each slice may // include a maximum of 1000 endpoints. diff --git a/pkg/registry/discovery/endpointslice/strategy.go b/pkg/registry/discovery/endpointslice/strategy.go index 6bb8359f1e4..62571f294be 100644 --- a/pkg/registry/discovery/endpointslice/strategy.go +++ b/pkg/registry/discovery/endpointslice/strategy.go @@ -18,6 +18,7 @@ package endpointslice import ( "context" + "fmt" corev1 "k8s.io/api/core/v1" discoveryv1beta1 "k8s.io/api/discovery/v1beta1" @@ -92,7 +93,13 @@ func (endpointSliceStrategy) Validate(ctx context.Context, obj runtime.Object) f // WarningsOnCreate returns warnings for the creation of the given object. func (endpointSliceStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { - return nil + eps := obj.(*discovery.EndpointSlice) + if eps == nil { + return nil + } + var warnings []string + warnings = append(warnings, warnOnDeprecatedAddressType(eps.AddressType)...) + return warnings } // Canonicalize normalizes the object after validation. @@ -207,3 +214,11 @@ func getDeprecatedTopologyNodeNames(eps *discovery.EndpointSlice) sets.String { } return names } + +// warnOnDeprecatedAddressType returns a warning for endpointslices with FQDN AddressType +func warnOnDeprecatedAddressType(addressType discovery.AddressType) []string { + if addressType == discovery.AddressTypeFQDN { + return []string{fmt.Sprintf("%s: FQDN endpoints are deprecated", field.NewPath("spec").Child("addressType"))} + } + return nil +} diff --git a/pkg/registry/discovery/endpointslice/strategy_test.go b/pkg/registry/discovery/endpointslice/strategy_test.go index 6204fe5af62..b0158abca03 100644 --- a/pkg/registry/discovery/endpointslice/strategy_test.go +++ b/pkg/registry/discovery/endpointslice/strategy_test.go @@ -978,3 +978,39 @@ func Test_getDeprecatedTopologyNodeNames(t *testing.T) { }) } } + +func TestWarningsOnEndpointSliceAddressType(t *testing.T) { + tests := []struct { + name string + addressType discovery.AddressType + wantWarning bool + }{ + { + name: "AddressType = FQDN", + addressType: discovery.AddressTypeFQDN, + wantWarning: true, + }, + { + name: "AddressType = IPV4", + addressType: discovery.AddressTypeIPv4, + wantWarning: false, + }, + { + name: "AddressType = IPV6", + addressType: discovery.AddressTypeIPv6, + wantWarning: false, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + ctx := genericapirequest.WithRequestInfo(genericapirequest.NewContext(), &genericapirequest.RequestInfo{APIGroup: "discovery.k8s.io", APIVersion: "v1", Resource: "endpointslices"}) + edp := discovery.EndpointSlice{AddressType: tc.addressType} + got := Strategy.WarningsOnCreate(ctx, &edp) + if tc.wantWarning && len(got) == 0 { + t.Fatal("Failed warning was not returned") + } else if !tc.wantWarning && len(got) != 0 { + t.Fatalf("Failed warning was returned (%v)", got) + } + }) + } +}