Skip to content

Commit

Permalink
Merge pull request #21171 from alexandear/refactor-slices-contains
Browse files Browse the repository at this point in the history
Refactor: replace StringInSlice with slices.Contains
  • Loading branch information
openshift-merge-bot[bot] authored Jan 5, 2024
2 parents 19678e3 + 8bdf77a commit 0a316fa
Show file tree
Hide file tree
Showing 34 changed files with 79 additions and 133 deletions.
4 changes: 2 additions & 2 deletions cmd/podman/containers/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/containers/buildah/pkg/cli"
"github.com/containers/common/pkg/auth"
"github.com/containers/common/pkg/config"
cutil "github.com/containers/common/pkg/util"
"github.com/containers/image/v5/transports/alltransports"
"github.com/containers/image/v5/types"
"github.com/containers/podman/v4/cmd/podman/common"
Expand All @@ -24,6 +23,7 @@ import (
"github.com/containers/podman/v4/pkg/util"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"golang.org/x/term"
)

Expand Down Expand Up @@ -134,7 +134,7 @@ func create(cmd *cobra.Command, args []string) error {
if !cmd.Flags().Changed("pod") {
return errors.New("must specify pod value with init-ctr")
}
if !cutil.StringInSlice(initctr, []string{define.AlwaysInitContainer, define.OneShotInitContainer}) {
if !slices.Contains([]string{define.AlwaysInitContainer, define.OneShotInitContainer}, initctr) {
return fmt.Errorf("init-ctr value must be '%s' or '%s'", define.AlwaysInitContainer, define.OneShotInitContainer)
}
cliVals.InitContainerType = initctr
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/farm/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/containers/common/pkg/completion"
"github.com/containers/common/pkg/config"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/pkg/util"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
)

var (
Expand Down Expand Up @@ -55,7 +55,7 @@ func create(cmd *cobra.Command, args []string) error {

for _, c := range connections {
if _, ok := cfg.Engine.ServiceDestinations[c]; ok {
if util.StringInSlice(c, cfg.Farms.List[farmName]) {
if slices.Contains(cfg.Farms.List[farmName], c) {
// Don't add duplicate connections to a farm
continue
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/farm/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/containers/common/pkg/config"
"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/pkg/util"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
)

var (
Expand Down Expand Up @@ -91,7 +91,7 @@ func farmUpdate(cmd *cobra.Command, args []string) error {

for _, cRemove := range updateOpts.Remove {
connections := cfg.Farms.List[farmName]
if util.StringInSlice(cRemove, connections) {
if slices.Contains(connections, cRemove) {
delete(cMap, cRemove)
} else {
return fmt.Errorf("cannot remove from farm, %q is not a connection in the farm", cRemove)
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/images/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"strings"

"github.com/containers/common/pkg/completion"
"github.com/containers/common/pkg/util"
"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/parse"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"golang.org/x/term"
)

Expand All @@ -38,7 +38,7 @@ var (
if err != nil {
return err
}
if !util.StringInSlice(format, common.ValidSaveFormats) {
if !slices.Contains(common.ValidSaveFormats, format) {
return fmt.Errorf("format value must be one of %s", strings.Join(common.ValidSaveFormats, " "))
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/images/trust_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"regexp"

"github.com/containers/common/pkg/completion"
"github.com/containers/common/pkg/util"
"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
)

var (
Expand Down Expand Up @@ -60,7 +60,7 @@ func setTrust(cmd *cobra.Command, args []string) error {
return err
}

if !util.StringInSlice(setOptions.Type, validTrustTypes) {
if !slices.Contains(validTrustTypes, setOptions.Type) {
return fmt.Errorf("invalid choice: %s (choose from 'accept', 'reject', 'signedBy', 'sigstoreSigned')", setOptions.Type)
}
return registry.ImageEngine().SetTrust(registry.Context(), args, setOptions)
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/system/connection/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"github.com/containers/common/pkg/completion"
"github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/report"
"github.com/containers/common/pkg/util"
"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/cmd/podman/system"
"github.com/containers/podman/v4/cmd/podman/validate"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
)

var (
Expand Down Expand Up @@ -89,7 +89,7 @@ func inspect(cmd *cobra.Command, args []string) error {
}
rows := make([]namedDestination, 0)
for k, v := range cfg.Engine.ServiceDestinations {
if args != nil && !util.StringInSlice(k, args) {
if args != nil && !slices.Contains(args, k) {
continue
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/system/connection/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/containers/podman/v4/cmd/podman/common"
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/cmd/podman/system"
"github.com/containers/podman/v4/pkg/util"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
)

var (
Expand Down Expand Up @@ -80,7 +80,7 @@ func rm(cmd *cobra.Command, args []string) error {

// If there are existing farm, remove the deleted connection that might be part of a farm
for k, v := range cfg.Farms.List {
index := util.IndexOfStringInSlice(args[0], v)
index := slices.Index(v, args[0])
if index > -1 {
cfg.Farms.List[k] = append(v[:index], v[index+1:]...)
}
Expand Down
3 changes: 2 additions & 1 deletion libpod/container_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/opencontainers/runtime-tools/generate"
"github.com/opencontainers/selinux/go-selinux/label"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -1794,7 +1795,7 @@ func (c *Container) mountNamedVolume(v *ContainerNamedVolume, mountpoint string)
return nil, err
}
_, hasNoCopy := vol.config.Options["nocopy"]
if vol.state.NeedsCopyUp && !cutil.StringInSlice("nocopy", v.Options) && !hasNoCopy {
if vol.state.NeedsCopyUp && !slices.Contains(v.Options, "nocopy") && !hasNoCopy {
logrus.Debugf("Copying up contents from container %s to volume %s", c.ID(), vol.Name())

srcDir, err := securejoin.SecureJoin(mountpoint, v.Dest)
Expand Down
6 changes: 3 additions & 3 deletions libpod/container_internal_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/subscriptions"
"github.com/containers/common/pkg/umask"
cutil "github.com/containers/common/pkg/util"
is "github.com/containers/image/v5/storage"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/libpod/events"
Expand All @@ -54,6 +53,7 @@ import (
"github.com/opencontainers/selinux/go-selinux"
"github.com/opencontainers/selinux/go-selinux/label"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
"golang.org/x/sys/unix"
cdi "tags.cncf.io/container-device-interface/pkg/cdi"
)
Expand Down Expand Up @@ -193,7 +193,7 @@ func (c *Container) generateSpec(ctx context.Context) (s *spec.Spec, cleanupFunc
overrides := c.getUserOverrides()
execUser, err := lookup.GetUserGroupInfo(c.state.Mountpoint, c.config.User, overrides)
if err != nil {
if cutil.StringInSlice(c.config.User, c.config.HostUsers) {
if slices.Contains(c.config.HostUsers, c.config.User) {
execUser, err = lookupHostUser(c.config.User)
}
if err != nil {
Expand Down Expand Up @@ -2495,7 +2495,7 @@ func (c *Container) setHomeEnvIfNeeded() error {
overrides := c.getUserOverrides()
execUser, err := lookup.GetUserGroupInfo(c.state.Mountpoint, c.config.User, overrides)
if err != nil {
if cutil.StringInSlice(c.config.User, c.config.HostUsers) {
if slices.Contains(c.config.HostUsers, c.config.User) {
execUser, err = lookupHostUser(c.config.User)
}

Expand Down
4 changes: 2 additions & 2 deletions libpod/container_top_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (
"syscall"
"unsafe"

"github.com/containers/common/pkg/util"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/rootless"
"github.com/containers/psgo"
"github.com/containers/storage/pkg/reexec"
"github.com/google/shlex"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -231,7 +231,7 @@ func (c *Container) Top(descriptors []string) ([]string, error) {
// Only use ps(1) from the host when we know the container was not started with CAP_SYS_PTRACE,
// with it the container can access /proc/$pid/ files and potentially escape the container fs.
if c.config.Spec.Process.Capabilities != nil &&
!util.StringInSlice("CAP_SYS_PTRACE", c.config.Spec.Process.Capabilities.Effective) {
!slices.Contains(c.config.Spec.Process.Capabilities.Effective, "CAP_SYS_PTRACE") {
var retry bool
output, retry, err = c.execPS(psDescriptors)
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions libpod/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (

"github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/config"
cutil "github.com/containers/common/pkg/util"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/annotations"
"github.com/containers/podman/v4/pkg/domain/entities"
Expand All @@ -32,6 +31,7 @@ import (
"github.com/containers/podman/v4/pkg/util"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
)

// GenerateForKube takes a slice of libpod containers and generates
Expand Down Expand Up @@ -729,7 +729,7 @@ func simplePodWithV1Containers(ctx context.Context, ctrs []*Container, getServic
for _, ulimit := range ctr.config.Spec.Process.Rlimits {
finalUlimit := strings.ToLower(strings.ReplaceAll(ulimit.Type, "RLIMIT_", "")) + "=" + strconv.Itoa(int(ulimit.Soft)) + ":" + strconv.Itoa(int(ulimit.Hard))
// compare ulimit with default list so we don't add it twice
if cutil.StringInSlice(finalUlimit, defaultUlimits) {
if slices.Contains(defaultUlimits, finalUlimit) {
continue
}

Expand Down Expand Up @@ -768,7 +768,7 @@ func simplePodWithV1Containers(ctx context.Context, ctrs []*Container, getServic
podDNS.Nameservers = make([]string, 0)
}
for _, s := range servers {
if !cutil.StringInSlice(s, podDNS.Nameservers) { // only append if it does not exist
if !slices.Contains(podDNS.Nameservers, s) { // only append if it does not exist
podDNS.Nameservers = append(podDNS.Nameservers, s)
}
}
Expand All @@ -779,7 +779,7 @@ func simplePodWithV1Containers(ctx context.Context, ctrs []*Container, getServic
podDNS.Searches = make([]string, 0)
}
for _, d := range domains {
if !cutil.StringInSlice(d, podDNS.Searches) { // only append if it does not exist
if !slices.Contains(podDNS.Searches, d) { // only append if it does not exist
podDNS.Searches = append(podDNS.Searches, d)
}
}
Expand All @@ -796,7 +796,7 @@ func simplePodWithV1Containers(ctx context.Context, ctrs []*Container, getServic
podName := removeUnderscores(ctrs[0].Name())
// Check if the pod name and container name will end up conflicting
// Append -pod if so
if cutil.StringInSlice(podName, ctrNames) {
if slices.Contains(ctrNames, podName) {
podName += "-pod"
}

Expand Down Expand Up @@ -1114,7 +1114,7 @@ func libpodMountsToKubeVolumeMounts(c *Container) ([]v1.VolumeMount, []v1.Volume

// generateKubePersistentVolumeClaim converts a ContainerNamedVolume to a Kubernetes PersistentVolumeClaim
func generateKubePersistentVolumeClaim(v *ContainerNamedVolume) (v1.VolumeMount, v1.Volume) {
ro := cutil.StringInSlice("ro", v.Options)
ro := slices.Contains(v.Options, "ro")

// To avoid naming conflicts with any host path mounts, add a unique suffix to the volume's name.
name := v.Name + "-pvc"
Expand Down Expand Up @@ -1175,7 +1175,7 @@ func generateKubeVolumeMount(m specs.Mount) (v1.VolumeMount, v1.Volume, error) {
}
vm.Name = name
vm.MountPath = m.Destination
if cutil.StringInSlice("ro", m.Options) {
if slices.Contains(m.Options, "ro") {
vm.ReadOnly = true
}

Expand Down Expand Up @@ -1216,7 +1216,7 @@ func determineCapAddDropFromCapabilities(defaultCaps, containerCaps []string) *v
// Find caps in the defaultCaps but not in the container's
// those indicate a dropped cap
for _, capability := range defaultCaps {
if !cutil.StringInSlice(capability, containerCaps) {
if !slices.Contains(containerCaps, capability) {
if _, ok := dedupDrop[capability]; !ok {
drop = append(drop, v1.Capability(capability))
dedupDrop[capability] = true
Expand All @@ -1226,7 +1226,7 @@ func determineCapAddDropFromCapabilities(defaultCaps, containerCaps []string) *v
// Find caps in the container but not in the defaults; those indicate
// an added cap
for _, capability := range containerCaps {
if !cutil.StringInSlice(capability, defaultCaps) {
if !slices.Contains(defaultCaps, capability) {
if _, ok := dedupAdd[capability]; !ok {
add = append(add, v1.Capability(capability))
dedupAdd[capability] = true
Expand Down
4 changes: 2 additions & 2 deletions libpod/networking_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
"github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/machine"
"github.com/containers/common/pkg/util"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/libpod/events"
"github.com/containers/podman/v4/pkg/namespaces"
"github.com/containers/podman/v4/pkg/rootless"
"github.com/containers/storage/pkg/lockfile"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
)

// convertPortMappings will remove the HostIP part from the ports when running inside podman machine.
Expand Down Expand Up @@ -597,7 +597,7 @@ func getFreeInterfaceName(networks map[string]types.PerNetworkOptions) string {
}
for i := 0; i < 100000; i++ {
ifName := fmt.Sprintf("eth%d", i)
if !util.StringInSlice(ifName, ifNames) {
if !slices.Contains(ifNames, ifName) {
return ifName
}
}
Expand Down
6 changes: 3 additions & 3 deletions libpod/runtime_ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/cgroups"
"github.com/containers/common/pkg/config"
cutil "github.com/containers/common/pkg/util"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/libpod/events"
"github.com/containers/podman/v4/libpod/shutdown"
Expand All @@ -31,6 +30,7 @@ import (
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
)

// Contains the public Runtime API for containers
Expand Down Expand Up @@ -256,7 +256,7 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
for _, opts := range ctr.config.Networks {
if opts.InterfaceName != "" {
// check that no name is assigned to more than network
if cutil.StringInSlice(opts.InterfaceName, usedIfNames) {
if slices.Contains(usedIfNames, opts.InterfaceName) {
return nil, fmt.Errorf("network interface name %q is already assigned to another network", opts.InterfaceName)
}
usedIfNames = append(usedIfNames, opts.InterfaceName)
Expand All @@ -272,7 +272,7 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai
if opts.InterfaceName == "" {
for i < 100000 {
ifName := fmt.Sprintf("eth%d", i)
if !cutil.StringInSlice(ifName, usedIfNames) {
if !slices.Contains(usedIfNames, ifName) {
opts.InterfaceName = ifName
usedIfNames = append(usedIfNames, ifName)
break
Expand Down
4 changes: 2 additions & 2 deletions libpod/runtime_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"fmt"
"time"

"github.com/containers/common/pkg/util"
"github.com/containers/podman/v4/libpod/define"
"golang.org/x/exp/slices"
)

// Contains the public Runtime API for pods
Expand Down Expand Up @@ -145,7 +145,7 @@ func (r *Runtime) GetRunningPods() ([]*Pod, error) {
}
// Assemble running pods
for _, c := range containers {
if !util.StringInSlice(c.PodID(), pods) {
if !slices.Contains(pods, c.PodID()) {
pods = append(pods, c.PodID())
pod, err := r.GetPod(c.PodID())
if err != nil {
Expand Down
Loading

0 comments on commit 0a316fa

Please sign in to comment.