diff --git a/cloud/linode/instances.go b/cloud/linode/instances.go index fd344f98..d834771c 100644 --- a/cloud/linode/instances.go +++ b/cloud/linode/instances.go @@ -85,22 +85,16 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client) if vpcName == "" { continue } - vpcID, err := GetVPCID(client, strings.TrimSpace(vpcName)) + resp, err := GetVPCIPAddresses(ctx, client, vpcName) if err != nil { klog.Errorf("failed updating instances cache for VPC %s. Error: %s", vpcName, err.Error()) continue } - if vpcID != 0 { - resp, err := client.ListVPCIPAddresses(ctx, vpcID, linodego.NewListOptions(0, "")) - if err != nil { - return err - } - for _, r := range resp { - if r.Address == nil { - continue - } - vpcNodes[r.LinodeID] = append(vpcNodes[r.LinodeID], *r.Address) + for _, r := range resp { + if r.Address == nil { + continue } + vpcNodes[r.LinodeID] = append(vpcNodes[r.LinodeID], *r.Address) } } diff --git a/cloud/linode/route_controller.go b/cloud/linode/route_controller.go index fbfe9bcb..8b3bdf47 100644 --- a/cloud/linode/route_controller.go +++ b/cloud/linode/route_controller.go @@ -27,12 +27,12 @@ type routeCache struct { } // RefreshCache checks if cache has expired and updates it accordingly -func (rc *routeCache) refreshRoutes(ctx context.Context, client client.Client) error { +func (rc *routeCache) refreshRoutes(ctx context.Context, client client.Client) { rc.Mu.Lock() defer rc.Mu.Unlock() if time.Since(rc.lastUpdate) < rc.ttl { - return nil + return } vpcNodes := map[int][]linodego.VPCIP{} @@ -42,15 +42,11 @@ func (rc *routeCache) refreshRoutes(ctx context.Context, client client.Client) e if vpcName == "" { continue } - vpcID, err := GetVPCID(client, strings.TrimSpace(vpcName)) + resp, err := GetVPCIPAddresses(ctx, client, vpcName) if err != nil { klog.Errorf("failed updating cache for VPC %s. Error: %s", vpcName, err.Error()) continue } - resp, err := client.ListVPCIPAddresses(ctx, vpcID, linodego.NewListOptions(0, "")) - if err != nil { - return err - } for _, r := range resp { vpcNodes[r.LinodeID] = append(vpcNodes[r.LinodeID], r) } @@ -58,7 +54,6 @@ func (rc *routeCache) refreshRoutes(ctx context.Context, client client.Client) e rc.routes = vpcNodes rc.lastUpdate = time.Now() - return nil } type routes struct { @@ -104,10 +99,7 @@ func (r *routes) instanceRoutesByID(id int) ([]linodego.VPCIP, error) { // getInstanceRoutes returns routes for given instance id // It refreshes routeCache if it has expired func (r *routes) getInstanceRoutes(ctx context.Context, id int) ([]linodego.VPCIP, error) { - if err := r.routeCache.refreshRoutes(ctx, r.client); err != nil { - return nil, err - } - + r.routeCache.refreshRoutes(ctx, r.client) return r.instanceRoutesByID(id) } diff --git a/cloud/linode/vpc.go b/cloud/linode/vpc.go index fa505523..274904aa 100644 --- a/cloud/linode/vpc.go +++ b/cloud/linode/vpc.go @@ -3,10 +3,12 @@ package linode import ( "context" "fmt" + "strings" "sync" "github.com/linode/linode-cloud-controller-manager/cloud/linode/client" "github.com/linode/linodego" + "k8s.io/klog/v2" ) var ( @@ -35,7 +37,7 @@ func GetAllVPCIDs() []int { } // GetVPCID returns the VPC id of given VPC label -func GetVPCID(client client.Client, vpcName string) (int, error) { +func GetVPCID(ctx context.Context, client client.Client, vpcName string) (int, error) { Mu.Lock() defer Mu.Unlock() @@ -43,7 +45,7 @@ func GetVPCID(client client.Client, vpcName string) (int, error) { if vpcid, ok := vpcIDs[vpcName]; ok { return vpcid, nil } - vpcs, err := client.ListVPCs(context.TODO(), &linodego.ListOptions{}) + vpcs, err := client.ListVPCs(ctx, &linodego.ListOptions{}) if err != nil { return 0, err } @@ -55,3 +57,22 @@ func GetVPCID(client client.Client, vpcName string) (int, error) { } return 0, vpcLookupError{vpcName} } + +// GetVPCIPAddresses returns vpc ip's for given VPC label +func GetVPCIPAddresses(ctx context.Context, client client.Client, vpcName string) ([]linodego.VPCIP, error) { + vpcID, err := GetVPCID(ctx, client, strings.TrimSpace(vpcName)) + if err != nil { + return nil, err + } + resp, err := client.ListVPCIPAddresses(ctx, vpcID, linodego.NewListOptions(0, "")) + if err != nil { + if strings.Contains(err.Error(), "Not found") { + Mu.Lock() + defer Mu.Unlock() + klog.Errorf("vpc %s not found. Deleting entry from cache", vpcName) + delete(vpcIDs, vpcName) + } + return nil, err + } + return resp, nil +}