Skip to content

Commit

Permalink
Merge pull request kubernetes#13782 from zhan9san/feature/cache-images
Browse files Browse the repository at this point in the history
update AliyunMirror image prefix
  • Loading branch information
spowelljr authored Jun 8, 2022
2 parents 5ef6bd4 + 7c4389c commit fdd027b
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 63 deletions.
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/start_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func getRepository(cmd *cobra.Command, k8sVersion string) string {
repository = autoSelectedRepository
}

if repository == "registry.cn-hangzhou.aliyuncs.com/google_containers" {
if repository == constants.AliyunMirror {
download.SetAliyunMirror()
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/addons/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func EnableOrDisableAddon(cc *config.ClusterConfig, name string, val string) err
exit.Error(reason.HostSaveProfile, "Failed to persist images", err)
}

if cc.KubernetesConfig.ImageRepository == "registry.cn-hangzhou.aliyuncs.com/google_containers" {
if cc.KubernetesConfig.ImageRepository == constants.AliyunMirror {
images, customRegistries = assets.FixAddonImagesAndRegistries(addon, images, customRegistries)
}

Expand Down
13 changes: 12 additions & 1 deletion pkg/minikube/bootstrapper/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func coreDNS(v semver.Version, mirror string) string {
cv = findLatestTagFromRepository(fmt.Sprintf(tagURLTemplate, kubernetesRepo(mirror), imageName), cv)
}

if mirror == constants.AliyunMirror {
imageName = "coredns"
}

return fmt.Sprintf("%s:%s", path.Join(kubernetesRepo(mirror), imageName), cv)
}

Expand Down Expand Up @@ -161,7 +165,14 @@ func auxiliary(mirror string) []string {

// storageProvisioner returns the minikube storage provisioner image
func storageProvisioner(mirror string) string {
return path.Join(minikubeRepo(mirror), "storage-provisioner:"+version.GetStorageProvisionerVersion())
cv := version.GetStorageProvisionerVersion()
in := "k8s-minikube/storage-provisioner:" + cv
if mirror == "" {
mirror = "gcr.io"
} else if mirror == constants.AliyunMirror {
in = "storage-provisioner:" + cv
}
return path.Join(mirror, in)
}

// KindNet returns the image used for kindnet
Expand Down
50 changes: 50 additions & 0 deletions pkg/minikube/bootstrapper/images/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,46 @@ func TestGetLatestTag(t *testing.T) {
}
}

func TestEssentialsAliyunMirror(t *testing.T) {
var testCases = []struct {
version string
images []string
}{

{"v1.21.0", strings.Split(strings.Trim(`
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.21.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.21.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.21.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.21.0
registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:v1.8.0
`, "\n"), "\n")},
{"v1.22.0", strings.Split(strings.Trim(`
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.22.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.22.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.22.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.22.0
registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.5
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.0-0
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:v1.8.4
`, "\n"), "\n")},
}
for _, tc := range testCases {
t.Run(tc.version, func(t *testing.T) {
v, err := semver.Make(strings.TrimPrefix(tc.version, "v"))
if err != nil {
t.Fatal(err)
}
want := tc.images
got := essentials("registry.cn-hangzhou.aliyuncs.com/google_containers", v)
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("images mismatch (-want +got):\n%s", diff)
}
})
}
}

func TestAuxiliary(t *testing.T) {
want := []string{
"gcr.io/k8s-minikube/storage-provisioner:" + version.GetStorageProvisionerVersion(),
Expand All @@ -147,6 +187,16 @@ func TestAuxiliaryMirror(t *testing.T) {
}
}

func TestAuxiliaryAliyunMirror(t *testing.T) {
want := []string{
"registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner:" + version.GetStorageProvisionerVersion(),
}
got := auxiliary("registry.cn-hangzhou.aliyuncs.com/google_containers")
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("images mismatch (-want +got):\n%s", diff)
}
}

