Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding validation for Topology annotations #116612

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/api/service/warnings.go
Expand Up @@ -31,6 +31,10 @@ func GetWarningsForService(service, oldService *api.Service) []string {
}
var warnings []string

if _, ok := service.Annotations[api.DeprecatedAnnotationTopologyAwareHints]; ok {
warnings = append(warnings, fmt.Sprintf("annotation %s is deprecated, please use %s instead", api.DeprecatedAnnotationTopologyAwareHints, api.AnnotationTopologyMode))
}

if helper.IsServiceIPSet(service) {
for i, clusterIP := range service.Spec.ClusterIPs {
warnings = append(warnings, getWarningsForIP(field.NewPath("spec").Child("clusterIPs").Index(i), clusterIP)...)
Expand Down
34 changes: 34 additions & 0 deletions pkg/api/service/warnings_test.go
Expand Up @@ -26,6 +26,40 @@ import (
api "k8s.io/kubernetes/pkg/apis/core"
)

func TestGetWarningsForService(t *testing.T) {
testCases := []struct {
name string
tweakSvc func(svc *api.Service) // Given a basic valid service, each test case can customize it.
numWarnings int
}{
{
name: "new topology mode set",
tweakSvc: func(s *api.Service) {
s.Annotations = map[string]string{api.AnnotationTopologyMode: "foo"}
},
numWarnings: 0,
},
{
name: "deprecated hints annotation set",
tweakSvc: func(s *api.Service) {
s.Annotations = map[string]string{api.DeprecatedAnnotationTopologyAwareHints: "foo"}
},
numWarnings: 1,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
svc := &api.Service{}
tc.tweakSvc(svc)
warnings := GetWarningsForService(svc, svc)
if len(warnings) != tc.numWarnings {
t.Errorf("Unexpected warning list: %v", warnings)
}
})
}
}

func TestGetWarningsForServiceClusterIPs(t *testing.T) {
service := func(clusterIPs []string) *api.Service {
svc := api.Service{
Expand Down
11 changes: 10 additions & 1 deletion pkg/apis/core/validation/validation.go
Expand Up @@ -4892,7 +4892,16 @@ var supportedServiceIPFamilyPolicy = sets.NewString(string(core.IPFamilyPolicySi

// ValidateService tests if required fields/annotations of a Service are valid.
func ValidateService(service *core.Service) field.ErrorList {
allErrs := ValidateObjectMeta(&service.ObjectMeta, true, ValidateServiceName, field.NewPath("metadata"))
metaPath := field.NewPath("metadata")
allErrs := ValidateObjectMeta(&service.ObjectMeta, true, ValidateServiceName, metaPath)

topologyHintsVal, topologyHintsSet := service.Annotations[core.DeprecatedAnnotationTopologyAwareHints]
topologyModeVal, topologyModeSet := service.Annotations[core.AnnotationTopologyMode]

if topologyModeSet && topologyHintsSet && topologyModeVal != topologyHintsVal {
message := fmt.Sprintf("must match annotations[%s] when both are specified", core.DeprecatedAnnotationTopologyAwareHints)
allErrs = append(allErrs, field.Invalid(metaPath.Child("annotations").Key(core.AnnotationTopologyMode), topologyModeVal, message))
}

specPath := field.NewPath("spec")

Expand Down
16 changes: 16 additions & 0 deletions pkg/apis/core/validation/validation_test.go
Expand Up @@ -15961,6 +15961,14 @@ func TestValidateServiceCreate(t *testing.T) {
},
numErrs: 1,
},
{
name: "topology annotations are mismatched",
tweakSvc: func(s *core.Service) {
s.Annotations[core.DeprecatedAnnotationTopologyAwareHints] = "original"
s.Annotations[core.AnnotationTopologyMode] = "different"
},
numErrs: 1,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -18597,6 +18605,14 @@ func TestValidateServiceUpdate(t *testing.T) {
},
numErrs: 0,
},
{
name: "topology annotations are mismatched",
tweakSvc: func(oldSvc, newSvc *core.Service) {
newSvc.Annotations[core.DeprecatedAnnotationTopologyAwareHints] = "original"
newSvc.Annotations[core.AnnotationTopologyMode] = "different"
},
numErrs: 1,
},
}

for _, tc := range testCases {
Expand Down