Skip to content

Commit 384baa0

Browse files
committed
Golang 1.24.1 / Golangci-lint 1.64.7
This patch updates VM Operator to use Golang 1.24.1 and Golangci-lint 1.64.7 (as it supports Golang 1.24.1). This patch also addresses several new linter errors: * Loop variables no longer need to be captured due to Go 1.22 * There are a massive number of false positives regarding int overflows. See securego/gosec#1212 for more information. For now the safecast package is used to avoid these linter errors. The linter directive G115 was not disabled because it *does* catch some valid issues, such as trying to convert an int32 to a uint8. There is also a good blog on the issue at https://dev.to/ccoveille/about-the-gosec-g115-drama-or-how-i-faced-back-integer-conversion-overflow-in-go-1302.
1 parent 3235431 commit 384baa0

File tree

25 files changed

+418
-363
lines changed

25 files changed

+418
-363
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ env:
66
# sometimes we need to explicitly override the Go version, ex. CVEs,
77
# when it is not possible to update the go.mod version yet, ex. the
88
# internal builds do not yet support that version of Go.
9-
GO_VERSION: 1.23.6
9+
GO_VERSION:
1010

1111
on:
1212
pull_request:

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ linters:
170170
enable:
171171
- asciicheck
172172
- bodyclose
173+
- copyloopvar
173174
- depguard
174175
- dogsled
175176
- errcheck
176177
- errorlint
177-
- exportloopref
178178
- goconst
179179
- gocritic
180180
- gocyclo

Makefile

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,17 +285,19 @@ lint: ## Run all the lint targets
285285

286286
GOLANGCI_LINT_FLAGS ?= --fast=true
287287
GOLANGCI_LINT_ABS_PATH := $(abspath $(GOLANGCI_LINT))
288+
289+
GO_MOD_DIRS_TO_LINT := $(GO_MOD_DIRS)
290+
GO_MOD_DIRS_TO_LINT := $(filter-out ./external%,$(GO_MOD_DIRS_TO_LINT))
291+
GO_MOD_DIRS_TO_LINT := $(filter-out ./hack/tools%,$(GO_MOD_DIRS_TO_LINT))
292+
GO_LINT_DIR_TARGETS := $(addprefix lint-,$(GO_MOD_DIRS_TO_LINT))
293+
294+
.PHONY: $(GO_LINT_DIR_TARGETS)
295+
$(GO_LINT_DIR_TARGETS): | $(GOLANGCI_LINT)
296+
cd $(subst lint-,,$@) && $(GOLANGCI_LINT_ABS_PATH) run -v $(GOLANGCI_LINT_FLAGS)
297+
288298
.PHONY: lint-go
289-
lint-go: $(GOLANGCI_LINT)
299+
lint-go: $(GO_LINT_DIR_TARGETS)
290300
lint-go: ## Lint codebase
291-
@for dir in $(GO_MOD_DIRS); do \
292-
if [[ "$$dir" == ./external* || "$$dir" == ./hack/tools* ]]; then \
293-
echo "Skipping $$dir"; \
294-
continue; \
295-
fi; \
296-
echo "Running golangci-lint in $$dir"; \
297-
(cd $$dir && $(GOLANGCI_LINT_ABS_PATH) run -v $(GOLANGCI_LINT_FLAGS)); \
298-
done
299301

300302
.PHONY: lint-go-full
301303
lint-go-full: GOLANGCI_LINT_FLAGS = --fast=false

