Skip to content

Commit 0637f31

Browse files
author
Rahul Sharma
committed
remove vpc from cache if it doesn't exist
1 parent 913f2b6 commit 0637f31

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

cloud/linode/instances.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,16 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)
8585
if vpcName == "" {
8686
continue
8787
}
88-
vpcID, err := GetVPCID(client, strings.TrimSpace(vpcName))
88+
resp, err := GetVPCIPAddresses(ctx, client, vpcName)
8989
if err != nil {
9090
klog.Errorf("failed updating instances cache for VPC %s. Error: %s", vpcName, err.Error())
9191
continue
9292
}
93-
if vpcID != 0 {
94-
resp, err := client.ListVPCIPAddresses(ctx, vpcID, linodego.NewListOptions(0, ""))
95-
if err != nil {
96-
return err
97-
}
98-
for _, r := range resp {
99-
if r.Address == nil {
100-
continue
101-
}
102-
vpcNodes[r.LinodeID] = append(vpcNodes[r.LinodeID], *r.Address)
93+
for _, r := range resp {
94+
if r.Address == nil {
95+
continue
10396
}
97+
vpcNodes[r.LinodeID] = append(vpcNodes[r.LinodeID], *r.Address)
10498
}
10599
}
106100

cloud/linode/route_controller.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,11 @@ func (rc *routeCache) refreshRoutes(ctx context.Context, client client.Client) e
4242
if vpcName == "" {
4343
continue
4444
}
45-
vpcID, err := GetVPCID(client, strings.TrimSpace(vpcName))
45+
resp, err := GetVPCIPAddresses(ctx, client, vpcName)
4646
if err != nil {
4747
klog.Errorf("failed updating cache for VPC %s. Error: %s", vpcName, err.Error())
4848
continue
4949
}
50-
resp, err := client.ListVPCIPAddresses(ctx, vpcID, linodego.NewListOptions(0, ""))
51-
if err != nil {
52-
return err
53-
}
5450
for _, r := range resp {
5551
vpcNodes[r.LinodeID] = append(vpcNodes[r.LinodeID], r)
5652
}

cloud/linode/vpc.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package linode
33
import (
44
"context"
55
"fmt"
6+
"strings"
67
"sync"
78

89
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
910
"github.com/linode/linodego"
11+
"k8s.io/klog/v2"
1012
)
1113

1214
var (
@@ -35,15 +37,15 @@ func GetAllVPCIDs() []int {
3537
}
3638

3739
// GetVPCID returns the VPC id of given VPC label
38-
func GetVPCID(client client.Client, vpcName string) (int, error) {
40+
func GetVPCID(ctx context.Context, client client.Client, vpcName string) (int, error) {
3941
Mu.Lock()
4042
defer Mu.Unlock()
4143

4244
// check if map contains vpc id for given label
4345
if vpcid, ok := vpcIDs[vpcName]; ok {
4446
return vpcid, nil
4547
}
46-
vpcs, err := client.ListVPCs(context.TODO(), &linodego.ListOptions{})
48+
vpcs, err := client.ListVPCs(ctx, &linodego.ListOptions{})
4749
if err != nil {
4850
return 0, err
4951
}
@@ -55,3 +57,22 @@ func GetVPCID(client client.Client, vpcName string) (int, error) {
5557
}
5658
return 0, vpcLookupError{vpcName}
5759
}
60+
61+
// GetVPCIPAddresses returns vpc ip's for given VPC label
62+
func GetVPCIPAddresses(ctx context.Context, client client.Client, vpcName string) ([]linodego.VPCIP, error) {
63+
vpcID, err := GetVPCID(ctx, client, strings.TrimSpace(vpcName))
64+
if err != nil {
65+
return nil, err
66+
}
67+
resp, err := client.ListVPCIPAddresses(ctx, vpcID, linodego.NewListOptions(0, ""))
68+
if err != nil {
69+
if strings.Contains(err.Error(), "Not found") {
70+
Mu.Lock()
71+
defer Mu.Unlock()
72+
klog.Errorf("vpc %s not found. Deleting entry from cache", vpcName)
73+
delete(vpcIDs, vpcName)
74+
}
75+
return nil, err
76+
}
77+
return resp, nil
78+
}

0 commit comments

Comments
 (0)