Skip to content

Commit 286e220

Browse files
committed
Add more linters
1 parent 3d41684 commit 286e220

13 files changed

+84
-47
lines changed

.golangci.yml

+23-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ linters-settings:
1414
- name: error-strings
1515
- name: errorf
1616
- name: exported
17-
- name: if-return
1817
- name: increment-decrement
1918
- name: indent-error-flow
2019
- name: package-comments
@@ -29,33 +28,53 @@ linters-settings:
2928
- name: var-declaration
3029
- name: var-naming
3130
govet:
32-
check-shadowing: true
3331
enable-all: true
34-
3532
linters:
3633
enable:
34+
- asasalint
3735
- asciicheck
3836
- bidichk
37+
- contextcheck
3938
- dupword
39+
- durationcheck
4040
- errcheck
41+
- errchkjson
42+
- errname
4143
- errorlint
44+
- exportloopref
45+
- fatcontext
46+
- forcetypeassert
47+
- gocheckcompilerdirectives
48+
- gochecksumtype
49+
- gocritic
50+
- godot
4251
- gofmt
4352
- gofumpt
4453
- goimports
4554
- gosec
4655
- gosimple
56+
- gosmopolitan
4757
- govet
4858
- ineffassign
59+
- intrange
4960
- makezero
5061
- misspell
62+
- musttag
5163
- nilerr
5264
- noctx
65+
- nolintlint
66+
- paralleltest
5367
- perfsprint
68+
- prealloc
5469
- predeclared
5570
- reassign
5671
- revive
5772
- staticcheck
73+
- stylecheck
5874
- tagalign
75+
- tenv
76+
- testpackage
77+
- thelper
5978
- tparallel
6079
- typecheck
6180
- unconvert
@@ -64,6 +83,7 @@ linters:
6483
- usestdlibvars
6584
- wastedassign
6685
- whitespace
86+
- wrapcheck
6787
disable-all: true
6888
issues:
6989
max-issues-per-linter: 0

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
.PHONY: test
44
test:
5-
go test ./...
5+
go test -v -shuffle=on -race ./...
66

77
.PHONY: lint
88
lint:

cmd/sync/aws.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import (
1717
yaml "gopkg.in/yaml.v2"
1818
)
1919

20-
// AWSClient allows you to get the list of IP addresses of instances of an Auto Scaling group. It implements the CloudProvider interface
20+
// AWSClient allows you to get the list of IP addresses of instances of an Auto Scaling group. It implements the CloudProvider interface.
2121
type AWSClient struct {
2222
svcEC2 *ec2.Client
2323
svcAutoscaling *autoscaling.Client
2424
config *awsConfig
2525
}
2626

