image_list.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package e2enode
  14. import (
  15. "fmt"
  16. "os"
  17. "os/exec"
  18. "os/user"
  19. "time"
  20. "k8s.io/klog"
  21. "k8s.io/apimachinery/pkg/util/sets"
  22. internalapi "k8s.io/cri-api/pkg/apis"
  23. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  24. commontest "k8s.io/kubernetes/test/e2e/common"
  25. "k8s.io/kubernetes/test/e2e/framework"
  26. "k8s.io/kubernetes/test/e2e/framework/gpu"
  27. imageutils "k8s.io/kubernetes/test/utils/image"
  28. )
  29. const (
  30. // Number of attempts to pull an image.
  31. maxImagePullRetries = 5
  32. // Sleep duration between image pull retry attempts.
  33. imagePullRetryDelay = time.Second
  34. )
  35. // NodeImageWhiteList is a list of images used in node e2e test. These images will be prepulled
  36. // before test running so that the image pulling won't fail in actual test.
  37. var NodeImageWhiteList = sets.NewString(
  38. imageutils.GetE2EImage(imageutils.Agnhost),
  39. "google/cadvisor:latest",
  40. "k8s.gcr.io/stress:v1",
  41. busyboxImage,
  42. "k8s.gcr.io/busybox@sha256:4bdd623e848417d96127e16037743f0cd8b528c026e9175e22a84f639eca58ff",
  43. imageutils.GetE2EImage(imageutils.Nginx),
  44. imageutils.GetE2EImage(imageutils.Perl),
  45. imageutils.GetE2EImage(imageutils.Nonewprivs),
  46. imageutils.GetPauseImageName(),
  47. getGPUDevicePluginImage(),
  48. "gcr.io/kubernetes-e2e-test-images/node-perf/npb-is:1.0",
  49. "gcr.io/kubernetes-e2e-test-images/node-perf/npb-ep:1.0",
  50. "gcr.io/kubernetes-e2e-test-images/node-perf/tf-wide-deep-amd64:1.0",
  51. )
  52. // updateImageWhiteList updates the framework.ImageWhiteList with
  53. // 1. the hard coded lists
  54. // 2. the ones passed in from framework.TestContext.ExtraEnvs
  55. // So this function needs to be called after the extra envs are applied.
  56. func updateImageWhiteList() {
  57. // Union NodeImageWhiteList and CommonImageWhiteList into the framework image white list.
  58. framework.ImageWhiteList = NodeImageWhiteList.Union(commontest.CommonImageWhiteList)
  59. // Images from extra envs
  60. framework.ImageWhiteList.Insert(getNodeProblemDetectorImage())
  61. }
  62. func getNodeProblemDetectorImage() string {
  63. const defaultImage string = "k8s.gcr.io/node-problem-detector:v0.6.2"
  64. image := os.Getenv("NODE_PROBLEM_DETECTOR_IMAGE")
  65. if image == "" {
  66. image = defaultImage
  67. }
  68. return image
  69. }
  70. // puller represents a generic image puller
  71. type puller interface {
  72. // Pull pulls an image by name
  73. Pull(image string) ([]byte, error)
  74. // Name returns the name of the specific puller implementation
  75. Name() string
  76. }
  77. type dockerPuller struct {
  78. }
  79. func (dp *dockerPuller) Name() string {
  80. return "docker"
  81. }
  82. func (dp *dockerPuller) Pull(image string) ([]byte, error) {
  83. return exec.Command("docker", "pull", image).CombinedOutput()
  84. }
  85. type remotePuller struct {
  86. imageService internalapi.ImageManagerService
  87. }
  88. func (rp *remotePuller) Name() string {
  89. return "CRI"
  90. }
  91. func (rp *remotePuller) Pull(image string) ([]byte, error) {
  92. imageStatus, err := rp.imageService.ImageStatus(&runtimeapi.ImageSpec{Image: image})
  93. if err == nil && imageStatus != nil {
  94. return nil, nil
  95. }
  96. _, err = rp.imageService.PullImage(&runtimeapi.ImageSpec{Image: image}, nil, nil)
  97. return nil, err
  98. }
  99. func getPuller() (puller, error) {
  100. runtime := framework.TestContext.ContainerRuntime
  101. switch runtime {
  102. case "docker":
  103. return &dockerPuller{}, nil
  104. case "remote":
  105. _, is, err := getCRIClient()
  106. if err != nil {
  107. return nil, err
  108. }
  109. return &remotePuller{
  110. imageService: is,
  111. }, nil
  112. }
  113. return nil, fmt.Errorf("can't prepull images, unknown container runtime %q", runtime)
  114. }
  115. // PrePullAllImages pre-fetches all images tests depend on so that we don't fail in an actual test.
  116. func PrePullAllImages() error {
  117. puller, err := getPuller()
  118. if err != nil {
  119. return err
  120. }
  121. usr, err := user.Current()
  122. if err != nil {
  123. return err
  124. }
  125. images := framework.ImageWhiteList.List()
  126. klog.V(4).Infof("Pre-pulling images with %s %+v", puller.Name(), images)
  127. for _, image := range images {
  128. var (
  129. err error
  130. output []byte
  131. )
  132. for i := 0; i < maxImagePullRetries; i++ {
  133. if i > 0 {
  134. time.Sleep(imagePullRetryDelay)
  135. }
  136. if output, err = puller.Pull(image); err == nil {
  137. break
  138. }
  139. klog.Warningf("Failed to pull %s as user %q, retrying in %s (%d of %d): %v",
  140. image, usr.Username, imagePullRetryDelay.String(), i+1, maxImagePullRetries, err)
  141. }
  142. if err != nil {
  143. klog.Warningf("Could not pre-pull image %s %v output: %s", image, err, output)
  144. return err
  145. }
  146. }
  147. return nil
  148. }
  149. // getGPUDevicePluginImage returns the image of GPU device plugin.
  150. func getGPUDevicePluginImage() string {
  151. ds, err := framework.DsFromManifest(gpu.GPUDevicePluginDSYAML)
  152. if err != nil {
  153. klog.Errorf("Failed to parse the device plugin image: %v", err)
  154. return ""
  155. }
  156. if ds == nil {
  157. klog.Errorf("Failed to parse the device plugin image: the extracted DaemonSet is nil")
  158. return ""
  159. }
  160. if len(ds.Spec.Template.Spec.Containers) < 1 {
  161. klog.Errorf("Failed to parse the device plugin image: cannot extract the container from YAML")
  162. return ""
  163. }
  164. return ds.Spec.Template.Spec.Containers[0].Image
  165. }