Skip to content
Open
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
4 changes: 2 additions & 2 deletions cloud/linode/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,15 @@ func TestNewCloud(t *testing.T) {
options.Options.VPCNames = []string{"vpc-test1", "vpc-test2"}
options.Options.NodeBalancerBackendIPv4SubnetName = "t1"
services.VpcIDs = map[string]int{"vpc-test1": 1, "vpc-test2": 2, "vpc-test3": 3}
services.SubnetIDs = map[string]int{"t1": 1, "t2": 2, "t3": 3}
services.SubnetIDs = map[services.SubnetKey]int{{VPCID: 1, Name: "t1"}: 1, {VPCID: 1, Name: "t2"}: 2, {VPCID: 1, Name: "t3"}: 3}
defer func() {
options.Options.LoadBalancerType = ""
options.Options.EnableRouteController = rtEnabled
options.Options.VPCNames = []string{}
options.Options.NodeBalancerBackendIPv4SubnetID = 0
options.Options.NodeBalancerBackendIPv4SubnetName = ""
services.VpcIDs = map[string]int{}
services.SubnetIDs = map[string]int{}
services.SubnetIDs = map[services.SubnetKey]int{}
}()
_, err := newCloud()
assert.Error(t, err, "expected error if incorrect loadbalancertype is set")
Expand Down
2 changes: 1 addition & 1 deletion cloud/linode/fake_linode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func newFake(t *testing.T) *fakeAPI {
t.Helper()
services.Mu.Lock()
services.VpcIDs = make(map[string]int)
services.SubnetIDs = make(map[string]int)
services.SubnetIDs = make(map[services.SubnetKey]int)
services.Mu.Unlock()

fake := &fakeAPI{
Expand Down
2 changes: 1 addition & 1 deletion cloud/linode/nodebalancer_backend_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestGetVPCCreateOptionsWithReservedBackendRange(t *testing.T) {

services.Mu.Lock()
services.VpcIDs = map[string]int{"test-vpc": 123}
services.SubnetIDs = map[string]int{"test-subnet": 456}
services.SubnetIDs = map[services.SubnetKey]int{{VPCID: 123, Name: "test-subnet"}: 456}
services.Mu.Unlock()

linodeClient.EXPECT().
Expand Down
3 changes: 2 additions & 1 deletion cloud/linode/route_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func TestListRoutes(t *testing.T) {
services.VpcIDs["test"] = 1
services.VpcIDs["abc"] = 2
options.Options.SubnetNames = []string{"default"}
services.SubnetIDs["default"] = 1
services.SubnetIDs[services.SubnetKey{VPCID: 1, Name: "default"}] = 1
services.SubnetIDs[services.SubnetKey{VPCID: 2, Name: "default"}] = 1
options.Options.EnableRouteController = true

nodeID := 123
Expand Down
22 changes: 15 additions & 7 deletions cloud/linode/services/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ var (
Mu sync.RWMutex
// VpcIDs map stores vpc id's for given vpc labels
VpcIDs = make(map[string]int, 0)
// SubnetIDs map stores subnet id's for given subnet labels
SubnetIDs = make(map[string]int, 0)
// SubnetIDs map stores subnet id's for given VPC ID and subnet label pairs
SubnetIDs = make(map[SubnetKey]int, 0)
)

// SubnetKey identifies a subnet by its parent VPC and label.
// Subnet labels are only unique within a VPC.
type SubnetKey struct {
VPCID int
Name string
}

type vpcLookupError struct {
value string
}
Expand Down Expand Up @@ -77,13 +84,14 @@ func GetVPCID(ctx context.Context, linodeClient client.Client, vpcName string) (
return 0, vpcLookupError{vpcName}
}

// GetSubnetID returns the subnet ID of given subnet label
// GetSubnetID returns the subnet ID of given subnet label within the given VPC
func GetSubnetID(ctx context.Context, linodeClient client.Client, vpcID int, subnetName string) (int, error) {
Mu.Lock()
defer Mu.Unlock()

// Check if map contains the id for the given label
if subnetid, ok := SubnetIDs[subnetName]; ok {
// Check if map contains the id for the given VPC and label
key := SubnetKey{VPCID: vpcID, Name: subnetName}
if subnetid, ok := SubnetIDs[key]; ok {
return subnetid, nil
}
// Otherwise, get it from linodego.ListVPCSubnets()
Expand All @@ -93,7 +101,7 @@ func GetSubnetID(ctx context.Context, linodeClient client.Client, vpcID int, sub
}
for i := range subnets {
if subnets[i].Label == subnetName {
SubnetIDs[subnetName] = subnets[i].ID
SubnetIDs[key] = subnets[i].ID
return subnets[i].ID, nil
}
}
Expand Down Expand Up @@ -251,7 +259,7 @@ func resolveSubnetNames(linodeClient client.Client, vpcID int) ([]string, error)
return nil, fmt.Errorf("failed to get subnet %d for VPC %d: %w", subnetID, vpcID, err)
}

SubnetIDs[subnet.Label] = subnet.ID
SubnetIDs[SubnetKey{VPCID: vpcID, Name: subnet.Label}] = subnet.ID
subnetNames = append(subnetNames, subnet.Label)
}

Expand Down
58 changes: 43 additions & 15 deletions cloud/linode/services/vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ func TestGetVPCIPAddresses(t *testing.T) {
defer func() { options.Options.SubnetNames = sn }()
options.Options.SubnetNames = []string{"subnet4"}
VpcIDs = map[string]int{"test1": 1}
SubnetIDs = map[string]int{"subnet1": 1}
SubnetIDs = map[SubnetKey]int{{VPCID: 1, Name: "subnet1"}: 1}
client.EXPECT().ListVPCs(gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPC{{ID: 10, Label: "test10"}}, nil)
client.EXPECT().ListVPCSubnets(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPCSubnet{{ID: 4, Label: "subnet4"}}, nil)
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPCIP{}, nil)
_, err := GetVPCIPAddresses(t.Context(), client, "test10")
require.NoError(t, err)
_, exists := SubnetIDs["subnet4"]
_, exists := SubnetIDs[SubnetKey{VPCID: 10, Name: "subnet4"}]
assert.True(t, exists, "subnet4 should be present in subnetIDs map")
})
}
Expand All @@ -173,37 +173,37 @@ func TestGetSubnetID(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
SubnetIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
SubnetIDs = map[SubnetKey]int{{Name: "test1"}: 1, {Name: "test2"}: 2, {Name: "test3"}: 3}
got, err := GetSubnetID(t.Context(), client, 0, "test3")
if err != nil {
t.Errorf("GetSubnetID() error = %v", err)
return
}
if got != SubnetIDs["test3"] {
t.Errorf("GetSubnetID() = %v, want %v", got, SubnetIDs["test3"])
if got != SubnetIDs[SubnetKey{Name: "test3"}] {
t.Errorf("GetSubnetID() = %v, want %v", got, SubnetIDs[SubnetKey{Name: "test3"}])
}
})

t.Run("subnetID not in cache and listVPCSubnets return error", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
SubnetIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
SubnetIDs = map[SubnetKey]int{{Name: "test1"}: 1, {Name: "test2"}: 2, {Name: "test3"}: 3}
client.EXPECT().ListVPCSubnets(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPCSubnet{}, errors.New("error"))
got, err := GetSubnetID(t.Context(), client, 0, "test4")
require.Error(t, err)
if got != 0 {
t.Errorf("GetSubnetID() = %v, want %v", got, 0)
}
_, exists := SubnetIDs["test4"]
_, exists := SubnetIDs[SubnetKey{Name: "test4"}]
assert.False(t, exists, "subnet4 should not be present in subnetIDs")
})

t.Run("subnetID not in cache and listVPCSubnets return nothing", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
SubnetIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
SubnetIDs = map[SubnetKey]int{{Name: "test1"}: 1, {Name: "test2"}: 2, {Name: "test3"}: 3}
client.EXPECT().ListVPCSubnets(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPCSubnet{}, nil)
got, err := GetSubnetID(t.Context(), client, 0, "test4")
require.ErrorIs(t, err, subnetLookupError{"test4"})
Expand All @@ -216,7 +216,7 @@ func TestGetSubnetID(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
SubnetIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
SubnetIDs = map[SubnetKey]int{{Name: "test1"}: 1, {Name: "test2"}: 2, {Name: "test3"}: 3}
client.EXPECT().ListVPCSubnets(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPCSubnet{{ID: 4, Label: "test4"}}, nil)
got, err := GetSubnetID(t.Context(), client, 0, "test4")
require.NoError(t, err)
Expand All @@ -226,6 +226,34 @@ func TestGetSubnetID(t *testing.T) {
})
}

func TestGetSubnetIDSameNameDifferentVPCs(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
prev := SubnetIDs
defer func() { SubnetIDs = prev }()
SubnetIDs = map[SubnetKey]int{}

client.EXPECT().ListVPCSubnets(gomock.Any(), 101, gomock.Any()).Times(1).Return([]linodego.VPCSubnet{{ID: 1001, Label: "default"}}, nil)
client.EXPECT().ListVPCSubnets(gomock.Any(), 202, gomock.Any()).Times(1).Return([]linodego.VPCSubnet{{ID: 2001, Label: "default"}}, nil)

got, err := GetSubnetID(t.Context(), client, 101, "default")
require.NoError(t, err)
assert.Equal(t, 1001, got)

got, err = GetSubnetID(t.Context(), client, 202, "default")
require.NoError(t, err)
assert.Equal(t, 2001, got, "subnet cached for another VPC must not be returned")

// both are now served from the cache
got, err = GetSubnetID(t.Context(), client, 101, "default")
require.NoError(t, err)
assert.Equal(t, 1001, got)
got, err = GetSubnetID(t.Context(), client, 202, "default")
require.NoError(t, err)
assert.Equal(t, 2001, got)
}

func TestGetNodeBalancerBackendIPv4SubnetID(t *testing.T) {
t.Run("VPC not found", func(t *testing.T) {
ctrl := gomock.NewController(t)
Expand All @@ -241,7 +269,7 @@ func TestGetNodeBalancerBackendIPv4SubnetID(t *testing.T) {
}()
options.Options.VPCNames = []string{"vpc-test1", "vpc-test2", "vpc-test3"}
VpcIDs = map[string]int{"vpc-test2": 2, "vpc-test3": 3}
SubnetIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
SubnetIDs = map[SubnetKey]int{{Name: "test1"}: 1, {Name: "test2"}: 2, {Name: "test3"}: 3}
client.EXPECT().ListVPCs(gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPC{}, errors.New("error"))
subnetID, err := GetNodeBalancerBackendIPv4SubnetID(client)
require.Error(t, err)
Expand All @@ -258,7 +286,7 @@ func TestGetNodeBalancerBackendIPv4SubnetID(t *testing.T) {
defer func() { options.Options.VPCNames = currVPCNames }()
options.Options.VPCNames = []string{"vpc-test1", "vpc-test2", "vpc-test3"}
VpcIDs = map[string]int{"vpc-test1": 1, "vpc-test2": 2, "vpc-test3": 3}
SubnetIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
SubnetIDs = map[SubnetKey]int{{Name: "test1"}: 1, {Name: "test2"}: 2, {Name: "test3"}: 3}
client.EXPECT().ListVPCSubnets(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPCSubnet{}, errors.New("error"))
subnetID, err := GetNodeBalancerBackendIPv4SubnetID(client)
require.Error(t, err)
Expand All @@ -280,7 +308,7 @@ func TestGetNodeBalancerBackendIPv4SubnetID(t *testing.T) {
options.Options.VPCNames = []string{"vpc-test1", "vpc-test2", "vpc-test3"}
options.Options.NodeBalancerBackendIPv4SubnetName = "test4"
VpcIDs = map[string]int{"vpc-test1": 1, "vpc-test2": 2, "vpc-test3": 3}
SubnetIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
SubnetIDs = map[SubnetKey]int{{Name: "test1"}: 1, {Name: "test2"}: 2, {Name: "test3"}: 3}
client.EXPECT().ListVPCSubnets(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPCSubnet{{ID: 4, Label: "test4"}}, nil)
subnetID, err := GetNodeBalancerBackendIPv4SubnetID(client)
require.NoError(t, err)
Expand Down Expand Up @@ -439,7 +467,7 @@ func Test_validateAndSetVPCSubnetFlags(t *testing.T) {
options.Options.VPCIDs = currVPCIDs
options.Options.SubnetIDs = currSubnetIDs
VpcIDs = map[string]int{}
SubnetIDs = map[string]int{}
SubnetIDs = map[SubnetKey]int{}
}()
options.Options.VPCIDs = []int{1}
options.Options.SubnetIDs = []int{1, 2}
Expand All @@ -459,7 +487,7 @@ func Test_validateAndSetVPCSubnetFlags(t *testing.T) {
options.Options.VPCIDs = currVPCIDs
options.Options.SubnetIDs = currSubnetIDs
VpcIDs = map[string]int{}
SubnetIDs = map[string]int{}
SubnetIDs = map[SubnetKey]int{}
}()
options.Options.VPCIDs = []int{1}
options.Options.SubnetIDs = []int{1, 2}
Expand All @@ -480,7 +508,7 @@ func Test_validateAndSetVPCSubnetFlags(t *testing.T) {
options.Options.VPCNames = currVPCNames
options.Options.SubnetNames = currSubnetNames
VpcIDs = map[string]int{}
SubnetIDs = map[string]int{}
SubnetIDs = map[SubnetKey]int{}
}()
options.Options.VPCNames = []string{"vpc1"}
options.Options.SubnetNames = []string{"subnet1", "subnet2"}
Expand Down
2 changes: 1 addition & 1 deletion cloud/nodeipam/ipam/cloud_allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func setNodeIPAMSubnetOptionsForTest(t *testing.T, vpcNames, subnetNames []strin
options.Options.VPCNames = vpcNames
options.Options.SubnetNames = subnetNames
services.VpcIDs = map[string]int{}
services.SubnetIDs = map[string]int{}
services.SubnetIDs = map[services.SubnetKey]int{}

t.Cleanup(func() {
options.Options.VPCNames = prevVPCNames
Expand Down
Loading