Skip to content

Commit 49368d3

Browse files
committed
Fixes validation on image repository URL when it contains port but not schema
Refactor the way validations are done to use regular expression Explicitly use "auto" as the imageRepo instead of an empty string when URL received is invalid
1 parent 9c35d50 commit 49368d3

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

cmd/minikube/cmd/start.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,32 +1353,39 @@ func validateRegistryMirror() {
13531353
// args match the format of registry.cn-hangzhou.aliyuncs.com/google_containers
13541354
// also "<hostname>[:<port>]"
13551355
func validateImageRepository(imageRepo string) (validImageRepo string) {
1356+
expression := regexp.MustCompile(`^(?:(\w+)\:\/\/)?([-a-zA-Z0-9]{1,}(?:\.[-a-zA-Z]{1,}){0,})(?:\:(\d+))?(\/.*)?$`)
13561357

13571358
if strings.ToLower(imageRepo) == "auto" {
1358-
validImageRepo = "auto"
1359+
imageRepo = "auto"
13591360
}
1360-
URL, err := url.Parse(imageRepo)
13611361

1362-
if err != nil {
1363-
klog.Errorln("Error Parsing URL: ", err)
1362+
if !expression.MatchString(imageRepo) {
1363+
klog.Errorln("Provided repository is not a valid URL. Defaulting to \"auto\"")
1364+
imageRepo = "auto"
13641365
}
13651366

13661367
var imageRepoPort string
1368+
groups := expression.FindStringSubmatch(imageRepo)
1369+
1370+
scheme := groups[1]
1371+
hostname := groups[2]
1372+
port := groups[3]
1373+
path := groups[4]
13671374

1368-
if URL.Port() != "" && strings.Contains(imageRepo, ":"+URL.Port()) {
1369-
imageRepoPort = ":" + URL.Port()
1375+
if port != "" && strings.Contains(imageRepo, ":"+port) {
1376+
imageRepoPort = ":" + port
13701377
}
13711378

13721379
// tips when imageRepo ended with a trailing /.
13731380
if strings.HasSuffix(imageRepo, "/") {
1374-
out.Infof("The --image-repository flag your provided ended with a trailing / that could cause conflict in kuberentes, removed automatically")
1381+
out.Infof("The --image-repository flag your provided ended with a trailing / that could cause conflict in kubernetes, removed automatically")
13751382
}
13761383
// tips when imageRepo started with scheme such as http(s).
1377-
if URL.Scheme != "" {
1378-
out.Infof("The --image-repository flag your provided contains Scheme: {{.scheme}}, which will be removed automatically", out.V{"scheme": URL.Scheme})
1384+
if scheme != "" {
1385+
out.Infof("The --image-repository flag you provided contains Scheme: {{.scheme}}, which will be removed automatically", out.V{"scheme": scheme})
13791386
}
13801387

1381-
validImageRepo = URL.Hostname() + imageRepoPort + strings.TrimSuffix(URL.Path, "/")
1388+
validImageRepo = hostname + imageRepoPort + strings.TrimSuffix(path, "/")
13821389

13831390
return validImageRepo
13841391
}

cmd/minikube/cmd/start_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,14 @@ func TestValidateImageRepository(t *testing.T) {
329329
imageRepository: "auto",
330330
validImageRepository: "auto",
331331
},
332+
{
333+
imageRepository: "$$$$invalid",
334+
validImageRepository: "auto",
335+
},
336+
{
337+
imageRepository: "",
338+
validImageRepository: "auto",
339+
},
332340
{
333341
imageRepository: "http://registry.test.com/google_containers/",
334342
validImageRepository: "registry.test.com/google_containers",
@@ -353,6 +361,10 @@ func TestValidateImageRepository(t *testing.T) {
353361
imageRepository: "https://registry.test.com:6666/google_containers",
354362
validImageRepository: "registry.test.com:6666/google_containers",
355363
},
364+
{
365+
imageRepository: "registry.test.com:6666/google_containers",
366+
validImageRepository: "registry.test.com:6666/google_containers",
367+
},
356368
}
357369

358370
for _, test := range tests {

0 commit comments

Comments
 (0)