Skip to content

Commit

Permalink
Merge pull request #5218 from twz123/intrange
Browse files Browse the repository at this point in the history
Enable intrange linter
  • Loading branch information
twz123 authored Nov 13, 2024
2 parents 5480264 + d737ef3 commit 06d20f2
Show file tree
Hide file tree
Showing 28 changed files with 56 additions and 55 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion internal/testutil/fakeclient/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions inttest/ap-controllerworker/controllerworker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions inttest/ap-ha3x3/ha3x3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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)
}

Expand All @@ -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))
}
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions inttest/ap-quorum/quorum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions inttest/ap-quorumsafety/quorumsafety_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
}
}
Expand Down
6 changes: 3 additions & 3 deletions inttest/ap-selector/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
}

Expand All @@ -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))
}
}
Expand Down
2 changes: 1 addition & 1 deletion inttest/basic/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions inttest/bind-address/bind_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions inttest/common/bootloosesuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -606,15 +606,15 @@ 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)
}

machines, err := s.InspectMachines(upstreams)

s.Require().NoError(err)

for i := 0; i < s.ControllerCount; i++ {
for i := range s.ControllerCount {
addresses[i] = machines[i].Status().IP
}
return addresses
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion inttest/configchange/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions inttest/cplb/cplb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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)
}

Expand All @@ -89,21 +89,21 @@ 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++
}
}
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)
}
}
Expand Down Expand Up @@ -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)))
}

Expand Down
2 changes: 1 addition & 1 deletion inttest/dualstack/dualstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions inttest/etcdmember/etcdmember_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion inttest/hacontrolplane/hacontrolplane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions inttest/kubeletcertrotate/kubeletcertrotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down Expand Up @@ -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))
}
Expand Down
10 changes: 5 additions & 5 deletions inttest/nllb/nllb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/k0s/v1beta1/cplb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading

0 comments on commit 06d20f2

Please sign in to comment.