Skip to content

Commit 19b6379

Browse files
authored
[CI] Golangci-lint update part 2 (#344)
* Fixed remaining goconst errors * Fixed varnamelen errors * Fixed prealloc errors * Temporarily resolved depgaurd reflect error
1 parent aa06e1c commit 19b6379

File tree

7 files changed

+103
-90
lines changed

7 files changed

+103
-90
lines changed

.golangci.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ linters-settings:
1717
- "$all"
1818
- "!$test"
1919
deny:
20-
- pkg: "reflect"
21-
desc: "Reflection is never clear."
20+
# TODO: Remove reflect from loadbalancers.go and reinstate this requirement
21+
#- pkg: "reflect"
22+
# desc: "Reflection is never clear."
2223
- pkg: "gob"
2324
desc: "Please convert types manually"
2425

@@ -112,7 +113,7 @@ linters:
112113
- copyloopvar
113114
#- cyclop
114115
- decorder
115-
#- depguard
116+
- depguard
116117
- dogsled
117118
- dupl
118119
- dupword
@@ -123,16 +124,16 @@ linters:
123124
- exhaustive
124125
#- forbidigo
125126
#- forcetypeassert
126-
#- gci
127+
- gci
127128
- gocheckcompilerdirectives
128129
- gofmt
129130
- goimports
130131
#- gocognit
131-
#- goconst
132+
- goconst
132133
#- gocritic
133134
- gofumpt
134135
- goprintffuncname
135-
#- gosec
136+
- gosec
136137
- importas
137138
- loggercheck
138139
- maintidx
@@ -147,15 +148,15 @@ linters:
147148
- nolintlint
148149
- nosprintfhostport
149150
#- paralleltest
150-
#- prealloc
151+
- prealloc
151152
- predeclared
152153
- reassign
153154
#- tenv
154155
#- thelper
155156
- unconvert
156157
- unparam
157158
- usestdlibvars
158-
#- varnamelen
159+
- varnamelen
159160
- wastedassign
160161
- whitespace
161162

cloud/linode/cilium_loadbalancers_test.go

Lines changed: 48 additions & 42 deletions
Large diffs are not rendered by default.

cloud/linode/instances.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ func (nc *nodeCache) refreshInstances(ctx context.Context, client client.Client)
9999
}
100100

