Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation checking for minikube profile #5624

Merged
merged 1 commit into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/minikube/cmd/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ var ProfileCmd = &cobra.Command{
}

profile := args[0]
/**
we need to add code over here to check whether the profile
name is in the list of reserved keywords
*/
if pkgConfig.ProfileNameInReservedKeywords(profile) {
out.ErrT(out.FailureType, `Profile name "{{.profilename}}" is minikube keyword. To delete profile use command minikube delete -p <profile name> `, out.V{"profilename": profile})
os.Exit(0)
}

if profile == "default" {
profile = "minikube"
} else {
Expand Down
14 changes: 13 additions & 1 deletion pkg/minikube/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/golang/glog"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/util/lock"
)

var keywords = []string{"start", "stop", "status", "delete", "config", "open", "profile", "addons", "cache", "logs"}

// IsValid checks if the profile has the essential info needed for a profile
func (p *Profile) IsValid() bool {
if p.Config == nil {
return false
}

if p.Config.MachineConfig.VMDriver == "" {
return false
}
Expand All @@ -42,6 +44,16 @@ func (p *Profile) IsValid() bool {
return true
}

// check if the profile is an internal keywords
func ProfileNameInReservedKeywords(name string) bool {
for _, v := range keywords {
if strings.EqualFold(v, name) {
return true
}
}
return false
}

// ProfileExists returns true if there is a profile config (regardless of being valid)
func ProfileExists(name string, miniHome ...string) bool {
miniPath := localpath.MiniPath()
Expand Down
26 changes: 26 additions & 0 deletions pkg/minikube/config/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,32 @@ func TestListProfiles(t *testing.T) {
}
}

func TestProfileNameInReservedKeywords(t *testing.T) {
var testCases = []struct {
name string
expected bool
}{
{"start", true},
{"stop", true},
{"status", true},
{"delete", true},
{"config", true},
{"open", true},
{"profile", true},
{"addons", true},
{"cache", true},
{"logs", true},
{"myprofile", false},
{"log", false},
}
for _, tt := range testCases {
got := ProfileNameInReservedKeywords(tt.name)
if got != tt.expected {
t.Errorf("expected ProfileNameInReservedKeywords(%s)=%t but got %t ", tt.name, tt.expected, got)
}
}
}

func TestProfileExists(t *testing.T) {
miniDir, err := filepath.Abs("./testdata/.minikube2")
if err != nil {
Expand Down