controllers/virtualmachine/volume/volume_controller.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,6 @@ func (r *Reconciler) reconcileInstanceStoragePVCs(ctx *pkgctx.VolumeContext) (bo
374374
createPVCs := len(selectedNode) > 0
375375

376376
for _, pvc := range pvcList {
377-
pvc := pvc
378-
379377
if !pvc.DeletionTimestamp.IsZero() {
380378
// Ignore PVC that is being deleted. Likely this is from a previous failed
381379
// placement and CSI hasn't fully cleaned up yet (a finalizer is still present).

controllers/virtualmachine/volume/volume_controller_unit_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,6 @@ func adjustPVCCreationTimestamp(ctx *pkgctx.VolumeContext, testCtx *builder.Unit
13941394
Expect(err).ToNot(HaveOccurred())
13951395

13961396
for _, pvc := range pvcList {
1397-
pvc := pvc
13981397
pvc.CreationTimestamp = metav1.NewTime(time.Now().Add(-2 * pkgcfg.FromContext(ctx).InstanceStorage.PVPlacementFailedTTL))
13991398
Expect(testCtx.Client.Update(ctx, &pvc)).To(Succeed())
14001399
}
@@ -1431,7 +1430,6 @@ func patchInstanceStoragePVCs(ctx *pkgctx.VolumeContext, testCtx *builder.UnitTe
14311430
Expect(err).ToNot(HaveOccurred())
14321431

14331432
for _, pvc := range pvcList {
1434-
pvc := pvc
14351433
if setStatusBound {
14361434
pvc.Status.Phase = corev1.ClaimBound
14371435
Expect(testCtx.Client.Status().Update(ctx, &pvc)).To(Succeed())

controllers/virtualmachinereplicaset/virtualmachinereplicaset_controller.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828
"strings"
2929
"time"
3030

31+
"github.com/ccoveille/go-safecast"
32+
"github.com/go-logr/logr"
3133
apierrors "k8s.io/apimachinery/pkg/api/errors"
3234
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3335
"k8s.io/apimachinery/pkg/labels"
@@ -41,8 +43,6 @@ import (
4143
"sigs.k8s.io/controller-runtime/pkg/manager"
4244
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4345

44-
"github.com/go-logr/logr"
45-
4646
vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha4"
4747
"github.com/vmware-tanzu/vm-operator/pkg/conditions"
4848
pkgcfg "github.com/vmware-tanzu/vm-operator/pkg/config"
@@ -154,7 +154,6 @@ func (r *Reconciler) getReplicaSetsForVM(
154154

155155
var rss []*vmopv1.VirtualMachineReplicaSet
156156
for _, rs := range rsList.Items {
157-
rs := rs
158157
selector, err := metav1.LabelSelectorAsSelector(rs.Spec.Selector)
159158
if err != nil {
160159
continue
@@ -623,9 +622,9 @@ func (r *Reconciler) updateStatus(
623622

624623
}
625624

626-
newStatus.Replicas = int32(len(filteredVMs))
627-
newStatus.FullyLabeledReplicas = int32(fullyLabeledReplicasCount)
628-
newStatus.ReadyReplicas = int32(readyReplicasCount)
625+
newStatus.Replicas = safecast.MustConvert[int32](len(filteredVMs))
626+
newStatus.FullyLabeledReplicas = safecast.MustConvert[int32](fullyLabeledReplicasCount)
627+
newStatus.ReadyReplicas = safecast.MustConvert[int32](readyReplicasCount)
629628

630629
// Copy the newly calculated status into the VirtualMachineReplicaSet.
631630
if rs.Status.Replicas != newStatus.Replicas ||

controllers/virtualmachineservice/virtualmachineservice_controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"reflect"
1111
"strings"
1212

13+
"github.com/ccoveille/go-safecast"
1314
"github.com/go-logr/logr"
1415
corev1 "k8s.io/api/core/v1"
1516
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -699,7 +700,11 @@ func (r *ReconcileVirtualMachineService) generateSubsetsForService(
699700
}
700701

701702
subset.Ports = append(subset.Ports,
702-
corev1.EndpointPort{Name: portName, Port: int32(portNum), Protocol: portProto})
703+
corev1.EndpointPort{
704+
Name: portName,
705+
Port: safecast.MustConvert[int32](portNum),
706+
Protocol: portProto,
707+
})
703708
}
704709

705710
subsets = append(subsets, subset)

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/vmware-tanzu/vm-operator
22

3-
go 1.23.5
3+
go 1.24.1
44

55
replace (
66
github.com/vmware-tanzu/vm-operator/api => ./api
@@ -15,6 +15,7 @@ replace (
1515
)
1616

1717
require (
18+
github.com/ccoveille/go-safecast v1.6.0
1819
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
1920
github.com/go-logr/logr v1.4.2
2021
github.com/google/go-cmp v0.7.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
22
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
33
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
44
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
5+
github.com/ccoveille/go-safecast v1.6.0 h1:kxc0VIsdEaYoKZbDiGBZBV62zAp0RdtFNH6E3Krev8s=
6+
github.com/ccoveille/go-safecast v1.6.0/go.mod h1:QqwNjxQ7DAqY0C721OIO9InMk9zCwcsO7tnRuHytad8=
57
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
68
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
79
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=

0 commit comments

Comments
 (0)