Skip to content
Merged
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
2 changes: 2 additions & 0 deletions cloud/linode/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type Client interface {

UpdateInstanceConfigInterface(context.Context, int, int, int, linodego.InstanceConfigInterfaceUpdateOptions) (*linodego.InstanceConfigInterface, error)

GetVPC(context.Context, int) (*linodego.VPC, error)
GetVPCSubnet(context.Context, int, int) (*linodego.VPCSubnet, error)
ListVPCs(context.Context, *linodego.ListOptions) ([]linodego.VPC, error)
ListVPCIPAddresses(context.Context, int, *linodego.ListOptions) ([]linodego.VPCIP, error)
ListVPCSubnets(context.Context, int, *linodego.ListOptions) ([]linodego.VPCSubnet, error)
Expand Down
26 changes: 26 additions & 0 deletions cloud/linode/client/client_with_metrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions cloud/linode/client/mocks/mock_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions cloud/linode/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"io"
"net"
"os"
"regexp"
"strconv"
"time"
"regexp"

"github.com/spf13/pflag"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -39,8 +39,10 @@ var Options struct {
LinodeGoDebug bool
EnableRouteController bool
EnableTokenHealthChecker bool
VPCNames string
SubnetNames string
VPCNames []string
VPCIDs []int
SubnetNames []string
SubnetIDs []int
LoadBalancerType string
BGPNodeSelector string
IpHolderSuffix string
Expand Down Expand Up @@ -139,10 +141,9 @@ func newCloud() (cloudprovider.Interface, error) {
healthChecker = newHealthChecker(linodeClient, tokenHealthCheckPeriod, Options.GlobalStopChannel)
}

// SubnetNames can't be used without VPCNames also being set
if Options.SubnetNames != "" && Options.VPCNames == "" {
klog.Warningf("failed to set flag subnet-names: vpc-names must be set to a non-empty value")
Options.SubnetNames = ""
err = validateAndSetVPCSubnetFlags(linodeClient)
if err != nil {
return nil, fmt.Errorf("failed to validate VPC and subnet flags: %w", err)
}

if Options.NodeBalancerBackendIPv4SubnetID != 0 && Options.NodeBalancerBackendIPv4SubnetName != "" {
Expand Down
12 changes: 6 additions & 6 deletions cloud/linode/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func TestNewCloudRouteControllerDisabled(t *testing.T) {
Options.NodeBalancerPrefix = "ccm"

t.Run("should not fail if vpc is empty and routecontroller is disabled", func(t *testing.T) {
Options.VPCNames = ""
Options.VPCNames = []string{}
Options.EnableRouteController = false
_, err := newCloud()
assert.NoError(t, err)
})

t.Run("fail if vpcname is empty and routecontroller is enabled", func(t *testing.T) {
Options.VPCNames = ""
Options.VPCNames = []string{}
Options.EnableRouteController = true
_, err := newCloud()
assert.Error(t, err)
Expand Down Expand Up @@ -61,11 +61,11 @@ func TestNewCloud(t *testing.T) {
})

t.Run("should fail if both nodeBalancerBackendIPv4SubnetID and nodeBalancerBackendIPv4SubnetName are set", func(t *testing.T) {
Options.VPCNames = "tt"
Options.VPCNames = []string{"tt"}
Options.NodeBalancerBackendIPv4SubnetID = 12345
Options.NodeBalancerBackendIPv4SubnetName = "test-subnet"
defer func() {
Options.VPCNames = ""
Options.VPCNames = []string{}
Options.NodeBalancerBackendIPv4SubnetID = 0
Options.NodeBalancerBackendIPv4SubnetName = ""
}()
Expand All @@ -77,14 +77,14 @@ func TestNewCloud(t *testing.T) {
rtEnabled := Options.EnableRouteController
Options.EnableRouteController = false
Options.LoadBalancerType = "test"
Options.VPCNames = "vpc-test1,vpc-test2"
Options.VPCNames = []string{"vpc-test1", "vpc-test2"}
Options.NodeBalancerBackendIPv4SubnetName = "t1"
vpcIDs = map[string]int{"vpc-test1": 1, "vpc-test2": 2, "vpc-test3": 3}
subnetIDs = map[string]int{"t1": 1, "t2": 2, "t3": 3}
defer func() {
Options.LoadBalancerType = ""
Options.EnableRouteController = rtEnabled
Options.VPCNames = ""
Options.VPCNames = []string{}
Options.NodeBalancerBackendIPv4SubnetID = 0
Options.NodeBalancerBackendIPv4SubnetName = ""
vpcIDs = map[string]int{}
Expand Down
7 changes: 3 additions & 4 deletions cloud/linode/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)

// If running within VPC, find instances and store their ips
vpcNodes := map[int][]string{}
vpcNames := strings.Split(Options.VPCNames, ",")
for _, v := range vpcNames {
vpcName := strings.TrimSpace(v)
for _, name := range Options.VPCNames {
vpcName := strings.TrimSpace(name)
if vpcName == "" {
continue
}
Expand All @@ -102,7 +101,7 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)
newNodes := make(map[int]linodeInstance, len(instances))
for index, instance := range instances {
// if running within VPC, only store instances in cache which are part of VPC
if Options.VPCNames != "" && len(vpcNodes[instance.ID]) == 0 {
if len(Options.VPCNames) > 0 && len(vpcNodes[instance.ID]) == 0 {
continue
}
node := linodeInstance{
Expand Down
4 changes: 2 additions & 2 deletions cloud/linode/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func TestMetadataRetrieval(t *testing.T) {
ipv6Addr := "2001::8a2e:370:7348"
linodeType := typeG6

Options.VPCNames = "test"
Options.VPCNames = []string{"test"}
vpcIDs["test"] = 1
Options.EnableRouteController = true

Expand Down Expand Up @@ -229,7 +229,7 @@ func TestMetadataRetrieval(t *testing.T) {
},
}, meta.NodeAddresses)

Options.VPCNames = ""
Options.VPCNames = []string{}
})

ipTests := []struct {
Expand Down
12 changes: 6 additions & 6 deletions cloud/linode/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func (l *loadbalancers) updateNodeBalancer(
return err
}
}
if Options.VPCNames != "" && !Options.DisableNodeBalancerVPCBackends {
if len(Options.VPCNames) > 0 && !Options.DisableNodeBalancerVPCBackends {
var id int
id, err = l.getSubnetIDForSVC(ctx, service)
if err != nil {
Expand Down Expand Up @@ -831,7 +831,7 @@ func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName stri
Type: nbType,
}

if Options.VPCNames != "" && !Options.DisableNodeBalancerVPCBackends {
if len(Options.VPCNames) > 0 && !Options.DisableNodeBalancerVPCBackends {
createOpts.VPCs, err = l.getVPCCreateOptions(ctx, service)
if err != nil {
return nil, err
Expand Down Expand Up @@ -971,7 +971,7 @@ func (l *loadbalancers) addTLSCert(ctx context.Context, service *v1.Service, nbC
// 3. If CCM is configured with --nodebalancer-backend-ipv4-subnet-id, it will be used as the subnet ID.
// 4. Else, use first VPCName and SubnetName to calculate subnet id for the service.
func (l *loadbalancers) getSubnetIDForSVC(ctx context.Context, service *v1.Service) (int, error) {
if Options.VPCNames == "" {
if len(Options.VPCNames) == 0 {
return 0, fmt.Errorf("CCM not configured with VPC, cannot create NodeBalancer with specified annotation")
}
// Check if the service has an annotation for NodeBalancerBackendSubnetID
Expand All @@ -992,7 +992,7 @@ func (l *loadbalancers) getSubnetIDForSVC(ctx context.Context, service *v1.Servi
return Options.NodeBalancerBackendIPv4SubnetID, nil
}

vpcName := strings.Split(Options.VPCNames, ",")[0]
vpcName := Options.VPCNames[0]
if vpcOk {
vpcName = specifiedVPCName
}
Expand All @@ -1001,7 +1001,7 @@ func (l *loadbalancers) getSubnetIDForSVC(ctx context.Context, service *v1.Servi
return 0, err
}

subnetName := strings.Split(Options.SubnetNames, ",")[0]
subnetName := Options.SubnetNames[0]
if subnetOk {
subnetName = specifiedSubnetName
}
Expand Down Expand Up @@ -1030,7 +1030,7 @@ func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, clusterNam
return nil, err
}
}
if Options.VPCNames != "" && !Options.DisableNodeBalancerVPCBackends {
if len(Options.VPCNames) > 0 && !Options.DisableNodeBalancerVPCBackends {
id, err := l.getSubnetIDForSVC(ctx, service)
if err != nil {
return nil, err
Expand Down
29 changes: 16 additions & 13 deletions cloud/linode/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,8 @@ func testCreateNodeBalancerWithVPCBackend(t *testing.T, client *linodego.Client,
Options.VPCNames = vpcNames
Options.SubnetNames = subnetNames
}()
Options.VPCNames = "test1"
Options.SubnetNames = defaultSubnet
Options.VPCNames = []string{"test1"}
Options.SubnetNames = []string{defaultSubnet}
_, _ = client.CreateVPC(t.Context(), linodego.VPCCreateOptions{
Label: "test1",
Description: "",
Expand Down Expand Up @@ -669,8 +669,8 @@ func testUpdateNodeBalancerWithVPCBackend(t *testing.T, client *linodego.Client,
Options.VPCNames = vpcNames
Options.SubnetNames = subnetNames
}()
Options.VPCNames = "test1"
Options.SubnetNames = defaultSubnet
Options.VPCNames = []string{"test1"}
Options.SubnetNames = []string{defaultSubnet}
_, _ = client.CreateVPC(t.Context(), linodego.VPCCreateOptions{
Label: "test1",
Description: "",
Expand Down Expand Up @@ -757,8 +757,8 @@ func testCreateNodeBalancerWithVPCOnlySubnetFlag(t *testing.T, client *linodego.
Options.SubnetNames = subnetNames
Options.NodeBalancerBackendIPv4Subnet = nbBackendSubnet
}()
Options.VPCNames = "test-subflag"
Options.SubnetNames = defaultSubnet
Options.VPCNames = []string{"test-subflag"}
Options.SubnetNames = []string{defaultSubnet}
Options.NodeBalancerBackendIPv4Subnet = "10.254.0.0/24"
_, _ = client.CreateVPC(t.Context(), linodego.VPCCreateOptions{
Label: "test-subflag",
Expand Down Expand Up @@ -851,8 +851,8 @@ func testCreateNodeBalancerWithVPCNoFlagOrAnnotation(t *testing.T, client *linod
Options.VPCNames = vpcNames
Options.SubnetNames = subnetNames
}()
Options.VPCNames = "test-noflags"
Options.SubnetNames = defaultSubnet
Options.VPCNames = []string{"test-noflags"}
Options.SubnetNames = []string{defaultSubnet}
_, _ = client.CreateVPC(t.Context(), linodego.VPCCreateOptions{
Label: "test-noflags",
Description: "",
Expand Down Expand Up @@ -934,8 +934,8 @@ func testCreateNodeBalancerWithVPCAnnotationOnly(t *testing.T, client *linodego.
Options.VPCNames = vpcNames
Options.SubnetNames = subnetNames
}()
Options.VPCNames = "test-onlyannotation"
Options.SubnetNames = defaultSubnet
Options.VPCNames = []string{"test-onlyannotation"}
Options.SubnetNames = []string{defaultSubnet}
_, _ = client.CreateVPC(t.Context(), linodego.VPCCreateOptions{
Label: "test-onlyannotation",
Description: "",
Expand Down Expand Up @@ -1024,8 +1024,8 @@ func testCreateNodeBalancerWithVPCOnlySubnetIDFlag(t *testing.T, client *linodeg
Options.SubnetNames = subnetNames
Options.NodeBalancerBackendIPv4SubnetID = nbBackendSubnetID
}()
Options.VPCNames = "test1"
Options.SubnetNames = defaultSubnet
Options.VPCNames = []string{"test1"}
Options.SubnetNames = []string{defaultSubnet}
Options.NodeBalancerBackendIPv4SubnetID = 1111
_, _ = client.CreateVPC(t.Context(), linodego.VPCCreateOptions{
Label: "test-subid-flag",
Expand Down Expand Up @@ -1105,12 +1105,15 @@ func testCreateNodeBalancerWithVPCAnnotationOverwrite(t *testing.T, client *lino

// provision multiple vpcs
vpcNames := Options.VPCNames
subnetNames := Options.SubnetNames
nodebalancerBackendIPv4Subnet := Options.NodeBalancerBackendIPv4Subnet
defer func() {
Options.VPCNames = vpcNames
Options.SubnetNames = subnetNames
Options.NodeBalancerBackendIPv4Subnet = nodebalancerBackendIPv4Subnet
}()
Options.VPCNames = "test1"
Options.VPCNames = []string{"test1"}
Options.SubnetNames = []string{defaultSubnet}
Options.NodeBalancerBackendIPv4Subnet = "10.100.0.0/24"

_, _ = client.CreateVPC(t.Context(), linodego.VPCCreateOptions{
Expand Down
5 changes: 2 additions & 3 deletions cloud/linode/route_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ func (rc *routeCache) refreshRoutes(ctx context.Context, client client.Client) {
}

vpcNodes := map[int][]linodego.VPCIP{}
vpcNames := strings.Split(Options.VPCNames, ",")
for _, v := range vpcNames {
for _, v := range Options.VPCNames {
vpcName := strings.TrimSpace(v)
if vpcName == "" {
continue
Expand Down Expand Up @@ -74,7 +73,7 @@ func newRoutes(client client.Client, instanceCache *instances) (cloudprovider.Ro
}
klog.V(3).Infof("TTL for routeCache set to %d seconds", timeout)

if Options.EnableRouteController && Options.VPCNames == "" {
if Options.EnableRouteController && len(Options.VPCNames) == 0 {
return nil, fmt.Errorf("cannot enable route controller as vpc-names is empty")
}

Expand Down
8 changes: 5 additions & 3 deletions cloud/linode/route_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import (
)

func TestListRoutes(t *testing.T) {
Options.VPCNames = "test,abc"
Options.VPCNames = []string{"test", "abc"}
vpcIDs["test"] = 1
vpcIDs["abc"] = 2
Options.SubnetNames = []string{"default"}
subnetIDs["default"] = 1
Options.EnableRouteController = true

nodeID := 123
Expand Down Expand Up @@ -312,7 +314,7 @@ func TestListRoutes(t *testing.T) {

func TestCreateRoute(t *testing.T) {
ctx := t.Context()
Options.VPCNames = "dummy"
Options.VPCNames = []string{"dummy"}
vpcIDs["dummy"] = 1
Options.EnableRouteController = true

Expand Down Expand Up @@ -463,7 +465,7 @@ func TestCreateRoute(t *testing.T) {
}

func TestDeleteRoute(t *testing.T) {
Options.VPCNames = "dummy"
Options.VPCNames = []string{"dummy"}
vpcIDs["dummy"] = 1
Options.EnableRouteController = true

Expand Down
Loading
Loading