Skip to content

Commit

Permalink
Merge pull request #10760 from yxxhero/master
Browse files Browse the repository at this point in the history
add validateImageRepository func for validates --image-repository args
  • Loading branch information
medyagh authored Mar 10, 2021
2 parents df7cb64 + f653337 commit b3b4807
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
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)
}
})
}

}

0 comments on commit b3b4807

Please sign in to comment.