func TestCNI(t *testing.T) {
// images used by k8s.io/minikube/pkg/minikube/cni
var testCases = []struct {
Expand Down
10 changes: 0 additions & 10 deletions pkg/minikube/bootstrapper/images/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ limitations under the License.

package images

import "path"

// DefaultKubernetesRepo is the default Kubernetes repository
const DefaultKubernetesRepo = "k8s.gcr.io"

Expand All @@ -28,11 +26,3 @@ func kubernetesRepo(mirror string) string {
}
return DefaultKubernetesRepo
}

// minikubeRepo returns the official minikube repository, or an alternate
func minikubeRepo(mirror string) string {
if mirror == "" {
mirror = "gcr.io"
}
return path.Join(mirror, "k8s-minikube")
}
24 changes: 0 additions & 24 deletions pkg/minikube/bootstrapper/images/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,3 @@ func Test_kubernetesRepo(t *testing.T) {
}

}

func Test_minikubeRepo(t *testing.T) {
tests := []struct {
mirror string
want string
}{
{
"",
"gcr.io/k8s-minikube",
},
{
"mirror.k8s.io",
"mirror.k8s.io/k8s-minikube",
},
}

for _, tc := range tests {
got := minikubeRepo(tc.mirror)
if !cmp.Equal(got, tc.want) {
t.Errorf("mirror miss match, want: %s, got: %s", tc.want, got)
}
}

}
5 changes: 4 additions & 1 deletion pkg/minikube/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ const (
MountTypeFlag = "type"
// MountUIDFlag is the flag used to set the mount UID
MountUIDFlag = "uid"

// Mirror CN
AliyunMirror = "registry.cn-hangzhou.aliyuncs.com/google_containers"
)

var (
Expand Down Expand Up @@ -177,7 +180,7 @@ var (
// ImageRepositories contains all known image repositories
ImageRepositories = map[string][]string{
"global": {""},
"cn": {"registry.cn-hangzhou.aliyuncs.com/google_containers"},
"cn": {AliyunMirror},
}
// KubernetesReleaseBinaries are Kubernetes release binaries required for
// kubeadm (kubelet, kubeadm) and the addon manager (kubectl)
Expand Down
26 changes: 2 additions & 24 deletions pkg/minikube/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,12 @@ func retrieveImage(ref name.Reference, imgName string) (v1.Image, string, error)
}
}
if useRemote {
ref, canonicalName, err := fixRemoteImageName(ref, imgName)
if err != nil {
return nil, "", err
}
cname := canonicalName(ref)
img, err = retrieveRemote(ref, defaultPlatform)
if err == nil {
img, err = fixPlatform(ref, img, defaultPlatform)
if err == nil {
return img, canonicalName, nil
return img, cname, nil
}
}
}
Expand Down Expand Up @@ -321,22 +318,3 @@ func normalizeTagName(image string) string {
}
return base + ":" + tag
}

func fixRemoteImageName(ref name.Reference, imgName string) (name.Reference, string, error) {
const aliyunMirror = "registry.cn-hangzhou.aliyuncs.com/google_containers/"
if strings.HasPrefix(imgName, aliyunMirror) {
// for aliyun registry must strip namespace from image name, e.g.
// registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns:v1.8.0 will not work
// registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.8.0 does work
image := strings.TrimPrefix(imgName, aliyunMirror)
image = strings.TrimPrefix(image, "k8s-minikube/")
image = strings.TrimPrefix(image, "kubernetesui/")
image = strings.TrimPrefix(image, "coredns/")
remoteRef, err := name.ParseReference(aliyunMirror+image, name.WeakValidation)
if err != nil {
return nil, "", err
}
return remoteRef, canonicalName(ref), nil
}
return ref, canonicalName(ref), nil
}
2 changes: 1 addition & 1 deletion pkg/minikube/node/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func imagesInConfigFile() ([]string, error) {

func updateKicImageRepo(imgName string, repo string) string {
image := strings.TrimPrefix(imgName, "gcr.io/")
if repo == "registry.cn-hangzhou.aliyuncs.com/google_containers" {
if repo == constants.AliyunMirror {
// for aliyun registry must strip namespace from image name, e.g.
// registry.cn-hangzhou.aliyuncs.com/google_containers/k8s-minikube/kicbase:v0.0.25 will not work
// registry.cn-hangzhou.aliyuncs.com/google_containers/kicbase:v0.0.25 does work
Expand Down

0 comments on commit fdd027b

Please sign in to comment.