Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulait committed Mar 11, 2024
1 parent cb872ad commit 2f2b569
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 25 deletions.
45 changes: 45 additions & 0 deletions cloud/linode/client/mock_client_test.go

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

34 changes: 16 additions & 18 deletions cloud/linode/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package linode
import (
"context"
"fmt"
"net"
"os"
"strconv"
"sync"
Expand Down Expand Up @@ -34,7 +33,7 @@ type nodeCache struct {
}

// getInstanceIPv4Addresses returns all ipv4 addresses configured on a linode.
func (nc *nodeCache) getInstanceIPv4Addresses(ctx context.Context, id int, client Client) ([]nodeIP, error) {
func (nc *nodeCache) getInstanceIPv4Addresses(ctx context.Context, id int, client client.Client) ([]nodeIP, error) {
// Retrieve ipaddresses for the linode
addresses, err := client.GetInstanceIPAddresses(ctx, id)
if err != nil {
Expand All @@ -43,29 +42,32 @@ func (nc *nodeCache) getInstanceIPv4Addresses(ctx context.Context, id int, clien

var ips []nodeIP
if len(addresses.IPv4.Public) != 0 {
ips = append(ips, nodeIP{ip: addresses.IPv4.Public[0].Address, ipType: v1.NodeExternalIP})
}
if len(addresses.IPv4.Private) != 0 {
ips = append(ips, nodeIP{ip: addresses.IPv4.Private[0].Address, ipType: v1.NodeInternalIP})
for _, ip := range addresses.IPv4.Public {
ips = append(ips, nodeIP{ip: ip.Address, ipType: v1.NodeExternalIP})
}
}

// Retrieve instance configs for the linode
configs, err := client.ListInstanceConfigs(ctx, id, &linodego.ListOptions{})
if err != nil {
if err != nil || len(configs) == 0 {
return nil, err
}

if len(configs) == 0 {
return ips, nil
}

// Iterate over interfaces in config and find VPC specific ips
for _, iface := range configs[0].Interfaces {
if iface.VPCID != nil && iface.IPv4.VPC != "" {
ips = append(ips, nodeIP{ip: iface.IPv4.VPC, ipType: v1.NodeInternalIP})
}
}

// NOTE: We specifically store VPC ips first so that if they exist, they are
// used as internal ip for the nodes than the private ip
if len(addresses.IPv4.Private) != 0 {
for _, ip := range addresses.IPv4.Private {
ips = append(ips, nodeIP{ip: ip.Address, ipType: v1.NodeInternalIP})
}
}

return ips, nil
}

Expand All @@ -88,7 +90,7 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)
wg := sync.WaitGroup{}
for _, instance := range instances {
wg.Add(1)
go func(ctx context.Context, instance linodego.Instance, client Client) {
go func(ctx context.Context, instance linodego.Instance) {
defer wg.Done()

addresses, err := nc.getInstanceIPv4Addresses(ctx, instance.ID, client)
Expand All @@ -100,7 +102,7 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)
defer nc.mtx.Unlock()
nc.nodes[instance.ID] = &instance
nc.ips[instance.ID] = addresses
}(ctx, instance, client)
}(ctx, instance)
}

wg.Wait()
Expand Down Expand Up @@ -253,11 +255,7 @@ func (i *instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloud
addresses := []v1.NodeAddress{{Type: v1.NodeHostName, Address: linode.Label}}

for _, ip := range ips {
addrType := v1.NodeExternalIP
if Options.LinodeNodePrivateSubnet.Contains(net.ParseIP(ip.ip)) {
addrType = v1.NodeInternalIP
}
addresses = append(addresses, v1.NodeAddress{Type: addrType, Address: ip.ip})
addresses = append(addresses, v1.NodeAddress{Type: ip.ipType, Address: ip.ip})
}

// note that Zone is omitted as it's not a thing in Linode
Expand Down
91 changes: 89 additions & 2 deletions cloud/linode/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ func TestInstanceExists(t *testing.T) {
Type: "g6-standard-2",
},
}, nil)
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), 123).Times(1).Return(&linodego.InstanceIPAddressResponse{
IPv4: &linodego.InstanceIPv4Response{
Public: []*linodego.InstanceIP{
{Address: "45.76.101.25"},
},
Private: []*linodego.InstanceIP{
{Address: "192.168.133.65"},
},
},
IPv6: nil,
}, nil)
client.EXPECT().ListInstanceConfigs(gomock.Any(), 123, gomock.Any()).Times(1).Return([]linodego.InstanceConfig{}, nil)

