From 2642c492fe490904a2ab4914e2381f41be86ace2 Mon Sep 17 00:00:00 2001 From: Martin Schuppert Date: Thu, 14 Dec 2023 18:05:08 +0100 Subject: [PATCH] [service] Add AddLabel()/AddAnnotation for RoutedOverrideSpec and OverrideSpec --- modules/common/service/service.go | 33 ++++++ modules/common/service/service_test.go | 158 +++++++++++++++++++++++++ 2 files changed, 191 insertions(+) diff --git a/modules/common/service/service.go b/modules/common/service/service.go index 8e087753..2e9e3dc4 100644 --- a/modules/common/service/service.go +++ b/modules/common/service/service.go @@ -146,6 +146,39 @@ func (s *Service) AddAnnotation(anno map[string]string) { s.service.Annotations = util.MergeStringMaps(s.service.Annotations, anno) } +// AddAnnotation - Adds annotation and merges it with the current set +func (s *RoutedOverrideSpec) AddAnnotation(anno map[string]string) { + if s.EmbeddedLabelsAnnotations == nil { + s.EmbeddedLabelsAnnotations = &EmbeddedLabelsAnnotations{} + } + s.Annotations = util.MergeMaps(s.Annotations, anno) +} + +// AddLabel - Adds label and merges it with the current set +func (s *RoutedOverrideSpec) AddLabel(label map[string]string) { + if s.EmbeddedLabelsAnnotations == nil { + s.EmbeddedLabelsAnnotations = &EmbeddedLabelsAnnotations{} + } + + s.Labels = util.MergeMaps(s.Labels, label) +} + +// AddAnnotation - Adds annotation and merges it with the current set +func (s *OverrideSpec) AddAnnotation(anno map[string]string) { + if s.EmbeddedLabelsAnnotations == nil { + s.EmbeddedLabelsAnnotations = &EmbeddedLabelsAnnotations{} + } + s.Annotations = util.MergeMaps(s.Annotations, anno) +} + +// AddLabel - Adds label and merges it with the current set +func (s *OverrideSpec) AddLabel(label map[string]string) { + if s.EmbeddedLabelsAnnotations == nil { + s.EmbeddedLabelsAnnotations = &EmbeddedLabelsAnnotations{} + } + s.Labels = util.MergeMaps(s.Labels, label) +} + // GetAPIEndpoint - returns the API endpoint URL for the service to register in keystone. func (s *Service) GetAPIEndpoint(endpointURL *string, protocol *Protocol, path string) (string, error) { var apiEndpoint *url.URL diff --git a/modules/common/service/service_test.go b/modules/common/service/service_test.go index 6e68c736..52c4d73e 100644 --- a/modules/common/service/service_test.go +++ b/modules/common/service/service_test.go @@ -424,3 +424,161 @@ func TestToOverrideServiceSpec(t *testing.T) { }) } } + +func TestOverrideSpecAddAnnotation(t *testing.T) { + tests := []struct { + name string + override OverrideSpec + annotation map[string]string + want OverrideSpec + }{ + { + name: "No override, no custom annotation", + override: OverrideSpec{}, + annotation: map[string]string{}, + want: OverrideSpec{ + EmbeddedLabelsAnnotations: &EmbeddedLabelsAnnotations{ + Annotations: map[string]string{}, + }, + }, + }, + { + name: "override, no custom annotation", + override: OverrideSpec{EmbeddedLabelsAnnotations: &EmbeddedLabelsAnnotations{ + Annotations: map[string]string{"key": "val"}, + }}, + annotation: map[string]string{}, + want: OverrideSpec{ + EmbeddedLabelsAnnotations: &EmbeddedLabelsAnnotations{ + Annotations: map[string]string{"key": "val"}, + }, + }, + }, + { + name: "override, additional custom annotation", + override: OverrideSpec{EmbeddedLabelsAnnotations: &EmbeddedLabelsAnnotations{ + Annotations: map[string]string{"key": "val"}, + }}, + annotation: map[string]string{"custom": "val"}, + want: OverrideSpec{ + EmbeddedLabelsAnnotations: &EmbeddedLabelsAnnotations{ + Annotations: map[string]string{ + "key": "val", + "custom": "val", + }, + }, + }, + }, + { + name: "override, additional custom same annotation", + override: OverrideSpec{EmbeddedLabelsAnnotations: &EmbeddedLabelsAnnotations{ + Annotations: map[string]string{"key": "val"}, + }}, + annotation: map[string]string{"key": "custom"}, + want: OverrideSpec{ + EmbeddedLabelsAnnotations: &EmbeddedLabelsAnnotations{ + Annotations: map[string]string{ + "key": "val", + }, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := NewWithT(t) + + tt.override.AddAnnotation(tt.annotation) + g.Expect(tt.override.Annotations).To(Equal(tt.want.Annotations)) + }) + } +} + +func TestRoutedOverrideSpecAddAnnotation(t *testing.T) { + tests := []struct { + name string + override RoutedOverrideSpec + annotation map[string]string + want RoutedOverrideSpec + }{ + { + name: "No override, no custom annotation", + override: RoutedOverrideSpec{}, + annotation: map[string]string{}, + want: RoutedOverrideSpec{ + OverrideSpec: OverrideSpec{ + EmbeddedLabelsAnnotations: &EmbeddedLabelsAnnotations{ + Annotations: map[string]string{}, + }, + }, + }, + }, + { + name: "override, no custom annotation", + override: RoutedOverrideSpec{ + OverrideSpec: OverrideSpec{ + EmbeddedLabelsAnnotations: &EmbeddedLabelsAnnotations{ + Annotations: map[string]string{"key": "val"}, + }, + }}, + annotation: map[string]string{}, + want: RoutedOverrideSpec{ + OverrideSpec: OverrideSpec{ + + EmbeddedLabelsAnnotations: &EmbeddedLabelsAnnotations{ + Annotations: map[string]string{"key": "val"}, + }, + }, + }, + }, + { + name: "override, additional custom annotation", + override: RoutedOverrideSpec{ + OverrideSpec: OverrideSpec{ + EmbeddedLabelsAnnotations: &EmbeddedLabelsAnnotations{ + Annotations: map[string]string{"key": "val"}, + }, + }}, + annotation: map[string]string{"custom": "val"}, + want: RoutedOverrideSpec{ + OverrideSpec: OverrideSpec{ + EmbeddedLabelsAnnotations: &EmbeddedLabelsAnnotations{ + Annotations: map[string]string{ + "key": "val", + "custom": "val", + }, + }, + }, + }, + }, + { + name: "override, additional custom same annotation", + override: RoutedOverrideSpec{ + OverrideSpec: OverrideSpec{ + EmbeddedLabelsAnnotations: &EmbeddedLabelsAnnotations{ + Annotations: map[string]string{"key": "val"}, + }, + }}, + annotation: map[string]string{"key": "custom"}, + want: RoutedOverrideSpec{ + OverrideSpec: OverrideSpec{ + EmbeddedLabelsAnnotations: &EmbeddedLabelsAnnotations{ + Annotations: map[string]string{ + "key": "val", + }, + }, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := NewWithT(t) + + tt.override.AddAnnotation(tt.annotation) + g.Expect(tt.override.Annotations).To(Equal(tt.want.Annotations)) + }) + } +}