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>
This commit is contained in:
Paweł Gronowski
2025-12-16 13:06:20 +01:00
parent 51d1974500
commit 0ae3f972ad
7 changed files with 12 additions and 30 deletions

View File

@@ -543,12 +543,7 @@ func (nDB *NetworkDB) bulkSyncTables() {
// successfully completed bulk sync in this iteration. // successfully completed bulk sync in this iteration.
updatedNetworks := make([]string, 0, len(networks)) updatedNetworks := make([]string, 0, len(networks))
for _, nid := range networks { for _, nid := range networks {
var found bool if !slices.Contains(completed, nid) {
if slices.Contains(completed, nid) {
found = true
}
if !found {
updatedNetworks = append(updatedNetworks, nid) updatedNetworks = append(updatedNetworks, nid)
} }
} }

View File

@@ -154,10 +154,7 @@ func (nDB *NetworkDB) handleTableEvent(tEvent *TableEvent, isBulkSync bool) bool
network, ok := nDB.thisNodeNetworks[tEvent.NetworkID] network, ok := nDB.thisNodeNetworks[tEvent.NetworkID]
// Check if the owner of the event is still part of the network // Check if the owner of the event is still part of the network
nodes := nDB.networkNodes[tEvent.NetworkID] nodes := nDB.networkNodes[tEvent.NetworkID]
var nodePresent bool nodePresent := slices.Contains(nodes, tEvent.NodeName)
if slices.Contains(nodes, tEvent.NodeName) {
nodePresent = true
}
if !ok || network.leaving || !nodePresent { 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 // I'm out of the network OR the event owner is not anymore part of the network so do not propagate

View File

@@ -730,8 +730,7 @@ func (nDB *NetworkDB) LeaveNetwork(nid string) error {
// in the passed network only if it is not already present. Caller // in the passed network only if it is not already present. Caller
// should hold the NetworkDB lock while calling this // should hold the NetworkDB lock while calling this
func (nDB *NetworkDB) addNetworkNode(nid string, nodeName string) { func (nDB *NetworkDB) addNetworkNode(nid string, nodeName string) {
nodes := nDB.networkNodes[nid] if slices.Contains(nDB.networkNodes[nid], nodeName) {
if slices.Contains(nodes, nodeName) {
return return
} }

View File

@@ -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 // withMounts sets the container's mounts
func withMounts(daemon *Daemon, daemonCfg *configStore, c *container.Container, mounts []container.Mount) coci.SpecOpts { 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 { 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 continue
} }
if _, ok := userMounts[m.Destination]; !ok { 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") s.Mounts[i].Options = append(s.Mounts[i].Options, "ro")
} }
} }

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"testing" "testing"
"github.com/google/go-cmp/cmp/cmpopts" "github.com/google/go-cmp/cmp/cmpopts"
@@ -119,7 +120,7 @@ func TestIpcPrivateVsReadonly(t *testing.T) {
if m.Destination != "/dev/shm" { if m.Destination != "/dev/shm" {
continue continue
} }
assert.Check(t, is.Equal(false, inSlice(m.Options, "ro"))) assert.Check(t, is.Equal(false, slices.Contains(m.Options, "ro")))
} }
} }

View File

@@ -38,11 +38,6 @@ func knownCapabilities() map[string]*struct{} {
return knownCaps 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" const allCapabilities = "ALL"
// NormalizeLegacyCapabilities normalizes, and validates CapAdd/CapDrop capabilities // NormalizeLegacyCapabilities normalizes, and validates CapAdd/CapDrop capabilities
@@ -99,20 +94,20 @@ func TweakCapabilities(basics, adds, drops []string, privileged bool) ([]string,
var caps []string var caps []string
switch { switch {
case inSlice(capAdd, allCapabilities): case slices.Contains(capAdd, allCapabilities):
// Add all capabilities except ones on capDrop // Add all capabilities except ones on capDrop
for _, c := range GetAllCapabilities() { for _, c := range GetAllCapabilities() {
if !inSlice(capDrop, c) { if !slices.Contains(capDrop, c) {
caps = append(caps, c) caps = append(caps, c)
} }
} }
case inSlice(capDrop, allCapabilities): case slices.Contains(capDrop, allCapabilities):
// "Drop" all capabilities; use what's in capAdd instead // "Drop" all capabilities; use what's in capAdd instead
caps = capAdd caps = capAdd
default: default:
// First drop some capabilities // First drop some capabilities
for _, c := range basics { for _, c := range basics {
if !inSlice(capDrop, c) { if !slices.Contains(capDrop, c) {
caps = append(caps, c) caps = append(caps, c)
} }
} }

View File

@@ -2,6 +2,7 @@ package caps
import ( import (
"context" "context"
"slices"
"sync" "sync"
ccaps "github.com/containerd/containerd/v2/pkg/cap" ccaps "github.com/containerd/containerd/v2/pkg/cap"
@@ -27,7 +28,7 @@ func initCaps() {
// old (pre-detection) behavior, and prevents creating containers with // old (pre-detection) behavior, and prevents creating containers with
// no capabilities. The OCI runtime or kernel may still refuse capa- // no capabilities. The OCI runtime or kernel may still refuse capa-
// bilities that are not available, and produce an error in that case. // 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 knownCaps[capName] = nil
continue continue
} }