Skip to content

Commit 79504e9

Browse files
committed
make package for linode services
1 parent c0300b8 commit 79504e9

17 files changed

+99
-100
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fmt:
8080
.PHONY: test
8181
# we say code is not worth testing unless it's formatted
8282
test: fmt codegen
83-
go test -v -coverpkg=./sentry,./cloud/linode/client,./cloud/linode/firewall,./cloud/linode,./cloud/linode/utils,./cloud/linode/cache,./cloud/nodeipam,./cloud/nodeipam/ipam -coverprofile ./coverage.out -cover ./sentry/... ./cloud/... $(TEST_ARGS)
83+
go test -v -coverpkg=./sentry,./cloud/linode/client,./cloud/linode,./cloud/linode/utils,./cloud/linode/services,./cloud/nodeipam,./cloud/nodeipam/ipam -coverprofile ./coverage.out -cover ./sentry/... ./cloud/... $(TEST_ARGS)
8484

8585
.PHONY: build-linux
8686
build-linux: codegen

cloud/linode/cloud.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
cloudprovider "k8s.io/cloud-provider"
1515
"k8s.io/klog/v2"
1616

17-
"github.com/linode/linode-cloud-controller-manager/cloud/linode/cache"
1817
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
1918
"github.com/linode/linode-cloud-controller-manager/cloud/linode/options"
19+
"github.com/linode/linode-cloud-controller-manager/cloud/linode/services"
2020
)
2121

