Merge pull request #14763 from spowelljr/fixDuplicateArmPreload

Remove duplicated k8s versions from preload generation
pull/14782/head
Steven Powell 2022-08-10 16:20:27 -07:00 committed by GitHub
commit 6a7be9a0cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 21 deletions

View File

@ -32,6 +32,7 @@ import (
"github.com/spf13/viper"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/download"
"k8s.io/minikube/pkg/util"
)
const (
@ -129,11 +130,12 @@ func collectK8sVers() ([]string, error) {
}
k8sVersions = recent
}
return append([]string{
versions := append([]string{
constants.DefaultKubernetesVersion,
constants.NewestKubernetesVersion,
constants.OldestKubernetesVersion,
}, k8sVersions...), nil
}, k8sVersions...)
return util.RemoveDuplicateStrings(versions), nil
}
func makePreload(cfg preloadCfg) error {

View File

@ -51,6 +51,7 @@ import (
"k8s.io/minikube/pkg/minikube/registry"
"k8s.io/minikube/pkg/minikube/style"
"k8s.io/minikube/pkg/minikube/vmpath"
"k8s.io/minikube/pkg/util"
"k8s.io/minikube/pkg/util/lock"
)
@ -108,15 +109,7 @@ func engineOptions(cfg config.ClusterConfig) *engine.Options {
// get docker env from user specifiec config
dockerEnv = append(dockerEnv, cfg.DockerEnv...)
// remove duplicates
seen := map[string]bool{}
uniqueEnvs := []string{}
for e := range dockerEnv {
if !seen[dockerEnv[e]] {
seen[dockerEnv[e]] = true
uniqueEnvs = append(uniqueEnvs, dockerEnv[e])
}
}
uniqueEnvs := util.RemoveDuplicateStrings(dockerEnv)
o := engine.Options{
Env: uniqueEnvs,

View File

@ -29,6 +29,7 @@ import (
"k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/util"
)
// EnvVars are variables we plumb through to the underlying container runtime
@ -184,16 +185,7 @@ func SetDockerEnv() []string {
}
}
// remove duplicates
seen := map[string]bool{}
uniqueEnvs := []string{}
for e := range config.DockerEnv {
if !seen[config.DockerEnv[e]] {
seen[config.DockerEnv[e]] = true
uniqueEnvs = append(uniqueEnvs, config.DockerEnv[e])
}
}
config.DockerEnv = uniqueEnvs
config.DockerEnv = util.RemoveDuplicateStrings(config.DockerEnv)
return config.DockerEnv
}

View File

@ -109,3 +109,17 @@ func MaybeChownDirRecursiveToMinikubeUser(dir string) error {
func ParseKubernetesVersion(version string) (semver.Version, error) {
return semver.Make(version[1:])
}
// RemoveDuplicateStrings takes a string slice and returns a slice without duplicates
func RemoveDuplicateStrings(initial []string) []string {
var result []string
m := make(map[string]bool, len(initial))
for _, v := range initial {
if _, ok := m[v]; ok {
continue
}
m[v] = true
result = append(result, v)
}
return result
}

View File

@ -23,6 +23,7 @@ import (
"testing"
"github.com/blang/semver/v4"
"github.com/google/go-cmp/cmp"
)
func TestGetBinaryDownloadURL(t *testing.T) {
@ -171,3 +172,45 @@ func TestMaybeChownDirRecursiveToMinikubeUser(t *testing.T) {
})
}
}
func TestRemoveDuplicateStrings(t *testing.T) {
testCases := []struct {
desc string
slice []string
want []string
}{
{
desc: "NoDuplicates",
slice: []string{"alpha", "bravo", "charlie"},
want: []string{"alpha", "bravo", "charlie"},
},
{
desc: "AdjacentDuplicates",
slice: []string{"alpha", "bravo", "bravo", "charlie"},
want: []string{"alpha", "bravo", "charlie"},
},
{
desc: "NonAdjacentDuplicates",
slice: []string{"alpha", "bravo", "alpha", "charlie"},
want: []string{"alpha", "bravo", "charlie"},
},
{
desc: "MultipleDuplicates",
slice: []string{"alpha", "bravo", "alpha", "alpha", "charlie", "charlie", "alpha", "bravo"},
want: []string{"alpha", "bravo", "charlie"},
},
{
desc: "UnsortedDuplicates",
slice: []string{"charlie", "bravo", "alpha", "bravo"},
want: []string{"charlie", "bravo", "alpha"},
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
got := RemoveDuplicateStrings(tc.slice)
if diff := cmp.Diff(got, tc.want); diff != "" {
t.Errorf("RemoveDuplicateStrings(%v) = %v, want: %v", tc.slice, got, tc.want)
}
})
}
}