Skip to content

Commit e4c8917

Browse files
authored
Merge pull request #21681 from nirs/slices-contains
config: Use slices.Contains
2 parents bb8b53f + 595bbf5 commit e4c8917

File tree

9 files changed

+20
-99
lines changed

9 files changed

+20
-99
lines changed

cmd/minikube/cmd/config/prompt.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io"
2222
"log"
2323
"os"
24+
"slices"
2425
"strings"
2526

2627
"golang.org/x/term"
@@ -44,9 +45,9 @@ func AskForYesNoConfirmation(s string, posResponses, negResponses []string) bool
4445
}
4546

4647
switch r := strings.ToLower(strings.TrimSpace(response)); {
47-
case containsString(posResponses, r):
48+
case slices.Contains(posResponses, r):
4849
return true
49-
case containsString(negResponses, r):
50+
case slices.Contains(negResponses, r):
5051
return false
5152
default:
5253
out.Err("Please type yes or no:")
@@ -137,22 +138,6 @@ func AskForPasswordValue(s string) string {
137138
return result
138139
}
139140

140-
// posString returns the first index of element in slice.
141-
// If slice does not contain element, returns -1.
142-
func posString(slice []string, element string) int {
143-
for index, elem := range slice {
144-
if elem == element {
145-
return index
146-
}
147-
}
148-
return -1
149-
}
150-
151-
// containsString returns true if slice contains element
152-
func containsString(slice []string, element string) bool {
153-
return posString(slice, element) != -1
154-
}
155-
156141
// AskForStaticValidatedValue asks for a single value to enter and check for valid input
157142
func AskForStaticValidatedValue(s string, validator func(s string) bool) string {
158143
reader := bufio.NewReader(os.Stdin)

cmd/minikube/cmd/start.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"os/user"
2929
"regexp"
3030
"runtime"
31+
"slices"
3132
"sort"
3233
"strconv"
3334
"strings"
@@ -1373,8 +1374,8 @@ func validateFlags(cmd *cobra.Command, drvName string) { //nolint:gocyclo
13731374

13741375
// check that kubeadm extra args contain only allowed parameters
13751376
for param := range config.ExtraOptions.AsMap().Get(bsutil.Kubeadm) {
1376-
if !config.ContainsParam(bsutil.KubeadmExtraArgsAllowed[bsutil.KubeadmCmdParam], param) &&
1377-
!config.ContainsParam(bsutil.KubeadmExtraArgsAllowed[bsutil.KubeadmConfigParam], param) {
1377+
if !slices.Contains(bsutil.KubeadmExtraArgsAllowed[bsutil.KubeadmCmdParam], param) &&
1378+
!slices.Contains(bsutil.KubeadmExtraArgsAllowed[bsutil.KubeadmConfigParam], param) {
13781379
exit.Message(reason.Usage, "Sorry, the kubeadm.{{.parameter_name}} parameter is currently not supported by --extra-config", out.V{"parameter_name": param})
13791380
}
13801381
}
@@ -1812,7 +1813,7 @@ func validateKubernetesVersion(old *config.ClusterConfig) {
18121813
}
18131814
if nvs.GT(newestVersion) {
18141815
out.WarningT("Specified Kubernetes version {{.specified}} is newer than the newest supported version: {{.newest}}. Use `minikube config defaults kubernetes-version` for details.", out.V{"specified": nvs, "newest": constants.NewestKubernetesVersion})
1815-
if contains(constants.ValidKubernetesVersions, kubernetesVer) {
1816+
if slices.Contains(constants.ValidKubernetesVersions, kubernetesVer) {
18161817
out.Styled(style.Check, "Kubernetes version {{.specified}} found in version list", out.V{"specified": nvs})
18171818
} else {
18181819
out.WarningT("Specified Kubernetes version {{.specified}} not found in Kubernetes version list", out.V{"specified": nvs})
@@ -2098,14 +2099,3 @@ func startNerdctld() {
20982099
exit.Error(reason.StartNerdctld, fmt.Sprintf("Failed to set up DOCKER_HOST: %s", rest.Output()), err)
20992100
}
21002101
}
2101-
2102-
// contains checks whether the parameter slice contains the parameter string
2103-
func contains(sl []string, s string) bool {
2104-
for _, k := range sl {
2105-
if s == k {
2106-
return true
2107-
}
2108-
2109-
}
2110-
return false
2111-
}

pkg/addons/validations.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package addons
1919
import (
2020
"errors"
2121
"fmt"
22+
"slices"
2223
"strconv"
2324

2425
"github.com/spf13/viper"
@@ -66,7 +67,7 @@ func isVolumesnapshotsEnabled(cc *config.ClusterConfig, _, value string) error {
6667
// assets.Addons[].IsEnabled() returns the current status of the addon or default value.
6768
// config.AddonList contains list of addons to be enabled.
6869
addonList := viper.GetStringSlice(config.AddonListFlag)
69-
isVolumesnapshotsEnabled := assets.Addons[volumesnapshotsAddon].IsEnabled(cc) || contains(addonList, volumesnapshotsAddon)
70+
isVolumesnapshotsEnabled := assets.Addons[volumesnapshotsAddon].IsEnabled(cc) || slices.Contains(addonList, volumesnapshotsAddon)
7071
if isCsiDriverEnabled && !isVolumesnapshotsEnabled {
7172
// just print out a warning directly, we don't want to return any errors since
7273
// that would prevent the addon from being enabled (callbacks wouldn't be run)
@@ -94,12 +95,3 @@ func isAddonValid(name string) (*Addon, bool) {
9495
}
9596
return nil, false
9697
}
97-
98-
func contains(slice []string, val string) bool {
99-
for _, item := range slice {
100-
if item == val {
101-
return true
102-
}
103-
}
104-
return false
105-
}

pkg/addons/validations_test.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,39 +48,6 @@ func TestIsAddonValid(t *testing.T) {
4848
}
4949
}
5050

51-
func TestContains(t *testing.T) {
52-
tests := []struct {
53-
slice []string
54-
str string
55-
expected bool
56-
}{
57-
{
58-
slice: []string{},
59-
str: "test",
60-
expected: false,
61-
},
62-
{
63-
slice: []string{"test", "test1"},
64-
str: "test1",
65-
expected: true,
66-
},
67-
{
68-
slice: []string{"test", "test1"},
69-
str: "test2",
70-
expected: false,
71-
},
72-
}
73-
74-
for _, test := range tests {
75-
t.Run(test.str, func(t *testing.T) {
76-
actual := contains(test.slice, test.str)
77-
if test.expected != actual {
78-
t.Fatalf("slice: %v\nstr: %v\nexpected: %v\nactual:%v\n", test.slice, test.str, test.expected, actual)
79-
}
80-
})
81-
}
82-
}
83-
8451
func TestIsKVMDriverForNVIDIA(t *testing.T) {
8552
tests := []struct {
8653
cc *config.ClusterConfig

pkg/minikube/bootstrapper/bsutil/extraconfig.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package bsutil
1919

2020
import (
2121
"fmt"
22+
"slices"
2223
"sort"
2324
"strings"
2425

@@ -87,7 +88,7 @@ func CreateFlagsFromExtraArgs(extraOptions config.ExtraOptionSlice) string {
8788
// kubeadm allows only a small set of parameters to be supplied from the command line when the --config param
8889
// is specified, here we remove those that are not allowed
8990
for opt := range kubeadmExtraOpts {
90-
if !config.ContainsParam(KubeadmExtraArgsAllowed[KubeadmCmdParam], opt) {
91+
if !slices.Contains(KubeadmExtraArgsAllowed[KubeadmCmdParam], opt) {
9192
// kubeadmExtraOpts is a copy so safe to delete
9293
delete(kubeadmExtraOpts, opt)
9394
}

pkg/minikube/bootstrapper/bsutil/kubeadm.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"bytes"
2222
"fmt"
2323
"path"
24+
"slices"
2425

2526
"github.com/blang/semver/v4"
2627
"github.com/pkg/errors"
@@ -261,7 +262,7 @@ func kubeletConfigOpts(extraOpts config.ExtraOptionSlice) map[string]string {
261262
if eo.Component != Kubelet {
262263
continue
263264
}
264-
if config.ContainsParam(kubeletConfigParams, eo.Key) {
265+
if slices.Contains(kubeletConfigParams, eo.Key) {
265266
args[eo.Key] = eo.Value
266267
}
267268
}

pkg/minikube/cluster/pause.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package cluster
1818

1919
import (
20+
"slices"
2021
"time"
2122

2223
"github.com/pkg/errors"
@@ -136,10 +137,5 @@ func doesNamespaceContainKubeSystem(namespaces []string) bool {
136137
if namespaces == nil {
137138
return true
138139
}
139-
for _, ns := range namespaces {
140-
if ns == "kube-system" {
141-
return true
142-
}
143-
}
144-
return false
140+
return slices.Contains(namespaces, "kube-system")
145141
}

pkg/minikube/config/extra_options.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package config
1818

1919
import (
2020
"fmt"
21+
"slices"
2122
"strings"
2223

2324
"k8s.io/klog/v2"
@@ -109,7 +110,7 @@ func (es *ExtraOptionSlice) String() string {
109110
// component is not specified, all of the components are used.
110111
func (es *ExtraOptionSlice) Get(key string, component ...string) string {
111112
for _, opt := range *es {
112-
if component == nil || ContainsParam(component, opt.Component) {
113+
if component == nil || slices.Contains(component, opt.Component) {
113114
if opt.Key == key {
114115
return opt.Value
115116
}
@@ -141,17 +142,6 @@ func (cm ComponentExtraOptionMap) Get(component string) map[string]string {
141142
return cm[component]
142143
}
143144

144-
// ContainsParam checks if a given slice of strings contains the provided string.
145-
// If a modifier func is provided, it is called with the slice item before the comparison.
146-
func ContainsParam(slice []string, s string) bool {
147-
for _, item := range slice {
148-
if item == s {
149-
return true
150-
}
151-
}
152-
return false
153-
}
154-
155145
// NewUnversionedOption returns a VersionedExtraOption that applies to all versions.
156146
func NewUnversionedOption(component, k, v string) VersionedExtraOption {
157147
return VersionedExtraOption{

pkg/minikube/extract/extract.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"net/url"
2626
"os"
2727
"path/filepath"
28+
"slices"
2829
"strconv"
2930
"strings"
3031

@@ -349,10 +350,8 @@ func checkString(s string) string {
349350
}
350351

351352
// Don't translate excluded strings
352-
for _, e := range exclude {
353-
if e == stringToTranslate {
354-
return ""
355-
}
353+
if slices.Contains(exclude, stringToTranslate) {
354+
return ""
356355
}
357356

358357
// Remove unnecessary backslashes

0 commit comments

Comments
 (0)