Skip to content

Commit

Permalink
Merge pull request openstack-k8s-operators#418 from stuggi/annotation…
Browse files Browse the repository at this point in the history
…_label_merge

[service] Add AddLabel()/AddAnnotation for RoutedOverrideSpec and OverrideSpec
  • Loading branch information
stuggi authored Dec 18, 2023
2 parents 9acca00 + 2642c49 commit 3e74094
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
33 changes: 33 additions & 0 deletions modules/common/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
158 changes: 158 additions & 0 deletions modules/common/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
}

0 comments on commit 3e74094

Please sign in to comment.