image_list.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 e2e_node
  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. "google/cadvisor:latest",
  39. "k8s.gcr.io/stress:v1",
  40. busyboxImage,
  41. "k8s.gcr.io/busybox@sha256:4bdd623e848417d96127e16037743f0cd8b528c026e9175e22a84f639eca58ff",
  42. imageutils.GetE2EImage(imageutils.Nginx),
  43. imageutils.GetE2EImage(imageutils.Perl),
  44. imageutils.GetE2EImage(imageutils.ServeHostname),
  45. imageutils.GetE2EImage(imageutils.Netexec),
  46. imageutils.GetE2EImage(imageutils.Nonewprivs),
  47. imageutils.GetPauseImageName(),
  48. gpu.GetGPUDevicePluginImage(),
  49. "gcr.io/kubernetes-e2e-test-images/node-perf/npb-is:1.0",
  50. "gcr.io/kubernetes-e2e-test-images/node-perf/npb-ep:1.0",
  51. "gcr.io/kubernetes-e2e-test-images/node-perf/tf-wide-deep-amd64:1.0",
  52. )
  53. // updateImageWhiteList updates the framework.ImageWhiteList with
  54. // 1. the hard coded lists
  55. // 2. the ones passed in from framework.TestContext.ExtraEnvs
  56. // So this function needs to be called after the extra envs are applied.
  57. func updateImageWhiteList() {
  58. // Union NodeImageWhiteList and CommonImageWhiteList into the framework image white list.
  59. framework.ImageWhiteList = NodeImageWhiteList.Union(commontest.CommonImageWhiteList)
  60. // Images from extra envs
  61. framework.ImageWhiteList.Insert(getNodeProblemDetectorImage())
  62. }
  63. func getNodeProblemDetectorImage() string {
  64. const defaultImage string = "k8s.gcr.io/node-problem-detector:v0.6.2"
  65. image := os.Getenv("NODE_PROBLEM_DETECTOR_IMAGE")
  66. if image == "" {
  67. image = defaultImage
  68. }
  69. return image
  70. }
  71. // puller represents a generic image puller
  72. type puller interface {
  73. // Pull pulls an image by name
  74. Pull(image string) ([]byte, error)
  75. // Name returns the name of the specific puller implementation
  76. Name() string
  77. }
  78. type dockerPuller struct {
  79. }
  80. func (dp *dockerPuller) Name() string {
  81. return "docker"
  82. }
  83. func (dp *dockerPuller) Pull(image string) ([]byte, error) {
  84. // TODO(random-liu): Use docker client to get rid of docker binary dependency.
  85. return exec.Command("docker", "pull", image).CombinedOutput()
  86. }
  87. type remotePuller struct {
  88. imageService internalapi.ImageManagerService
  89. }
  90. func (rp *remotePuller) Name() string {
  91. return "CRI"
  92. }
  93. func (rp *remotePuller) Pull(image string) ([]byte, error) {
  94. imageStatus, err := rp.imageService.ImageStatus(&runtimeapi.ImageSpec{Image: image})
  95. if err == nil && imageStatus != nil {
  96. return nil, nil
  97. }
  98. _, err = rp.imageService.PullImage(&runtimeapi.ImageSpec{Image: image}, nil, nil)
  99. return nil, err
  100. }
  101. func getPuller() (puller, error) {
  102. runtime := framework.TestContext.ContainerRuntime
  103. switch runtime {
  104. case "docker":
  105. return &dockerPuller{}, nil
  106. case "remote":
  107. _, is, err := getCRIClient()
  108. if err != nil {
  109. return nil, err
  110. }
  111. return &remotePuller{
  112. imageService: is,
  113. }, nil
  114. }
  115. return nil, fmt.Errorf("can't prepull images, unknown container runtime %q", runtime)
  116. }
  117. // Pre-fetch all images tests depend on so that we don't fail in an actual test.
  118. func PrePullAllImages() error {
  119. puller, err := getPuller()
  120. if err != nil {
  121. return err
  122. }
  123. usr, err := user.Current()
  124. if err != nil {
  125. return err
  126. }
  127. images := framework.ImageWhiteList.List()
  128. klog.V(4).Infof("Pre-pulling images with %s %+v", puller.Name(), images)
  129. for _, image := range images {
  130. var (
  131. err error
  132. output []byte
  133. )
  134. for i := 0; i < maxImagePullRetries; i++ {
  135. if i > 0 {
  136. time.Sleep(imagePullRetryDelay)
  137. }
  138. if output, err = puller.Pull(image); err == nil {
  139. break
  140. }
  141. klog.Warningf("Failed to pull %s as user %q, retrying in %s (%d of %d): %v",
  142. image, usr.Username, imagePullRetryDelay.String(), i+1, maxImagePullRetries, err)
  143. }
  144. if err != nil {
  145. klog.Warningf("Could not pre-pull image %s %v output: %s", image, err, output)
  146. return err
  147. }
  148. }
  149. return nil
  150. }