add helpers and unit test

pull/7793/head
Medya Gh 2020-04-20 13:32:55 -07:00
parent 366dc8d127
commit 16c2077106
No known key found for this signature in database
GPG Key ID: 7CF7792C6DF3245C
4 changed files with 68 additions and 18 deletions

View File

@ -0,0 +1,40 @@
/*
Copyright 2020 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 cruntime
import (
"testing"
)
func TestAddRepoTagToImageName(t *testing.T) {
var tests = []struct {
imgName string
want string
}{
{"kubernetesui/dashboard:v2.0.0-rc6", "docker.io/kubernetesui/dashboard:v2.0.0-rc6"},
{"kubernetesui/metrics-scraper:v1.0.2", "docker.io/kubernetesui/metrics-scraper:v1.0.2"},
{"gcr.io/k8s-minikube/storage-provisioner:v1.8.1", "gcr.io/k8s-minikube/storage-provisioner:v1.8.1"},
}
for _, tc := range tests {
t.Run(tc.imgName, func(t *testing.T) {
got := addRepoTagToImageName(tc.imgName)
if got != tc.want {
t.Errorf("expected image name to be: %q but got %q", tc.want, got)
}
})
}
}

View File

@ -398,11 +398,7 @@ func ContainerdImagesPreloaded(runner command.Runner, images []string) bool {
found := false
for _, ji := range jsonImages.Images {
for _, rt := range ji.RepoTags {
// in crictl docker.io is appened to images without repo organization
// for example "kubernetesui/dashboard:v2.0.0-rc6 will show up as "docker.io/kubernetesui/dashboard:v2.0.0-rc6"
if !strings.Contains(i, ".io/") {
i = "docker.io/" + i
}
i = addRepoTagToImageName(i)
if i == rt {
found = true
break
@ -421,3 +417,15 @@ func ContainerdImagesPreloaded(runner command.Runner, images []string) bool {
glog.Infof("all images are preloaded for containerd runtime.")
return true
}
// addRepoTagToImageName makes sure the image name has a repo tag in it.
// in crictl images list have the repo tag prepended to them
// for example "kubernetesui/dashboard:v2.0.0 will show up as "docker.io/kubernetesui/dashboard:v2.0.0"
// warning this is only meant for kuberentes images where we know the GCR addreses have .io in them
// not mean to be used for public images
func addRepoTagToImageName(imgName string) string {
if !strings.Contains(imgName, ".io/") {
return "docker.io/" + imgName
} // else it already has repo name dont add anything
return imgName
}

View File

@ -220,3 +220,14 @@ func enableIPForwarding(cr CommandRunner) error {
}
return nil
}
// ImagesPreloaded returns true if all images have been preloaded
func ImagesPreloaded(containerRuntime string, runner command.Runner, images []string) bool {
if containerRuntime == "docker" {
return DockerImagesPreloaded(runner, images)
}
if containerRuntime == "containerd" {
return ContainerdImagesPreloaded(runner, images)
}
return false
}

View File

@ -64,19 +64,10 @@ func CacheImagesForBootstrapper(imageRepository string, version string, clusterB
// LoadImages loads previously cached images into the container runtime
func LoadImages(cc *config.ClusterConfig, runner command.Runner, images []string, cacheDir string) error {
if cc.KubernetesConfig.ContainerRuntime == "docker" {
// Skip loading images if images already exist
if cruntime.DockerImagesPreloaded(runner, images) {
glog.Infof("Images are preloaded, skipping loading")
return nil
}
}
if cc.KubernetesConfig.ContainerRuntime == "containerd" {
// Skip loading images if images already exist
if cruntime.ContainerdImagesPreloaded(runner, images) {
glog.Infof("Images are preloaded, skipping loading")
return nil
}
// Skip loading images if images already exist
if cruntime.ImagesPreloaded(cc.KubernetesConfig.ContainerRuntime, runner, images) {
glog.Infof("Images are preloaded, skipping loading")
return nil
}
glog.Infof("LoadImages start: %s", images)
start := time.Now()