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

Refactor service tag removal (fixing some bugs). #97

Merged
merged 1 commit into from
Aug 5, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions pkg/controllers/loadbalancer/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package loadbalancer
import (
"context"
"fmt"
"slices"
"strings"
"sync"

Expand Down Expand Up @@ -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
Expand Down
38 changes: 18 additions & 20 deletions pkg/controllers/loadbalancer/loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down