exists, err := instances.InstanceExists(ctx, node)
assert.NoError(t, err)
Expand All @@ -78,6 +90,18 @@ func TestInstanceExists(t *testing.T) {
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{
{ID: 123, Label: name},
}, nil)
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), 123).Times(1).Return(&linodego.InstanceIPAddressResponse{
IPv4: &linodego.InstanceIPv4Response{
Public: []*linodego.InstanceIP{
{Address: "45.76.101.25"},
},
Private: []*linodego.InstanceIP{
{Address: "192.168.133.65"},
},
},
IPv6: nil,
}, nil)
client.EXPECT().ListInstanceConfigs(gomock.Any(), 123, gomock.Any()).Times(1).Return([]linodego.InstanceConfig{}, nil)

exists, err := instances.InstanceExists(ctx, node)
assert.NoError(t, err)
Expand Down Expand Up @@ -127,13 +151,27 @@ func TestMetadataRetrieval(t *testing.T) {
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{
{ID: id, Label: name, Type: linodeType, Region: region, IPv4: []*net.IP{&publicIPv4, &privateIPv4}},
}, nil)
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
IPv4: &linodego.InstanceIPv4Response{
Public: []*linodego.InstanceIP{
{Address: "45.76.101.25"},
},
Private: []*linodego.InstanceIP{
{Address: "192.168.133.65"},
},
},
IPv6: nil,
}, nil)
client.EXPECT().ListInstanceConfigs(gomock.Any(), 123, gomock.Any()).Times(1).Return([]linodego.InstanceConfig{
{ID: 123456},
}, nil)

meta, err := instances.InstanceMetadata(ctx, node)
assert.NoError(t, err)
assert.Equal(t, providerIDPrefix+strconv.Itoa(id), meta.ProviderID)
assert.Equal(t, region, meta.Region)
assert.Equal(t, linodeType, meta.InstanceType)
assert.Equal(t, meta.NodeAddresses, []v1.NodeAddress{
assert.Equal(t, []v1.NodeAddress{
{
Type: v1.NodeHostName,
Address: name,
Expand All @@ -146,7 +184,7 @@ func TestMetadataRetrieval(t *testing.T) {
Type: v1.NodeInternalIP,
Address: privateIPv4.String(),
},
})
}, meta.NodeAddresses)
})

ipTests := []struct {
Expand Down Expand Up @@ -197,19 +235,38 @@ func TestMetadataRetrieval(t *testing.T) {
node := nodeWithProviderID(providerID)

ips := make([]*net.IP, 0, len(test.inputIPs))
pubIPs := make([]*linodego.InstanceIP, 0)
privIPs := make([]*linodego.InstanceIP, 0)
for _, ip := range test.inputIPs {
parsed := net.ParseIP(ip)
if parsed == nil {
t.Fatalf("cannot parse %v as an ipv4", ip)
}
ips = append(ips, &parsed)
if parsed.IsPrivate() {
privIPs = append(privIPs, &linodego.InstanceIP{Address: ip})
} else {
pubIPs = append(pubIPs, &linodego.InstanceIP{Address: ip})
}
}

ipv4s := &linodego.InstanceIPv4Response{
Public: pubIPs,
Private: privIPs,
}

linodeType := "g6-standard-1"
region := "us-east"
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{
{ID: id, Label: name, Type: linodeType, Region: region, IPv4: ips},
}, nil)
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
IPv4: ipv4s,
IPv6: nil,
}, nil)
client.EXPECT().ListInstanceConfigs(gomock.Any(), id, gomock.Any()).Times(1).Return([]linodego.InstanceConfig{
{ID: 123456},
}, nil)

meta, err := instances.InstanceMetadata(ctx, node)

Expand Down Expand Up @@ -281,6 +338,16 @@ func TestInstanceShutdown(t *testing.T) {
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{
{ID: id, Label: "offline-linode", Status: linodego.InstanceOffline},
}, nil)
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
IPv4: &linodego.InstanceIPv4Response{
Public: []*linodego.InstanceIP{},
Private: []*linodego.InstanceIP{},
},
IPv6: nil,
}, nil)
client.EXPECT().ListInstanceConfigs(gomock.Any(), id, gomock.Any()).Times(1).Return([]linodego.InstanceConfig{
{ID: 123456},
}, nil)
shutdown, err := instances.InstanceShutdown(ctx, node)

