image_list.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. "k8s.io/kubernetes/test/e2e/framework/testfiles"
  28. imageutils "k8s.io/kubernetes/test/utils/image"
  29. )
  30. const (
  31. // Number of attempts to pull an image.
  32. maxImagePullRetries = 5
  33. // Sleep duration between image pull retry attempts.
  34. imagePullRetryDelay = time.Second
  35. )
  36. // NodeImageWhiteList is a list of images used in node e2e test. These images will be prepulled
  37. // before test running so that the image pulling won't fail in actual test.
  38. var NodeImageWhiteList = sets.NewString(
  39. imageutils.GetE2EImage(imageutils.Agnhost),
  40. "google/cadvisor:latest",
  41. "k8s.gcr.io/stress:v1",
  42. busyboxImage,
  43. "k8s.gcr.io/busybox@sha256:4bdd623e848417d96127e16037743f0cd8b528c026e9175e22a84f639eca58ff",
  44. imageutils.GetE2EImage(imageutils.Nginx),
  45. imageutils.GetE2EImage(imageutils.Perl),
  46. imageutils.GetE2EImage(imageutils.Nonewprivs),
  47. imageutils.GetPauseImageName(),
  48. 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. framework.ImageWhiteList.Insert(getSRIOVDevicePluginImage())
  63. }
  64. func getNodeProblemDetectorImage() string {
  65. const defaultImage string = "k8s.gcr.io/node-problem-detector:v0.6.2"
  66. image := os.Getenv("NODE_PROBLEM_DETECTOR_IMAGE")
  67. if image == "" {
  68. image = defaultImage
  69. }
  70. return image
  71. }
  72. // puller represents a generic image puller
  73. type puller interface {
  74. // Pull pulls an image by name
  75. Pull(image string) ([]byte, error)
  76. // Name returns the name of the specific puller implementation
  77. Name() string
  78. }
  79. type dockerPuller struct {
  80. }
  81. func (dp *dockerPuller) Name() string {
  82. return "docker"
  83. }
  84. func (dp *dockerPuller) Pull(image string) ([]byte, error) {
  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. // PrePullAllImages pre-fetches 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. }
  151. // getGPUDevicePluginImage returns the image of GPU device plugin.
  152. func getGPUDevicePluginImage() string {
  153. ds, err := framework.DsFromManifest(gpu.GPUDevicePluginDSYAML)
  154. if err != nil {
  155. klog.Errorf("Failed to parse the device plugin image: %v", err)
  156. return ""
  157. }
  158. if ds == nil {
  159. klog.Errorf("Failed to parse the device plugin image: the extracted DaemonSet is nil")
  160. return ""
  161. }
  162. if len(ds.Spec.Template.Spec.Containers) < 1 {
  163. klog.Errorf("Failed to parse the device plugin image: cannot extract the container from YAML")
  164. return ""
  165. }
  166. return ds.Spec.Template.Spec.Containers[0].Image
  167. }
  168. // getSRIOVDevicePluginImage returns the image of SRIOV device plugin.
  169. func getSRIOVDevicePluginImage() string {
  170. data, err := testfiles.Read(SRIOVDevicePluginDSYAML)
  171. if err != nil {
  172. klog.Errorf("Failed to read the device plugin manifest: %v", err)
  173. return ""
  174. }
  175. ds, err := framework.DsFromData(data)
  176. if err != nil {
  177. klog.Errorf("Failed to parse the device plugin image: %v", err)
  178. return ""
  179. }
  180. if ds == nil {
  181. klog.Errorf("Failed to parse the device plugin image: the extracted DaemonSet is nil")
  182. return ""
  183. }
  184. if len(ds.Spec.Template.Spec.Containers) < 1 {
  185. klog.Errorf("Failed to parse the device plugin image: cannot extract the container from YAML")
  186. return ""
  187. }
  188. return ds.Spec.Template.Spec.Containers[0].Image
  189. }