mirror of
https://github.com/moby/moby.git
synced 2026-01-11 02:31:44 +00:00
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:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user