From 0ae3f972adb6b346cee2e2e9937b7812170f9219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Tue, 16 Dec 2025 13:06:20 +0100 Subject: [PATCH] daemon: Simplify slices.Contains usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove unnecessary intermediate variables and helper functions when using slices.Contains. Signed-off-by: Paweł Gronowski --- daemon/libnetwork/networkdb/cluster.go | 7 +------ daemon/libnetwork/networkdb/delegate.go | 5 +---- daemon/libnetwork/networkdb/networkdb.go | 3 +-- daemon/oci_linux.go | 8 +------- daemon/oci_linux_test.go | 3 ++- daemon/pkg/oci/caps/utils.go | 13 ++++--------- daemon/pkg/oci/caps/utils_linux.go | 3 ++- 7 files changed, 12 insertions(+), 30 deletions(-) diff --git a/daemon/libnetwork/networkdb/cluster.go b/daemon/libnetwork/networkdb/cluster.go index 2ed3a2c619..3c7ceb7c34 100644 --- a/daemon/libnetwork/networkdb/cluster.go +++ b/daemon/libnetwork/networkdb/cluster.go @@ -543,12 +543,7 @@ func (nDB *NetworkDB) bulkSyncTables() { // successfully completed bulk sync in this iteration. updatedNetworks := make([]string, 0, len(networks)) for _, nid := range networks { - var found bool - if slices.Contains(completed, nid) { - found = true - } - - if !found { + if !slices.Contains(completed, nid) { updatedNetworks = append(updatedNetworks, nid) } } diff --git a/daemon/libnetwork/networkdb/delegate.go b/daemon/libnetwork/networkdb/delegate.go index 85bdc37af5..9b363eb545 100644 --- a/daemon/libnetwork/networkdb/delegate.go +++ b/daemon/libnetwork/networkdb/delegate.go @@ -154,10 +154,7 @@ func (nDB *NetworkDB) handleTableEvent(tEvent *TableEvent, isBulkSync bool) bool network, ok := nDB.thisNodeNetworks[tEvent.NetworkID] // Check if the owner of the event is still part of the network nodes := nDB.networkNodes[tEvent.NetworkID] - var nodePresent bool - if slices.Contains(nodes, tEvent.NodeName) { - nodePresent = true - } + nodePresent := slices.Contains(nodes, tEvent.NodeName) if !ok || network.leaving || !nodePresent { // I'm out of the network OR the event owner is not anymore part of the network so do not propagate diff --git a/daemon/libnetwork/networkdb/networkdb.go b/daemon/libnetwork/networkdb/networkdb.go index 6b97bff2ff..3e077b9cb5 100644 --- a/daemon/libnetwork/networkdb/networkdb.go +++ b/daemon/libnetwork/networkdb/networkdb.go @@ -730,8 +730,7 @@ func (nDB *NetworkDB) LeaveNetwork(nid string) error { // in the passed network only if it is not already present. Caller // should hold the NetworkDB lock while calling this func (nDB *NetworkDB) addNetworkNode(nid string, nodeName string) { - nodes := nDB.networkNodes[nid] - if slices.Contains(nodes, nodeName) { + if slices.Contains(nDB.networkNodes[nid], nodeName) { return } diff --git a/daemon/oci_linux.go b/daemon/oci_linux.go index 445fd6a4da..0c8f282138 100644 --- a/daemon/oci_linux.go +++ b/daemon/oci_linux.go @@ -466,12 +466,6 @@ var ( } ) -// inSlice tests whether a string is contained in a slice of strings or not. -// Comparison is case sensitive -func inSlice(slice []string, s string) bool { - return slices.Contains(slice, s) -} - // withMounts sets the container's mounts func withMounts(daemon *Daemon, daemonCfg *configStore, c *container.Container, mounts []container.Mount) coci.SpecOpts { return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error { @@ -641,7 +635,7 @@ func withMounts(daemon *Daemon, daemonCfg *configStore, c *container.Container, continue } if _, ok := userMounts[m.Destination]; !ok { - if !inSlice(m.Options, "ro") { + if !slices.Contains(m.Options, "ro") { s.Mounts[i].Options = append(s.Mounts[i].Options, "ro") } } diff --git a/daemon/oci_linux_test.go b/daemon/oci_linux_test.go index 8a6320f590..9e2c361a83 100644 --- a/daemon/oci_linux_test.go +++ b/daemon/oci_linux_test.go @@ -4,6 +4,7 @@ import ( "context" "os" "path/filepath" + "slices" "testing" "github.com/google/go-cmp/cmp/cmpopts" @@ -119,7 +120,7 @@ func TestIpcPrivateVsReadonly(t *testing.T) { if m.Destination != "/dev/shm" { continue } - assert.Check(t, is.Equal(false, inSlice(m.Options, "ro"))) + assert.Check(t, is.Equal(false, slices.Contains(m.Options, "ro"))) } } diff --git a/daemon/pkg/oci/caps/utils.go b/daemon/pkg/oci/caps/utils.go index 9bed97b297..aa3513e808 100644 --- a/daemon/pkg/oci/caps/utils.go +++ b/daemon/pkg/oci/caps/utils.go @@ -38,11 +38,6 @@ func knownCapabilities() map[string]*struct{} { return knownCaps } -// inSlice tests whether a string is contained in a slice of strings or not. -func inSlice(slice []string, s string) bool { - return slices.Contains(slice, s) -} - const allCapabilities = "ALL" // NormalizeLegacyCapabilities normalizes, and validates CapAdd/CapDrop capabilities @@ -99,20 +94,20 @@ func TweakCapabilities(basics, adds, drops []string, privileged bool) ([]string, var caps []string switch { - case inSlice(capAdd, allCapabilities): + case slices.Contains(capAdd, allCapabilities): // Add all capabilities except ones on capDrop for _, c := range GetAllCapabilities() { - if !inSlice(capDrop, c) { + if !slices.Contains(capDrop, c) { caps = append(caps, c) } } - case inSlice(capDrop, allCapabilities): + case slices.Contains(capDrop, allCapabilities): // "Drop" all capabilities; use what's in capAdd instead caps = capAdd default: // First drop some capabilities for _, c := range basics { - if !inSlice(capDrop, c) { + if !slices.Contains(capDrop, c) { caps = append(caps, c) } } diff --git a/daemon/pkg/oci/caps/utils_linux.go b/daemon/pkg/oci/caps/utils_linux.go index e40b84b5fe..da90ef68a8 100644 --- a/daemon/pkg/oci/caps/utils_linux.go +++ b/daemon/pkg/oci/caps/utils_linux.go @@ -2,6 +2,7 @@ package caps import ( "context" + "slices" "sync" ccaps "github.com/containerd/containerd/v2/pkg/cap" @@ -27,7 +28,7 @@ func initCaps() { // old (pre-detection) behavior, and prevents creating containers with // no capabilities. The OCI runtime or kernel may still refuse capa- // bilities that are not available, and produce an error in that case. - if len(curCaps) > 0 && !inSlice(curCaps, capName) { + if len(curCaps) > 0 && !slices.Contains(curCaps, capName) { knownCaps[capName] = nil continue }