Skip to content

Commit 8f63208

Browse files
committed
Triggers repository detection only when --image-repository=auto
1 parent 39c0ec1 commit 8f63208

File tree

2 files changed

+48
-49
lines changed

2 files changed

+48
-49
lines changed

cmd/minikube/cmd/start.go

+46-47
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func init() {
133133
startCmd.Flags().String(serviceCIDR, pkgutil.DefaultServiceCIDR, "The CIDR to be used for service cluster IPs.")
134134
startCmd.Flags().StringSliceVar(&insecureRegistry, "insecure-registry", nil, "Insecure Docker registries to pass to the Docker daemon. The default service CIDR range will automatically be added.")
135135
startCmd.Flags().StringSliceVar(&registryMirror, "registry-mirror", nil, "Registry mirrors to pass to the Docker daemon")
136-
startCmd.Flags().String(imageRepository, "", "Alternative image repository to pull docker images from. This can be used when you have limited access to gcr.io. For Chinese mainland users, you may use local gcr.io mirrors such as registry.cn-hangzhou.aliyuncs.com/google_containers")
136+
startCmd.Flags().String(imageRepository, "", "Alternative image repository to pull docker images from. This can be used when you have limited access to gcr.io. Set it to \"auto\" to let minikube decide one for you. For Chinese mainland users, you may use local gcr.io mirrors such as registry.cn-hangzhou.aliyuncs.com/google_containers")
137137
startCmd.Flags().String(imageMirrorCountry, "", "Country code of the image mirror to be used. Leave empty to use the global one. For Chinese mainland users, set it to cn")
138138
startCmd.Flags().String(containerRuntime, "docker", "The container runtime to be used (docker, crio, containerd)")
139139
startCmd.Flags().String(criSocket, "", "The cri socket path to be used")
@@ -181,28 +181,6 @@ func runStart(cmd *cobra.Command, args []string) {
181181
exit.WithError("Failed to generate config", err)
182182
}
183183

184-
if config.KubernetesConfig.ImageRepository == "" {
185-
console.OutStyle("connectivity", "checking main repository and mirrors for images")
186-
found, repository, err := selectImageRepository(config)
187-
if err != nil {
188-
exit.WithError("Failed to check main repository and mirrors for images for images", err)
189-
}
190-
191-
if !found {
192-
if repository == "" {
193-
exit.WithCode(exit.Failure, "None of known repositories is accessible. Consider specifying an alternative image repository with --image-repository flag")
194-
} else {
195-
console.Warning("None of known repositories in your location is accessible. Use %s as fallback.", repository)
196-
}
197-
}
198-
199-
config.KubernetesConfig.ImageRepository = repository
200-
}
201-
202-
if config.KubernetesConfig.ImageRepository != "" {
203-
console.OutStyle("success", "using image repository %s", config.KubernetesConfig.ImageRepository)
204-
}
205-
206184
// For non-"none", the ISO is required to boot, so block until it is downloaded
207185
if viper.GetString(vmDriver) != constants.DriverNone {
208186
if err := cluster.CacheISO(config.MachineConfig); err != nil {
@@ -293,29 +271,35 @@ func showKubectlConnectInfo(kubeconfig *pkgutil.KubeConfigSetup) {
293271
}
294272
}
295273

296-
func selectImageRepository(config cfg.Config) (bool, string, error) {
274+
func selectImageRepository(k8sVersion string) (bool, string, error) {
275+
var tryCountries []string
276+
var fallback string
277+
297278
mirrorCountry := strings.ToLower(viper.GetString(imageMirrorCountry))
298-
repos := constants.ImageRepositories
299-
var countries []string
300279
if mirrorCountry != "" {
301-
_, ok := repos[mirrorCountry]
302-
if !ok {
280+
localRepos, ok := constants.ImageRepositories[mirrorCountry]
281+
if !ok || len(localRepos) <= 0 {
303282
return false, "", fmt.Errorf("invalid image mirror country code: %s", mirrorCountry)
304283
}
305-
countries = []string{mirrorCountry, ""}
284+
285+
tryCountries = append(tryCountries, mirrorCountry)
286+
287+
// we'll use the first repository as fallback
288+
// when none of the mirrors in the given location is available
289+
fallback = localRepos[0]
306290

307291
} else {
308-
// make sure global is preferred
309-
countries = []string{""}
310-
for k := range repos {
311-
if k != "" {
312-
countries = append(countries, k)
292+
// always make sure global is preferred
293+
tryCountries = append(tryCountries, "global")
294+
for k := range constants.ImageRepositories {
295+
if strings.ToLower(k) != "global" {
296+
tryCountries = append(tryCountries, k)
313297
}
314298
}
315299
}
316300

317301
checkRepository := func(repo string) error {
318-
podInfraContainerImage, _ := constants.GetKubeadmCachedImages(repo, config.KubernetesConfig.KubernetesVersion)
302+
podInfraContainerImage, _ := constants.GetKubeadmCachedImages(repo, k8sVersion)
319303

320304
ref, err := name.ParseReference(podInfraContainerImage, name.WeakValidation)
321305
if err != nil {
@@ -326,8 +310,8 @@ func selectImageRepository(config cfg.Config) (bool, string, error) {
326310
return err
327311
}
328312

329-
for _, code := range countries {
330-
localRepos := repos[code]
313+
for _, code := range tryCountries {
314+
localRepos := constants.ImageRepositories[code]
331315
for _, repo := range localRepos {
332316
err := checkRepository(repo)
333317
if err == nil {
@@ -336,15 +320,7 @@ func selectImageRepository(config cfg.Config) (bool, string, error) {
336320
}
337321
}
338322

339-
if mirrorCountry != "" {
340-
if localRepos, ok := constants.ImageRepositories[mirrorCountry]; ok && len(localRepos) > 0 {
341-
// none of the mirrors in the given location is available
342-
// use the first as fallback
343-
return false, localRepos[0], nil
344-
}
345-
}
346-
347-
return false, "", nil
323+
return false, fallback, nil
348324
}
349325

350326
// validateConfig validates the supplied configuration against known bad combinations
@@ -417,6 +393,29 @@ func generateConfig(cmd *cobra.Command, k8sVersion string) (cfg.Config, error) {
417393
}
418394
}
419395

396+
repository := viper.GetString(imageRepository)
397+
if strings.ToLower(repository) == "auto" {
398+
console.OutStyle("connectivity", "checking main repository and mirrors for images")
399+
found, autoSelectedRepository, err := selectImageRepository(k8sVersion)
400+
if err != nil {
401+
exit.WithError("Failed to check main repository and mirrors for images for images", err)
402+
}
403+
404+
if !found {
405+
if autoSelectedRepository == "" {
406+
exit.WithCode(exit.Failure, "None of known repositories is accessible. Consider specifying an alternative image repository with --image-repository flag")
407+
} else {
408+
console.Warning("None of known repositories in your location is accessible. Use %s as fallback.", autoSelectedRepository)
409+
}
410+
}
411+
412+
repository = autoSelectedRepository
413+
}
414+
415+
if repository != "" {
416+
console.OutStyle("success", "using image repository %s", repository)
417+
}
418+
420419
cfg := cfg.Config{
421420
MachineConfig: cfg.MachineConfig{
422421
MinikubeISO: viper.GetString(isoURL),
@@ -457,7 +456,7 @@ func generateConfig(cmd *cobra.Command, k8sVersion string) (cfg.Config, error) {
457456
CRISocket: viper.GetString(criSocket),
458457
NetworkPlugin: selectedNetworkPlugin,
459458
ServiceCIDR: viper.GetString(serviceCIDR),
460-
ImageRepository: viper.GetString(imageRepository),
459+
ImageRepository: repository,
461460
ExtraOptions: extraOptions,
462461
ShouldLoadCachedImages: viper.GetBool(cacheImages),
463462
EnableDefaultCNI: selectedEnableDefaultCNI,

pkg/minikube/constants/constants.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ const (
213213

214214
// ImageRepositories contains all known image repositories
215215
var ImageRepositories = map[string][]string{
216-
"": {""}, // global
217-
"cn": {"registry.cn-hangzhou.aliyuncs.com/google_containers"},
216+
"global": {""},
217+
"cn": {"registry.cn-hangzhou.aliyuncs.com/google_containers"},
218218
}
219219

220220
// GetKubernetesReleaseURL gets the location of a kubernetes client

0 commit comments

Comments
 (0)