Skip to content

Commit

Permalink
chore: clients: consolidate Linode clients
Browse files Browse the repository at this point in the history
Consolidates the `Linode*Client` sub-clients into one generic interface
(`LinodeClient`).
  • Loading branch information
cbang-akamai committed May 9, 2024
1 parent 92df0b2 commit 4fb6483
Show file tree
Hide file tree
Showing 17 changed files with 330 additions and 267 deletions.
11 changes: 6 additions & 5 deletions clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
// LinodeClient is an interface that defines the methods that a Linode client must have to interact with Linode.
// It defines all the functions that are required to create, delete, and get resources
// from Linode such as object storage buckets, node balancers, linodes, and VPCs.
type LinodeMachineClient interface {
type LinodeClient interface {
LinodeNodeBalancerClient
LinodeInstanceClient
LinodeVPCClient
LinodeObjectStorageClient
}

// LinodeInstanceClient defines the methods that a Linode client must have to interact with Linode's Instance service.
// LinodeInstanceClient defines the methods that interact with Linode's Instance service.
type LinodeInstanceClient interface {
GetInstanceIPAddresses(ctx context.Context, linodeID int) (*linodego.InstanceIPAddressResponse, error)
ListInstances(ctx context.Context, opts *linodego.ListOptions) ([]linodego.Instance, error)
Expand All @@ -35,15 +36,15 @@ type LinodeInstanceClient interface {
ListStackscripts(ctx context.Context, opts *linodego.ListOptions) ([]linodego.Stackscript, error)
}

// LinodeVPCClient defines the methods that a Linode client must have to interact with Linode's VPC service.
// LinodeVPCClient defines the methods that interact with Linode's VPC service.
type LinodeVPCClient interface {
GetVPC(ctx context.Context, vpcID int) (*linodego.VPC, error)
ListVPCs(ctx context.Context, opts *linodego.ListOptions) ([]linodego.VPC, error)
CreateVPC(ctx context.Context, opts linodego.VPCCreateOptions) (*linodego.VPC, error)
DeleteVPC(ctx context.Context, vpcID int) error
}

// LinodeNodeBalancerClient defines the methods that a Linode client must have to interact with Linode's Node Balancer service.
// LinodeNodeBalancerClient defines the methods that interact with Linode's Node Balancer service.
type LinodeNodeBalancerClient interface {
ListNodeBalancers(ctx context.Context, opts *linodego.ListOptions) ([]linodego.NodeBalancer, error)
CreateNodeBalancer(ctx context.Context, opts linodego.NodeBalancerCreateOptions) (*linodego.NodeBalancer, error)
Expand All @@ -53,7 +54,7 @@ type LinodeNodeBalancerClient interface {
CreateNodeBalancerNode(ctx context.Context, nodebalancerID int, configID int, opts linodego.NodeBalancerNodeCreateOptions) (*linodego.NodeBalancerNode, error)
}

// LinodeObjectStorageClient defines the methods that a Linode client must have to interact with Linode's Object Storage service.
// LinodeObjectStorageClient defines the methods that interact with Linode's Object Storage service.
type LinodeObjectStorageClient interface {
GetObjectStorageBucket(ctx context.Context, cluster, label string) (*linodego.ObjectStorageBucket, error)
CreateObjectStorageBucket(ctx context.Context, opts linodego.ObjectStorageBucketCreateOptions) (*linodego.ObjectStorageBucket, error)
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func NewClusterScope(ctx context.Context, apiKey string, params ClusterScopePara
type ClusterScope struct {
Client K8sClient
PatchHelper *patch.Helper
LinodeClient LinodeNodeBalancerClient
LinodeClient LinodeClient
Cluster *clusterv1.Cluster
LinodeCluster *infrav1alpha1.LinodeCluster
}
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type MachineScope struct {
PatchHelper *patch.Helper
Cluster *clusterv1.Cluster
Machine *clusterv1.Machine
LinodeClient LinodeMachineClient
LinodeClient LinodeClient
LinodeCluster *infrav1alpha1.LinodeCluster
LinodeMachine *infrav1alpha1.LinodeMachine
}
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/object_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type ObjectStorageBucketScope struct {
Client K8sClient
Bucket *infrav1alpha1.LinodeObjectStorageBucket
Logger logr.Logger
LinodeClient LinodeObjectStorageClient
LinodeClient LinodeClient
PatchHelper *patch.Helper
}

Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/object_storage_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestNewObjectStorageBucketScope(t *testing.T) {
args args
expectedErr error
expects func(k8s *mock.MockK8sClient)
clientBuildFunc func(apiKey string) (LinodeObjectStorageClient, error)
clientBuildFunc func(apiKey string) (LinodeClient, error)
}{
{
name: "Success - Pass in valid args and get a valid ObjectStorageBucketScope",
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type VPCScope struct {
Client K8sClient

PatchHelper *patch.Helper
LinodeClient LinodeVPCClient
LinodeClient LinodeClient
LinodeVPC *infrav1alpha1.LinodeVPC
}

Expand Down
76 changes: 38 additions & 38 deletions cloud/services/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestCreateNodeBalancer(t *testing.T) {
tests := []struct {
name string
clusterScope *scope.ClusterScope
expects func(*mock.MockLinodeMachineClient)
expects func(*mock.MockLinodeClient)
expectedNodeBalancer *linodego.NodeBalancer
expectedError error
}{
Expand All @@ -42,7 +42,7 @@ func TestCreateNodeBalancer(t *testing.T) {
},
},
},
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().ListNodeBalancers(gomock.Any(), gomock.Any()).Return([]linodego.NodeBalancer{}, nil)
mockClient.EXPECT().CreateNodeBalancer(gomock.Any(), gomock.Any()).Return(&linodego.NodeBalancer{
ID: 1234,
Expand All @@ -67,7 +67,7 @@ func TestCreateNodeBalancer(t *testing.T) {
},
},
},
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().ListNodeBalancers(gomock.Any(), gomock.Any()).Return([]linodego.NodeBalancer{
{
ID: 1234,
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestCreateNodeBalancer(t *testing.T) {
},
},
},
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().ListNodeBalancers(gomock.Any(), gomock.Any()).Return([]linodego.NodeBalancer{
{
ID: 1234,
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestCreateNodeBalancer(t *testing.T) {
},
},
},
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().ListNodeBalancers(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("Unable to list NodeBalancers"))
},
expectedError: fmt.Errorf("Unable to list NodeBalancers"),
Expand All @@ -148,7 +148,7 @@ func TestCreateNodeBalancer(t *testing.T) {
},
},
},
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().ListNodeBalancers(gomock.Any(), gomock.Any()).Return([]linodego.NodeBalancer{}, nil)
mockClient.EXPECT().CreateNodeBalancer(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("Unable to create NodeBalancer"))
},
Expand All @@ -163,11 +163,11 @@ func TestCreateNodeBalancer(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

MockLinodeMachineClient := mock.NewMockLinodeMachineClient(ctrl)
MockLinodeClient := mock.NewMockLinodeClient(ctrl)

testcase.clusterScope.LinodeClient = MockLinodeMachineClient
testcase.clusterScope.LinodeClient = MockLinodeClient

testcase.expects(MockLinodeMachineClient)
testcase.expects(MockLinodeClient)

got, err := CreateNodeBalancer(context.Background(), testcase.clusterScope, logr.Discard())
if testcase.expectedError != nil {
Expand All @@ -188,7 +188,7 @@ func TestCreateNodeBalancerConfig(t *testing.T) {
clusterScope *scope.ClusterScope
expectedConfig *linodego.NodeBalancerConfig
expectedError error
expects func(*mock.MockLinodeMachineClient)
expects func(*mock.MockLinodeClient)
}{
{
name: "Success - Create NodeBalancerConfig using default LB port",
Expand All @@ -213,7 +213,7 @@ func TestCreateNodeBalancerConfig(t *testing.T) {
Check: linodego.CheckConnection,
NodeBalancerID: 1234,
},
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().CreateNodeBalancerConfig(gomock.Any(), gomock.Any(), gomock.Any()).Return(&linodego.NodeBalancerConfig{
Port: defaultLBPort,
Protocol: linodego.ProtocolTCP,
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestCreateNodeBalancerConfig(t *testing.T) {
Check: linodego.CheckConnection,
NodeBalancerID: 1234,
},
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().CreateNodeBalancerConfig(gomock.Any(), gomock.Any(), gomock.Any()).Return(&linodego.NodeBalancerConfig{
Port: 80,
Protocol: linodego.ProtocolTCP,
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestCreateNodeBalancerConfig(t *testing.T) {
NodeBalancerID: 1234,
},
expectedError: fmt.Errorf("error creating NodeBalancerConfig"),
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().CreateNodeBalancerConfig(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("error creating NodeBalancerConfig"))
},
},
Expand All @@ -294,11 +294,11 @@ func TestCreateNodeBalancerConfig(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

MockLinodeMachineClient := mock.NewMockLinodeMachineClient(ctrl)
MockLinodeClient := mock.NewMockLinodeClient(ctrl)

testcase.clusterScope.LinodeClient = MockLinodeMachineClient
testcase.clusterScope.LinodeClient = MockLinodeClient

testcase.expects(MockLinodeMachineClient)
testcase.expects(MockLinodeClient)

got, err := CreateNodeBalancerConfig(context.Background(), testcase.clusterScope, logr.Discard())
if testcase.expectedError != nil {
Expand All @@ -318,7 +318,7 @@ func TestAddNodeToNBConditions(t *testing.T) {
name string
machineScope *scope.MachineScope
expectedError error
expects func(*mock.MockLinodeMachineClient)
expects func(*mock.MockLinodeClient)
}{
{
name: "Error - NodeBalancerConfigID are is set",
Expand Down Expand Up @@ -356,7 +356,7 @@ func TestAddNodeToNBConditions(t *testing.T) {
},
},
expectedError: fmt.Errorf("nil NodeBalancer Config ID"),
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().GetInstanceIPAddresses(gomock.Any(), gomock.Any()).Return(&linodego.InstanceIPAddressResponse{
IPv4: &linodego.InstanceIPv4Response{
Private: []*linodego.InstanceIP{
Expand Down Expand Up @@ -391,7 +391,7 @@ func TestAddNodeToNBConditions(t *testing.T) {
},
},
expectedError: fmt.Errorf("no private IP address"),
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().GetInstanceIPAddresses(gomock.Any(), gomock.Any()).Return(&linodego.InstanceIPAddressResponse{
IPv4: &linodego.InstanceIPv4Response{
Private: []*linodego.InstanceIP{},
Expand Down Expand Up @@ -422,7 +422,7 @@ func TestAddNodeToNBConditions(t *testing.T) {
},
},
expectedError: fmt.Errorf("could not get instance IP addresses"),
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().GetInstanceIPAddresses(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("could not get instance IP addresses"))
},
},
Expand All @@ -434,11 +434,11 @@ func TestAddNodeToNBConditions(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

MockLinodeMachineClient := mock.NewMockLinodeMachineClient(ctrl)
MockLinodeClient := mock.NewMockLinodeClient(ctrl)

testcase.machineScope.LinodeClient = MockLinodeMachineClient
testcase.machineScope.LinodeClient = MockLinodeClient

testcase.expects(MockLinodeMachineClient)
testcase.expects(MockLinodeClient)

err := AddNodeToNB(context.Background(), logr.Discard(), testcase.machineScope)
if testcase.expectedError != nil {
Expand All @@ -455,7 +455,7 @@ func TestAddNodeToNBFullWorkflow(t *testing.T) {
name string
machineScope *scope.MachineScope
expectedError error
expects func(*mock.MockLinodeMachineClient)
expects func(*mock.MockLinodeClient)
}{
{
name: "If the machine is not a control plane node, do nothing",
Expand All @@ -473,7 +473,7 @@ func TestAddNodeToNBFullWorkflow(t *testing.T) {
},
},
},
expects: func(*mock.MockLinodeMachineClient) {},
expects: func(*mock.MockLinodeClient) {},
},
{
name: "Success - If the machine is a control plane node, add the node to the NodeBalancer",
Expand Down Expand Up @@ -515,7 +515,7 @@ func TestAddNodeToNBFullWorkflow(t *testing.T) {
},
},
},
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().GetInstanceIPAddresses(gomock.Any(), gomock.Any()).Return(&linodego.InstanceIPAddressResponse{
IPv4: &linodego.InstanceIPv4Response{
Private: []*linodego.InstanceIP{
Expand Down Expand Up @@ -569,7 +569,7 @@ func TestAddNodeToNBFullWorkflow(t *testing.T) {
},
},
expectedError: fmt.Errorf("could not create node balancer node"),
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().GetInstanceIPAddresses(gomock.Any(), gomock.Any()).Return(&linodego.InstanceIPAddressResponse{
IPv4: &linodego.InstanceIPv4Response{
Private: []*linodego.InstanceIP{
Expand All @@ -590,11 +590,11 @@ func TestAddNodeToNBFullWorkflow(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

MockLinodeMachineClient := mock.NewMockLinodeMachineClient(ctrl)
MockLinodeClient := mock.NewMockLinodeClient(ctrl)

testcase.machineScope.LinodeClient = MockLinodeMachineClient
testcase.machineScope.LinodeClient = MockLinodeClient

testcase.expects(MockLinodeMachineClient)
testcase.expects(MockLinodeClient)

err := AddNodeToNB(context.Background(), logr.Discard(), testcase.machineScope)
if testcase.expectedError != nil {
Expand All @@ -611,7 +611,7 @@ func TestDeleteNodeFromNB(t *testing.T) {
name string
machineScope *scope.MachineScope
expectedError error
expects func(*mock.MockLinodeMachineClient)
expects func(*mock.MockLinodeClient)
}{
// TODO: Add test cases.
{
Expand All @@ -630,7 +630,7 @@ func TestDeleteNodeFromNB(t *testing.T) {
},
},
},
expects: func(*mock.MockLinodeMachineClient) {},
expects: func(*mock.MockLinodeClient) {},
},
{
name: "NodeBalancer is already deleted",
Expand Down Expand Up @@ -663,7 +663,7 @@ func TestDeleteNodeFromNB(t *testing.T) {
},
},
},
expects: func(*mock.MockLinodeMachineClient) {},
expects: func(*mock.MockLinodeClient) {},
},
{
name: "Success - Delete Node from NodeBalancer",
Expand Down Expand Up @@ -700,7 +700,7 @@ func TestDeleteNodeFromNB(t *testing.T) {
},
},
},
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().DeleteNodeBalancerNode(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
},
},
Expand Down Expand Up @@ -740,7 +740,7 @@ func TestDeleteNodeFromNB(t *testing.T) {
},
},
expectedError: fmt.Errorf("error deleting node from NodeBalancer"),
expects: func(mockClient *mock.MockLinodeMachineClient) {
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().DeleteNodeBalancerNode(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf("error deleting node from NodeBalancer"))
},
},
Expand All @@ -753,11 +753,11 @@ func TestDeleteNodeFromNB(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

MockLinodeMachineClient := mock.NewMockLinodeMachineClient(ctrl)
MockLinodeClient := mock.NewMockLinodeClient(ctrl)

testcase.machineScope.LinodeClient = MockLinodeMachineClient
testcase.machineScope.LinodeClient = MockLinodeClient

testcase.expects(MockLinodeMachineClient)
testcase.expects(MockLinodeClient)

err := DeleteNodeFromNB(context.Background(), logr.Discard(), testcase.machineScope)
if testcase.expectedError != nil {
Expand Down
Loading

0 comments on commit 4fb6483

Please sign in to comment.