Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulait committed Nov 13, 2024
1 parent 69a1bab commit 4618ff9
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 24 deletions.
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 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)
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
}
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
6 changes: 1 addition & 5 deletions cloud/linode/route_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,11 @@ func (rc *routeCache) refreshRoutes(ctx context.Context, client client.Client) e
if vpcName == "" {
continue
}
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
}
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)
}
Expand Down
75 changes: 69 additions & 6 deletions cloud/linode/route_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
)

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

nodeID := 123
Expand All @@ -36,7 +37,7 @@ func TestListRoutes(t *testing.T) {
assert.NoError(t, err)

client.EXPECT().ListInstances(gomock.Any(), gomock.Any()).Times(1).Return([]linodego.Instance{}, nil)
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPCIP{}, nil)
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(2).Return([]linodego.VPCIP{}, nil)
routes, err := routeController.ListRoutes(ctx, "test")
assert.NoError(t, err)
assert.Empty(t, routes)
Expand All @@ -59,7 +60,7 @@ func TestListRoutes(t *testing.T) {
assert.NoError(t, err)

client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{validInstance}, nil)
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPCIP{}, nil)
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(2).Return([]linodego.VPCIP{}, nil)
routes, err := routeController.ListRoutes(ctx, "test")
assert.NoError(t, err)
assert.Empty(t, routes)
Expand All @@ -85,7 +86,7 @@ func TestListRoutes(t *testing.T) {
assert.NoError(t, err)

client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{validInstance}, nil)
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(2).Return(noRoutesInVPC, nil)
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(4).Return(noRoutesInVPC, nil)
routes, err := routeController.ListRoutes(ctx, "test")
assert.NoError(t, err)
assert.Empty(t, routes)
Expand Down Expand Up @@ -126,7 +127,7 @@ func TestListRoutes(t *testing.T) {
assert.NoError(t, err)

client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{validInstance}, nil)
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(2).Return(routesInVPC, nil)
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(4).Return(routesInVPC, nil)
routes, err := routeController.ListRoutes(ctx, "test")
assert.NoError(t, err)
assert.NotEmpty(t, routes)
Expand Down Expand Up @@ -167,11 +168,73 @@ func TestListRoutes(t *testing.T) {
assert.NoError(t, err)

client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{validInstance}, nil)
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(2).Return(routesInDifferentVPC, nil)
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(4).Return(routesInDifferentVPC, nil)
routes, err := routeController.ListRoutes(ctx, "test")
assert.NoError(t, err)
assert.Empty(t, routes)
})

t.Run("should return routes if multiple instances exists, connected to VPCs and ip_ranges configured", func(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
routeController, err := newRoutes(client)
assert.NoError(t, err)

vpcIP2 := "10.0.0.3"
addressRange3 := "10.192.40.0/24"
addressRange4 := "10.192.50.0/24"

validInstance2 := linodego.Instance{
ID: 124,
Label: "mock-instance2",
Type: linodeType,
Region: region,
IPv4: []*net.IP{&publicIPv4, &privateIPv4},
}

routesInVPC2 := []linodego.VPCIP{
{
Address: &vpcIP2,
AddressRange: nil,
VPCID: vpcIDs["abc"],
NAT1To1: nil,
LinodeID: 124,
},
{
Address: nil,
AddressRange: &addressRange3,
VPCID: vpcIDs["abc"],
NAT1To1: nil,
LinodeID: 124,
},
{
Address: nil,
AddressRange: &addressRange4,
VPCID: vpcIDs["abc"],
NAT1To1: nil,
LinodeID: 124,
},
}

client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{validInstance, validInstance2}, nil)
c1 := client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return(routesInVPC, nil)
c2 := client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).After(c1).Times(1).Return(routesInVPC2, nil)
c3 := client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).After(c2).Times(1).Return(routesInVPC, nil)
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).After(c3).Times(1).Return(routesInVPC2, nil)
routes, err := routeController.ListRoutes(ctx, "test")
assert.NoError(t, err)
assert.NotEmpty(t, routes)
cidrs := make([]string, len(routes))
for i, value := range routes {
cidrs[i] = value.DestinationCIDR
}
assert.Contains(t, cidrs, addressRange1)
assert.Contains(t, cidrs, addressRange2)
assert.Contains(t, cidrs, addressRange3)
assert.Contains(t, cidrs, addressRange4)
})
}

func TestCreateRoute(t *testing.T) {
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 @@ package linode
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,15 +37,15 @@ func GetAllVPCIDs() []int {
}

// 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{})
vpcs, err := client.ListVPCs(ctx, &linodego.ListOptions{})
if err != nil {
return 0, err
}
Expand All @@ -55,3 +57,22 @@ func GetVPCID(client client.Client, vpcName string) (int, error) {
}
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
}
resp, err := client.ListVPCIPAddresses(ctx, vpcID, linodego.NewListOptions(0, ""))
if err != nil {
if strings.Contains(err.Error(), "Not found") {
Mu.Lock()
defer Mu.Unlock()
klog.Errorf("vpc %s not found. Deleting entry from cache", vpcName)
delete(vpcIDs, vpcName)
}
return nil, err
}
return resp, nil
}

0 comments on commit 4618ff9

Please sign in to comment.