未验证 提交 16c20771 编写于 作者: M Medya Gh

add helpers and unit test

上级 366dc8d1
/*
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)
}
})
}
}
......@@ -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
}
......@@ -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
}
......@@ -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()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册