Skip to content

Commit 144f651

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

File tree

2 files changed

+49
-50
lines changed

2 files changed

+49
-50
lines changed

cmd/minikube/cmd/start.go

+47-48
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,34 @@ func showKubectlConnectInfo(kubeconfig *pkgutil.KubeConfigSetup) {
293271
}
294272
}
295273

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

307290
} else {
308-
// make sure global is preferred
309-
countries = []string{""}
310-
for k := range repos {
311-
if k != "" {
312-
countries = append(countries, k)
291+
// always make sure global is preferred
292+
tryCountries = append(tryCountries, "global")
293+
for k := range constants.ImageRepositories {
294+
if strings.ToLower(k) != "global" {
295+
tryCountries = append(tryCountries, k)
313296
}
314297
}
315298
}
316299

317300
checkRepository := func(repo string) error {
318-
podInfraContainerImage, _ := constants.GetKubeadmCachedImages(repo, config.KubernetesConfig.KubernetesVersion)
301+
podInfraContainerImage, _ := constants.GetKubeadmCachedImages(repo, k8sVersion)
319302

320303
ref, err := name.ParseReference(podInfraContainerImage, name.WeakValidation)
321304
if err != nil {
@@ -326,8 +309,8 @@ func selectImageRepository(config cfg.Config) (bool, string, error) {
326309
return err
327310
}
328311

329-
for _, code := range countries {
330-
localRepos := repos[code]
312+
for _, code := range tryCountries {
313+
localRepos := constants.ImageRepositories[code]
331314
for _, repo := range localRepos {
332315
err := checkRepository(repo)
333316
if err == nil {
@@ -336,15 +319,7 @@ func selectImageRepository(config cfg.Config) (bool, string, error) {
336319
}
337320
}
338321

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
322+
return false, fallback, nil
348323
}
349324

350325
// validateConfig validates the supplied configuration against known bad combinations
@@ -417,6 +392,30 @@ func generateConfig(cmd *cobra.Command, k8sVersion string) (cfg.Config, error) {
417392
}
418393
}
419394

395+
repository := viper.GetString(imageRepository)
396+
mirrorCountry := strings.ToLower(viper.GetString(imageMirrorCountry))
397+
if strings.ToLower(repository) == "auto" || mirrorCountry != "" {
398+
console.OutStyle("connectivity", "checking main repository and mirrors for images")
399+
found, autoSelectedRepository, err := selectImageRepository(mirrorCountry, 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)