Skip to content

Commit

Permalink
Add more linters
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome committed Jul 4, 2024
1 parent 3d41684 commit 672da50
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 47 deletions.
26 changes: 23 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ linters-settings:
- name: error-strings
- name: errorf
- name: exported
- name: if-return
- name: increment-decrement
- name: indent-error-flow
- name: package-comments
Expand All @@ -29,33 +28,52 @@ linters-settings:
- name: var-declaration
- name: var-naming
govet:
check-shadowing: true
enable-all: true

linters:
enable:
- asasalint
- asciicheck
- bidichk
- contextcheck
- dupword
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- exportloopref
- fatcontext
- forcetypeassert
- gocheckcompilerdirectives
- gochecksumtype
- gocritic
- godot
- gofmt
- gofumpt
- goimports
- gosec
- gosimple
- gosmopolitan
- govet
- ineffassign
- intrange
- makezero
- misspell
- musttag
- nilerr
- noctx
- nolintlint
- perfsprint
- prealloc
- paralleltest
- predeclared
- reassign
- revive
- staticcheck
- stylecheck
- tagalign
- tenv
- thelper
- tparallel
- typecheck
- unconvert
Expand All @@ -64,6 +82,8 @@ linters:
- usestdlibvars
- wastedassign
- whitespace
- wrapcheck
- testpackage
disable-all: true
issues:
max-issues-per-linter: 0
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

.PHONY: test
test:
go test ./...
go test -v -shuffle=on -race ./...

.PHONY: lint
lint:
Expand Down
32 changes: 16 additions & 16 deletions cmd/sync/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import (
yaml "gopkg.in/yaml.v2"
)

// AWSClient allows you to get the list of IP addresses of instances of an Auto Scaling group. It implements the CloudProvider interface
// AWSClient allows you to get the list of IP addresses of instances of an Auto Scaling group. It implements the CloudProvider interface.
type AWSClient struct {
svcEC2 *ec2.Client
svcAutoscaling *autoscaling.Client
config *awsConfig
}

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

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

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

// GetUpstreams returns the Upstreams list
// GetUpstreams returns the Upstreams list.
func (client *AWSClient) GetUpstreams() []Upstream {
var upstreams []Upstream
for i := 0; i < len(client.config.Upstreams); i++ {
upstreams := make([]Upstream, 0, len(client.config.Upstreams))
for i := range len(client.config.Upstreams) {
u := Upstream{
Name: client.config.Upstreams[i].Name,
Port: client.config.Upstreams[i].Port,
Expand All @@ -81,13 +81,13 @@ func (client *AWSClient) GetUpstreams() []Upstream {
return upstreams
}

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

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

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

// parseAWSConfig parses and validates AWSClient config
// parseAWSConfig parses and validates AWSClient config.
func parseAWSConfig(data []byte) (*awsConfig, error) {
cfg := &awsConfig{}
err := yaml.Unmarshal(data, cfg)
if err != nil {
return nil, err
return nil, fmt.Errorf("error unmarshalling AWS config: %w", err)
}

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

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

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

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

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

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

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

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

for _, ins := range response.AutoScalingInstances {
Expand Down
4 changes: 4 additions & 0 deletions cmd/sync/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func getInvalidAWSConfigInput() []*testInputAWS {
}

func TestValidateAWSConfigNotValid(t *testing.T) {
t.Parallel()
input := getInvalidAWSConfigInput()

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

func TestValidateAWSConfigValid(t *testing.T) {
t.Parallel()
cfg := getValidAWSConfig()

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

func TestGetUpstreamsAWS(t *testing.T) {
t.Parallel()
cfg := getValidAWSConfig()
upstreams := []awsUpstream{
{
Expand Down Expand Up @@ -165,6 +168,7 @@ func areEqualUpstreamsAWS(u1 awsUpstream, u2 Upstream) bool {
}

func TestPrepareBatches(t *testing.T) {
t.Parallel()
const maxItems = 3
ids := []string{"i-394ujfs", "i-dfdinf", "i-fsfsf", "i-8hr83hfwif", "i-nsnsnan"}
instanceIDs := make([]string, len(ids))
Expand Down
22 changes: 11 additions & 11 deletions cmd/sync/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
yaml "gopkg.in/yaml.v2"
)

// AzureClient allows you to get the list of IP addresses of VirtualMachines of a VirtualMachine Scale Set. It implements the CloudProvider interface
// AzureClient allows you to get the list of IP addresses of VirtualMachines of a VirtualMachine Scale Set. It implements the CloudProvider interface.
type AzureClient struct {
config *azureConfig
vMSSClient compute.VirtualMachineScaleSetsClient
iFaceClient network.InterfacesClient
}

// NewAzureClient creates an AzureClient
// NewAzureClient creates an AzureClient.
func NewAzureClient(data []byte) (*AzureClient, error) {
azureClient := &AzureClient{}
cfg, err := parseAzureConfig(data)
Expand All @@ -36,12 +36,12 @@ func NewAzureClient(data []byte) (*AzureClient, error) {
return azureClient, nil
}

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

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

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

ctx := context.TODO()

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

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

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

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

// GetUpstreams returns the Upstreams list
// GetUpstreams returns the Upstreams list.
func (client *AzureClient) GetUpstreams() []Upstream {
var upstreams []Upstream
for i := 0; i < len(client.config.Upstreams); i++ {
upstreams := make([]Upstream, 0, len(client.config.Upstreams))
for i := range len(client.config.Upstreams) {
u := Upstream{
Name: client.config.Upstreams[i].Name,
Port: client.config.Upstreams[i].Port,
Expand Down
5 changes: 5 additions & 0 deletions cmd/sync/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func getInvalidAzureConfigInput() []*testInputAzure {
}

func TestValidateAzureConfigNotValid(t *testing.T) {
t.Parallel()
input := getInvalidAzureConfigInput()

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

func TestValidateAzureConfigValid(t *testing.T) {
t.Parallel()
cfg := getValidAzureConfig()

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

func TestGetPrimaryIPFromInterfaceIPConfiguration(t *testing.T) {
t.Parallel()
primary := true
address := "127.0.0.1"
ipConfig := network.InterfaceIPConfiguration{
Expand All @@ -115,6 +118,7 @@ func TestGetPrimaryIPFromInterfaceIPConfiguration(t *testing.T) {
}

func TestGetPrimaryIPFromInterfaceIPConfigurationFail(t *testing.T) {
t.Parallel()
primaryFalse := false
primaryTrue := true
tests := []struct {
Expand Down Expand Up @@ -158,6 +162,7 @@ func TestGetPrimaryIPFromInterfaceIPConfigurationFail(t *testing.T) {
}

func TestGetUpstreamsAzure(t *testing.T) {
t.Parallel()
cfg := getValidAzureConfig()
upstreams := []azureUpstream{
{
Expand Down
Loading

0 comments on commit 672da50

Please sign in to comment.