Skip to content

Commit

Permalink
Linter updates due to bumping go.mod to Go v1.22
Browse files Browse the repository at this point in the history
Updating our controller-runtime and Kubernetes dependencies bumped our
minimum Go version to v1.22. That in turn enables some new linters,
since we no longer need to copy range vars in Go v1.22.

Signed-off-by: Nic Cope <nicc@rk0n.org>
  • Loading branch information
negz committed May 7, 2024
1 parent be9b5f6 commit 9e68916
Show file tree
Hide file tree
Showing 24 changed files with 16 additions and 34 deletions.
13 changes: 10 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ linters:
# to communicate what the bool means.
- nonamedreturns

# TODO(negz): We do want these as of Go v1.22.
- copyloopvar
- intrange
# Warns about taking the address of a range variable. This isn't an issue in
# Go v1.22 and above: https://tip.golang.org/doc/go1.22
- exportloopref

linters-settings:
errcheck:
Expand Down Expand Up @@ -287,6 +287,13 @@ issues:
linters:
- gosec
- gas

# This is about implicit memory aliasing in a range loop.
# This is a false positive with Go v1.22 and above.
- text: "G601:"
linters:
- gosec
- gas

# Some k8s dependencies do not have JSON tags on all fields in structs.
- path: k8s.io/
Expand Down
2 changes: 0 additions & 2 deletions apis/pkg/v1/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,6 @@ type PackageRevisionList interface {
func (p *ProviderRevisionList) GetRevisions() []PackageRevision {
prs := make([]PackageRevision, len(p.Items))
for i, r := range p.Items {
r := r // Pin range variable so we can take its address.
prs[i] = &r
}
return prs
Expand All @@ -736,7 +735,6 @@ func (p *ProviderRevisionList) GetRevisions() []PackageRevision {
func (p *ConfigurationRevisionList) GetRevisions() []PackageRevision {
prs := make([]PackageRevision, len(p.Items))
for i, r := range p.Items {
r := r // Pin range variable so we can take its address.
prs[i] = &r
}
return prs
Expand Down
1 change: 0 additions & 1 deletion apis/pkg/v1beta1/function_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ func (r *FunctionRevision) SetCommonLabels(l map[string]string) {
func (p *FunctionRevisionList) GetRevisions() []v1.PackageRevision {
prs := make([]v1.PackageRevision, len(p.Items))
for i, r := range p.Items {
r := r // Pin range variable so we can take its address.
prs[i] = &r
}
return prs
Expand Down
2 changes: 0 additions & 2 deletions apis/pkg/v1beta1/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ type LockPackage struct {
func ToNodes(pkgs ...LockPackage) []dag.Node {
nodes := make([]dag.Node, len(pkgs))
for i, r := range pkgs {
r := r // Pin range variable so we can take its address.
nodes[i] = &r
}
return nodes
Expand All @@ -75,7 +74,6 @@ func (l *LockPackage) Identifier() string {
func (l *LockPackage) Neighbors() []dag.Node {
nodes := make([]dag.Node, len(l.Dependencies))
for i, r := range l.Dependencies {
r := r // Pin range variable so we can take its address.
nodes[i] = &r
}
return nodes
Expand Down
1 change: 0 additions & 1 deletion cmd/crank/beta/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ func filterExtraResources(ers []unstructured.Unstructured, selector *fnv1beta1.R
}
out := &fnv1beta1.Resources{}
for _, er := range ers {
er := er
if selector.GetApiVersion() != er.GetAPIVersion() {
continue
}
Expand Down
1 change: 0 additions & 1 deletion cmd/crank/xpkg/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ func (c *pushCmd) Run(logger logging.Logger) error { //nolint:gocognit // This f
adds := make([]mutate.IndexAddendum, len(c.PackageFiles))
g, ctx := errgroup.WithContext(context.Background())
for i, file := range c.PackageFiles {
i, file := i, file // Pin range variables for use in goroutine
g.Go(func() error {
img, err := tarball.ImageFromPath(filepath.Clean(file), nil)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,6 @@ func (e *ExistingExtraResourcesFetcher) Fetch(ctx context.Context, rs *v1beta1.R

resources := make([]*v1beta1.Resource, len(list.Items))
for i, r := range list.Items {
r := r
o, err := AsStruct(&r)
if err != nil {
return nil, errors.Wrap(err, errExtraResourceAsStruct)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func AssociateByOrder(t []v1.ComposedTemplate, r []corev1.ObjectReference) []Tem
j = len(r)
}

for i := 0; i < j; i++ {
for i := range j {
a[i].Reference = r[i]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func sortConfigs(ec []v1alpha1.EnvironmentConfig, f string) error {
}, len(ec))

var valsKind reflect.Kind
for i := 0; i < len(ec); i++ {
for i := range len(ec) {
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&ec[i])
if err != nil {
return err
Expand Down
2 changes: 0 additions & 2 deletions internal/controller/apiextensions/definition/composed.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ func (i *composedResourceInformers) cleanupComposedResourceInformers(ctx context
// fast enough for now. It's all in-memory.
referenced := make(map[schema.GroupVersionKind]bool)
for _, crd := range crds.Items {
crd := crd

if !xcrd.IsEstablished(crd.Status) {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/apiextensions/usage/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,14 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
if u.Spec.ReplayDeletion != nil && *u.Spec.ReplayDeletion && used.GetAnnotations() != nil {
if policy, ok := used.GetAnnotations()[usage.AnnotationKeyDeletionAttempt]; ok {
// We have already recorded a deletion attempt and want to replay deletion, let's delete the used resource.
//nolint:contextcheck // See comment on Delete below.

//nolint:contextcheck // We cannot use the context from the reconcile function since it will be cancelled after the reconciliation.
go func() {
// We do the deletion async and after some delay to make sure the usage is deleted before the
// deletion attempt. We remove the finalizer on this Usage right below, so, we know it will disappear
// very soon.
time.Sleep(2 * time.Second)
log.Info("Replaying deletion of the used resource", "apiVersion", used.GetAPIVersion(), "kind", used.GetKind(), "name", used.GetName(), "policy", policy)
// We cannot use the context from the reconcile function since it will be cancelled after the reconciliation.
if err = r.client.Delete(context.Background(), used, client.PropagationPolicy(policy)); err != nil {
log.Info("Error when replaying deletion of the used resource", "apiVersion", used.GetAPIVersion(), "kind", used.GetKind(), "name", used.GetName(), "err", err)
}
Expand Down
3 changes: 0 additions & 3 deletions internal/controller/pkg/revision/establisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ func (e *APIEstablisher) ReleaseObjects(ctx context.Context, parent v1.PackageRe
g, ctx := errgroup.WithContext(ctx)
g.SetLimit(maxConcurrentEstablishers)
for _, ref := range allObjs {
ref := ref // Pin the loop variable.
g.Go(func() error {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -235,7 +234,6 @@ func (e *APIEstablisher) validate(ctx context.Context, objs []runtime.Object, pa
g.SetLimit(maxConcurrentEstablishers)
out := make(chan currentDesired, len(objs))
for _, res := range objs {
res := res // Pin the range variable before using it in a Goroutine.
g.Go(func() error {
// Assert desired object to resource.Object so that we can access its
// metadata.
Expand Down Expand Up @@ -393,7 +391,6 @@ func (e *APIEstablisher) establish(ctx context.Context, allObjs []currentDesired
g.SetLimit(maxConcurrentEstablishers)
out := make(chan xpv1.TypedReference, len(allObjs))
for _, cd := range allObjs {
cd := cd // Pin the loop variable.
g.Go(func() error {
if !cd.Exists {
// Only create a missing resource if we are going to control it.
Expand Down
1 change: 0 additions & 1 deletion internal/controller/rbac/definition/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco

applied := make([]string, 0)
for _, cr := range r.rbac.RenderClusterRoles(d) {
cr := cr // Pin range variable so we can take its address.
log := log.WithValues("role-name", cr.GetName())
origRV := ""
err := r.client.Apply(ctx, &cr,
Expand Down
1 change: 0 additions & 1 deletion internal/controller/rbac/namespace/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
var applied []string //nolint:prealloc // We don't know how many roles we'll apply.
for _, rl := range r.rbac.RenderRoles(ns, l.Items) {
log := log.WithValues("role-name", rl.GetName())
rl := rl // Pin range variable so we can take its address.

err := r.client.Apply(ctx, &rl, resource.MustBeControllableBy(ns.GetUID()), resource.AllowUpdateIf(RolesDiffer))
if resource.IsNotAllowed(err) {
Expand Down
1 change: 0 additions & 1 deletion internal/controller/rbac/provider/roles/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco

applied := make([]string, 0)
for _, cr := range r.rbac.RenderClusterRoles(pr, resources) {
cr := cr // Pin range variable so we can take its address.
log := log.WithValues("role-name", cr.GetName())
origRV := ""
err := r.client.Apply(ctx, &cr,
Expand Down
2 changes: 0 additions & 2 deletions internal/dag/dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func (s *simpleNode) Neighbors() []Node {
nodes := make([]Node, len(s.neighbors))
i := 0
for _, r := range s.neighbors {
r := r // Pin range variable so we can take its address.
nodes[i] = &r
i++
}
Expand All @@ -58,7 +57,6 @@ func (s *simpleNode) AddNeighbors(nodes ...Node) error {
func toNodes(n []simpleNode) []Node {
nodes := make([]Node, len(n))
for i, r := range n {
r := r // Pin range variable so we can take its address.
nodes[i] = &r
}
return nodes
Expand Down
2 changes: 0 additions & 2 deletions internal/dag/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type SimpleFuzzNode struct {
func toNodesFuzz(n []SimpleFuzzNode) []Node {
nodes := make([]Node, len(n))
for i, r := range n {
r := r // Pin range variable so we can take its address.
nodes[i] = &r
}
return nodes
Expand Down Expand Up @@ -59,7 +58,6 @@ func (s *SimpleFuzzNode) Neighbors() []Node {
nodes := make([]Node, len(s.NeighborsField))
i := 0
for _, r := range s.NeighborsField {
r := r // Pin range variable so we can take its address.
nodes[i] = &r
i++
}
Expand Down
2 changes: 1 addition & 1 deletion internal/names/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (r *nameGenerator) GenerateName(ctx context.Context, cd resource.Object) er
// locally. To reduce that risk even further the caller must employ a
// conflict recovery mechanism.
maxTries := 10
for i := 0; i < maxTries; i++ {
for range maxTries {
name := r.namer.GenerateName(cd.GetGenerateName())
obj := composite.Unstructured{}
obj.SetGroupVersionKind(cd.GetObjectKind().GroupVersionKind())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ func (v *validator) getNeededCRDs(ctx context.Context, comp *v1.Composition) (ma
// Get schema for all Managed Resource Definitions defined by
// comp.Spec.Resources.
for _, res := range comp.Spec.Resources {
res := res
gvk, err := composition.GetBaseObjectGVK(&res)
if err != nil {
return nil, []error{err}
Expand Down
2 changes: 1 addition & 1 deletion internal/xpkg/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func FuzzFindXpkgInDir(f *testing.F) {
fs.Remove(createdFile)
}
}()
for i := 0; i < noOfFiles%500; i++ {
for range noOfFiles % 500 {
fname, err := ff.GetString()
if err != nil {
t.Skip()
Expand Down
1 change: 0 additions & 1 deletion internal/xpkg/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func TryConvert(obj runtime.Object, candidates ...conversion.Hub) (runtime.Objec
}

for _, c := range candidates {
c := c
if err := cvt.ConvertTo(c); err == nil {
return c, true
}
Expand Down
1 change: 0 additions & 1 deletion pkg/validation/apiextensions/v1/composition/patches.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ func validateTransformsChainIOTypes(transforms []v1.Transform, fromType xpschema
return "", field.InternalError(field.NewPath("transforms"), err)
}
for i, transform := range transforms {
transform := transform
err := IsValidInputForTransform(&transform, inputType)
if err != nil && inputType != "" {
return "", field.Invalid(field.NewPath("transforms").Index(i), transform, err.Error())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,6 @@ func defaultGKToCRDs() map[schema.GroupKind]apiextensions.CustomResourceDefiniti
crds := []apiextensions.CustomResourceDefinition{*defaultManagedCrdBuilder().build(), *defaultCompositeCrdBuilder().build()}
m := make(map[schema.GroupKind]apiextensions.CustomResourceDefinition, len(crds))
for _, crd := range crds {
crd := crd
m[schema.GroupKind{
Group: crd.Spec.Group,
Kind: crd.Spec.Names.Kind,
Expand Down
1 change: 0 additions & 1 deletion test/e2e/funcs/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,6 @@ func valueOrError(s string, err error) string {
func itemsToObjects(items []unstructured.Unstructured) []client.Object {
objects := make([]client.Object, len(items))
for i, item := range items {
item := item // unalias loop variable
objects[i] = &item
}
return objects
Expand Down

0 comments on commit 9e68916

Please sign in to comment.