2222
const (
@@ -40,7 +40,7 @@ type linodeCloud struct {
4040
}
4141

4242
var (
43-
instanceCache *cache.Instances
43+
instanceCache *services.Instances
4444
ipHolderCharLimit int = 23
4545
NodeBalancerPrefixCharLimit int = 19
4646
)
@@ -110,7 +110,7 @@ func newCloud() (cloudprovider.Interface, error) {
110110
healthChecker = newHealthChecker(linodeClient, tokenHealthCheckPeriod, options.Options.GlobalStopChannel)
111111
}
112112

113-
err = cache.ValidateAndSetVPCSubnetFlags(linodeClient)
113+
err = services.ValidateAndSetVPCSubnetFlags(linodeClient)
114114
if err != nil {
115115
return nil, fmt.Errorf("failed to validate VPC and subnet flags: %w", err)
116116
}
@@ -124,14 +124,14 @@ func newCloud() (cloudprovider.Interface, error) {
124124
options.Options.NodeBalancerBackendIPv4SubnetID = 0
125125
options.Options.NodeBalancerBackendIPv4SubnetName = ""
126126
} else if options.Options.NodeBalancerBackendIPv4SubnetName != "" {
127-
options.Options.NodeBalancerBackendIPv4SubnetID, err = cache.GetNodeBalancerBackendIPv4SubnetID(linodeClient)
127+
options.Options.NodeBalancerBackendIPv4SubnetID, err = services.GetNodeBalancerBackendIPv4SubnetID(linodeClient)
128128
if err != nil {
129129
return nil, fmt.Errorf("failed to get backend IPv4 subnet ID for subnet name %s: %w", options.Options.NodeBalancerBackendIPv4SubnetName, err)
130130
}
131131
klog.Infof("Using NodeBalancer backend IPv4 subnet ID %d for subnet name %s", options.Options.NodeBalancerBackendIPv4SubnetID, options.Options.NodeBalancerBackendIPv4SubnetName)
132132
}
133133

134-
instanceCache = cache.NewInstances(linodeClient)
134+
instanceCache = services.NewInstances(linodeClient)
135135
routes, err := newRoutes(linodeClient, instanceCache)
136136
if err != nil {
137137
return nil, fmt.Errorf("routes client was not created successfully: %w", err)

cloud/linode/cloud_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"github.com/stretchr/testify/require"
1111
cloudprovider "k8s.io/cloud-provider"
1212

13-
"github.com/linode/linode-cloud-controller-manager/cloud/linode/cache"
1413
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client/mocks"
1514
"github.com/linode/linode-cloud-controller-manager/cloud/linode/options"
15+
"github.com/linode/linode-cloud-controller-manager/cloud/linode/services"
1616
)
1717

1818
func TestNewCloudRouteControllerDisabled(t *testing.T) {
@@ -82,16 +82,16 @@ func TestNewCloud(t *testing.T) {
8282
options.Options.LoadBalancerType = "test"
8383
options.Options.VPCNames = []string{"vpc-test1", "vpc-test2"}
8484
options.Options.NodeBalancerBackendIPv4SubnetName = "t1"
85-
cache.VpcIDs = map[string]int{"vpc-test1": 1, "vpc-test2": 2, "vpc-test3": 3}
86-
cache.SubnetIDs = map[string]int{"t1": 1, "t2": 2, "t3": 3}
85+
services.VpcIDs = map[string]int{"vpc-test1": 1, "vpc-test2": 2, "vpc-test3": 3}
86+
services.SubnetIDs = map[string]int{"t1": 1, "t2": 2, "t3": 3}
8787
defer func() {
8888
options.Options.LoadBalancerType = ""
8989
options.Options.EnableRouteController = rtEnabled
9090
options.Options.VPCNames = []string{}
9191
options.Options.NodeBalancerBackendIPv4SubnetID = 0
9292
options.Options.NodeBalancerBackendIPv4SubnetName = ""
93-
cache.VpcIDs = map[string]int{}
94-
cache.SubnetIDs = map[string]int{}
93+
services.VpcIDs = map[string]int{}
94+
services.SubnetIDs = map[string]int{}
9595
}()
9696
_, err := newCloud()
9797
assert.Error(t, err, "expected error if incorrect loadbalancertype is set")
@@ -182,7 +182,7 @@ func Test_linodeCloud_LoadBalancer(t *testing.T) {
182182
name: "should return loadbalancer interface",
183183
fields: fields{
184184
client: client,
185-
instances: cache.NewInstances(client),
185+
instances: services.NewInstances(client),
186186
loadbalancers: newLoadbalancers(client, "us-east"),
187187
routes: nil,
188188
},
@@ -229,11 +229,11 @@ func Test_linodeCloud_InstancesV2(t *testing.T) {
229229
name: "should return instances interface",
230230
fields: fields{
231231
client: client,
232-
instances: cache.NewInstances(client),
232+
instances: services.NewInstances(client),
233233
loadbalancers: newLoadbalancers(client, "us-east"),
234234
routes: nil,
235235
},
236-
want: cache.NewInstances(client),
236+
want: services.NewInstances(client),
237237
want1: true,
238238
},
239239
}
@@ -276,7 +276,7 @@ func Test_linodeCloud_Instances(t *testing.T) {
276276
name: "should return nil",
277277
fields: fields{
278278
client: client,
279-
instances: cache.NewInstances(client),
279+
instances: services.NewInstances(client),
280280
loadbalancers: newLoadbalancers(client, "us-east"),
281281
routes: nil,
282282
},
@@ -323,7 +323,7 @@ func Test_linodeCloud_Zones(t *testing.T) {
323323
name: "should return nil",
324324
fields: fields{
325325
client: client,
326-
instances: cache.NewInstances(client),
326+
instances: services.NewInstances(client),
327327
loadbalancers: newLoadbalancers(client, "us-east"),
328328
routes: nil,
329329
},
@@ -370,7 +370,7 @@ func Test_linodeCloud_Clusters(t *testing.T) {
370370
name: "should return nil",
371371
fields: fields{
372372
client: client,
373-
instances: cache.NewInstances(client),
373+
instances: services.NewInstances(client),
374374
loadbalancers: newLoadbalancers(client, "us-east"),
375375
routes: nil,
376376
},
@@ -419,7 +419,7 @@ func Test_linodeCloud_Routes(t *testing.T) {
419419
name: "should return nil",
420420
fields: fields{
421421
client: client,
422-
instances: cache.NewInstances(client),
422+
instances: services.NewInstances(client),
423423
loadbalancers: newLoadbalancers(client, "us-east"),
424424
routes: r,
425425
EnableRouteController: false,
@@ -431,7 +431,7 @@ func Test_linodeCloud_Routes(t *testing.T) {
431431
name: "should return routes interface",
432432
fields: fields{
433433
client: client,
434-
instances: cache.NewInstances(client),
434+
instances: services.NewInstances(client),
435435
loadbalancers: newLoadbalancers(client, "us-east"),
436436
routes: r,
437437
EnableRouteController: true,

cloud/linode/loadbalancers.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ import (
2727
"k8s.io/klog/v2"
2828

2929
"github.com/linode/linode-cloud-controller-manager/cloud/annotations"
30-
"github.com/linode/linode-cloud-controller-manager/cloud/linode/cache"
3130
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
32-
"github.com/linode/linode-cloud-controller-manager/cloud/linode/firewall"
3331
"github.com/linode/linode-cloud-controller-manager/cloud/linode/options"
32+
"github.com/linode/linode-cloud-controller-manager/cloud/linode/services"
3433
"github.com/linode/linode-cloud-controller-manager/sentry"
3534
)
3635

@@ -396,7 +395,7 @@ func (l *loadbalancers) updateNodeBalancer(
396395
}
397396
}
398397

399-
fwClient := firewall.LinodeClient{Client: l.client}
398+
fwClient := services.LinodeClient{Client: l.client}
400399
err = fwClient.UpdateNodeBalancerFirewall(ctx, l.GetLoadBalancerName(ctx, clusterName, service), tags, service, nb)
401400
if err != nil {
402401
return err
@@ -652,7 +651,7 @@ func (l *loadbalancers) EnsureLoadBalancerDeleted(ctx context.Context, clusterNa
652651
return nil
653652
}
654653

655-
fwClient := firewall.LinodeClient{Client: l.client}
654+
fwClient := services.LinodeClient{Client: l.client}
656655
if err = fwClient.DeleteNodeBalancerFirewall(ctx, service, nb); err != nil {
657656
return err
658657
}
@@ -851,7 +850,7 @@ func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName stri
851850
// There's no firewallID already set, see if we need to create a new fw, look for the acl annotation.
852851
_, ok := service.GetAnnotations()[annotations.AnnLinodeCloudFirewallACL]
853852
if ok {
854-
fwcreateOpts, err := firewall.CreateFirewallOptsForSvc(label, tags, service)
853+
fwcreateOpts, err := services.CreateFirewallOptsForSvc(label, tags, service)
855854
if err != nil {
856855
return nil, err
857856
}
@@ -998,7 +997,7 @@ func (l *loadbalancers) getSubnetIDForSVC(ctx context.Context, service *v1.Servi
998997
if vpcOk {
999998
vpcName = specifiedVPCName
1000999
}
1001-
vpcID, err := cache.GetVPCID(ctx, l.client, vpcName)
1000+
vpcID, err := services.GetVPCID(ctx, l.client, vpcName)
10021001
if err != nil {
10031002
return 0, err
10041003
}
@@ -1009,7 +1008,7 @@ func (l *loadbalancers) getSubnetIDForSVC(ctx context.Context, service *v1.Servi
10091008
}
10101009

10111010
// Use the VPC ID and Subnet Name to get the subnet ID
1012-
return cache.GetSubnetID(ctx, l.client, vpcID, subnetName)
1011+
return services.GetSubnetID(ctx, l.client, vpcID, subnetName)
10131012
}
10141013

10151014
// buildLoadBalancerRequest returns a linodego.NodeBalancer

cloud/linode/loadbalancers_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import (
3030

3131
"github.com/linode/linode-cloud-controller-manager/cloud/annotations"
3232
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
33-
"github.com/linode/linode-cloud-controller-manager/cloud/linode/firewall"
3433
"github.com/linode/linode-cloud-controller-manager/cloud/linode/options"
34+
"github.com/linode/linode-cloud-controller-manager/cloud/linode/services"
3535
)
3636

3737
const testCert string = `-----BEGIN CERTIFICATE-----
@@ -511,8 +511,8 @@ func testCreateNodeBalanceWithNoAllowOrDenyList(t *testing.T, client *linodego.C
511511
}
512512

513513
err := testCreateNodeBalancer(t, client, f, annotations, nil)
514-
if err == nil || !stderrors.Is(err, firewall.ErrInvalidFWConfig) {
515-
t.Fatalf("expected a %v error, got %v", firewall.ErrInvalidFWConfig, err)
514+
if err == nil || !stderrors.Is(err, services.ErrInvalidFWConfig) {
515+
t.Fatalf("expected a %v error, got %v", services.ErrInvalidFWConfig, err)
516516
}
517517
}
518518

@@ -533,8 +533,8 @@ func testCreateNodeBalanceWithBothAllowOrDenyList(t *testing.T, client *linodego
533533
}
534534

535535
err := testCreateNodeBalancer(t, client, f, annotations, nil)
536-
if err == nil || !stderrors.Is(err, firewall.ErrInvalidFWConfig) {
537-
t.Fatalf("expected a %v error, got %v", firewall.ErrInvalidFWConfig, err)
536+
if err == nil || !stderrors.Is(err, services.ErrInvalidFWConfig) {
537+
t.Fatalf("expected a %v error, got %v", services.ErrInvalidFWConfig, err)
538538
}
539539
}
540540

@@ -1950,7 +1950,7 @@ func testUpdateLoadBalancerAddNewFirewall(t *testing.T, client *linodego.Client,
19501950
}
19511951
svc.Status.LoadBalancer = *lbStatus
19521952
stubServiceUpdate(fakeClientset, svc)
1953-
fwClient := firewall.LinodeClient{Client: client}
1953+
fwClient := services.LinodeClient{Client: client}
19541954
fw, err := fwClient.CreateFirewall(t.Context(), linodego.FirewallCreateOptions{
19551955
Label: "test",
19561956
Rules: linodego.FirewallRuleSet{Inbound: []linodego.FirewallRule{{
@@ -2313,7 +2313,7 @@ func testUpdateLoadBalancerUpdateFirewallRemoveACLaddID(t *testing.T, client *li
23132313
t.Errorf("expected IP, got %v", fwIPs)
23142314
}
23152315

2316-
fwClient := firewall.LinodeClient{Client: client}
2316+
fwClient := services.LinodeClient{Client: client}
23172317
fw, err := fwClient.CreateFirewall(t.Context(), linodego.FirewallCreateOptions{
23182318
Label: "test",
23192319
Rules: linodego.FirewallRuleSet{Inbound: []linodego.FirewallRule{{
@@ -2411,7 +2411,7 @@ func testUpdateLoadBalancerUpdateFirewallRemoveIDaddACL(t *testing.T, client *li
24112411
fakeClientset := fake.NewSimpleClientset()
24122412
lb.kubeClient = fakeClientset
24132413

2414-
fwClient := firewall.LinodeClient{Client: client}
2414+
fwClient := services.LinodeClient{Client: client}
24152415
fw, err := fwClient.CreateFirewall(t.Context(), linodego.FirewallCreateOptions{
24162416
Label: "test",
24172417
Rules: linodego.FirewallRuleSet{Inbound: []linodego.FirewallRule{{
@@ -2798,7 +2798,7 @@ func testUpdateLoadBalancerUpdateFirewall(t *testing.T, client *linodego.Client,
27982798
_ = lb.EnsureLoadBalancerDeleted(t.Context(), "linodelb", svc)
27992799
}()
28002800

2801-
fwClient := firewall.LinodeClient{Client: client}
2801+
fwClient := services.LinodeClient{Client: client}
28022802
fw, err := fwClient.CreateFirewall(t.Context(), firewallCreateOpts)
28032803
if err != nil {
28042804
t.Errorf("Error creating firewall %s", err)
@@ -2931,7 +2931,7 @@ func testUpdateLoadBalancerDeleteFirewallRemoveID(t *testing.T, client *linodego
29312931
_ = lb.EnsureLoadBalancerDeleted(t.Context(), "linodelb", svc)
29322932
}()
29332933

2934-
fwClient := firewall.LinodeClient{Client: client}
2934+
fwClient := services.LinodeClient{Client: client}
29352935
fw, err := fwClient.CreateFirewall(t.Context(), firewallCreateOpts)
29362936
if err != nil {
29372937
t.Errorf("Error in creating firewall %s", err)

cloud/linode/node_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"k8s.io/klog/v2"
2222

2323
"github.com/linode/linode-cloud-controller-manager/cloud/annotations"
24-
ccmCache "github.com/linode/linode-cloud-controller-manager/cloud/linode/cache"
2524
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
2625
"github.com/linode/linode-cloud-controller-manager/cloud/linode/options"
26+
"github.com/linode/linode-cloud-controller-manager/cloud/linode/services"
2727
ccmUtils "github.com/linode/linode-cloud-controller-manager/cloud/linode/utils"
2828
)
2929

@@ -45,7 +45,7 @@ type nodeController struct {
4545
sync.RWMutex
4646

4747
client client.Client
48-
instances *ccmCache.Instances
48+
instances *services.Instances
4949
kubeclient kubernetes.Interface
5050
informer v1informers.NodeInformer
5151

@@ -157,7 +157,7 @@ func newK8sNodeCache() *k8sNodeCache {
157157
}
158158
}
159159

160-
func newNodeController(kubeclient kubernetes.Interface, client client.Client, informer v1informers.NodeInformer, instanceCache *ccmCache.Instances) *nodeController {
160+
func newNodeController(kubeclient kubernetes.Interface, client client.Client, informer v1informers.NodeInformer, instanceCache *services.Instances) *nodeController {
161161
timeout := defaultMetadataTTL
162162
if raw, ok := os.LookupEnv("LINODE_METADATA_TTL"); ok {
163163
if t, err := strconv.Atoi(raw); t > 0 && err == nil {

cloud/linode/node_controller_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
"k8s.io/client-go/util/workqueue"
1919

2020
"github.com/linode/linode-cloud-controller-manager/cloud/annotations"
21-
"github.com/linode/linode-cloud-controller-manager/cloud/linode/cache"
2221
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client/mocks"
22+
"github.com/linode/linode-cloud-controller-manager/cloud/linode/services"
2323
)
2424

2525
func TestNodeController_Run(t *testing.T) {
@@ -31,7 +31,7 @@ func TestNodeController_Run(t *testing.T) {
3131
informer := informers.NewSharedInformerFactory(kubeClient, 0).Core().V1().Nodes()
3232
mockQueue := workqueue.NewTypedDelayingQueueWithConfig(workqueue.TypedDelayingQueueConfig[nodeRequest]{Name: "test"})
3333

34-
nodeCtrl := newNodeController(kubeClient, client, informer, cache.NewInstances(client))
34+
nodeCtrl := newNodeController(kubeClient, client, informer, services.NewInstances(client))
3535
nodeCtrl.queue = mockQueue
3636
nodeCtrl.ttl = 1 * time.Second
3737

@@ -84,7 +84,7 @@ func TestNodeController_processNext(t *testing.T) {
8484

8585
controller := &nodeController{
8686
kubeclient: kubeClient,
87-
instances: cache.NewInstances(client),
87+
instances: services.NewInstances(client),
8888
queue: queue,
8989
metadataLastUpdate: make(map[string]time.Time),
9090
ttl: defaultMetadataTTL,
@@ -138,7 +138,7 @@ func TestNodeController_processNext(t *testing.T) {
138138
defer func() {
139139
controller.instances = currInstances
140140
}()
141-
controller.instances = cache.NewInstances(client)
141+
controller.instances = services.NewInstances(client)
142142
registeredK8sNodeCache.lastUpdate = time.Now().Add(-15 * time.Minute)
143143
controller.addNodeToQueue(node2)
144144
publicIP := net.ParseIP("172.234.31.123")
@@ -169,7 +169,7 @@ func TestNodeController_processNext(t *testing.T) {
169169
controller.queue = queue
170170
controller.addNodeToQueue(node)
171171
client := mocks.NewMockClient(ctrl)
172-
controller.instances = cache.NewInstances(client)
172+
controller.instances = services.NewInstances(client)
173173
retryInterval = 1 * time.Nanosecond
174174
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{}, &linodego.Error{Code: http.StatusTooManyRequests, Message: "Too many requests"})
175175
result := controller.processNext()
@@ -185,7 +185,7 @@ func TestNodeController_processNext(t *testing.T) {
185185
controller.queue = queue
186186
controller.addNodeToQueue(node)
187187
client := mocks.NewMockClient(ctrl)
188-
controller.instances = cache.NewInstances(client)
188+
controller.instances = services.NewInstances(client)
189189
retryInterval = 1 * time.Nanosecond
190190
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{}, &linodego.Error{Code: http.StatusInternalServerError, Message: "Too many requests"})
191191
result := controller.processNext()
@@ -214,7 +214,7 @@ func TestNodeController_handleNode(t *testing.T) {
214214
_, err := kubeClient.CoreV1().Nodes().Create(t.Context(), node, metav1.CreateOptions{})
215215
require.NoError(t, err, "expected no error during node creation")
216216

217-
instCache := cache.NewInstances(client)
217+
instCache := services.NewInstances(client)
218218

219219
t.Setenv("LINODE_METADATA_TTL", "30")
220220
nodeCtrl := newNodeController(kubeClient, client, nil, instCache)
@@ -249,15 +249,15 @@ func TestNodeController_handleNode(t *testing.T) {
249249

250250
// Lookup failure for linode instance
251251
client = mocks.NewMockClient(ctrl)
252-
nodeCtrl.instances = cache.NewInstances(client)
252+
nodeCtrl.instances = services.NewInstances(client)
253253
nodeCtrl.metadataLastUpdate["test-node"] = time.Now().Add(-2 * nodeCtrl.ttl)
254254
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{}, errors.New("lookup failed"))
255255
err = nodeCtrl.handleNode(t.Context(), node)
256256
require.Error(t, err, "expected error during handleNode, got nil")
257257

258258
// All fields already set
259259
client = mocks.NewMockClient(ctrl)
260-
nodeCtrl.instances = cache.NewInstances(client)
260+
nodeCtrl.instances = services.NewInstances(client)
261261
nodeCtrl.metadataLastUpdate["test-node"] = time.Now().Add(-2 * nodeCtrl.ttl)
262262
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{
263263
{ID: 123, Label: "test-node", IPv4: []*net.IP{&publicIP, &privateIP}, IPv6: publicIPv6SLAAC, HostUUID: "123"},

0 commit comments

Comments
 (0)