From d737ef3efa48a8216d8cc1043a4b87aee7867190 Mon Sep 17 00:00:00 2001 From: Tom Wieczorek Date: Mon, 11 Nov 2024 17:31:25 +0100 Subject: [PATCH] Enable intrange linter and fix lints on the way Signed-off-by: Tom Wieczorek --- .golangci.yml | 1 + internal/testutil/fakeclient/tracker.go | 2 +- .../ap-controllerworker/controllerworker_test.go | 6 +++--- inttest/ap-ha3x3/ha3x3_test.go | 8 ++++---- inttest/ap-quorum/quorum_test.go | 4 ++-- inttest/ap-quorumsafety/quorumsafety_test.go | 4 ++-- inttest/ap-selector/selector_test.go | 6 +++--- inttest/basic/basic_test.go | 2 +- inttest/bind-address/bind_address_test.go | 4 ++-- inttest/common/bootloosesuite.go | 14 +++++++------- inttest/configchange/config_test.go | 2 +- inttest/cplb/cplb_test.go | 12 ++++++------ inttest/dualstack/dualstack_test.go | 2 +- inttest/etcdmember/etcdmember_test.go | 4 ++-- inttest/hacontrolplane/hacontrolplane_test.go | 2 +- .../kubeletcertrotate/kubeletcertrotate_test.go | 4 ++-- inttest/nllb/nllb_test.go | 10 +++++----- pkg/apis/k0s/v1beta1/cplb_test.go | 2 +- .../cmdprovider/airgapupdate/schedulablewait.go | 2 +- .../plans/cmdprovider/k0supdate/schedulablewait.go | 2 +- .../plans/cmdprovider/k0supdate/utils/find_test.go | 2 +- pkg/autopilot/controller/plans/core/lookup.go | 2 +- pkg/autopilot/signaling/v2/marshal.go | 4 ++-- pkg/autopilot/updater/api.go | 2 +- pkg/component/controller/cplb_unix.go | 2 +- pkg/component/prober/events_test.go | 2 +- pkg/debounce/debounce_test.go | 2 +- pkg/supervisor/supervisor_test.go | 2 +- 28 files changed, 56 insertions(+), 55 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index fc415a734044..e46129443f95 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -16,6 +16,7 @@ linters: - errorlint # Find code that will cause problems with Go's error wrapping scheme - gofmt # Checks whether code was gofmt-ed - goheader # Checks is file headers matche a given pattern + - intrange # checking for loops that could use an integer range - revive # Stricter drop-in replacement for golint linters-settings: diff --git a/internal/testutil/fakeclient/tracker.go b/internal/testutil/fakeclient/tracker.go index 0b3ac9259cf6..66eb1516a7a0 100644 --- a/internal/testutil/fakeclient/tracker.go +++ b/internal/testutil/fakeclient/tracker.go @@ -56,7 +56,7 @@ func NewClientset[T any](discovery *discoveryfake.FakeDiscovery, tracker testing // Set the fake clientset's discovery and tracker. ty := reflect.TypeOf(p).Elem() - for i := 0; i < ty.NumField(); i++ { + for i := range ty.NumField() { f := ty.Field(i) if f.Name == "discovery" && f.Type == reflect.TypeFor[*discoveryfake.FakeDiscovery]() { *(**discoveryfake.FakeDiscovery)(unsafe.Add(unsafe.Pointer(p), f.Offset)) = discovery diff --git a/inttest/ap-controllerworker/controllerworker_test.go b/inttest/ap-controllerworker/controllerworker_test.go index 7fa1e07b32ca..ecbd7583cbcc 100644 --- a/inttest/ap-controllerworker/controllerworker_test.go +++ b/inttest/ap-controllerworker/controllerworker_test.go @@ -57,7 +57,7 @@ func (s *controllerworkerSuite) SetupTest() { // ipAddress := s.GetControllerIPAddress(0) var joinToken string - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { nodeName, require := s.ControllerNode(idx), s.Require() address := s.GetIPAddress(nodeName) @@ -106,7 +106,7 @@ func (s *controllerworkerSuite) SetupTest() { } // Final sanity -- ensure all nodes see each other according to etcd - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.Require().Len(s.GetMembers(idx), s.BootlooseSuite.ControllerCount) } } @@ -171,7 +171,7 @@ spec: kc, err := s.KubeClient(s.ControllerNode(0)) s.NoError(err) - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { nodeName, require := s.ControllerNode(idx), s.Require() require.NoError(s.WaitForNodeReady(nodeName, kc)) // Wait till we see kubelet reporting the expected version. diff --git a/inttest/ap-ha3x3/ha3x3_test.go b/inttest/ap-ha3x3/ha3x3_test.go index e8b43c956f4f..1ffd34faa72d 100644 --- a/inttest/ap-ha3x3/ha3x3_test.go +++ b/inttest/ap-ha3x3/ha3x3_test.go @@ -51,7 +51,7 @@ func (s *ha3x3Suite) SetupTest() { ipAddress := s.GetLBAddress() var joinToken string - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.Require().NoError(s.WaitForSSH(s.ControllerNode(idx), 2*time.Minute, 1*time.Second)) s.PutFile(s.ControllerNode(idx), "/tmp/k0s.yaml", fmt.Sprintf(haControllerConfig, ipAddress)) @@ -74,7 +74,7 @@ func (s *ha3x3Suite) SetupTest() { } // Final sanity -- ensure all nodes see each other according to etcd - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.Require().Len(s.GetMembers(idx), s.BootlooseSuite.ControllerCount) } @@ -88,7 +88,7 @@ func (s *ha3x3Suite) SetupTest() { client, err := s.KubeClient(s.ControllerNode(0)) s.Require().NoError(err) - for idx := 0; idx < s.BootlooseSuite.WorkerCount; idx++ { + for idx := range s.BootlooseSuite.WorkerCount { s.Require().NoError(s.WaitForNodeReady(s.WorkerNode(idx), client)) } } @@ -194,7 +194,7 @@ spec: s.Equal(iptablesModeBeforeUpdate, iptablesModeAfterUpdate) } - for idx := 0; idx < s.ControllerCount; idx++ { + for idx := range s.ControllerCount { node := s.ControllerNode(idx) s.Run("kubelet-config_component_nonexistence/"+node, func() { ssh, err := s.SSH(ctx, node) diff --git a/inttest/ap-quorum/quorum_test.go b/inttest/ap-quorum/quorum_test.go index fec18789ab1b..b85f37ca447b 100644 --- a/inttest/ap-quorum/quorum_test.go +++ b/inttest/ap-quorum/quorum_test.go @@ -46,7 +46,7 @@ func (s *quorumSuite) SetupTest() { ipAddress := s.GetControllerIPAddress(0) var joinToken string - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.Require().NoError(s.WaitForSSH(s.ControllerNode(idx), 2*time.Minute, 1*time.Second)) s.PutFile(s.ControllerNode(idx), "/tmp/k0s.yaml", fmt.Sprintf(k0sConfigWithMultiController, ipAddress)) @@ -70,7 +70,7 @@ func (s *quorumSuite) SetupTest() { } // Final sanity -- ensure all nodes see each other according to etcd - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.Require().Len(s.GetMembers(idx), s.BootlooseSuite.ControllerCount) } } diff --git a/inttest/ap-quorumsafety/quorumsafety_test.go b/inttest/ap-quorumsafety/quorumsafety_test.go index 81c36b9e2697..817f5ca09edc 100644 --- a/inttest/ap-quorumsafety/quorumsafety_test.go +++ b/inttest/ap-quorumsafety/quorumsafety_test.go @@ -46,7 +46,7 @@ func (s *quorumSafetySuite) SetupTest() { ipAddress := s.GetControllerIPAddress(0) var joinToken string - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.Require().NoError(s.WaitForSSH(s.ControllerNode(idx), 2*time.Minute, 1*time.Second)) s.PutFile(s.ControllerNode(idx), "/tmp/k0s.yaml", fmt.Sprintf(k0sConfigWithMultiController, ipAddress)) @@ -70,7 +70,7 @@ func (s *quorumSafetySuite) SetupTest() { } // Final sanity -- ensure all nodes see each other according to etcd - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.Require().Len(s.GetMembers(idx), s.BootlooseSuite.ControllerCount) } } diff --git a/inttest/ap-selector/selector_test.go b/inttest/ap-selector/selector_test.go index 743c8c91cece..10bad0a086f3 100644 --- a/inttest/ap-selector/selector_test.go +++ b/inttest/ap-selector/selector_test.go @@ -47,7 +47,7 @@ func (s *selectorSuite) SetupTest() { ipAddress := s.GetLBAddress() var joinToken string - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.Require().NoError(s.WaitForSSH(s.ControllerNode(idx), 2*time.Minute, 1*time.Second)) s.PutFile(s.ControllerNode(idx), "/tmp/k0s.yaml", fmt.Sprintf(selectorControllerConfig, ipAddress)) @@ -71,7 +71,7 @@ func (s *selectorSuite) SetupTest() { } // Final sanity -- ensure all nodes see each other according to etcd - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.Require().Len(s.GetMembers(idx), s.BootlooseSuite.ControllerCount) } @@ -85,7 +85,7 @@ func (s *selectorSuite) SetupTest() { client, err := s.KubeClient(s.ControllerNode(0)) s.Require().NoError(err) - for idx := 0; idx < s.BootlooseSuite.WorkerCount; idx++ { + for idx := range s.BootlooseSuite.WorkerCount { s.Require().NoError(s.WaitForNodeReady(s.WorkerNode(idx), client)) } } diff --git a/inttest/basic/basic_test.go b/inttest/basic/basic_test.go index 00536543a5a0..89e9487f03f9 100644 --- a/inttest/basic/basic_test.go +++ b/inttest/basic/basic_test.go @@ -108,7 +108,7 @@ func (s *BasicSuite) TestK0sGetsUp() { // We need to first wait till we see pod logs, that's a signal that konnectivity tunnels are up and thus we can then connect to kubelet // via the API. s.Require().NoError(common.WaitForPodLogs(ctx, kc, "kube-system")) - for i := 0; i < s.WorkerCount; i++ { + for i := range s.WorkerCount { node := s.WorkerNode(i) s.T().Logf("checking that we can connect to kubelet metrics on %s", node) s.Require().NoError(common.VerifyKubeletMetrics(ctx, kc, node)) diff --git a/inttest/bind-address/bind_address_test.go b/inttest/bind-address/bind_address_test.go index 31002a4f82d2..6c1dffa98cc6 100644 --- a/inttest/bind-address/bind_address_test.go +++ b/inttest/bind-address/bind_address_test.go @@ -47,7 +47,7 @@ func (s *suite) TestCustomizedBindAddress() { ctx := s.Context() { - for i := 0; i < s.ControllerCount; i++ { + for i := range s.ControllerCount { config, err := yaml.Marshal(&v1beta1.ClusterConfig{ Spec: &v1beta1.ClusterSpec{ API: func() *v1beta1.APISpec { @@ -89,7 +89,7 @@ func (s *suite) TestCustomizedBindAddress() { s.Require().NoError(err) eg, _ := errgroup.WithContext(ctx) - for i := 0; i < s.WorkerCount; i++ { + for i := range s.WorkerCount { nodeName := s.WorkerNode(i) eg.Go(func() error { if err := s.WaitForNodeReady(nodeName, clients); err != nil { diff --git a/inttest/common/bootloosesuite.go b/inttest/common/bootloosesuite.go index 0e1a5aeb0379..3ea0a98b453d 100644 --- a/inttest/common/bootloosesuite.go +++ b/inttest/common/bootloosesuite.go @@ -232,13 +232,13 @@ func signalAwareCtx(parent context.Context) (context.Context, context.CancelCaus // Each node is tried in parallel for ~30secs max func (s *BootlooseSuite) waitForSSH(ctx context.Context) { nodes := []string{} - for i := 0; i < s.ControllerCount; i++ { + for i := range s.ControllerCount { nodes = append(nodes, s.ControllerNode(i)) } - for i := 0; i < s.WorkerCount; i++ { + for i := range s.WorkerCount { nodes = append(nodes, s.WorkerNode(i)) } - for i := 0; i < s.K0smotronWorkerCount; i++ { + for i := range s.K0smotronWorkerCount { nodes = append(nodes, s.K0smotronNode(i)) } if s.WithLB { @@ -606,7 +606,7 @@ listen stats func (s *BootlooseSuite) getControllersIPAddresses() []string { upstreams := make([]string, s.ControllerCount) addresses := make([]string, s.ControllerCount) - for i := 0; i < s.ControllerCount; i++ { + for i := range s.ControllerCount { upstreams[i] = fmt.Sprintf("controller%d", i) } @@ -614,7 +614,7 @@ func (s *BootlooseSuite) getControllersIPAddresses() []string { s.Require().NoError(err) - for i := 0; i < s.ControllerCount; i++ { + for i := range s.ControllerCount { addresses[i] = machines[i].Status().IP } return addresses @@ -664,7 +664,7 @@ func (s *BootlooseSuite) GetJoinToken(role string, extraArgs ...string) (string, // ImportK0smotrtonImages imports func (s *BootlooseSuite) ImportK0smotronImages(ctx context.Context) error { - for i := 0; i < s.WorkerCount; i++ { + for i := range s.WorkerCount { workerNode := s.WorkerNode(i) s.T().Logf("Importing images in %s", workerNode) sshWorker, err := s.SSH(s.Context(), workerNode) @@ -692,7 +692,7 @@ func (s *BootlooseSuite) RunWorkers(args ...string) error { // RunWorkersWithToken joins all the workers to the cluster with the given token func (s *BootlooseSuite) RunWorkersWithToken(token string, args ...string) error { - for i := 0; i < s.WorkerCount; i++ { + for i := range s.WorkerCount { err := s.RunWithToken(s.WorkerNode(i), token, args...) if err != nil { return err diff --git a/inttest/configchange/config_test.go b/inttest/configchange/config_test.go index 7426f849acdf..58a6cf30c4a4 100644 --- a/inttest/configchange/config_test.go +++ b/inttest/configchange/config_test.go @@ -179,7 +179,7 @@ func (s *ConfigSuite) TestK0sGetsUp() { s.Require().NoError(err) defer configMapWatch.Stop() timeout := time.After(20 * time.Second) - for i := 0; i < 2; i++ { + for range 2 { select { case event := <-configMapWatch.ResultChan(): cm := event.Object.(*corev1.ConfigMap) diff --git a/inttest/cplb/cplb_test.go b/inttest/cplb/cplb_test.go index 477d6c66112a..6145212390d3 100644 --- a/inttest/cplb/cplb_test.go +++ b/inttest/cplb/cplb_test.go @@ -55,7 +55,7 @@ func (s *keepalivedSuite) TestK0sGetsUp() { ctx := s.Context() var joinToken string - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.Require().NoError(s.WaitForSSH(s.ControllerNode(idx), 2*time.Minute, 1*time.Second)) s.PutFile(s.ControllerNode(idx), "/tmp/k0s.yaml", fmt.Sprintf(haControllerConfig, lb, lb)) @@ -72,7 +72,7 @@ func (s *keepalivedSuite) TestK0sGetsUp() { } // Final sanity -- ensure all nodes see each other according to etcd - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.Require().Len(s.GetMembers(idx), s.BootlooseSuite.ControllerCount) } @@ -89,13 +89,13 @@ func (s *keepalivedSuite) TestK0sGetsUp() { s.Require().NoError(s.WaitForNodeReady(s.WorkerNode(0), client)) // Verify that all servers have the dummy interface - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.checkDummy(ctx, s.ControllerNode(idx), lb) } // Verify that only one controller has the VIP in eth0 count := 0 - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { if s.hasVIP(ctx, s.ControllerNode(idx), lb) { count++ } @@ -103,7 +103,7 @@ func (s *keepalivedSuite) TestK0sGetsUp() { s.Require().Equal(1, count, "Expected exactly one controller to have the VIP") // Verify that the real servers are present in the ipvsadm output - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.validateRealServers(ctx, s.ControllerNode(idx), lb) } } @@ -136,7 +136,7 @@ func (s *keepalivedSuite) validateRealServers(ctx context.Context, node string, defer ssh.Disconnect() servers := []string{} - for i := 0; i < s.BootlooseSuite.ControllerCount; i++ { + for i := range s.BootlooseSuite.ControllerCount { servers = append(servers, s.GetIPAddress(s.ControllerNode(i))) } diff --git a/inttest/dualstack/dualstack_test.go b/inttest/dualstack/dualstack_test.go index a92802b89b11..73ea2e764a4c 100644 --- a/inttest/dualstack/dualstack_test.go +++ b/inttest/dualstack/dualstack_test.go @@ -113,7 +113,7 @@ func (s *DualstackSuite) SetupSuite() { err = s.WaitForNodeReady(s.WorkerNode(1), client) s.Require().NoError(err) - for i := 0; i < s.WorkerCount; i++ { + for i := range s.WorkerCount { ssh, err := s.SSH(s.Context(), s.WorkerNode(i)) s.Require().NoError(err) defer ssh.Disconnect() diff --git a/inttest/etcdmember/etcdmember_test.go b/inttest/etcdmember/etcdmember_test.go index 43187802bf28..8447def2d344 100644 --- a/inttest/etcdmember/etcdmember_test.go +++ b/inttest/etcdmember/etcdmember_test.go @@ -56,7 +56,7 @@ func (s *EtcdMemberSuite) getMembers(ctx context.Context, fromControllerIdx int) func (s *EtcdMemberSuite) TestDeregistration() { ctx := s.Context() var joinToken string - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.Require().NoError(s.WaitForSSH(s.ControllerNode(idx), 2*time.Minute, 1*time.Second)) // Note that the token is intentionally empty for the first controller @@ -72,7 +72,7 @@ func (s *EtcdMemberSuite) TestDeregistration() { } // Final sanity -- ensure all nodes see each other according to etcd - for idx := 0; idx < s.BootlooseSuite.ControllerCount; idx++ { + for idx := range s.BootlooseSuite.ControllerCount { s.Require().Len(s.GetMembers(idx), s.BootlooseSuite.ControllerCount) } kc, err := s.KubeClient(s.ControllerNode(0)) diff --git a/inttest/hacontrolplane/hacontrolplane_test.go b/inttest/hacontrolplane/hacontrolplane_test.go index 5facbf8d3030..3930e438cf52 100644 --- a/inttest/hacontrolplane/hacontrolplane_test.go +++ b/inttest/hacontrolplane/hacontrolplane_test.go @@ -54,7 +54,7 @@ func (s *HAControlplaneSuite) makeNodeLeave(executeOnControllerIdx int, peerAddr sshCon, err := s.SSH(s.Context(), s.ControllerNode(executeOnControllerIdx)) s.Require().NoError(err) defer sshCon.Disconnect() - for i := 0; i < 20; i++ { + for range 20 { _, err := sshCon.ExecWithOutput(s.Context(), fmt.Sprintf("/usr/local/bin/k0s etcd leave --peer-address %s", peerAddress)) if err == nil { break diff --git a/inttest/kubeletcertrotate/kubeletcertrotate_test.go b/inttest/kubeletcertrotate/kubeletcertrotate_test.go index 418283b992f0..380c86843a62 100644 --- a/inttest/kubeletcertrotate/kubeletcertrotate_test.go +++ b/inttest/kubeletcertrotate/kubeletcertrotate_test.go @@ -64,7 +64,7 @@ func (s *kubeletCertRotateSuite) SetupTest() { client, err := s.KubeClient(s.ControllerNode(0)) s.Require().NoError(err) - for idx := 0; idx < s.BootlooseSuite.WorkerCount; idx++ { + for idx := range s.BootlooseSuite.WorkerCount { s.Require().NoError(s.WaitForNodeReady(s.WorkerNode(idx), client)) } @@ -160,7 +160,7 @@ func (s *kubeletCertRotateSuite) TestApply() { // // Leaving this as 1 for now until the issue is fixed. - for i := 0; i < 1; i++ { + for i := range 1 { s.T().Logf("Applying autopilot plan #%d", i) s.applyPlan(fmt.Sprintf("id%d", i)) } diff --git a/inttest/nllb/nllb_test.go b/inttest/nllb/nllb_test.go index 7d67ae79edb2..4af7edc2c748 100644 --- a/inttest/nllb/nllb_test.go +++ b/inttest/nllb/nllb_test.go @@ -80,7 +80,7 @@ func (s *suite) TestNodeLocalLoadBalancing() { }) s.Require().NoError(err) - for i := 0; i < s.ControllerCount; i++ { + for i := range s.ControllerCount { s.WriteFileContent(s.ControllerNode(i), "/tmp/k0s.yaml", config) } } @@ -98,7 +98,7 @@ func (s *suite) TestNodeLocalLoadBalancing() { s.Require().NoError(err) eg, _ := errgroup.WithContext(ctx) - for i := 0; i < s.WorkerCount; i++ { + for i := range s.WorkerCount { nodeName := s.WorkerNode(i) eg.Go(func() error { if err := s.WaitForNodeReady(nodeName, clients); err != nil { @@ -130,7 +130,7 @@ func (s *suite) TestNodeLocalLoadBalancing() { }) workerNameToRestart := s.WorkerNode(0) - for i := 0; i < s.ControllerCount; i++ { + for i := range s.ControllerCount { controllerName := s.ControllerNode(i) s.Run(fmt.Sprintf("stop_%s_before_%s", workerNameToRestart, controllerName), func() { err := s.StopWorker(workerNameToRestart) @@ -216,7 +216,7 @@ func (s *suite) TestNodeLocalLoadBalancing() { func (s *suite) checkClusterReadiness(ctx context.Context, clients *kubernetes.Clientset, numControllers int, degradedControllers ...string) error { eg, ctx := errgroup.WithContext(ctx) - for i := 0; i < numControllers; i++ { + for i := range numControllers { nodeName := s.ControllerNode(i) degraded := slices.Contains(degradedControllers, nodeName) @@ -240,7 +240,7 @@ func (s *suite) checkClusterReadiness(ctx context.Context, clients *kubernetes.C }) } - for i := 0; i < s.WorkerCount; i++ { + for i := range s.WorkerCount { nodeName := s.WorkerNode(i) eg.Go(func() error { diff --git a/pkg/apis/k0s/v1beta1/cplb_test.go b/pkg/apis/k0s/v1beta1/cplb_test.go index f51e1c57b15c..676ce40501e6 100644 --- a/pkg/apis/k0s/v1beta1/cplb_test.go +++ b/pkg/apis/k0s/v1beta1/cplb_test.go @@ -130,7 +130,7 @@ func (s *CPLBSuite) TestValidateVRRPInstances() { s.Require().Empty(err, "Test case %s expected no errors. Got: %v", tt.name, err) s.T().Log(k.VRRPInstances) s.Require().Equal(len(tt.expectedVRRPs), len(k.VRRPInstances), "Expected and actual VRRPInstances length mismatch") - for i := 0; i < len(tt.expectedVRRPs); i++ { + for i := range tt.expectedVRRPs { s.Require().Equal(tt.expectedVRRPs[i].Interface, k.VRRPInstances[i].Interface, "Interface mismatch") s.Require().Equal(tt.expectedVRRPs[i].VirtualRouterID, k.VRRPInstances[i].VirtualRouterID, "Virtual router ID mismatch") s.Require().Equal(tt.expectedVRRPs[i].AdvertIntervalSeconds, k.VRRPInstances[i].AdvertIntervalSeconds, "Advertisement interval mismatch") diff --git a/pkg/autopilot/controller/plans/cmdprovider/airgapupdate/schedulablewait.go b/pkg/autopilot/controller/plans/cmdprovider/airgapupdate/schedulablewait.go index 92ad12684be6..51ff05c1f2cf 100644 --- a/pkg/autopilot/controller/plans/cmdprovider/airgapupdate/schedulablewait.go +++ b/pkg/autopilot/controller/plans/cmdprovider/airgapupdate/schedulablewait.go @@ -64,7 +64,7 @@ func (aup *airgapupdate) SchedulableWait(ctx context.Context, planID string, cmd // against the current state maintained in the plan status. This ensures that any signal nodes that // have been transitioned to 'Completed' will also appear in the plan status as 'Completed'. func (aup *airgapupdate) reconcileSignalNodeStatusTarget(ctx context.Context, planID string, cmdStatus apv1beta2.PlanCommandStatus, delegate apdel.ControllerDelegate, signalNodes []apv1beta2.PlanCommandTargetStatus) { - for i := 0; i < len(signalNodes); i++ { + for i := range signalNodes { if signalNodes[i].State == appc.SignalCompleted { continue } diff --git a/pkg/autopilot/controller/plans/cmdprovider/k0supdate/schedulablewait.go b/pkg/autopilot/controller/plans/cmdprovider/k0supdate/schedulablewait.go index 48bab2166d30..023f28e1f60a 100644 --- a/pkg/autopilot/controller/plans/cmdprovider/k0supdate/schedulablewait.go +++ b/pkg/autopilot/controller/plans/cmdprovider/k0supdate/schedulablewait.go @@ -106,7 +106,7 @@ func (kp *k0supdate) reconcileSignalNodeStatus(ctx context.Context, planID strin // against the current state maintained in the plan status. This ensures that any signal nodes that // have been transitioned to 'Completed' will also appear in the plan status as 'Completed'. func (kp *k0supdate) reconcileSignalNodeStatusTarget(ctx context.Context, planID string, cmdStatus apv1beta2.PlanCommandStatus, delegate apdel.ControllerDelegate, signalNodes []apv1beta2.PlanCommandTargetStatus) { - for i := 0; i < len(signalNodes); i++ { + for i := range signalNodes { key := delegate.CreateNamespacedName(signalNodes[i].Name) signalNode := delegate.CreateObject() diff --git a/pkg/autopilot/controller/plans/cmdprovider/k0supdate/utils/find_test.go b/pkg/autopilot/controller/plans/cmdprovider/k0supdate/utils/find_test.go index 4042b528fc31..8e9ca29278d0 100644 --- a/pkg/autopilot/controller/plans/cmdprovider/k0supdate/utils/find_test.go +++ b/pkg/autopilot/controller/plans/cmdprovider/k0supdate/utils/find_test.go @@ -40,7 +40,7 @@ func TestFindNextPendingRandom(t *testing.T) { // randomness is working. Its possible that one node can be chosen // 100% of the time, but its unlikely. - for i := 0; i < 1000; i++ { + for range 1000 { node, err := FindNextPendingRandom(nodes) assert.NoError(t, err) assert.NotNil(t, node) diff --git a/pkg/autopilot/controller/plans/core/lookup.go b/pkg/autopilot/controller/plans/core/lookup.go index cae4e67fec94..b3191cdc0a4a 100644 --- a/pkg/autopilot/controller/plans/core/lookup.go +++ b/pkg/autopilot/controller/plans/core/lookup.go @@ -29,7 +29,7 @@ import ( func planCommandProviderLookup(pcpm PlanCommandProviderMap, cmd apv1beta2.PlanCommand) (string, PlanCommandProvider, bool) { rpcmd := reflect.Indirect(reflect.ValueOf(cmd)) - for i := 0; i < rpcmd.NumField(); i++ { + for i := range rpcmd.NumField() { v := rpcmd.Field(i) if v.Kind() == reflect.Pointer && !v.IsNil() { diff --git a/pkg/autopilot/signaling/v2/marshal.go b/pkg/autopilot/signaling/v2/marshal.go index 3d8e9e29131c..73324d6e88e8 100644 --- a/pkg/autopilot/signaling/v2/marshal.go +++ b/pkg/autopilot/signaling/v2/marshal.go @@ -29,7 +29,7 @@ func Marshal(m map[string]string, value interface{}) { fields := reflect.TypeOf(value) values := reflect.ValueOf(value) - for i := 0; i < fields.NumField(); i++ { + for i := range fields.NumField() { field := fields.Field(i) value := values.Field(i) @@ -62,7 +62,7 @@ func Unmarshal(m map[string]string, uftc UnmarshalFieldTypeCollector, ufvc Unmar fields := uftc() values := ufvc() - for i := 0; i < fields.NumField(); i++ { + for i := range fields.NumField() { field := fields.Field(i) value := values.Field(i) diff --git a/pkg/autopilot/updater/api.go b/pkg/autopilot/updater/api.go index 9c4452d81b71..cecdc5c879ed 100644 --- a/pkg/autopilot/updater/api.go +++ b/pkg/autopilot/updater/api.go @@ -114,7 +114,7 @@ func comparePrerelease(v, o string) int { } // Iterate over each part of the prereleases to compare the differences. - for i := 0; i < l; i++ { + for i := range l { // Since the lentgh of the parts can be different we need to create // a placeholder. This is to avoid out of bounds issues. stemp := "" diff --git a/pkg/component/controller/cplb_unix.go b/pkg/component/controller/cplb_unix.go index 6d14cfdfeb4f..b56bfdc699c4 100644 --- a/pkg/component/controller/cplb_unix.go +++ b/pkg/component/controller/cplb_unix.go @@ -243,7 +243,7 @@ func (k *Keepalived) ensureLinkAddresses(linkName string, expectedAddresses []st } // Remove unexpected addresses - for i := 0; i < len(linkAddrs); i++ { + for i := range linkAddrs { strAddr := strAddrs[i] linkAddr := linkAddrs[i] if !slices.Contains(expectedAddresses, strAddrs[i]) { diff --git a/pkg/component/prober/events_test.go b/pkg/component/prober/events_test.go index 74d5ef1633ac..891ed2732805 100644 --- a/pkg/component/prober/events_test.go +++ b/pkg/component/prober/events_test.go @@ -83,7 +83,7 @@ func TestEvents(t *testing.T) { emitter := &EventEmitter{ events: make(chan Event, 10), } - for i := 0; i < 20; i++ { + for range 20 { emitter.Emit("Test event") } }) diff --git a/pkg/debounce/debounce_test.go b/pkg/debounce/debounce_test.go index 5ead4811cc64..c9a9466be8d9 100644 --- a/pkg/debounce/debounce_test.go +++ b/pkg/debounce/debounce_test.go @@ -49,7 +49,7 @@ func TestDebounce(t *testing.T) { runReturned := make(chan error) go func() { runReturned <- debouncer.Run(ctx) }() - for i := 0; i < 1000; i++ { + for range 1000 { time.Sleep(10 * time.Millisecond) if atomic.LoadInt32(&lastItem) == numEvents { break diff --git a/pkg/supervisor/supervisor_test.go b/pkg/supervisor/supervisor_test.go index 925486546b24..c247f25a1502 100644 --- a/pkg/supervisor/supervisor_test.go +++ b/pkg/supervisor/supervisor_test.go @@ -229,7 +229,7 @@ func TestMultiThread(t *testing.T) { assert.NoError(t, s.Supervise(), "Failed to start") t.Cleanup(s.Stop) - for i := 0; i < 255; i++ { + for range 255 { wg.Add(1) go func() { defer wg.Done()