27-
// NewAWSClient creates and configures an AWSClient
27+
// NewAWSClient creates and configures an AWSClient.
2828
func NewAWSClient(data []byte) (*AWSClient, error) {
2929
awsClient := &AWSClient{}
3030
cfg, err := parseAWSConfig(data)
@@ -37,7 +37,7 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
3737

3838
conf, loadErr := config.LoadDefaultConfig(context.TODO())
3939
if loadErr != nil {
40-
return nil, loadErr
40+
return nil, fmt.Errorf("unable to load default AWS config: %w", loadErr)
4141
}
4242

4343
client := imds.NewFromConfig(conf, func(o *imds.Options) {
@@ -61,10 +61,10 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
6161
return awsClient, nil
6262
}
6363

64-
// GetUpstreams returns the Upstreams list
64+
// GetUpstreams returns the Upstreams list.
6565
func (client *AWSClient) GetUpstreams() []Upstream {
66-
var upstreams []Upstream
67-
for i := 0; i < len(client.config.Upstreams); i++ {
66+
upstreams := make([]Upstream, 0, len(client.config.Upstreams))
67+
for i := range len(client.config.Upstreams) {
6868
u := Upstream{
6969
Name: client.config.Upstreams[i].Name,
7070
Port: client.config.Upstreams[i].Port,
@@ -81,13 +81,13 @@ func (client *AWSClient) GetUpstreams() []Upstream {
8181
return upstreams
8282
}
8383

84-
// configure configures the AWSClient with necessary parameters
84+
// configure configures the AWSClient with necessary parameters.
8585
func (client *AWSClient) configure() error {
8686
httpClient := &http.Client{Timeout: connTimeoutInSecs * time.Second}
8787

8888
cfg, err := config.LoadDefaultConfig(context.TODO())
8989
if err != nil {
90-
return err
90+
return fmt.Errorf("unable to load default AWS config: %w", err)
9191
}
9292

9393
client.svcEC2 = ec2.NewFromConfig(cfg, func(o *ec2.Options) {
@@ -103,12 +103,12 @@ func (client *AWSClient) configure() error {
103103
return nil
104104
}
105105

106-
// parseAWSConfig parses and validates AWSClient config
106+
// parseAWSConfig parses and validates AWSClient config.
107107
func parseAWSConfig(data []byte) (*awsConfig, error) {
108108
cfg := &awsConfig{}
109109
err := yaml.Unmarshal(data, cfg)
110110
if err != nil {
111-
return nil, err
111+
return nil, fmt.Errorf("error unmarshalling AWS config: %w", err)
112112
}
113113

114114
err = validateAWSConfig(cfg)
@@ -119,7 +119,7 @@ func parseAWSConfig(data []byte) (*awsConfig, error) {
119119
return cfg, nil
120120
}
121121

122-
// CheckIfScalingGroupExists checks if the Auto Scaling group exists
122+
// CheckIfScalingGroupExists checks if the Auto Scaling group exists.
123123
func (client *AWSClient) CheckIfScalingGroupExists(name string) (bool, error) {
124124
params := &ec2.DescribeInstancesInput{
125125
Filters: []types.Filter{
@@ -140,7 +140,7 @@ func (client *AWSClient) CheckIfScalingGroupExists(name string) (bool, error) {
140140
return len(response.Reservations) > 0, nil
141141
}
142142

143-
// GetPrivateIPsForScalingGroup returns the list of IP addresses of instances of the Auto Scaling group
143+
// GetPrivateIPsForScalingGroup returns the list of IP addresses of instances of the Auto Scaling group.
144144
func (client *AWSClient) GetPrivateIPsForScalingGroup(name string) ([]string, error) {
145145
var onlyInService bool
146146
for _, u := range client.GetUpstreams() {
@@ -162,7 +162,7 @@ func (client *AWSClient) GetPrivateIPsForScalingGroup(name string) ([]string, er
162162

163163
response, err := client.svcEC2.DescribeInstances(context.Background(), params)
164164
if err != nil {
165-
return nil, err
165+
return nil, fmt.Errorf("couldn't describe instances: %w", err)
166166
}
167167

168168
if len(response.Reservations) == 0 {
@@ -193,14 +193,14 @@ func (client *AWSClient) GetPrivateIPsForScalingGroup(name string) ([]string, er
193193
return result, nil
194194
}
195195

196-
// getInstancesInService returns the list of instances that have LifecycleState == InService
196+
// getInstancesInService returns the list of instances that have LifecycleState == InService.
197197
func (client *AWSClient) getInstancesInService(insIDtoIP map[string]string) ([]string, error) {
198198
const maxItems = 50
199199
var result []string
200200
keys := reflect.ValueOf(insIDtoIP).MapKeys()
201201
instanceIDs := make([]string, len(keys))
202202

203-
for i := 0; i < len(keys); i++ {
203+
for i := range len(keys) {
204204
instanceIDs[i] = keys[i].String()
205205
}
206206

@@ -211,7 +211,7 @@ func (client *AWSClient) getInstancesInService(insIDtoIP map[string]string) ([]s
211211
}
212212
response, err := client.svcAutoscaling.DescribeAutoScalingInstances(context.Background(), params)
213213
if err != nil {
214-
return nil, err
214+
return nil, fmt.Errorf("couldn't describe AutoScaling instances: %w", err)
215215
}
216216

217217
for _, ins := range response.AutoScalingInstances {

cmd/sync/aws_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func getInvalidAWSConfigInput() []*testInputAWS {
7474
}
7575

7676
func TestValidateAWSConfigNotValid(t *testing.T) {
77+
t.Parallel()
7778
input := getInvalidAWSConfigInput()
7879

7980
for _, item := range input {
@@ -85,6 +86,7 @@ func TestValidateAWSConfigNotValid(t *testing.T) {
8586
}
8687

8788
func TestValidateAWSConfigValid(t *testing.T) {
89+
t.Parallel()
8890
cfg := getValidAWSConfig()
8991

9092
err := validateAWSConfig(cfg)
@@ -94,6 +96,7 @@ func TestValidateAWSConfigValid(t *testing.T) {
9496
}
9597

9698
func TestGetUpstreamsAWS(t *testing.T) {
99+
t.Parallel()
97100
cfg := getValidAWSConfig()
98101
upstreams := []awsUpstream{
99102
{
@@ -165,6 +168,7 @@ func areEqualUpstreamsAWS(u1 awsUpstream, u2 Upstream) bool {
165168
}
166169

167170
func TestPrepareBatches(t *testing.T) {
171+
t.Parallel()
168172
const maxItems = 3
169173
ids := []string{"i-394ujfs", "i-dfdinf", "i-fsfsf", "i-8hr83hfwif", "i-nsnsnan"}
170174
instanceIDs := make([]string, len(ids))

cmd/sync/azure.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111
yaml "gopkg.in/yaml.v2"
1212
)
1313

14-
// AzureClient allows you to get the list of IP addresses of VirtualMachines of a VirtualMachine Scale Set. It implements the CloudProvider interface
14+
// AzureClient allows you to get the list of IP addresses of VirtualMachines of a VirtualMachine Scale Set. It implements the CloudProvider interface.
1515
type AzureClient struct {
1616
config *azureConfig
1717
vMSSClient compute.VirtualMachineScaleSetsClient
1818
iFaceClient network.InterfacesClient
1919
}
2020

21-
// NewAzureClient creates an AzureClient
21+
// NewAzureClient creates an AzureClient.
2222
func NewAzureClient(data []byte) (*AzureClient, error) {
2323
azureClient := &AzureClient{}
2424
cfg, err := parseAzureConfig(data)
@@ -36,12 +36,12 @@ func NewAzureClient(data []byte) (*AzureClient, error) {
3636
return azureClient, nil
3737
}
3838

39-
// parseAzureConfig parses and validates AzureClient config
39+
// parseAzureConfig parses and validates AzureClient config.
4040
func parseAzureConfig(data []byte) (*azureConfig, error) {
4141
cfg := &azureConfig{}
4242
err := yaml.Unmarshal(data, cfg)
4343
if err != nil {
44-
return nil, err
44+
return nil, fmt.Errorf("couldn't unmarshal Azure config: %w", err)
4545
}
4646

4747
err = validateAzureConfig(cfg)
@@ -52,15 +52,15 @@ func parseAzureConfig(data []byte) (*azureConfig, error) {
5252
return cfg, nil
5353
}
5454

55-
// GetPrivateIPsForScalingGroup returns the list of IP addresses of instances of the Virtual Machine Scale Set
55+
// GetPrivateIPsForScalingGroup returns the list of IP addresses of instances of the Virtual Machine Scale Set.
5656
func (client *AzureClient) GetPrivateIPsForScalingGroup(name string) ([]string, error) {
5757
var ips []string
5858

5959
ctx := context.TODO()
6060

6161
for iFaces, err := client.iFaceClient.ListVirtualMachineScaleSetNetworkInterfaces(ctx, client.config.ResourceGroupName, name); iFaces.NotDone() || err != nil; err = iFaces.NextWithContext(ctx) {
6262
if err != nil {
63-
return nil, err
63+
return nil, fmt.Errorf("couldn't get the list of network interfaces: %w", err)
6464
}
6565

6666
for _, iFace := range iFaces.Values() {
@@ -102,7 +102,7 @@ func getPrimaryIPFromInterfaceIPConfiguration(ipConfig network.InterfaceIPConfig
102102
return *ipConfig.InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress
103103
}
104104

105-
// CheckIfScalingGroupExists checks if the Virtual Machine Scale Set exists
105+
// CheckIfScalingGroupExists checks if the Virtual Machine Scale Set exists.
106106
func (client *AzureClient) CheckIfScalingGroupExists(name string) (bool, error) {
107107
ctx := context.TODO()
108108
vmss, err := client.vMSSClient.Get(ctx, client.config.ResourceGroupName, name, "userData")
@@ -116,7 +116,7 @@ func (client *AzureClient) CheckIfScalingGroupExists(name string) (bool, error)
116116
func (client *AzureClient) configure() error {
117117
authorizer, err := auth.NewAuthorizerFromEnvironment()
118118
if err != nil {
119-
return err
119+
return fmt.Errorf("couldn't create authorizer: %w", err)
120120
}
121121

122122
client.vMSSClient = compute.NewVirtualMachineScaleSetsClient(client.config.SubscriptionID)
@@ -127,10 +127,10 @@ func (client *AzureClient) configure() error {
127127
return nil
128128
}
129129

130-
// GetUpstreams returns the Upstreams list
130+
// GetUpstreams returns the Upstreams list.
131131
func (client *AzureClient) GetUpstreams() []Upstream {
132-
var upstreams []Upstream
133-
for i := 0; i < len(client.config.Upstreams); i++ {
132+
upstreams := make([]Upstream, 0, len(client.config.Upstreams))
133+
for i := range len(client.config.Upstreams) {
134134
u := Upstream{
135135
Name: client.config.Upstreams[i].Name,
136136
Port: client.config.Upstreams[i].Port,

cmd/sync/azure_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func getInvalidAzureConfigInput() []*testInputAzure {
8080
}
8181

8282
func TestValidateAzureConfigNotValid(t *testing.T) {
83+
t.Parallel()
8384
input := getInvalidAzureConfigInput()
8485

8586
for _, item := range input {
@@ -91,6 +92,7 @@ func TestValidateAzureConfigNotValid(t *testing.T) {
9192
}
9293

9394
func TestValidateAzureConfigValid(t *testing.T) {
95+
t.Parallel()
9496
cfg := getValidAzureConfig()
9597

9698
err := validateAzureConfig(cfg)
@@ -100,6 +102,7 @@ func TestValidateAzureConfigValid(t *testing.T) {
100102
}
101103

102104
func TestGetPrimaryIPFromInterfaceIPConfiguration(t *testing.T) {
105+
t.Parallel()
103106
primary := true
104107
address := "127.0.0.1"
105108
ipConfig := network.InterfaceIPConfiguration{
@@ -115,6 +118,7 @@ func TestGetPrimaryIPFromInterfaceIPConfiguration(t *testing.T) {
115118
}
116119

117120
func TestGetPrimaryIPFromInterfaceIPConfigurationFail(t *testing.T) {
121+
t.Parallel()
118122
primaryFalse := false
119123
primaryTrue := true
120124
tests := []struct {
@@ -158,6 +162,7 @@ func TestGetPrimaryIPFromInterfaceIPConfigurationFail(t *testing.T) {
158162
}
159163

160164
func TestGetUpstreamsAzure(t *testing.T) {
165+
t.Parallel()
161166
cfg := getValidAzureConfig()
162167
upstreams := []azureUpstream{
163168
{

0 commit comments

Comments
 (0)