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

manage routes for instances in multiple vpcs in a single region #241

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 5 additions & 11 deletions cloud/linode/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,16 @@
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

Check warning on line 91 in cloud/linode/instances.go

View check run for this annotation

Codecov / codecov/patch

cloud/linode/instances.go#L90-L91

Added lines #L90 - L91 were not covered by tests
}
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)
}
}

Expand Down
16 changes: 4 additions & 12 deletions cloud/linode/route_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
}

// 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{}
Expand All @@ -40,25 +40,20 @@
for _, v := range vpcNames {
vpcName := strings.TrimSpace(v)
if vpcName == "" {
continue

Check warning on line 43 in cloud/linode/route_controller.go

View check run for this annotation

Codecov / codecov/patch

cloud/linode/route_controller.go#L43

Added line #L43 was not covered by tests
}
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

Check warning on line 48 in cloud/linode/route_controller.go

View check run for this annotation

Codecov / codecov/patch

cloud/linode/route_controller.go#L47-L48

Added lines #L47 - L48 were not covered by tests
}
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)
}
}

rc.routes = vpcNodes
rc.lastUpdate = time.Now()
return nil
}

type routes struct {
Expand Down Expand Up @@ -104,10 +99,7 @@
// 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)
}

Expand Down Expand Up @@ -162,7 +154,7 @@
return nil
}

intfRoutes = append(intfRoutes, *ir.AddressRange)

Check warning on line 157 in cloud/linode/route_controller.go

View check run for this annotation

Codecov / codecov/patch

cloud/linode/route_controller.go#L157

Added line #L157 was not covered by tests
}
}

Expand Down Expand Up @@ -214,7 +206,7 @@
continue
}

intfRoutes = append(intfRoutes, *ir.AddressRange)

Check warning on line 209 in cloud/linode/route_controller.go

View check run for this annotation

Codecov / codecov/patch

cloud/linode/route_controller.go#L209

Added line #L209 was not covered by tests
}
}

Expand Down
25 changes: 23 additions & 2 deletions cloud/linode/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import (
"context"
"fmt"
"strings"
"sync"

"github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
"github.com/linode/linodego"
"k8s.io/klog/v2"
)

var (
Expand Down Expand Up @@ -35,23 +37,42 @@
}

// 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()

// check if map contains vpc id for given label
if vpcid, ok := vpcIDs[vpcName]; ok {
return vpcid, nil
}
vpcs, err := client.ListVPCs(context.TODO(), &linodego.ListOptions{})
Copy link
Contributor

Choose a reason for hiding this comment

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

good catch 👍

vpcs, err := client.ListVPCs(ctx, &linodego.ListOptions{})

Check warning on line 48 in cloud/linode/vpc.go

View check run for this annotation

Codecov / codecov/patch

cloud/linode/vpc.go#L48

Added line #L48 was not covered by tests
if err != nil {
return 0, err
}
for _, vpc := range vpcs {
if vpc.Label == vpcName {
vpcIDs[vpcName] = vpc.ID

Check warning on line 54 in cloud/linode/vpc.go

View check run for this annotation

Codecov / codecov/patch

cloud/linode/vpc.go#L54

Added line #L54 was not covered by tests
return vpc.ID, nil
}
}
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
}

Check warning on line 66 in cloud/linode/vpc.go

View check run for this annotation

Codecov / codecov/patch

cloud/linode/vpc.go#L65-L66

Added lines #L65 - L66 were not covered by tests
resp, err := client.ListVPCIPAddresses(ctx, vpcID, linodego.NewListOptions(0, ""))
if err != nil {
if strings.Contains(err.Error(), "Not found") {
AshleyDumaine marked this conversation as resolved.
Show resolved Hide resolved
Mu.Lock()
defer Mu.Unlock()
klog.Errorf("vpc %s not found. Deleting entry from cache", vpcName)
delete(vpcIDs, vpcName)
}
return nil, err

Check warning on line 75 in cloud/linode/vpc.go

View check run for this annotation

Codecov / codecov/patch

cloud/linode/vpc.go#L69-L75

Added lines #L69 - L75 were not covered by tests
}
return resp, nil
}
Loading