Skip to content

Commit a8a2cd5

Browse files
committed
chore(lint): Run modernize on all packages
1 parent 6420c08 commit a8a2cd5

File tree

15 files changed

+41
-60
lines changed

15 files changed

+41
-60
lines changed

pkg/apis/machine/validation/machinedeployment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package validation
88
import (
99
"fmt"
1010
"math"
11-
"sort"
11+
"slices"
1212

1313
"github.com/gardener/machine-controller-manager/pkg/apis/machine"
1414
"k8s.io/apimachinery/pkg/util/intstr"
@@ -44,7 +44,7 @@ func validateUpdateStrategy(spec *machine.MachineDeploymentSpec, fldPath *field.
4444
supportedStrategies := sets.New(machine.RecreateMachineDeploymentStrategyType, machine.RollingUpdateMachineDeploymentStrategyType, machine.InPlaceUpdateMachineDeploymentStrategyType)
4545
if !supportedStrategies.Has(spec.Strategy.Type) {
4646
strategies := supportedStrategies.UnsortedList()
47-
sort.Slice(strategies, func(i, j int) bool { return strategies[i] < strategies[j] })
47+
slices.Sort(strategies)
4848
allErrs = append(allErrs, field.Invalid(fldPath.Child("strategy.type"), spec.Strategy.Type, fmt.Sprintf("strategy type must be one of %v", strategies)))
4949
}
5050

pkg/controller/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (c *controller) Run(workers int, stopCh <-chan struct{}) {
250250
// every time when the endpoint is called.
251251
prometheus.MustRegister(c)
252252

253-
for i := 0; i < workers; i++ {
253+
for range workers {
254254
worker.Run(c.machineSetQueue, "ClusterMachineSet", worker.DefaultMaxRetries, true, c.reconcileClusterMachineSet, stopCh, &waitGroup)
255255
worker.Run(c.machineDeploymentQueue, "ClusterMachineDeployment", worker.DefaultMaxRetries, true, c.reconcileClusterMachineDeployment, stopCh, &waitGroup)
256256
worker.Run(c.machineSafetyOvershootingQueue, "ClusterMachineSafetyOvershooting", worker.DefaultMaxRetries, true, c.reconcileClusterMachineSafetyOvershooting, stopCh, &waitGroup)

pkg/controller/controller_suite_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"flag"
1010
"fmt"
1111
"io"
12+
"maps"
1213
"testing"
1314
"time"
1415

@@ -24,7 +25,7 @@ import (
2425
"k8s.io/client-go/tools/record"
2526
"k8s.io/client-go/util/workqueue"
2627
"k8s.io/klog/v2"
27-
"k8s.io/utils/pointer"
28+
"k8s.io/utils/ptr"
2829

2930
machine_internal "github.com/gardener/machine-controller-manager/pkg/apis/machine"
3031
"github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
@@ -217,12 +218,8 @@ func newMachinesFromMachineSet(
217218
t := &machineSet.TypeMeta
218219

219220
finalLabels := make(map[string]string, 0)
220-
for k, v := range labels {
221-
finalLabels[k] = v
222-
}
223-
for k, v := range machineSet.Spec.Template.Labels {
224-
finalLabels[k] = v
225-
}
221+
maps.Copy(finalLabels, labels)
222+
maps.Copy(finalLabels, machineSet.Spec.Template.Labels)
226223

227224
return newMachines(
228225
machineCount,
@@ -233,8 +230,8 @@ func newMachinesFromMachineSet(
233230
Kind: t.Kind,
234231
Name: machineSet.Name,
235232
UID: machineSet.UID,
236-
BlockOwnerDeletion: pointer.BoolPtr(true),
237-
Controller: pointer.BoolPtr(true),
233+
BlockOwnerDeletion: ptr.To(true),
234+
Controller: ptr.To(true),
238235
},
239236
annotations,
240237
finalLabels,

pkg/controller/controller_utils.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"encoding/binary"
2828
"fmt"
2929
"hash/fnv"
30+
"maps"
3031
"strconv"
3132
"sync"
3233
"sync/atomic"
@@ -494,9 +495,7 @@ type MachineControlInterface interface {
494495

495496
func getMachinesLabelSet(template *v1alpha1.MachineTemplateSpec) labels.Set {
496497
desiredLabels := make(labels.Set)
497-
for k, v := range template.Labels {
498-
desiredLabels[k] = v
499-
}
498+
maps.Copy(desiredLabels, template.Labels)
500499
return desiredLabels
501500
}
502501

@@ -508,9 +507,7 @@ func getMachinesFinalizers(template *v1alpha1.MachineTemplateSpec) []string {
508507

509508
func getMachinesAnnotationSet(template *v1alpha1.MachineTemplateSpec, _ runtime.Object) labels.Set {
510509
desiredAnnotations := make(labels.Set)
511-
for k, v := range template.Annotations {
512-
desiredAnnotations[k] = v
513-
}
510+
maps.Copy(desiredAnnotations, template.Annotations)
514511
return desiredAnnotations
515512
}
516513

pkg/controller/deployment_sync.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,7 @@ func (dc *controller) scale(ctx context.Context, deployment *v1alpha1.MachineDep
470470
// Incorporate any leftovers to the largest machine set.
471471
if i == 0 && deploymentReplicasToAdd != 0 {
472472
leftover := deploymentReplicasToAdd - deploymentReplicasAdded
473-
nameToSize[is.Name] = nameToSize[is.Name] + leftover
474-
if nameToSize[is.Name] < 0 {
475-
nameToSize[is.Name] = 0
476-
}
473+
nameToSize[is.Name] = max(nameToSize[is.Name]+leftover, 0)
477474
klog.V(3).Infof("leftover proportion increase of %d done in largest machineSet %s", leftover, is.Name)
478475
}
479476

@@ -586,7 +583,7 @@ func (dc *controller) cleanupMachineDeployment(ctx context.Context, oldISs []*v1
586583
sort.Sort(MachineSetsByCreationTimestamp(cleanableISes))
587584
klog.V(4).Infof("Looking to cleanup old machine sets for deployment %q", deployment.Name)
588585

589-
for i := int32(0); i < diff; i++ {
586+
for i := range diff {
590587
is := cleanableISes[i]
591588
// Avoid delete machine set with non-zero replica counts
592589
if is.Status.Replicas != 0 || (is.Spec.Replicas) != 0 || is.Generation > is.Status.ObservedGeneration || is.DeletionTimestamp != nil {
@@ -621,12 +618,9 @@ func (dc *controller) syncMachineDeploymentStatus(ctx context.Context, allISs []
621618
func calculateDeploymentStatus(allISs []*v1alpha1.MachineSet, newIS *v1alpha1.MachineSet, deployment *v1alpha1.MachineDeployment) v1alpha1.MachineDeploymentStatus {
622619
availableReplicas := GetAvailableReplicaCountForMachineSets(allISs)
623620
totalReplicas := GetReplicaCountForMachineSets(allISs)
624-
unavailableReplicas := totalReplicas - availableReplicas
625621
// If unavailableReplicas is negative, then that means the Deployment has more available replicas running than
626622
// desired, e.g. whenever it scales down. In such a case we should simply default unavailableReplicas to zero.
627-
if unavailableReplicas < 0 {
628-
unavailableReplicas = 0
629-
}
623+
unavailableReplicas := max(totalReplicas-availableReplicas, 0)
630624

631625
status := v1alpha1.MachineDeploymentStatus{
632626
// TODO: Ensure that if we start retrying status updates, we won't pick up a new Generation value.

pkg/controller/machineset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ func (c *controller) manageReplicas(ctx context.Context, allMachines []*v1alpha1
445445
// retry the slow start process.
446446
if skippedMachines := diff - successfulCreations; skippedMachines > 0 {
447447
klog.V(2).Infof("Slow-start failure. Skipping creation of %d machines, decrementing expectations for %v %v/%v", skippedMachines, machineSet.Kind, machineSet.Namespace, machineSet.Name)
448-
for i := 0; i < skippedMachines; i++ {
448+
for range skippedMachines {
449449
// Decrement the expected number of creates because the informer won't observe this machine
450450
c.expectations.CreationObserved(machineSetKey)
451451
}
@@ -801,7 +801,7 @@ func (c *controller) updateMachineSetFinalizers(ctx context.Context, machineSet
801801
var err error
802802

803803
// Stop retrying if we exceed finalizerUpdateRetries - the machineSet will be requeued with rate limit
804-
for i := 0; i < finalizerUpdateRetries; i++ {
804+
for range finalizerUpdateRetries {
805805
// Get the latest version of the machineSet so that we can avoid conflicts
806806
machineSet, err = c.controlMachineClient.MachineSets(machineSet.Namespace).Get(ctx, machineSet.Name, metav1.GetOptions{})
807807
if err != nil {

pkg/controller/machineset_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1818
"k8s.io/apimachinery/pkg/runtime"
1919
"k8s.io/client-go/testing"
20-
"k8s.io/utils/pointer"
20+
"k8s.io/utils/ptr"
2121

2222
machinev1 "github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
2323
faketyped "github.com/gardener/machine-controller-manager/pkg/client/clientset/versioned/typed/machine/v1alpha1/fake"
@@ -181,7 +181,7 @@ var _ = Describe("machineset", func() {
181181
Kind: "MachineSet",
182182
Name: "MachineSet-test",
183183
UID: "1234567",
184-
Controller: pointer.Bool(true),
184+
Controller: ptr.To(true),
185185
},
186186
},
187187
},
@@ -318,7 +318,7 @@ var _ = Describe("machineset", func() {
318318
Kind: "MachineSet",
319319
Name: "MachineSet-test",
320320
UID: "1234567",
321-
Controller: pointer.Bool(true),
321+
Controller: ptr.To(true),
322322
},
323323
},
324324
},
@@ -397,7 +397,7 @@ var _ = Describe("machineset", func() {
397397
Kind: "MachineSet",
398398
Name: "MachineSet-test",
399399
UID: "1234567",
400-
Controller: pointer.Bool(true),
400+
Controller: ptr.To(true),
401401
},
402402
},
403403
},
@@ -450,7 +450,7 @@ var _ = Describe("machineset", func() {
450450
Kind: "MachineSet",
451451
Name: "MachineSet-test-dummy",
452452
UID: "1234567",
453-
Controller: pointer.Bool(true),
453+
Controller: ptr.To(true),
454454
},
455455
}
456456
},

pkg/test/integration/common/helpers/cluster.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package helpers
77
import (
88
"context"
99
"fmt"
10+
"maps"
1011

1112
mcmClientset "github.com/gardener/machine-controller-manager/pkg/client/clientset/versioned"
1213
v1 "k8s.io/api/core/v1"
@@ -89,13 +90,11 @@ func (c *Cluster) GetSecretData(machineClassName string, secretRefs ...*v1.Secre
8990
return secretData, nil
9091
}
9192

92-
func mergeDataMaps(in map[string][]byte, maps ...map[string][]byte) map[string][]byte {
93+
func mergeDataMaps(in map[string][]byte, dataMaps ...map[string][]byte) map[string][]byte {
9394
out := make(map[string][]byte)
9495

95-
for _, m := range append([]map[string][]byte{in}, maps...) {
96-
for k, v := range m {
97-
out[k] = v
98-
}
96+
for _, m := range append([]map[string][]byte{in}, dataMaps...) {
97+
maps.Copy(out, m)
9998
}
10099

101100
return out

pkg/util/hash/hash_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func TestDeepHashObject(t *testing.T) {
7676
if hash1 != hash2 {
7777
t.Fatalf("hash of the same object (%q) produced different results: %d vs %d", toString(tc()), hash1, hash2)
7878
}
79-
for i := 0; i < 100; i++ {
79+
for range 100 {
8080
hasher2 := adler32.New()
8181

8282
DeepHashObject(hasher1, tc())
@@ -122,7 +122,7 @@ func TestDeepObjectPointer(t *testing.T) {
122122
myUni3 := unicycle{licencePlateID: "blah", primaryWheel: &wheel3, tags: map[string]string{"color": "blue", "name": "john"}}
123123

124124
// Run it more than once to verify determinism of hasher.
125-
for i := 0; i < 100; i++ {
125+
for range 100 {
126126
hasher1 := adler32.New()
127127
hasher2 := adler32.New()
128128
hasher3 := adler32.New()

pkg/util/labels/labels.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package labels
2424
import (
2525
"encoding/json"
2626
"fmt"
27+
"maps"
2728

2829
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2930
)
@@ -37,9 +38,7 @@ func CloneAndAddLabel(labels map[string]string, labelKey, labelValue string) map
3738
}
3839
// Clone.
3940
newLabels := map[string]string{}
40-
for key, value := range labels {
41-
newLabels[key] = value
42-
}
41+
maps.Copy(newLabels, labels)
4342
newLabels[labelKey] = labelValue
4443
return newLabels
4544
}
@@ -53,9 +52,7 @@ func CloneAndRemoveLabel(labels map[string]string, labelKey string) map[string]s
5352
}
5453
// Clone.
5554
newLabels := map[string]string{}
56-
for key, value := range labels {
57-
newLabels[key] = value
58-
}
55+
maps.Copy(newLabels, labels)
5956
delete(newLabels, labelKey)
6057
return newLabels
6158
}
@@ -87,9 +84,7 @@ func CloneSelectorAndAddLabel(selector *metav1.LabelSelector, labelKey, labelVal
8784
// TODO(madhusudancs): Check if you can use deepCopy_extensions_LabelSelector here.
8885
newSelector.MatchLabels = make(map[string]string)
8986
if selector.MatchLabels != nil {
90-
for key, val := range selector.MatchLabels {
91-
newSelector.MatchLabels[key] = val
92-
}
87+
maps.Copy(newSelector.MatchLabels, selector.MatchLabels)
9388
}
9489
newSelector.MatchLabels[labelKey] = labelValue
9590

0 commit comments

Comments
 (0)