Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions pkg/ip/ipmasq_iptables_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"fmt"
"net"
"slices"
"strings"

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

// Create chain if doesn't exist
exists := false
chains, err := ipt.ListChains("nat")
if err != nil {
return fmt.Errorf("failed to list chains: %v", err)
}
for _, ch := range chains {
if ch == chain {
exists = true
break
}
}
if !exists {
if !slices.Contains(chains, chain) {
if err = ipt.NewChain("nat", chain); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ip/link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func makeVeth(name, vethPeerName string, mtu int, mac string, hostNS ns.NetNS) (
var peerName string
var veth netlink.Link
var err error
for i := 0; i < 10; i++ {
for range 10 {
if vethPeerName != "" {
peerName = vethPeerName
} else {
Expand Down
1 change: 0 additions & 1 deletion pkg/ip/utils_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build linux
// +build linux

// Copyright 2016 CNI authors
//
Expand Down
2 changes: 1 addition & 1 deletion pkg/netlinksafe/netlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (h Handle) Close() {
}

func retryOnIntr(f func() error) {
for attempt := 0; attempt < maxAttempts; attempt++ {
for range maxAttempts {
if err := f(); !errors.Is(err, netlink.ErrDumpInterrupted) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ns/ns_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ var _ = Describe("Linux namespace operations", func() {

var wg sync.WaitGroup
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
for range concurrency {
go func() {
defer wg.Done()
targetNetNS.Do(func(hostNS ns.NetNS) error {
Expand Down
9 changes: 3 additions & 6 deletions pkg/testutils/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package testutils

import (
"slices"

"github.com/containernetworking/cni/pkg/version"
)

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

// SpecVersionHasCHECK returns true if the given CNI specification version
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ var _ = Describe("Utils", func() {
It("is (nearly) perfect (injective function)", func() {
hashes := map[string]int{}

for i := 0; i < 1000; i++ {
for i := range 1000 {
name := fmt.Sprintf("string %d", i)
hashes[MustFormatHashWithPrefix(8, "", name)]++
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/ipam/dhcp/dhcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ var _ = Describe("DHCP Operations", func() {
wg.Add(3)
started := sync.WaitGroup{}
started.Add(3)
for i := 0; i < 3; i++ {
for range 3 {
go func() {
defer GinkgoRecover()

Expand Down
2 changes: 1 addition & 1 deletion plugins/ipam/dhcp/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func validateRoutes(t *testing.T, routes []*types.Route) {
t.Fatalf("wrong length slice; expected %v, got %v", len(expected), len(routes))
}

for i := 0; i < len(routes); i++ {
for i := range routes {
a := routes[i]
e := expected[i]

Expand Down
4 changes: 2 additions & 2 deletions plugins/main/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"net"
"os"
"runtime"
"sort"
"slices"
"syscall"
"time"

Expand Down Expand Up @@ -194,7 +194,7 @@ func collectVlanTrunk(vlanTrunk []*VlanTrunk) ([]int, error) {
for k := range vlanMap {
vlans = append(vlans, k)
}
sort.Slice(vlans, func(i int, j int) bool { return vlans[i] < vlans[j] })
slices.Sort(vlans)
return vlans, nil
}

Expand Down
8 changes: 2 additions & 6 deletions plugins/main/host-device/host-device.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"strings"

"github.com/vishvananda/netlink"
Expand Down Expand Up @@ -474,12 +475,7 @@ func hasDpdkDriver(pciaddr string) (bool, error) {
return false, err
}
driverName := driverStat.Name()
for _, drv := range userspaceDrivers {
if driverName == drv {
return true, nil
}
}
return false, nil
return slices.Contains(userspaceDrivers, driverName), nil
}

func printLink(dev netlink.Link, cniVersion string, containerNs ns.NetNS) error {
Expand Down
4 changes: 2 additions & 2 deletions plugins/meta/firewall/firewall_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var _ = Describe("firewall integration tests (ingressPolicy: same-bridge)", func
Expect(err).NotTo(HaveOccurred())
fmt.Fprintf(GinkgoWriter, "root namespace: %s\n", testRootNS.Path())

for i := 0; i < nsCount; i++ {
for i := range nsCount {
targetNS, err := testutils.NewNS()
Expect(err).NotTo(HaveOccurred())
fmt.Fprintf(GinkgoWriter, "namespace %d:%s\n", i, targetNS.Path())
Expand Down Expand Up @@ -202,7 +202,7 @@ func setupNetworks(cniConf *libcni.CNIConfig, testRootNS ns.NetNS, namespaces [n
) [nsCount]*types100.Result {
var results [nsCount]*types100.Result

for i := 0; i < nsCount; i++ {
for i := range nsCount {
runtimeConfig := libcni.RuntimeConf{
ContainerID: fmt.Sprintf("test-cni-firewall-%d", i),
NetNS: namespaces[i].Path(),
Expand Down
12 changes: 3 additions & 9 deletions plugins/meta/portmap/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"math/rand"
"runtime"
"slices"
"sync"

"github.com/coreos/go-iptables/iptables"
Expand Down Expand Up @@ -101,16 +102,9 @@ var _ = Describe("chain tests", func() {
Expect(err).NotTo(HaveOccurred())

// Verify the chain exists
ok := false
chains, err := ipt.ListChains(TABLE)
Expect(err).NotTo(HaveOccurred())
for _, chain := range chains {
if chain == testChain.name {
ok = true
break
}
}
if !ok {
if !slices.Contains(chains, testChain.name) {
Fail("Could not find created chain")
}

Expand Down Expand Up @@ -208,7 +202,7 @@ var _ = Describe("chain tests", func() {
err := testChain.setup(ipt)
Expect(err).NotTo(HaveOccurred())
errCh := make(chan error, N)
for i := 0; i < N; i++ {
for range N {
wg.Add(1)
go func() {
defer wg.Done()
Expand Down