assert.NoError(t, err)
Expand All @@ -294,6 +361,16 @@ func TestInstanceShutdown(t *testing.T) {
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{
{ID: id, Label: "shutting-down-linode", Status: linodego.InstanceShuttingDown},
}, nil)
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
IPv4: &linodego.InstanceIPv4Response{
Public: []*linodego.InstanceIP{},
Private: []*linodego.InstanceIP{},
},
IPv6: nil,
}, nil)
client.EXPECT().ListInstanceConfigs(gomock.Any(), id, gomock.Any()).Times(1).Return([]linodego.InstanceConfig{
{ID: 123456},
}, nil)
shutdown, err := instances.InstanceShutdown(ctx, node)

assert.NoError(t, err)
Expand All @@ -307,6 +384,16 @@ func TestInstanceShutdown(t *testing.T) {
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{
{ID: id, Label: "running-linode", Status: linodego.InstanceRunning},
}, nil)
client.EXPECT().GetInstanceIPAddresses(gomock.Any(), id).Times(1).Return(&linodego.InstanceIPAddressResponse{
IPv4: &linodego.InstanceIPv4Response{
Public: []*linodego.InstanceIP{},
Private: []*linodego.InstanceIP{},
},
IPv6: nil,
}, nil)
client.EXPECT().ListInstanceConfigs(gomock.Any(), id, gomock.Any()).Times(1).Return([]linodego.InstanceConfig{
{ID: 123456},
}, nil)
shutdown, err := instances.InstanceShutdown(ctx, node)

assert.NoError(t, err)
Expand Down
8 changes: 5 additions & 3 deletions cloud/linode/route_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"k8s.io/apimachinery/pkg/types"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/klog/v2"

"github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
)

type routeCache struct {
Expand All @@ -25,7 +27,7 @@ type routeCache struct {
ttl time.Duration
}

func (rc *routeCache) refreshRoutes(ctx context.Context, client Client) error {
func (rc *routeCache) refreshRoutes(ctx context.Context, client client.Client) error {
rc.Lock()
defer rc.Unlock()

Expand Down Expand Up @@ -63,12 +65,12 @@ func (rc *routeCache) refreshRoutes(ctx context.Context, client Client) error {

type routes struct {
vpcid int
client Client
client client.Client
instances *instances
routeCache *routeCache
}

func newRoutes(client Client) (cloudprovider.Routes, error) {
func newRoutes(client client.Client) (cloudprovider.Routes, error) {
timeout := 60
if raw, ok := os.LookupEnv("LINODE_ROUTES_CACHE_TTL"); ok {
if t, _ := strconv.Atoi(raw); t > 0 {
Expand Down
3 changes: 2 additions & 1 deletion cloud/linode/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
"github.com/linode/linodego"
)

Expand All @@ -16,7 +17,7 @@ func (e vpcLookupError) Error() string {
}

// getVPCID returns the VPC id using the VPC label
func getVPCID(client Client, vpcName string) (int, error) {
func getVPCID(client client.Client, vpcName string) (int, error) {
vpcs, err := client.ListVPCs(context.TODO(), &linodego.ListOptions{})
if err != nil {
return 0, err
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func main() {
command.Flags().BoolVar(&linode.Options.LinodeGoDebug, "linodego-debug", false, "enables debug output for the LinodeAPI wrapper")
command.Flags().BoolVar(&linode.Options.EnableRouteController, "enable-route-controller", false, "enables route_controller for ccm")
command.Flags().StringVar(&linode.Options.VPCName, "vpc-name", "", "vpc name whose routes will be managed by route-controller")
command.Flags().IPNetVar(&linode.Options.LinodeNodePrivateSubnet, "linode-node-private-subnet", net.IPNet{IP: net.ParseIP("192.168.128.0"), Mask: net.CIDRMask(8, 32)}, "specifies private network used by k8s")
command.Flags().IPNetVar(&linode.Options.LinodeNodePrivateSubnet, "linode-node-private-subnet", net.IPNet{IP: net.ParseIP("192.168.128.0"), Mask: net.CIDRMask(17, 32)}, "specifies private network used by k8s")

// Set static flags
command.Flags().VisitAll(func(fl *pflag.Flag) {
Expand Down

0 comments on commit 2f2b569

Please sign in to comment.