Skip to content

Commit 2d7d599

Browse files
committed
Simplify some code with slices and for-range.
Signed-off-by: Mads Jensen <atombrella@users.noreply.github.com>
1 parent adc3e6b commit 2d7d599

13 files changed

Lines changed: 20 additions & 40 deletions

File tree

pkg/ip/ipmasq_iptables_linux.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"errors"
1919
"fmt"
2020
"net"
21+
"slices"
2122
"strings"
2223

2324
"github.com/coreos/go-iptables/iptables"
@@ -62,18 +63,11 @@ func SetupIPMasq(ipn *net.IPNet, chain string, comment string) error {
6263
}
6364

6465
// Create chain if doesn't exist
65-
exists := false
6666
chains, err := ipt.ListChains("nat")
6767
if err != nil {
6868
return fmt.Errorf("failed to list chains: %v", err)
6969
}
70-
for _, ch := range chains {
71-
if ch == chain {
72-
exists = true
73-
break
74-
}
75-
}
76-
if !exists {
70+
if !slices.Contains(chains, chain) {
7771
if err = ipt.NewChain("nat", chain); err != nil {
7872
return err
7973
}

pkg/ip/link_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func makeVeth(name, vethPeerName string, mtu int, mac string, hostNS ns.NetNS) (
7373
var peerName string
7474
var veth netlink.Link
7575
var err error
76-
for i := 0; i < 10; i++ {
76+
for range 10 {
7777
if vethPeerName != "" {
7878
peerName = vethPeerName
7979
} else {

pkg/ip/utils_linux.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build linux
2-
// +build linux
32

43
// Copyright 2016 CNI authors
54
//

pkg/netlinksafe/netlink.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (h Handle) Close() {
5454
}
5555

5656
func retryOnIntr(f func() error) {
57-
for attempt := 0; attempt < maxAttempts; attempt++ {
57+
for range maxAttempts {
5858
if err := f(); !errors.Is(err, netlink.ErrDumpInterrupted) {
5959
return
6060
}

pkg/ns/ns_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ var _ = Describe("Linux namespace operations", func() {
129129

130130
var wg sync.WaitGroup
131131
wg.Add(concurrency)
132-
for i := 0; i < concurrency; i++ {
132+
for range concurrency {
133133
go func() {
134134
defer wg.Done()
135135
targetNetNS.Do(func(hostNS ns.NetNS) error {

pkg/testutils/testing.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package testutils
1616

1717
import (
18+
"slices"
19+
1820
"github.com/containernetworking/cni/pkg/version"
1921
)
2022

@@ -24,12 +26,7 @@ var AllSpecVersions = [...]string{"0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0", "
2426
// SpecVersionHasIPVersion returns true if the given CNI specification version
2527
// includes the "version" field in the IP address elements
2628
func SpecVersionHasIPVersion(ver string) bool {
27-
for _, i := range []string{"0.3.0", "0.3.1", "0.4.0"} {
28-
if ver == i {
29-
return true
30-
}
31-
}
32-
return false
29+
return slices.Contains([]string{"0.3.0", "0.3.1", "0.4.0"}, ver)
3330
}
3431

3532
// SpecVersionHasCHECK returns true if the given CNI specification version

pkg/utils/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ var _ = Describe("Utils", func() {
122122
It("is (nearly) perfect (injective function)", func() {
123123
hashes := map[string]int{}
124124

125-
for i := 0; i < 1000; i++ {
125+
for i := range 1000 {
126126
name := fmt.Sprintf("string %d", i)
127127
hashes[MustFormatHashWithPrefix(8, "", name)]++
128128
}

plugins/ipam/dhcp/dhcp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ var _ = Describe("DHCP Operations", func() {
342342
wg.Add(3)
343343
started := sync.WaitGroup{}
344344
started.Add(3)
345-
for i := 0; i < 3; i++ {
345+
for range 3 {
346346
go func() {
347347
defer GinkgoRecover()
348348

plugins/ipam/dhcp/options_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func validateRoutes(t *testing.T, routes []*types.Route) {
4646
t.Fatalf("wrong length slice; expected %v, got %v", len(expected), len(routes))
4747
}
4848

49-
for i := 0; i < len(routes); i++ {
49+
for i := range routes {
5050
a := routes[i]
5151
e := expected[i]
5252

plugins/main/bridge/bridge.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"net"
2222
"os"
2323
"runtime"
24-
"sort"
24+
"slices"
2525
"syscall"
2626
"time"
2727

@@ -194,7 +194,7 @@ func collectVlanTrunk(vlanTrunk []*VlanTrunk) ([]int, error) {
194194
for k := range vlanMap {
195195
vlans = append(vlans, k)
196196
}
197-
sort.Slice(vlans, func(i int, j int) bool { return vlans[i] < vlans[j] })
197+
slices.Sort(vlans)
198198
return vlans, nil
199199
}
200200

0 commit comments

Comments
 (0)