From 8b7aaba627b4f386a4cfc10e7e21e7a6ea0b8d52 Mon Sep 17 00:00:00 2001 From: Thomas Stromberg Date: Tue, 10 Dec 2019 07:58:19 -0800 Subject: [PATCH] Sync correct Kubernetes versions from kubeadm constants --- cmd/minikube/cmd/start.go | 4 +- pkg/minikube/bootstrapper/bootstrapper.go | 7 +- pkg/minikube/bootstrapper/images/images.go | 223 ++++--------------- pkg/minikube/bootstrapper/images/repo.go | 36 +++ pkg/minikube/bootstrapper/kubeadm/kubeadm.go | 7 +- pkg/minikube/cruntime/containerd.go | 2 +- pkg/minikube/cruntime/cri.go | 2 +- pkg/minikube/machine/cache_images.go | 5 +- test/integration/a_serial_test.go | 6 +- 9 files changed, 106 insertions(+), 186 deletions(-) create mode 100644 pkg/minikube/bootstrapper/images/repo.go diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index bba5f0fd5f..949e29d1f0 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -673,7 +673,7 @@ func selectImageRepository(mirrorCountry string, k8sVersion string) (bool, strin } checkRepository := func(repo string) error { - pauseImage := images.PauseImage(repo, k8sVersion) + pauseImage := images.Pause(repo) ref, err := name.ParseReference(pauseImage, name.WeakValidation) if err != nil { return err @@ -1108,7 +1108,7 @@ func tryRegistry(r command.Runner) { repo := viper.GetString(imageRepository) if repo == "" { - repo = images.DefaultImageRepo + repo = images.DefaultKubernetesRepo } opts = append(opts, fmt.Sprintf("https://%s/", repo)) diff --git a/pkg/minikube/bootstrapper/bootstrapper.go b/pkg/minikube/bootstrapper/bootstrapper.go index 119466da0a..81018a09ea 100644 --- a/pkg/minikube/bootstrapper/bootstrapper.go +++ b/pkg/minikube/bootstrapper/bootstrapper.go @@ -64,12 +64,11 @@ func GetCachedBinaryList(bootstrapper string) []string { } // GetCachedImageList returns the list of images for a version -func GetCachedImageList(imageRepository string, version string, bootstrapper string) []string { +func GetCachedImageList(imageRepository string, version string, bootstrapper string) ([]string, error) { switch bootstrapper { case BootstrapperTypeKubeadm: - images := images.CachedImages(imageRepository, version) - return images + return images.Kubeadm(imageRepository, version) default: - return []string{} + return []string{}, nil } } diff --git a/pkg/minikube/bootstrapper/images/images.go b/pkg/minikube/bootstrapper/images/images.go index a5f0dcaf35..247d41fb52 100644 --- a/pkg/minikube/bootstrapper/images/images.go +++ b/pkg/minikube/bootstrapper/images/images.go @@ -1,5 +1,5 @@ /* -Copyright 2016 The Kubernetes Authors All rights reserved. +Copyright 2019 The Kubernetes Authors All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -14,188 +14,63 @@ See the License for the specific language governing permissions and limitations under the License. */ +// Package images implements helpers for getting image names package images import ( + "path" "runtime" - "strings" - - "github.com/blang/semver" - "github.com/golang/glog" - minikubeVersion "k8s.io/minikube/pkg/version" ) -const ( - // DefaultImageRepo is the default repository for images - DefaultImageRepo = "k8s.gcr.io" - // DefaultMinikubeRepo is the default repository for minikube - DefaultMinikubeRepo = "gcr.io/k8s-minikube" -) - -// getImageRepositories returns either the k8s image registry on GCR or a mirror if specified -func getImageRepository(imageRepository string) string { - if imageRepository == "" { - imageRepository = DefaultImageRepo - } - if !strings.HasSuffix(imageRepository, "/") { - imageRepository += "/" - } - - return imageRepository -} - -// getMinikubeRepository returns either the minikube image registry on GCR or a mirror if specified -func getMinikubeRepository(imageRepository string) string { - minikubeRepository := imageRepository - if minikubeRepository == "" { - minikubeRepository = DefaultMinikubeRepo - } - if !strings.HasSuffix(minikubeRepository, "/") { - minikubeRepository += "/" - } - - return minikubeRepository -} - -// CachedImages gets the images to cache for kubeadm for a version -func CachedImages(imageRepositoryStr string, kubernetesVersionStr string) []string { - imageRepository := getImageRepository(imageRepositoryStr) - minikubeRepository := getMinikubeRepository(imageRepositoryStr) - - v1_16plus := semver.MustParseRange(">=1.16.0") - v1_14plus := semver.MustParseRange(">=1.14.0 <1.16.0") - v1_13 := semver.MustParseRange(">=1.13.0 <1.14.0") - v1_12 := semver.MustParseRange(">=1.12.0 <1.13.0") - v1_11 := semver.MustParseRange(">=1.11.0 <1.12.0") - v1_12plus := semver.MustParseRange(">=1.12.0") - - kubernetesVersion, err := semver.Make(strings.TrimPrefix(kubernetesVersionStr, minikubeVersion.VersionPrefix)) - if err != nil { - glog.Errorln("Error parsing version semver: ", err) - } - - var images []string - if v1_12plus(kubernetesVersion) { - images = append(images, []string{ - imageRepository + "kube-proxy" + ArchTag(false) + kubernetesVersionStr, - imageRepository + "kube-scheduler" + ArchTag(false) + kubernetesVersionStr, - imageRepository + "kube-controller-manager" + ArchTag(false) + kubernetesVersionStr, - imageRepository + "kube-apiserver" + ArchTag(false) + kubernetesVersionStr, - }...) - } else { - images = append(images, []string{ - imageRepository + "kube-proxy" + ArchTag(true) + kubernetesVersionStr, - imageRepository + "kube-scheduler" + ArchTag(true) + kubernetesVersionStr, - imageRepository + "kube-controller-manager" + ArchTag(true) + kubernetesVersionStr, - imageRepository + "kube-apiserver" + ArchTag(true) + kubernetesVersionStr, - }...) - } - - podInfraContainerImage := PauseImage(imageRepository, kubernetesVersionStr) - if v1_16plus(kubernetesVersion) { - images = append(images, []string{ - podInfraContainerImage, - imageRepository + "k8s-dns-kube-dns" + ArchTag(true) + "1.14.13", - imageRepository + "k8s-dns-dnsmasq-nanny" + ArchTag(true) + "1.14.13", - imageRepository + "k8s-dns-sidecar" + ArchTag(true) + "1.14.13", - imageRepository + "etcd" + ArchTag(false) + "3.3.15-0", - imageRepository + "coredns" + ArchTag(false) + "1.6.2", - }...) - - } else if v1_14plus(kubernetesVersion) { - images = append(images, []string{ - podInfraContainerImage, - imageRepository + "k8s-dns-kube-dns" + ArchTag(true) + "1.14.13", - imageRepository + "k8s-dns-dnsmasq-nanny" + ArchTag(true) + "1.14.13", - imageRepository + "k8s-dns-sidecar" + ArchTag(true) + "1.14.13", - imageRepository + "etcd" + ArchTag(false) + "3.3.10", - imageRepository + "coredns" + ArchTag(false) + "1.3.1", - }...) - - } else if v1_13(kubernetesVersion) { - images = append(images, []string{ - podInfraContainerImage, - imageRepository + "k8s-dns-kube-dns" + ArchTag(true) + "1.14.8", - imageRepository + "k8s-dns-dnsmasq-nanny" + ArchTag(true) + "1.14.8", - imageRepository + "k8s-dns-sidecar" + ArchTag(true) + "1.14.8", - imageRepository + "etcd" + ArchTag(false) + "3.2.24", - imageRepository + "coredns:1.2.6", - }...) - - } else if v1_12(kubernetesVersion) { - images = append(images, []string{ - podInfraContainerImage, - imageRepository + "k8s-dns-kube-dns" + ArchTag(true) + "1.14.8", - imageRepository + "k8s-dns-dnsmasq-nanny" + ArchTag(true) + "1.14.8", - imageRepository + "k8s-dns-sidecar" + ArchTag(true) + "1.14.8", - imageRepository + "etcd" + ArchTag(false) + "3.2.24", - imageRepository + "coredns:1.2.2", - }...) - - } else if v1_11(kubernetesVersion) { - images = append(images, []string{ - podInfraContainerImage, - imageRepository + "k8s-dns-kube-dns" + ArchTag(true) + "1.14.8", - imageRepository + "k8s-dns-dnsmasq-nanny" + ArchTag(true) + "1.14.8", - imageRepository + "k8s-dns-sidecar" + ArchTag(true) + "1.14.8", - imageRepository + "etcd" + ArchTag(true) + "3.2.18", - imageRepository + "coredns:1.1.3", - }...) - } - - images = append(images, []string{ - // This must match deploy/addons/dashboard/dashboard-dp.yaml - "kubernetesui/dashboard:v2.0.0-beta8", - imageRepository + "kube-addon-manager" + ArchTag(false) + "v9.0", - minikubeRepository + "storage-provisioner" + ArchTag(false) + "v1.8.1", - }...) - - return images -} - -// PauseImage returns the image name for pause image (for pod infra) -func PauseImage(imageRepositoryStr string, kubernetesVersionStr string) string { - imageRepository := getImageRepository(imageRepositoryStr) - - v1_16plus := semver.MustParseRange(">=1.16.0") - v1_14plus := semver.MustParseRange(">=1.14.0 <1.16.0") - v1_13 := semver.MustParseRange(">=1.13.0 <1.14.0") - v1_12 := semver.MustParseRange(">=1.12.0 <1.13.0") - v1_11 := semver.MustParseRange(">=1.11.0 <1.12.0") - - kubernetesVersion, err := semver.Make(strings.TrimPrefix(kubernetesVersionStr, minikubeVersion.VersionPrefix)) - if err != nil { - glog.Errorln("Error parsing version semver: ", err) - } - - var podInfraContainerImage string - switch { - case v1_16plus(kubernetesVersion): - podInfraContainerImage = imageRepository + "pause:3.1" - - case v1_14plus(kubernetesVersion): - podInfraContainerImage = imageRepository + "pause:3.1" - - case v1_13(kubernetesVersion): - podInfraContainerImage = imageRepository + "pause" + ArchTag(false) + "3.1" - - case v1_12(kubernetesVersion): - podInfraContainerImage = imageRepository + "pause:3.1" - - case v1_11(kubernetesVersion): - podInfraContainerImage = imageRepository + "pause" + ArchTag(false) + "3.1" - - default: - podInfraContainerImage = imageRepository + "pause" + ArchTag(false) + "3.0" - } - - return podInfraContainerImage -} - -// ArchTag returns the archtag for images +// ArchTag returns a CPU architecture suffix for images func ArchTag(hasTag bool) string { if runtime.GOARCH == "amd64" && !hasTag { return ":" } return "-" + runtime.GOARCH + ":" } + +// Auxiliary returns images that are helpful for running minikube +func Auxiliary(mirror string) []string { + return []string{ + addonManager(mirror), + storageProvisioner(mirror), + dashboardFrontend(mirror), + dashboardMetrics(mirror), + } +} + +// Pause returns the image name for the pause image +func Pause(mirror string) string { + // Should match `PauseVersion` in: + // https://github.com/kubernetes/kubernetes/blob/master/cmd/kubeadm/app/constants/constants.go + return path.Join(KubernetesRepo(mirror), "pause"+ArchTag(false)+"3.1") +} + +// storageProvisioner returns the minikube storage provisioner image +func storageProvisioner(mirror string) string { + return path.Join(minikubeRepo(mirror), "storage-provisioner"+ArchTag(false)+"v1.8.1") +} + +// addonManager returns the Kubernetes addon manager image +func addonManager(mirror string) string { + return path.Join(KubernetesRepo(mirror), "kube-addon-manager"+ArchTag(false)+"v9.0.2") +} + +// dashboardFrontend returns the image used for the dashboard frontend +func dashboardFrontend(repo string) string { + if repo == "" { + repo = "kubernetesui" + } + // See 'kubernetes-dashboard' in deploy/addons/dashboard/dashboard-dp.yaml + return path.Join(repo, "dashboard:v2.0.0-beta8") +} + +// dashboardMetrics returns the image used for the dashboard metrics scraper +func dashboardMetrics(repo string) string { + if repo == "" { + repo = "kubernetesui" + } + // See 'dashboard-metrics-scraper' in deploy/addons/dashboard/dashboard-dp.yaml + return path.Join(repo, "metrics-scraper:v1.0.2") +} diff --git a/pkg/minikube/bootstrapper/images/repo.go b/pkg/minikube/bootstrapper/images/repo.go new file mode 100644 index 0000000000..02cf44345b --- /dev/null +++ b/pkg/minikube/bootstrapper/images/repo.go @@ -0,0 +1,36 @@ +/* +Copyright 2019 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package images + +// DefaultKubernetesRepo is the default Kubernetes repository +const DefaultKubernetesRepo = "k8s.gcr.io" + +// KubernetesRepo returns the official Kubernetes repository, or an alternate +func KubernetesRepo(mirror string) string { + if mirror != "" { + return mirror + } + return DefaultKubernetesRepo +} + +// minikubeRepo returns the official minikube repository, or an alternate +func minikubeRepo(mirror string) string { + if mirror != "" { + return mirror + } + return "gcr.io/k8s-minikube" +} diff --git a/pkg/minikube/bootstrapper/kubeadm/kubeadm.go b/pkg/minikube/bootstrapper/kubeadm/kubeadm.go index 2643ec9d3c..52122891b7 100644 --- a/pkg/minikube/bootstrapper/kubeadm/kubeadm.go +++ b/pkg/minikube/bootstrapper/kubeadm/kubeadm.go @@ -596,7 +596,7 @@ func NewKubeletConfig(k8s config.KubernetesConfig, r cruntime.Manager) ([]byte, extraOpts["node-ip"] = k8s.NodeIP } - pauseImage := images.PauseImage(k8s.ImageRepository, k8s.KubernetesVersion) + pauseImage := images.Pause(k8s.ImageRepository) if _, ok := extraOpts["pod-infra-container-image"]; !ok && k8s.ImageRepository != "" && pauseImage != "" && k8s.ContainerRuntime != remoteContainerRuntime { extraOpts["pod-infra-container-image"] = pauseImage } @@ -630,7 +630,10 @@ func NewKubeletConfig(k8s config.KubernetesConfig, r cruntime.Manager) ([]byte, // UpdateCluster updates the cluster func (k *Bootstrapper) UpdateCluster(cfg config.KubernetesConfig) error { - images := images.CachedImages(cfg.ImageRepository, cfg.KubernetesVersion) + images, err := images.Kubeadm(cfg.ImageRepository, cfg.KubernetesVersion) + if err != nil { + return errors.Wrap(err, "kubeadm images") + } if cfg.ShouldLoadCachedImages { if err := machine.LoadImages(k.c, images, constants.ImageCacheDir); err != nil { out.FailureT("Unable to load cached images: {{.error}}", out.V{"error": err}) diff --git a/pkg/minikube/cruntime/containerd.go b/pkg/minikube/cruntime/containerd.go index 934f65c416..99a2daa797 100644 --- a/pkg/minikube/cruntime/containerd.go +++ b/pkg/minikube/cruntime/containerd.go @@ -175,7 +175,7 @@ func generateContainerdConfig(cr CommandRunner, imageRepository string, k8sVersi if err != nil { return err } - pauseImage := images.PauseImage(imageRepository, k8sVersion) + pauseImage := images.Pause(imageRepository) opts := struct{ PodInfraContainerImage string }{PodInfraContainerImage: pauseImage} var b bytes.Buffer if err := t.Execute(&b, opts); err != nil { diff --git a/pkg/minikube/cruntime/cri.go b/pkg/minikube/cruntime/cri.go index 173e652301..92c9a6af1f 100644 --- a/pkg/minikube/cruntime/cri.go +++ b/pkg/minikube/cruntime/cri.go @@ -413,7 +413,7 @@ func generateCRIOConfig(cr CommandRunner, imageRepository string, k8sVersion str if err != nil { return err } - pauseImage := images.PauseImage(imageRepository, k8sVersion) + pauseImage := images.Pause(imageRepository) opts := struct{ PodInfraContainerImage string }{PodInfraContainerImage: pauseImage} var b bytes.Buffer if err := t.Execute(&b, opts); err != nil { diff --git a/pkg/minikube/machine/cache_images.go b/pkg/minikube/machine/cache_images.go index 587a27f816..b0c4c6aeb7 100644 --- a/pkg/minikube/machine/cache_images.go +++ b/pkg/minikube/machine/cache_images.go @@ -55,7 +55,10 @@ var loadImageLock sync.Mutex // CacheImagesForBootstrapper will cache images for a bootstrapper func CacheImagesForBootstrapper(imageRepository string, version string, clusterBootstrapper string) error { - images := bootstrapper.GetCachedImageList(imageRepository, version, clusterBootstrapper) + images, err := bootstrapper.GetCachedImageList(imageRepository, version, clusterBootstrapper) + if err != nil { + return errors.Wrap(err, "cached images list") + } if err := CacheImages(images, constants.ImageCacheDir); err != nil { return errors.Wrapf(err, "Caching images for %s", clusterBootstrapper) diff --git a/test/integration/a_serial_test.go b/test/integration/a_serial_test.go index b71b3e877c..0efa3e7e57 100644 --- a/test/integration/a_serial_test.go +++ b/test/integration/a_serial_test.go @@ -67,7 +67,11 @@ func TestDownloadOnly(t *testing.T) { t.Errorf("%s failed: %v", args, err) } - imgs := images.CachedImages("", v) + imgs, err := images.Kubeadm("", v) + if err != nil { + t.Errorf("kubeadm images: %v", v) + } + for _, img := range imgs { img = strings.Replace(img, ":", "_", 1) // for example kube-scheduler:v1.15.2 --> kube-scheduler_v1.15.2 fp := filepath.Join(localpath.MiniPath(), "cache", "images", img)