Skip to content

Commit c883382

Browse files
authored
Merge pull request #5624 from nanikjava/f-fix-4883
Add validation checking for minikube profile
2 parents 523db20 + 824c911 commit c883382

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

Diff for: cmd/minikube/cmd/config/profile.go

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ var ProfileCmd = &cobra.Command{
4545
}
4646

4747
profile := args[0]
48+
/**
49+
we need to add code over here to check whether the profile
50+
name is in the list of reserved keywords
51+
*/
52+
if pkgConfig.ProfileNameInReservedKeywords(profile) {
53+
out.ErrT(out.FailureType, `Profile name "{{.profilename}}" is minikube keyword. To delete profile use command minikube delete -p <profile name> `, out.V{"profilename": profile})
54+
os.Exit(0)
55+
}
56+
4857
if profile == "default" {
4958
profile = "minikube"
5059
} else {

Diff for: pkg/minikube/config/profile.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"path/filepath"
24+
"strings"
2425

2526
"github.com/golang/glog"
2627
"k8s.io/minikube/pkg/minikube/localpath"
2728
"k8s.io/minikube/pkg/util/lock"
2829
)
2930

31+
var keywords = []string{"start", "stop", "status", "delete", "config", "open", "profile", "addons", "cache", "logs"}
32+
3033
// IsValid checks if the profile has the essential info needed for a profile
3134
func (p *Profile) IsValid() bool {
3235
if p.Config == nil {
3336
return false
3437
}
35-
3638
if p.Config.MachineConfig.VMDriver == "" {
3739
return false
3840
}
@@ -42,6 +44,16 @@ func (p *Profile) IsValid() bool {
4244
return true
4345
}
4446

47+
// check if the profile is an internal keywords
48+
func ProfileNameInReservedKeywords(name string) bool {
49+
for _, v := range keywords {
50+
if strings.EqualFold(v, name) {
51+
return true
52+
}
53+
}
54+
return false
55+
}
56+
4557
// ProfileExists returns true if there is a profile config (regardless of being valid)
4658
func ProfileExists(name string, miniHome ...string) bool {
4759
miniPath := localpath.MiniPath()

Diff for: pkg/minikube/config/profile_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,32 @@ func TestListProfiles(t *testing.T) {
7272
}
7373
}
7474

75+
func TestProfileNameInReservedKeywords(t *testing.T) {
76+
var testCases = []struct {
77+
name string
78+
expected bool
79+
}{
80+
{"start", true},
81+
{"stop", true},
82+
{"status", true},
83+
{"delete", true},
84+
{"config", true},
85+
{"open", true},
86+
{"profile", true},
87+
{"addons", true},
88+
{"cache", true},
89+
{"logs", true},
90+
{"myprofile", false},
91+
{"log", false},
92+
}
93+
for _, tt := range testCases {
94+
got := ProfileNameInReservedKeywords(tt.name)
95+
if got != tt.expected {
96+
t.Errorf("expected ProfileNameInReservedKeywords(%s)=%t but got %t ", tt.name, tt.expected, got)
97+
}
98+
}
99+
}
100+
75101
func TestProfileExists(t *testing.T) {
76102
miniDir, err := filepath.Abs("./testdata/.minikube2")
77103
if err != nil {

0 commit comments

Comments
 (0)