2016-05-20 03:04:58 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-05-20 03:04:58 +00:00
|
|
|
|
|
|
|
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 e2e_node
|
|
|
|
|
|
|
|
import (
|
2017-02-01 13:46:47 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-05-20 03:04:58 +00:00
|
|
|
"os/exec"
|
2016-08-04 22:04:01 +00:00
|
|
|
"os/user"
|
2016-05-25 23:37:04 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2016-05-26 16:16:43 +00:00
|
|
|
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2017-02-23 00:05:05 +00:00
|
|
|
internalapi "k8s.io/kubernetes/pkg/kubelet/apis/cri"
|
2017-06-07 08:54:28 +00:00
|
|
|
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
2017-02-01 13:46:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/remote"
|
2016-09-15 21:49:14 +00:00
|
|
|
commontest "k8s.io/kubernetes/test/e2e/common"
|
2016-05-26 16:16:43 +00:00
|
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
2017-08-29 08:32:08 +00:00
|
|
|
imageutils "k8s.io/kubernetes/test/utils/image"
|
2016-05-20 03:04:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-05-25 23:37:04 +00:00
|
|
|
// Number of attempts to pull an image.
|
|
|
|
maxImagePullRetries = 5
|
|
|
|
// Sleep duration between image pull retry attempts.
|
|
|
|
imagePullRetryDelay = time.Second
|
2017-02-01 13:46:47 +00:00
|
|
|
// connection timeout for gRPC image service connection
|
|
|
|
imageServiceConnectionTimeout = 15 * time.Minute
|
2016-05-20 03:04:58 +00:00
|
|
|
)
|
|
|
|
|
2016-09-15 21:49:14 +00:00
|
|
|
// NodeImageWhiteList is a list of images used in node e2e test. These images will be prepulled
|
|
|
|
// before test running so that the image pulling won't fail in actual test.
|
|
|
|
var NodeImageWhiteList = sets.NewString(
|
|
|
|
"google/cadvisor:latest",
|
|
|
|
"gcr.io/google-containers/stress:v1",
|
2017-08-29 08:32:08 +00:00
|
|
|
busyboxImage,
|
2016-09-30 18:51:26 +00:00
|
|
|
"gcr.io/google_containers/busybox@sha256:4bdd623e848417d96127e16037743f0cd8b528c026e9175e22a84f639eca58ff",
|
2017-06-22 00:01:25 +00:00
|
|
|
"gcr.io/google_containers/node-problem-detector:v0.4.1",
|
2017-08-29 08:32:08 +00:00
|
|
|
imageutils.GetE2EImage(imageutils.NginxSlim),
|
|
|
|
imageutils.GetE2EImage(imageutils.ServeHostname),
|
|
|
|
imageutils.GetE2EImage(imageutils.Netexec),
|
2017-06-06 06:11:29 +00:00
|
|
|
"gcr.io/google_containers/nonewprivs:1.2",
|
2016-09-15 21:49:14 +00:00
|
|
|
framework.GetPauseImageNameForHostArch(),
|
|
|
|
)
|
2016-05-20 03:04:58 +00:00
|
|
|
|
2016-09-15 21:49:14 +00:00
|
|
|
func init() {
|
|
|
|
// Union NodeImageWhiteList and CommonImageWhiteList into the framework image white list.
|
|
|
|
framework.ImageWhiteList = NodeImageWhiteList.Union(commontest.CommonImageWhiteList)
|
2016-05-20 03:04:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 13:46:47 +00:00
|
|
|
// puller represents a generic image puller
|
|
|
|
type puller interface {
|
|
|
|
// Pull pulls an image by name
|
|
|
|
Pull(image string) ([]byte, error)
|
|
|
|
// Name returns the name of the specific puller implementation
|
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type dockerPuller struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dp *dockerPuller) Name() string {
|
|
|
|
return "docker"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dp *dockerPuller) Pull(image string) ([]byte, error) {
|
|
|
|
// TODO(random-liu): Use docker client to get rid of docker binary dependency.
|
|
|
|
return exec.Command("docker", "pull", image).CombinedOutput()
|
|
|
|
}
|
|
|
|
|
|
|
|
type remotePuller struct {
|
2017-02-23 00:05:05 +00:00
|
|
|
imageService internalapi.ImageManagerService
|
2017-02-01 13:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *remotePuller) Name() string {
|
|
|
|
return "CRI"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *remotePuller) Pull(image string) ([]byte, error) {
|
|
|
|
// TODO(runcom): should we check if the image is already pulled with ImageStatus?
|
2017-02-23 00:05:05 +00:00
|
|
|
_, err := rp.imageService.PullImage(&runtimeapi.ImageSpec{Image: image}, nil)
|
2017-02-01 13:46:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPuller() (puller, error) {
|
|
|
|
runtime := framework.TestContext.ContainerRuntime
|
|
|
|
switch runtime {
|
|
|
|
case "docker":
|
|
|
|
return &dockerPuller{}, nil
|
|
|
|
case "remote":
|
|
|
|
endpoint := framework.TestContext.ContainerRuntimeEndpoint
|
|
|
|
if framework.TestContext.ImageServiceEndpoint != "" {
|
|
|
|
//ImageServiceEndpoint is the same as ContainerRuntimeEndpoint if not
|
|
|
|
//explicitly specified
|
|
|
|
//https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/kubelet.go#L517
|
|
|
|
endpoint = framework.TestContext.ImageServiceEndpoint
|
|
|
|
}
|
|
|
|
if endpoint == "" {
|
|
|
|
return nil, errors.New("can't prepull images, no remote endpoint provided")
|
|
|
|
}
|
|
|
|
is, err := remote.NewRemoteImageService(endpoint, imageServiceConnectionTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &remotePuller{
|
|
|
|
imageService: is,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("can't prepull images, unknown container runtime %q", runtime)
|
|
|
|
}
|
|
|
|
|
2016-09-15 21:49:14 +00:00
|
|
|
// Pre-fetch all images tests depend on so that we don't fail in an actual test.
|
2016-05-20 03:04:58 +00:00
|
|
|
func PrePullAllImages() error {
|
2017-02-01 13:46:47 +00:00
|
|
|
puller, err := getPuller()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-04 22:04:01 +00:00
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-15 21:49:14 +00:00
|
|
|
images := framework.ImageWhiteList.List()
|
2017-02-01 13:46:47 +00:00
|
|
|
glog.V(4).Infof("Pre-pulling images with %s %+v", puller.Name(), images)
|
2016-09-15 21:49:14 +00:00
|
|
|
for _, image := range images {
|
2016-05-25 23:37:04 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
output []byte
|
|
|
|
)
|
2016-06-03 23:37:58 +00:00
|
|
|
for i := 0; i < maxImagePullRetries; i++ {
|
|
|
|
if i > 0 {
|
|
|
|
time.Sleep(imagePullRetryDelay)
|
|
|
|
}
|
2017-02-01 13:46:47 +00:00
|
|
|
if output, err = puller.Pull(image); err == nil {
|
2016-05-25 23:37:04 +00:00
|
|
|
break
|
|
|
|
}
|
2016-08-04 22:04:01 +00:00
|
|
|
glog.Warningf("Failed to pull %s as user %q, retrying in %s (%d of %d): %v",
|
|
|
|
image, usr.Username, imagePullRetryDelay.String(), i+1, maxImagePullRetries, err)
|
2016-05-25 23:37:04 +00:00
|
|
|
}
|
2016-05-20 03:04:58 +00:00
|
|
|
if err != nil {
|
2017-02-01 13:46:47 +00:00
|
|
|
glog.Warningf("Could not pre-pull image %s %v output: %s", image, err, output)
|
2016-05-20 03:04:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|