diff --git a/cloud/linode/services/vpc.go b/cloud/linode/services/vpc.go index 239df73d..cacb3433 100644 --- a/cloud/linode/services/vpc.go +++ b/cloud/linode/services/vpc.go @@ -184,6 +184,9 @@ func GetVPCIPv6Addresses(ctx context.Context, linodeClient client.Client, vpcNam // It uses the first VPC name from Options.VPCNames to find the VPC ID and then retrieves the subnet ID // for the NodeBalancer backend IPv4 subnet name specified in Options.NodeBalancerBackendIPv4SubnetName. func GetNodeBalancerBackendIPv4SubnetID(linodeClient client.Client) (int, error) { + if len(options.Options.VPCNames) == 0 { + return 0, fmt.Errorf("--nodebalancer-backend-ipv4-subnet-name %q requires --vpc-names or --vpc-ids to be set", options.Options.NodeBalancerBackendIPv4SubnetName) + } // Get the VPC ID from the name vpcID, err := GetVPCID(context.TODO(), linodeClient, options.Options.VPCNames[0]) if err != nil { diff --git a/cloud/linode/services/vpc_test.go b/cloud/linode/services/vpc_test.go index 73f9fb82..0dbc5cec 100644 --- a/cloud/linode/services/vpc_test.go +++ b/cloud/linode/services/vpc_test.go @@ -227,6 +227,25 @@ func TestGetSubnetID(t *testing.T) { } func TestGetNodeBalancerBackendIPv4SubnetID(t *testing.T) { + t.Run("No VPC configured", func(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + client := mocks.NewMockClient(ctrl) + currVPCNames := options.Options.VPCNames + currNodeBalancerBackendIPv4SubnetName := options.Options.NodeBalancerBackendIPv4SubnetName + defer func() { + options.Options.VPCNames = currVPCNames + options.Options.NodeBalancerBackendIPv4SubnetName = currNodeBalancerBackendIPv4SubnetName + }() + options.Options.VPCNames = nil + options.Options.NodeBalancerBackendIPv4SubnetName = "nb-backends" + subnetID, err := GetNodeBalancerBackendIPv4SubnetID(client) + require.ErrorContains(t, err, "requires --vpc-names or --vpc-ids") + if subnetID != 0 { + t.Errorf("getNodeBalancerBackendIPv4SubnetID() = %v, want %v", subnetID, 0) + } + }) + t.Run("VPC not found", func(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish()