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

Filter NodeBalancers when we know their ipv4 #126

Merged
merged 4 commits into from
Mar 24, 2023
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
14 changes: 5 additions & 9 deletions cloud/linode/fake_linode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ type fakeRequest struct {
Method string
}

type filterStruct struct {
Label string `json:"label,omitempty"`
NbID string `json:"nodebalancer_id,omitempty"`
}

func newFake(t *testing.T) *fakeAPI {
return &fakeAPI{
t: t,
Expand Down Expand Up @@ -156,13 +151,13 @@ func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
data = append(data, *n)
}
} else {
var fs filterStruct
var fs map[string]string
err := json.Unmarshal([]byte(filter), &fs)
if err != nil {
f.t.Fatal(err)
}
for _, n := range f.nbc {
if strconv.Itoa(n.NodeBalancerID) == fs.NbID {
if strconv.Itoa(n.NodeBalancerID) == fs["nodebalancer_id"] {
data = append(data, *n)
}
}
Expand Down Expand Up @@ -209,13 +204,14 @@ func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
data = append(data, *n)
}
} else {
var fs filterStruct
var fs map[string]string
err := json.Unmarshal([]byte(filter), &fs)
if err != nil {
f.t.Fatal(err)
}
for _, n := range f.nb {
if *n.Label == fs.Label {
if (n.Label != nil && fs["label"] != "" && *n.Label == fs["label"]) ||
(fs["ipv4"] != "" && n.IPv4 != nil && *n.IPv4 == fs["ipv4"]) {
data = append(data, *n)
}
}
Expand Down
13 changes: 6 additions & 7 deletions cloud/linode/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,17 +450,16 @@ func (l *loadbalancers) getNodeBalancerByHostname(ctx context.Context, service *
}

func (l *loadbalancers) getNodeBalancerByIPv4(ctx context.Context, service *v1.Service, ipv4 string) (*linodego.NodeBalancer, error) {
lbs, err := l.client.ListNodeBalancers(ctx, nil)
filter := fmt.Sprintf(`{"ipv4": "%v"}`, ipv4)
lbs, err := l.client.ListNodeBalancers(ctx, &linodego.ListOptions{Filter: filter})
if err != nil {
return nil, err
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the loop below this may no longer be needed if we are getting the exact ip. Or it could be adjusted to be cleaner. Although leaving it how it is I don't think will harm anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Did you have something like this in mind?

if len(lbs) == 0 {
    return nil, lbNotFoundError{serviceNn: getServiceNn(service)}
}
klog.V(2).Infof("found NodeBalancer (%d) for service (%s) via IPv4 (%s)", lbs[0].ID, getServiceNn(service), ipv4)
return &lbs[0], nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I like that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Committed.

for _, lb := range lbs {
if *lb.IPv4 == ipv4 {
klog.V(2).Infof("found NodeBalancer (%d) for service (%s) via IPv4 (%s)", lb.ID, getServiceNn(service), ipv4)
return &lb, nil
}
if len(lbs) == 0 {
return nil, lbNotFoundError{serviceNn: getServiceNn(service)}
}
return nil, lbNotFoundError{serviceNn: getServiceNn(service)}
klog.V(2).Infof("found NodeBalancer (%d) for service (%s) via IPv4 (%s)", lbs[0].ID, getServiceNn(service), ipv4)
return &lbs[0], nil
}

func (l *loadbalancers) getNodeBalancerByID(ctx context.Context, service *v1.Service, id int) (*linodego.NodeBalancer, error) {
Expand Down
12 changes: 6 additions & 6 deletions cloud/linode/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,17 +379,17 @@ func testUpdateLoadBalancerAddPortAnnotation(t *testing.T, client *linodego.Clie

err = lb.UpdateLoadBalancer(context.TODO(), "linodelb", svc, nodes)
if err != nil {
t.Errorf("UpdateLoadBalancer returned an error while updated annotations: %s", err)
t.Fatalf("UpdateLoadBalancer returned an error while updated annotations: %s", err)
}

nb, err := lb.getNodeBalancerByStatus(context.TODO(), svc)
if err != nil {
t.Errorf("failed to get NodeBalancer by status: %v", err)
t.Fatalf("failed to get NodeBalancer by status: %v", err)
}

cfgs, errConfigs := client.ListNodeBalancerConfigs(context.TODO(), nb.ID, nil)
if errConfigs != nil {
t.Errorf("error getting NodeBalancer configs: %v", errConfigs)
t.Fatalf("error getting NodeBalancer configs: %v", errConfigs)
}

expectedPortConfigs := map[int]string{
Expand Down Expand Up @@ -468,17 +468,17 @@ func testUpdateLoadBalancerAddTLSPort(t *testing.T, client *linodego.Client, _ *
})
err = lb.UpdateLoadBalancer(context.TODO(), "linodelb", svc, nodes)
if err != nil {
t.Errorf("UpdateLoadBalancer returned an error while updated annotations: %s", err)
t.Fatalf("UpdateLoadBalancer returned an error while updated annotations: %s", err)
}

nb, err := lb.getNodeBalancerByStatus(context.TODO(), svc)
if err != nil {
t.Errorf("unexpected error: %v", err)
t.Fatalf("unexpected error: %v", err)
}

cfgs, errConfigs := client.ListNodeBalancerConfigs(context.TODO(), nb.ID, nil)
if errConfigs != nil {
t.Errorf("error getting NodeBalancer configs: %v", errConfigs)
t.Fatalf("error getting NodeBalancer configs: %v", errConfigs)
}

expectedPorts := map[int]struct{}{
Expand Down