Skip to content

Commit 5db00d3

Browse files
committed
address linting issues: update golangci-lint, update deprecated linting settings, fix int -> uint32 potential overflow, fix yaml package requiring yaml struct tags rather than json
1 parent 9b59c81 commit 5db00d3

File tree

7 files changed

+25
-25
lines changed

7 files changed

+25
-25
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on:
1111
env:
1212
# Common versions
1313
GO_VERSION: '1.23'
14-
GOLANGCI_VERSION: 'v1.56.2'
14+
GOLANGCI_VERSION: 'v1.64.2'
1515
DOCKER_BUILDX_VERSION: 'v0.8.2'
1616

1717
# Common users. We can't run a step 'if secrets.AWS_USR != ""' but we can run

.golangci.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
run:
22
deadline: 2m
33

4-
skip-files:
5-
- "zz_generated\\..+\\.go$"
6-
74
output:
85
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
9-
format: colored-line-number
6+
formats: colored-line-number
107

118
linters-settings:
129
errcheck:
@@ -18,14 +15,14 @@ linters-settings:
1815
# default is false: such cases aren't reported by default.
1916
check-blank: false
2017

21-
# [deprecated] comma-separated list of pairs of the form pkg:regex
18+
# comma-separated list of pairs of the form pkg:regex
2219
# the regex is used to ignore names within pkg. (default "fmt:.*").
2320
# see https://github.com/kisielk/errcheck#the-deprecated-method for details
24-
ignore: fmt:.*,io/ioutil:^Read.*
21+
exclude-functions: fmt:.*,io/ioutil:^Read.*
2522

2623
govet:
2724
# report about shadowed variables
28-
check-shadowing: false
25+
shadow: false
2926

3027
revive:
3128
# minimal confidence for issues, default is 0.8
@@ -179,6 +176,9 @@ issues:
179176
- gosec
180177
- gas
181178

179+
exclude-files:
180+
- "zz_generated\\..+\\.go$"
181+
182182
# Independently from option `exclude` we use default exclude patterns,
183183
# it can be disabled by this option. To list all
184184
# excluded by default patterns execute `golangci-lint run --help`.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ GO_STATIC_PACKAGES = $(GO_PROJECT)/cmd/provider
2121
GO_LDFLAGS += -X $(GO_PROJECT)/internal/version.Version=$(VERSION)
2222
GO_SUBDIRS += cmd internal apis
2323
GO111MODULE = on
24-
GOLANGCILINT_VERSION = 1.56.2
24+
GOLANGCILINT_VERSION = 1.64.2
2525
RUNNING_IN_CI = true
2626
-include build/makelib/golang.mk
2727

cmd/provider/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func main() {
5151
maxReconcileRate = app.Flag("max-reconcile-rate", "The maximum number of concurrent reconciliation operations.").Default("1").Int()
5252
artifactsHistoryLimit = app.Flag("artifacts-history-limit", "Each attempt to run the playbook/role generates a set of artifacts on disk. This settings limits how many of these to keep.").Default("10").Int()
5353
pollStateMetricInterval = app.Flag("poll-state-metric", "State metric recording interval").Default("5s").Duration()
54-
replicasCount = app.Flag("replicas", "Amount of replicas configured for the provider. When using more than 1 replica, reconciles will be sharded across them based on a modular hash.").Default("1").Int()
54+
replicasCount = app.Flag("replicas", "Amount of replicas configured for the provider. When using more than 1 replica, reconciles will be sharded across them based on a modular hash.").Default("1").Uint32()
5555
)
5656
kingpin.MustParse(app.Parse(os.Args[1:]))
5757

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ require (
1010
github.com/google/uuid v1.6.0
1111
github.com/spf13/afero v1.11.0
1212
gopkg.in/alecthomas/kingpin.v2 v2.2.6
13-
gopkg.in/yaml.v2 v2.4.0
1413
gotest.tools/v3 v3.5.1
1514
k8s.io/api v0.31.8
1615
k8s.io/apimachinery v0.31.8
1716
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
1817
sigs.k8s.io/controller-runtime v0.19.7
1918
sigs.k8s.io/controller-tools v0.16.5
19+
sigs.k8s.io/yaml v1.4.0
2020
)
2121

