Triggers repository detection only when --image-repository=auto

pull/3937/head
Zhongcheng Lao 2019-05-02 18:49:49 +08:00
parent 39c0ec1fa0
commit 144f651385
No known key found for this signature in database
GPG Key ID: 3B0C92A7E58EF413
2 changed files with 49 additions and 50 deletions

View File

@ -133,7 +133,7 @@ func init() {
startCmd.Flags().String(serviceCIDR, pkgutil.DefaultServiceCIDR, "The CIDR to be used for service cluster IPs.") startCmd.Flags().String(serviceCIDR, pkgutil.DefaultServiceCIDR, "The CIDR to be used for service cluster IPs.")
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.") 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.")
startCmd.Flags().StringSliceVar(&registryMirror, "registry-mirror", nil, "Registry mirrors to pass to the Docker daemon") startCmd.Flags().StringSliceVar(&registryMirror, "registry-mirror", nil, "Registry mirrors to pass to the Docker daemon")
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") 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")
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") 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")
startCmd.Flags().String(containerRuntime, "docker", "The container runtime to be used (docker, crio, containerd)") startCmd.Flags().String(containerRuntime, "docker", "The container runtime to be used (docker, crio, containerd)")
startCmd.Flags().String(criSocket, "", "The cri socket path to be used") startCmd.Flags().String(criSocket, "", "The cri socket path to be used")
@ -181,28 +181,6 @@ func runStart(cmd *cobra.Command, args []string) {
exit.WithError("Failed to generate config", err) exit.WithError("Failed to generate config", err)
} }
if config.KubernetesConfig.ImageRepository == "" {
console.OutStyle("connectivity", "checking main repository and mirrors for images")
found, repository, err := selectImageRepository(config)
if err != nil {
exit.WithError("Failed to check main repository and mirrors for images for images", err)
}
if !found {
if repository == "" {
exit.WithCode(exit.Failure, "None of known repositories is accessible. Consider specifying an alternative image repository with --image-repository flag")
} else {
console.Warning("None of known repositories in your location is accessible. Use %s as fallback.", repository)
}
}
config.KubernetesConfig.ImageRepository = repository
}
if config.KubernetesConfig.ImageRepository != "" {
console.OutStyle("success", "using image repository %s", config.KubernetesConfig.ImageRepository)
}
// For non-"none", the ISO is required to boot, so block until it is downloaded // For non-"none", the ISO is required to boot, so block until it is downloaded
if viper.GetString(vmDriver) != constants.DriverNone { if viper.GetString(vmDriver) != constants.DriverNone {
if err := cluster.CacheISO(config.MachineConfig); err != nil { if err := cluster.CacheISO(config.MachineConfig); err != nil {
@ -293,29 +271,34 @@ func showKubectlConnectInfo(kubeconfig *pkgutil.KubeConfigSetup) {
} }
} }
func selectImageRepository(config cfg.Config) (bool, string, error) { func selectImageRepository(mirrorCountry string, k8sVersion string) (bool, string, error) {
mirrorCountry := strings.ToLower(viper.GetString(imageMirrorCountry)) var tryCountries []string
repos := constants.ImageRepositories var fallback string
var countries []string
if mirrorCountry != "" { if mirrorCountry != "" {
_, ok := repos[mirrorCountry] localRepos, ok := constants.ImageRepositories[mirrorCountry]
if !ok { if !ok || len(localRepos) <= 0 {
return false, "", fmt.Errorf("invalid image mirror country code: %s", mirrorCountry) return false, "", fmt.Errorf("invalid image mirror country code: %s", mirrorCountry)
} }
countries = []string{mirrorCountry, ""}
tryCountries = append(tryCountries, mirrorCountry)
// we'll use the first repository as fallback
// when none of the mirrors in the given location is available
fallback = localRepos[0]
} else { } else {
// make sure global is preferred // always make sure global is preferred
countries = []string{""} tryCountries = append(tryCountries, "global")
for k := range repos { for k := range constants.ImageRepositories {
if k != "" { if strings.ToLower(k) != "global" {
countries = append(countries, k) tryCountries = append(tryCountries, k)
} }
} }
} }
checkRepository := func(repo string) error { checkRepository := func(repo string) error {
podInfraContainerImage, _ := constants.GetKubeadmCachedImages(repo, config.KubernetesConfig.KubernetesVersion) podInfraContainerImage, _ := constants.GetKubeadmCachedImages(repo, k8sVersion)
ref, err := name.ParseReference(podInfraContainerImage, name.WeakValidation) ref, err := name.ParseReference(podInfraContainerImage, name.WeakValidation)
if err != nil { if err != nil {
@ -326,8 +309,8 @@ func selectImageRepository(config cfg.Config) (bool, string, error) {
return err return err
} }
for _, code := range countries { for _, code := range tryCountries {
localRepos := repos[code] localRepos := constants.ImageRepositories[code]
for _, repo := range localRepos { for _, repo := range localRepos {
err := checkRepository(repo) err := checkRepository(repo)
if err == nil { if err == nil {
@ -336,15 +319,7 @@ func selectImageRepository(config cfg.Config) (bool, string, error) {
} }
} }
if mirrorCountry != "" { return false, fallback, nil
if localRepos, ok := constants.ImageRepositories[mirrorCountry]; ok && len(localRepos) > 0 {
// none of the mirrors in the given location is available
// use the first as fallback
return false, localRepos[0], nil
}
}
return false, "", nil
} }
// validateConfig validates the supplied configuration against known bad combinations // validateConfig validates the supplied configuration against known bad combinations
@ -417,6 +392,30 @@ func generateConfig(cmd *cobra.Command, k8sVersion string) (cfg.Config, error) {
} }
} }
repository := viper.GetString(imageRepository)
mirrorCountry := strings.ToLower(viper.GetString(imageMirrorCountry))
if strings.ToLower(repository) == "auto" || mirrorCountry != "" {
console.OutStyle("connectivity", "checking main repository and mirrors for images")
found, autoSelectedRepository, err := selectImageRepository(mirrorCountry, k8sVersion)
if err != nil {
exit.WithError("Failed to check main repository and mirrors for images for images", err)
}
if !found {
if autoSelectedRepository == "" {
exit.WithCode(exit.Failure, "None of known repositories is accessible. Consider specifying an alternative image repository with --image-repository flag")
} else {
console.Warning("None of known repositories in your location is accessible. Use %s as fallback.", autoSelectedRepository)
}
}
repository = autoSelectedRepository
}
if repository != "" {
console.OutStyle("success", "using image repository %s", repository)
}
cfg := cfg.Config{ cfg := cfg.Config{
MachineConfig: cfg.MachineConfig{ MachineConfig: cfg.MachineConfig{
MinikubeISO: viper.GetString(isoURL), MinikubeISO: viper.GetString(isoURL),
@ -457,7 +456,7 @@ func generateConfig(cmd *cobra.Command, k8sVersion string) (cfg.Config, error) {
CRISocket: viper.GetString(criSocket), CRISocket: viper.GetString(criSocket),
NetworkPlugin: selectedNetworkPlugin, NetworkPlugin: selectedNetworkPlugin,
ServiceCIDR: viper.GetString(serviceCIDR), ServiceCIDR: viper.GetString(serviceCIDR),
ImageRepository: viper.GetString(imageRepository), ImageRepository: repository,
ExtraOptions: extraOptions, ExtraOptions: extraOptions,
ShouldLoadCachedImages: viper.GetBool(cacheImages), ShouldLoadCachedImages: viper.GetBool(cacheImages),
EnableDefaultCNI: selectedEnableDefaultCNI, EnableDefaultCNI: selectedEnableDefaultCNI,

View File

@ -213,7 +213,7 @@ const (
// ImageRepositories contains all known image repositories // ImageRepositories contains all known image repositories
var ImageRepositories = map[string][]string{ var ImageRepositories = map[string][]string{
"": {""}, // global "global": {""},
"cn": {"registry.cn-hangzhou.aliyuncs.com/google_containers"}, "cn": {"registry.cn-hangzhou.aliyuncs.com/google_containers"},
} }