diff --git a/pkg/controllers/loadbalancer/loadbalancer.go b/pkg/controllers/loadbalancer/loadbalancer.go index 9daea54..fc3c1e6 100644 --- a/pkg/controllers/loadbalancer/loadbalancer.go +++ b/pkg/controllers/loadbalancer/loadbalancer.go @@ -3,6 +3,7 @@ package loadbalancer import ( "context" "fmt" + "slices" "strings" "sync" @@ -261,20 +262,11 @@ func (l *LoadBalancerController) EnsureLoadBalancerDeleted(ctx context.Context, // removes the service tag and checks whether it is the last service tag. func (l *LoadBalancerController) removeServiceTag(ip models.V1IPResponse, serviceTag string) ([]string, bool) { - count := 0 - newTags := []string{} - for _, t := range ip.Tags { - if strings.HasPrefix(t, tag.ClusterServiceFQN) { - count++ - } - if t == serviceTag { - continue - } - newTags = append(newTags, t) - } - last := (count <= 1) - klog.Infof("removing service tag '%s', last: %t, oldTags: %v, newTags: %v", serviceTag, last, ip.Tags, newTags) - return newTags, last + newTags := slices.DeleteFunc(ip.Tags, func(tag string) bool { + return tag == serviceTag + }) + + return newTags, len(newTags) == 0 } // UpdateMetalLBConfig the metallb config for given nodes diff --git a/pkg/controllers/loadbalancer/loadbalancer_test.go b/pkg/controllers/loadbalancer/loadbalancer_test.go index d156f5a..76ad32a 100644 --- a/pkg/controllers/loadbalancer/loadbalancer_test.go +++ b/pkg/controllers/loadbalancer/loadbalancer_test.go @@ -57,29 +57,27 @@ func TestLoadBalancerController_removeServiceTag(t *testing.T) { Tags: nil, }, serviceTag: testTag1, + want: nil, + wantLast: true, + }, + { + name: "two times own service tag", + ip: models.V1IPResponse{ + Tags: []string{testTag1, testTag1}, + }, + serviceTag: testTag1, want: []string{}, wantLast: true, }, - // TODO: this case is not covered: - // { - // name: "two times own service tag", - // ip: models.V1IPResponse{ - // Tags: []string{testTag1, testTag1}, - // }, - // serviceTag: testTag1, - // want: []string{}, - // wantLast: true, - // }, - // TODO: this case is not covered - // { - // name: "only other service tag", - // ip: models.V1IPResponse{ - // Tags: []string{testTag2}, - // }, - // serviceTag: testTag1, - // want: []string{testTag2}, - // wantLast: false, - // }, + { + name: "only other service tag", + ip: models.V1IPResponse{ + Tags: []string{testTag2}, + }, + serviceTag: testTag1, + want: []string{testTag2}, + wantLast: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {