Browse code

daemon: Simplify slices.Contains usage

Remove unnecessary intermediate variables and helper functions when
using slices.Contains.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>

Paweł Gronowski authored on 2025/12/16 21:06:20
Showing 7 changed files
... ...
@@ -543,12 +543,7 @@ func (nDB *NetworkDB) bulkSyncTables() {
543 543
 		// successfully completed bulk sync in this iteration.
544 544
 		updatedNetworks := make([]string, 0, len(networks))
545 545
 		for _, nid := range networks {
546
-			var found bool
547
-			if slices.Contains(completed, nid) {
548
-				found = true
549
-			}
550
-
551
-			if !found {
546
+			if !slices.Contains(completed, nid) {
552 547
 				updatedNetworks = append(updatedNetworks, nid)
553 548
 			}
554 549
 		}
... ...
@@ -154,10 +154,7 @@ func (nDB *NetworkDB) handleTableEvent(tEvent *TableEvent, isBulkSync bool) bool
154 154
 	network, ok := nDB.thisNodeNetworks[tEvent.NetworkID]
155 155
 	// Check if the owner of the event is still part of the network
156 156
 	nodes := nDB.networkNodes[tEvent.NetworkID]
157
-	var nodePresent bool
158
-	if slices.Contains(nodes, tEvent.NodeName) {
159
-		nodePresent = true
160
-	}
157
+	nodePresent := slices.Contains(nodes, tEvent.NodeName)
161 158
 
162 159
 	if !ok || network.leaving || !nodePresent {
163 160
 		// I'm out of the network OR the event owner is not anymore part of the network so do not propagate
... ...
@@ -730,8 +730,7 @@ func (nDB *NetworkDB) LeaveNetwork(nid string) error {
730 730
 // in the passed network only if it is not already present. Caller
731 731
 // should hold the NetworkDB lock while calling this
732 732
 func (nDB *NetworkDB) addNetworkNode(nid string, nodeName string) {
733
-	nodes := nDB.networkNodes[nid]
734
-	if slices.Contains(nodes, nodeName) {
733
+	if slices.Contains(nDB.networkNodes[nid], nodeName) {
735 734
 		return
736 735
 	}
737 736
 
... ...
@@ -466,12 +466,6 @@ var (
466 466
 	}
467 467
 )
468 468
 
469
-// inSlice tests whether a string is contained in a slice of strings or not.
470
-// Comparison is case sensitive
471
-func inSlice(slice []string, s string) bool {
472
-	return slices.Contains(slice, s)
473
-}
474
-
475 469
 // withMounts sets the container's mounts
476 470
 func withMounts(daemon *Daemon, daemonCfg *configStore, c *container.Container, mounts []container.Mount) coci.SpecOpts {
477 471
 	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,
641 641
 					continue
642 642
 				}
643 643
 				if _, ok := userMounts[m.Destination]; !ok {
644
-					if !inSlice(m.Options, "ro") {
644
+					if !slices.Contains(m.Options, "ro") {
645 645
 						s.Mounts[i].Options = append(s.Mounts[i].Options, "ro")
646 646
 					}
647 647
 				}
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"context"
5 5
 	"os"
6 6
 	"path/filepath"
7
+	"slices"
7 8
 	"testing"
8 9
 
9 10
 	"github.com/google/go-cmp/cmp/cmpopts"
... ...
@@ -119,7 +120,7 @@ func TestIpcPrivateVsReadonly(t *testing.T) {
119 119
 		if m.Destination != "/dev/shm" {
120 120
 			continue
121 121
 		}
122
-		assert.Check(t, is.Equal(false, inSlice(m.Options, "ro")))
122
+		assert.Check(t, is.Equal(false, slices.Contains(m.Options, "ro")))
123 123
 	}
124 124
 }
125 125
 
... ...
@@ -38,11 +38,6 @@ func knownCapabilities() map[string]*struct{} {
38 38
 	return knownCaps
39 39
 }
40 40
 
41
-// inSlice tests whether a string is contained in a slice of strings or not.
42
-func inSlice(slice []string, s string) bool {
43
-	return slices.Contains(slice, s)
44
-}
45
-
46 41
 const allCapabilities = "ALL"
47 42
 
48 43
 // NormalizeLegacyCapabilities normalizes, and validates CapAdd/CapDrop capabilities
... ...
@@ -99,20 +94,20 @@ func TweakCapabilities(basics, adds, drops []string, privileged bool) ([]string,
99 99
 	var caps []string
100 100
 
101 101
 	switch {
102
-	case inSlice(capAdd, allCapabilities):
102
+	case slices.Contains(capAdd, allCapabilities):
103 103
 		// Add all capabilities except ones on capDrop
104 104
 		for _, c := range GetAllCapabilities() {
105
-			if !inSlice(capDrop, c) {
105
+			if !slices.Contains(capDrop, c) {
106 106
 				caps = append(caps, c)
107 107
 			}
108 108
 		}
109
-	case inSlice(capDrop, allCapabilities):
109
+	case slices.Contains(capDrop, allCapabilities):
110 110
 		// "Drop" all capabilities; use what's in capAdd instead
111 111
 		caps = capAdd
112 112
 	default:
113 113
 		// First drop some capabilities
114 114
 		for _, c := range basics {
115
-			if !inSlice(capDrop, c) {
115
+			if !slices.Contains(capDrop, c) {
116 116
 				caps = append(caps, c)
117 117
 			}
118 118
 		}
... ...
@@ -2,6 +2,7 @@ package caps
2 2
 
3 3
 import (
4 4
 	"context"
5
+	"slices"
5 6
 	"sync"
6 7
 
7 8
 	ccaps "github.com/containerd/containerd/v2/pkg/cap"
... ...
@@ -27,7 +28,7 @@ func initCaps() {
27 27
 			// old (pre-detection) behavior, and prevents creating containers with
28 28
 			// no capabilities. The OCI runtime or kernel may still refuse capa-
29 29
 			// bilities that are not available, and produce an error in that case.
30
-			if len(curCaps) > 0 && !inSlice(curCaps, capName) {
30
+			if len(curCaps) > 0 && !slices.Contains(curCaps, capName) {
31 31
 				knownCaps[capName] = nil
32 32
 				continue
33 33
 			}