101101
newNodes := make(map[int]linodeInstance, len(instances))
102-
for i, instance := range instances {
102+
for index, instance := range instances {
103103
// if running within VPC, only store instances in cache which are part of VPC
104104
if Options.VPCNames != "" && len(vpcNodes[instance.ID]) == 0 {
105105
continue
106106
}
107107
node := linodeInstance{
108-
instance: &instances[i],
108+
instance: &instances[index],
109109
ips: nc.getInstanceAddresses(instance, vpcNodes[instance.ID]),
110110
}
111111
newNodes[instance.ID] = node

cloud/linode/instances_test.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ import (
1717
cloudprovider "k8s.io/cloud-provider"
1818

1919
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client/mocks"
20+
21+
)
22+
23+
const (
24+
instanceName string = "mock-instance"
25+
usEast string = "us-east"
26+
typeG6 string = "g6-standard-1"
2027
)
2128

2229
func nodeWithProviderID(providerID string) *v1.Node {
@@ -53,7 +60,7 @@ func TestInstanceExists(t *testing.T) {
5360
{
5461
ID: 123,
5562
Label: "mock",
56-
Region: "us-east",
63+
Region: usEast,
5764
Type: "g6-standard-2",
5865
},
5966
}, nil)
@@ -114,14 +121,13 @@ func TestMetadataRetrieval(t *testing.T) {
114121
t.Run("should return data when linode is found (by name)", func(t *testing.T) {
115122
instances := newInstances(client)
116123
id := 123
117-
name := "mock-instance"
118-
node := nodeWithName(name)
124+
node := nodeWithName(instanceName)
119125
publicIPv4 := net.ParseIP("45.76.101.25")
120126
privateIPv4 := net.ParseIP("192.168.133.65")
121-
linodeType := "g6-standard-1"
122-
region := "us-east"
127+
linodeType := typeG6
128+
region := usEast
123129
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{
124-
{ID: id, Label: name, Type: linodeType, Region: region, IPv4: []*net.IP{&publicIPv4, &privateIPv4}},
130+
{ID: id, Label: instanceName, Type: linodeType, Region: region, IPv4: []*net.IP{&publicIPv4, &privateIPv4}},
125131
}, nil)
126132

127133
meta, err := instances.InstanceMetadata(ctx, node)
@@ -132,7 +138,7 @@ func TestMetadataRetrieval(t *testing.T) {
132138
assert.Equal(t, []v1.NodeAddress{
133139
{
134140
Type: v1.NodeHostName,
135-
Address: name,
141+
Address: instanceName,
136142
},
137143
{
138144
Type: v1.NodeExternalIP,
@@ -148,23 +154,21 @@ func TestMetadataRetrieval(t *testing.T) {
148154
t.Run("should return data when linode is found (by name) and addresses must be in order", func(t *testing.T) {
149155
instances := newInstances(client)
150156
id := 123
151-
name := "mock-instance"
152-
node := nodeWithName(name)
157+
node := nodeWithName(instanceName)
153158
publicIPv4 := net.ParseIP("45.76.101.25")
154159
privateIPv4 := net.ParseIP("192.168.133.65")
155160
ipv6Addr := "2001::8a2e:370:7348"
156-
linodeType := "g6-standard-1"
157-
region := "us-east"
161+
linodeType := typeG6
158162

159163
Options.VPCNames = "test"
160164
vpcIDs["test"] = 1
161165
Options.EnableRouteController = true
162166

163167
instance := linodego.Instance{
164168
ID: id,
165-
Label: name,
169+
Label: instanceName,
166170
Type: linodeType,
167-
Region: region,
171+
Region: usEast,
168172
IPv4: []*net.IP{&publicIPv4, &privateIPv4},
169173
IPv6: ipv6Addr,
170174
}
@@ -201,12 +205,12 @@ func TestMetadataRetrieval(t *testing.T) {
201205
meta, err := instances.InstanceMetadata(ctx, node)
202206
assert.NoError(t, err)
203207
assert.Equal(t, providerIDPrefix+strconv.Itoa(id), meta.ProviderID)
204-
assert.Equal(t, region, meta.Region)
208+
assert.Equal(t, usEast, meta.Region)
205209
assert.Equal(t, linodeType, meta.InstanceType)
206210
assert.Equal(t, []v1.NodeAddress{
207211
{
208212
Type: v1.NodeHostName,
209-
Address: name,
213+
Address: instanceName,
210214
},
211215
{
212216
Type: v1.NodeInternalIP,
@@ -345,8 +349,8 @@ func TestMetadataRetrieval(t *testing.T) {
345349
ips = append(ips, &parsed)
346350
}
347351

348-
linodeType := "g6-standard-1"
349-
region := "us-east"
352+
linodeType := typeG6
353+
region := usEast
350354
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{
351355
{ID: id, Label: name, Type: linodeType, Region: region, IPv4: ips, IPv6: test.inputIPv6},
352356
}, nil)

cloud/linode/loadbalancers.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -861,16 +861,16 @@ func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, clusterNam
861861
return l.createNodeBalancer(ctx, clusterName, service, configs)
862862
}
863863

864-
func coerceString(s string, minLen, maxLen int, padding string) string {
864+
func coerceString(str string, minLen, maxLen int, padding string) string {
865865
if len(padding) == 0 {
866866
padding = "x"
867867
}
868-
if len(s) > maxLen {
869-
return s[:maxLen]
870-
} else if len(s) < minLen {
871-
return coerceString(fmt.Sprintf("%s%s", padding, s), minLen, maxLen, padding)
868+
if len(str) > maxLen {
869+
return str[:maxLen]
870+
} else if len(str) < minLen {
871+
return coerceString(fmt.Sprintf("%s%s", padding, str), minLen, maxLen, padding)
872872
}
873-
return s
873+
return str
874874
}
875875

876876
func (l *loadbalancers) buildNodeBalancerNodeConfigRebuildOptions(node *v1.Node, nodePort int32, subnetID int) linodego.NodeBalancerConfigRebuildNodeOptions {

cloud/linode/loadbalancers_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ o/aoxqmE0mN1lyCPOa9UP//LlsREkWVKI3+Wld/xERtzf66hjcH+ilsXDxxpMEXo
115115
bSiPJQsGIKtQvyCaZY2szyOoeUGgOId+He7ITlezxKrjdj+1pLMESvAxKeo=
116116
-----END RSA PRIVATE KEY-----`
117117

118+
const drop string = "DROP"
119+
118120
func TestCCMLoadBalancers(t *testing.T) {
119121
testCases := []struct {
120122
name string
@@ -1529,8 +1531,8 @@ func testUpdateLoadBalancerAddNewFirewallACL(t *testing.T, client *linodego.Clie
15291531
t.Fatalf("Firewalls attached when none specified")
15301532
}
15311533

1532-
var ipv4s []string
1533-
var ipv6s []string
1534+
ipv4s := make([]string, 0, 400)
1535+
ipv6s := make([]string, 0, 300)
15341536
i := 0
15351537
for i < 400 {
15361538
ipv4s = append(ipv4s, fmt.Sprintf("%d.%d.%d.%d", 192, rand.Int31n(255), rand.Int31n(255), rand.Int31n(255)))
@@ -1587,7 +1589,7 @@ func testUpdateLoadBalancerAddNewFirewallACL(t *testing.T, client *linodego.Clie
15871589
t.Fatalf("No firewalls found")
15881590
}
15891591

1590-
if firewallsNew[0].Rules.InboundPolicy != "DROP" {
1592+
if firewallsNew[0].Rules.InboundPolicy != drop {
15911593
t.Errorf("expected DROP inbound policy, got %s", firewallsNew[0].Rules.InboundPolicy)
15921594
}
15931595

@@ -1663,7 +1665,7 @@ func testUpdateLoadBalancerDeleteFirewallRemoveACL(t *testing.T, client *linodeg
16631665
t.Fatalf("No firewalls attached")
16641666
}
16651667

1666-
if firewalls[0].Rules.InboundPolicy != "DROP" {
1668+
if firewalls[0].Rules.InboundPolicy != drop {
16671669
t.Errorf("expected DROP inbound policy, got %s", firewalls[0].Rules.InboundPolicy)
16681670
}
16691671

@@ -1756,7 +1758,7 @@ func testUpdateLoadBalancerUpdateFirewallRemoveACLaddID(t *testing.T, client *li
17561758
t.Fatalf("No firewalls attached")
17571759
}
17581760

1759-
if firewalls[0].Rules.InboundPolicy != "DROP" {
1761+
if firewalls[0].Rules.InboundPolicy != drop {
17601762
t.Errorf("expected DROP inbound policy, got %s", firewalls[0].Rules.InboundPolicy)
17611763
}
17621764

@@ -1942,7 +1944,7 @@ func testUpdateLoadBalancerUpdateFirewallRemoveIDaddACL(t *testing.T, client *li
19421944
t.Fatalf("No attached firewalls found")
19431945
}
19441946

1945-
if firewallsNew[0].Rules.InboundPolicy != "DROP" {
1947+
if firewallsNew[0].Rules.InboundPolicy != drop {
19461948
t.Errorf("expected DROP inbound policy, got %s", firewallsNew[0].Rules.InboundPolicy)
19471949
}
19481950

@@ -2022,7 +2024,7 @@ func testUpdateLoadBalancerUpdateFirewallACL(t *testing.T, client *linodego.Clie
20222024
t.Fatalf("No firewalls attached")
20232025
}
20242026

2025-
if firewalls[0].Rules.InboundPolicy != "DROP" {
2027+
if firewalls[0].Rules.InboundPolicy != drop {
20262028
t.Errorf("expected DROP inbound policy, got %s", firewalls[0].Rules.InboundPolicy)
20272029
}
20282030

cloud/linode/node_controller.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,26 +185,26 @@ func (s *nodeController) handleNode(ctx context.Context, node *v1.Node) error {
185185

186186
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
187187
// Get a fresh copy of the node so the resource version is up-to-date
188-
n, err := s.kubeclient.CoreV1().Nodes().Get(ctx, node.Name, metav1.GetOptions{})
188+
nodeResult, err := s.kubeclient.CoreV1().Nodes().Get(ctx, node.Name, metav1.GetOptions{})
189189
if err != nil {
190190
return err
191191
}
192192

193193
// Try to update the node UUID if it has not been set
194-
if n.Labels[annotations.AnnLinodeHostUUID] != linode.HostUUID {
195-
n.Labels[annotations.AnnLinodeHostUUID] = linode.HostUUID
194+
if nodeResult.Labels[annotations.AnnLinodeHostUUID] != linode.HostUUID {
195+
nodeResult.Labels[annotations.AnnLinodeHostUUID] = linode.HostUUID
196196
}
197197

198198
// Try to update the node ProviderID if it has not been set
199-
if n.Spec.ProviderID == "" {
200-
n.Spec.ProviderID = providerIDPrefix + strconv.Itoa(linode.ID)
199+
if nodeResult.Spec.ProviderID == "" {
200+
nodeResult.Spec.ProviderID = providerIDPrefix + strconv.Itoa(linode.ID)
201201
}
202202

203203
// Try to update the expectedPrivateIP if its not set or doesn't match
204-
if n.Annotations[annotations.AnnLinodeNodePrivateIP] != expectedPrivateIP && expectedPrivateIP != "" {
205-
n.Annotations[annotations.AnnLinodeNodePrivateIP] = expectedPrivateIP
204+
if nodeResult.Annotations[annotations.AnnLinodeNodePrivateIP] != expectedPrivateIP && expectedPrivateIP != "" {
205+
nodeResult.Annotations[annotations.AnnLinodeNodePrivateIP] = expectedPrivateIP
206206
}
207-
_, err = s.kubeclient.CoreV1().Nodes().Update(ctx, n, metav1.UpdateOptions{})
207+
_, err = s.kubeclient.CoreV1().Nodes().Update(ctx, nodeResult, metav1.UpdateOptions{})
208208
return err
209209
}); err != nil {
210210
klog.V(1).ErrorS(err, "Node update error")

0 commit comments

Comments
 (0)