2222
require (
@@ -83,6 +83,7 @@ require (
8383
google.golang.org/grpc v1.65.0 // indirect
8484
google.golang.org/protobuf v1.34.2 // indirect
8585
gopkg.in/inf.v0 v0.9.1 // indirect
86+
gopkg.in/yaml.v2 v2.4.0 // indirect
8687
gopkg.in/yaml.v3 v3.0.1 // indirect
8788
k8s.io/apiextensions-apiserver v0.31.2 // indirect
8889
k8s.io/client-go v0.31.2 // indirect
@@ -91,5 +92,4 @@ require (
9192
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
9293
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
9394
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
94-
sigs.k8s.io/yaml v1.4.0 // indirect
9595
)

internal/controller/ansibleRun/ansibleRun.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939
"github.com/crossplane/crossplane-runtime/pkg/statemetrics"
4040
"github.com/google/uuid"
4141
"github.com/spf13/afero"
42-
"gopkg.in/yaml.v2"
4342
coordinationv1 "k8s.io/api/coordination/v1"
4443
v1 "k8s.io/api/core/v1"
4544
"k8s.io/apimachinery/pkg/api/equality"
@@ -49,6 +48,7 @@ import (
4948
"k8s.io/utils/ptr"
5049
ctrl "sigs.k8s.io/controller-runtime"
5150
"sigs.k8s.io/controller-runtime/pkg/client"
51+
"sigs.k8s.io/yaml"
5252

5353
"github.com/crossplane-contrib/provider-ansible/apis/v1alpha1"
5454
"github.com/crossplane-contrib/provider-ansible/internal/ansible"
@@ -109,7 +109,7 @@ type SetupOptions struct {
109109
AnsibleRolesPath string
110110
Timeout time.Duration
111111
ArtifactsHistoryLimit int
112-
ReplicasCount int
112+
ReplicasCount uint32
113113
ProviderCtx context.Context
114114
ProviderCancel context.CancelFunc
115115
}
@@ -539,11 +539,11 @@ func addBehaviorVars(pc *v1alpha1.ProviderConfig) map[string]string {
539539
return behaviorVars
540540
}
541541

542-
func (c *connector) generateLeaseName(index int) string {
542+
func (c *connector) generateLeaseName(index uint32) string {
543543
return fmt.Sprintf(leaseNameTemplate, index)
544544
}
545545

546-
func (c *connector) releaseLease(ctx context.Context, kube client.Client, index int) error {
546+
func (c *connector) releaseLease(ctx context.Context, kube client.Client, index uint32) error {
547547
leaseName := c.generateLeaseName(index)
548548
ns := "upbound-system"
549549

@@ -556,7 +556,7 @@ func (c *connector) releaseLease(ctx context.Context, kube client.Client, index
556556

557557
// Attempts to acquire or renew a lease for the current replica ID
558558
// Returns an error when unable to obtain the lease
559-
func (c *connector) acquireLease(ctx context.Context, kube client.Client, index int) error {
559+
func (c *connector) acquireLease(ctx context.Context, kube client.Client, index uint32) error {
560560
lease := &coordinationv1.Lease{}
561561
leaseName := c.generateLeaseName(index)
562562
leaseDurationSeconds := ptr.To(int32(leaseDurationSeconds))
@@ -613,19 +613,19 @@ func (c *connector) acquireLease(ctx context.Context, kube client.Client, index
613613

614614
// Finds an available shard and acquires a lease for it. Will attempt to obtain one indefinitely.
615615
// This will also start a background go-routine to renew the lease continuously and release it when the process receives a shutdown signal
616-
func (c *connector) acquireAndHoldShard(o controller.Options, s SetupOptions) (int, error) {
616+
func (c *connector) acquireAndHoldShard(o controller.Options, s SetupOptions) (uint32, error) {
617617
ctx := s.ProviderCtx
618-
currentShard := -1
618+
var currentShard uint32
619619

620620
cfg := ctrl.GetConfigOrDie()
621621
kube, err := client.New(cfg, client.Options{})
622622
if err != nil {
623-
return currentShard, err
623+
return 0, err
624624
}
625625

626626
AcquireLease:
627627
for {
628-
for i := 0; i < s.ReplicasCount; i++ {
628+
for i := uint32(0); i < s.ReplicasCount; i++ {
629629
if err := c.acquireLease(ctx, kube, i); err == nil {
630630
currentShard = i
631631
o.Logger.Debug("acquired lease", "id", i)

pkg/shardutil/shardutil.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// Define a predicate function to filter resources based on consistent hashing
12-
func IsResourceForShard(targetShard, totalShards int) predicate.Predicate {
12+
func IsResourceForShard(targetShard, totalShards uint32) predicate.Predicate {
1313
return predicate.Funcs{
1414
CreateFunc: func(e event.CreateEvent) bool {
1515
return isResourceForShardHelper(e.Object, targetShard, totalShards)
@@ -27,13 +27,13 @@ func IsResourceForShard(targetShard, totalShards int) predicate.Predicate {
2727
}
2828

2929
// Helper function to check if the resource belongs to the current shard
30-
func isResourceForShardHelper(obj client.Object, targetShard, totalShards int) bool {
30+
func isResourceForShardHelper(obj client.Object, targetShard, totalShards uint32) bool {
3131
// Calculate a hash of the resource name
3232
hash := hashString(obj.GetName())
3333
// Perform modulo operation to determine the shard
34-
shard := hash % uint32(totalShards)
34+
shard := hash % totalShards
3535
// Check if the shard matches the target shard
36-
return int(shard) == targetShard
36+
return shard == targetShard
3737
}
3838

3939
// Helper function to hash a string using FNV-1a

0 commit comments

Comments
 (0)