Adding validation for topology annotations

Change-Id: I50b3b05b859c69e98daca7c8fca0d3a76024eb80
This commit is contained in:
Rob Scott
2023-03-14 20:08:05 +00:00
parent e6f3e3dddd
commit 9e571c0424
4 changed files with 64 additions and 1 deletions

View File

@@ -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)...)

View File

@@ -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{