diff --git a/cloud/linode/cloud_test.go b/cloud/linode/cloud_test.go index 1aee6492..27b8297a 100644 --- a/cloud/linode/cloud_test.go +++ b/cloud/linode/cloud_test.go @@ -195,7 +195,7 @@ 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 @@ -203,7 +203,7 @@ func TestNewCloud(t *testing.T) { 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") diff --git a/cloud/linode/fake_linode_test.go b/cloud/linode/fake_linode_test.go index ed844512..10b394ef 100644 --- a/cloud/linode/fake_linode_test.go +++ b/cloud/linode/fake_linode_test.go @@ -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{ diff --git a/cloud/linode/nodebalancer_backend_range_test.go b/cloud/linode/nodebalancer_backend_range_test.go index a1897f3c..3a60d7e4 100644 --- a/cloud/linode/nodebalancer_backend_range_test.go +++ b/cloud/linode/nodebalancer_backend_range_test.go @@ -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(). diff --git a/cloud/linode/route_controller_test.go b/cloud/linode/route_controller_test.go index 2d2b9321..c3c902d1 100644 --- a/cloud/linode/route_controller_test.go +++ b/cloud/linode/route_controller_test.go @@ -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 diff --git a/cloud/linode/services/vpc.go b/cloud/linode/services/vpc.go index 239df73d..97f8c0a4 100644 --- a/cloud/linode/services/vpc.go +++ b/cloud/linode/services/vpc.go @@ -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 } @@ -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() @@ -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 } } @@ -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) } diff --git a/cloud/linode/services/vpc_test.go b/cloud/linode/services/vpc_test.go index 73f9fb82..99c38ed0 100644 --- a/cloud/linode/services/vpc_test.go +++ b/cloud/linode/services/vpc_test.go @@ -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") }) } @@ -173,14 +173,14 @@ 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"}]) } }) @@ -188,14 +188,14 @@ 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{}, 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") }) @@ -203,7 +203,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{}, nil) got, err := GetSubnetID(t.Context(), client, 0, "test4") require.ErrorIs(t, err, subnetLookupError{"test4"}) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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} @@ -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} @@ -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"} diff --git a/cloud/nodeipam/ipam/cloud_allocator_test.go b/cloud/nodeipam/ipam/cloud_allocator_test.go index 1e324498..56931733 100644 --- a/cloud/nodeipam/ipam/cloud_allocator_test.go +++ b/cloud/nodeipam/ipam/cloud_allocator_test.go @@ -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