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

Warn when a user tries to set a profile name that is unlikely to be valid #4999

Merged
merged 6 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions cmd/minikube/cmd/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ var ProfileCmd = &cobra.Command{
profile := args[0]
if profile == "default" {
profile = "minikube"
} else {
// not validating when it is default profile
errProfile, ok := ValidateProfile(profile)
if !ok && errProfile != nil {
out.FailureT(errProfile.Msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this PR looks good ! but we need to add a message to the end user, how to create a profile !

this PR will make it minikube start to be the only way to create a profile.

}
}
err := Set(pkgConfig.MachineProfile, profile)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions cmd/minikube/cmd/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/command"
"k8s.io/minikube/pkg/minikube/config"
pkgConfig "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
Expand Down Expand Up @@ -226,3 +227,42 @@ func EnableOrDisableStorageClasses(name, val string) error {

return EnableOrDisableAddon(name, val)
}

// ErrValidateProfile Error to validate profile
type ErrValidateProfile struct {
Name string
Msg string
}

func (e ErrValidateProfile) Error() string {
return e.Msg
}

// ValidateProfile checks if the profile user is trying to switch exists, else throws error
func ValidateProfile(profile string) (*ErrValidateProfile, bool) {

validProfiles, invalidProfiles, err := pkgConfig.ListProfiles()
if err != nil {
out.FailureT(err.Error())
}

// handling invalid profiles
for _, invalidProf := range invalidProfiles {
if profile == invalidProf.Name {
return &ErrValidateProfile{Name: profile, Msg: fmt.Sprintf("%q is an invalid profile", profile)}, false
}
}

profileFound := false
// valid profiles if found, setting profileFound to trueexpectedMsg
for _, prof := range validProfiles {
if prof.Name == profile {
profileFound = true
break
}
}
if !profileFound {
return &ErrValidateProfile{Name: profile, Msg: fmt.Sprintf("profile %q not found", profile)}, false
}
return nil, true
}
24 changes: 24 additions & 0 deletions cmd/minikube/cmd/config/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package config

import (
"fmt"
"testing"

"k8s.io/minikube/pkg/minikube/assets"
Expand Down Expand Up @@ -111,3 +112,26 @@ func TestIsAddonAlreadySet(t *testing.T) {
}
}
}

func TestValidateProfile(t *testing.T) {
testCases := []struct {
profileName string
}{
{
profileName: "82374328742_2974224498",
},
{
profileName: "minikube",
},
}

for _, test := range testCases {
profileNam := test.profileName
expectedMsg := fmt.Sprintf("profile %q not found", test.profileName)

err, ok := ValidateProfile(profileNam)
if !ok && err.Error() != expectedMsg {
t.Errorf("Didnt receive expected message")
}
}
}