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 validations --image-repository inputs #10760

Merged
merged 2 commits into from
Mar 10, 2021
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
28 changes: 28 additions & 0 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,10 @@ func validateFlags(cmd *cobra.Command, drvName string) {
validateListenAddress(viper.GetString(listenAddress))
}

if cmd.Flags().Changed(imageRepository) {
viper.Set(imageRepository, validateImageRepository(viper.GetString(imageRepository)))
}

if cmd.Flags().Changed(containerRuntime) {
runtime := strings.ToLower(viper.GetString(containerRuntime))

Expand Down Expand Up @@ -1208,6 +1212,30 @@ func validateRegistryMirror() {
}
}

// This function validates if the --image-repository
// args match the format of registry.cn-hangzhou.aliyuncs.com/google_containers
func validateImageRepository(imagRepo string) (vaildImageRepo string) {

if strings.ToLower(imagRepo) == "auto" {
vaildImageRepo = "auto"
}
URL, err := url.Parse(imagRepo)
if err != nil {
klog.Errorln("Error Parsing URL: ", err)
}
// tips when imagRepo ended with a trailing /.
if strings.HasSuffix(imagRepo, "/") {
out.Infof("The --image-repository flag your provided ended with a trailing / that could cause conflict in kuberentes, removed automatically")
}
// tips when imageRepo started with scheme.
if URL.Scheme != "" {
out.Infof("The --image-repository flag your provided contains Scheme: {{.scheme}}, it will be as a domian, removed automatically", out.V{"scheme": URL.Scheme})
}

vaildImageRepo = URL.Hostname() + strings.TrimSuffix(URL.Path, "/")
return
}

// This function validates if the --listen-address
// match the format 0.0.0.0
func validateListenAddress(listenAddr string) {
Expand Down
43 changes: 43 additions & 0 deletions cmd/minikube/cmd/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,46 @@ func TestBaseImageFlagDriverCombo(t *testing.T) {
})
}
}

func TestValidateImageRepository(t *testing.T) {
var tests = []struct {
imageRepository string
vaildImageRepository string
}{
{
imageRepository: "auto",
vaildImageRepository: "auto",
},
{
imageRepository: "http://registry.test.com/google_containers/",
vaildImageRepository: "registry.test.com/google_containers",
},
{
imageRepository: "https://registry.test.com/google_containers/",
vaildImageRepository: "registry.test.com/google_containers",
},
{
imageRepository: "registry.test.com/google_containers/",
vaildImageRepository: "registry.test.com/google_containers",
},
{
imageRepository: "http://registry.test.com/google_containers",
vaildImageRepository: "registry.test.com/google_containers",
},
{
imageRepository: "https://registry.test.com/google_containers",
vaildImageRepository: "registry.test.com/google_containers",
},
}

for _, test := range tests {
t.Run(test.imageRepository, func(t *testing.T) {
vaildImageRepository := validateImageRepository(test.imageRepository)
if vaildImageRepository != test.vaildImageRepository {
t.Errorf("validateImageRepository(imageRepo=%v): got %v, expected %v",
test.imageRepository, vaildImageRepository, test.vaildImageRepository)
}
})
}

}