From b232eef5f76b4ee3291c7a0e03370c9a26584f8c Mon Sep 17 00:00:00 2001 From: Victor Marmol Date: Mon, 27 Apr 2015 13:48:25 -0700 Subject: [PATCH] Move image PullPolicy logic to pullImage(). This will allow us to remove the Docker-specific logic in pullImageAndRunContainer() and re-use pullImage() in other runtimes. --- pkg/kubelet/kubelet.go | 56 ++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 7754c62b449..28255cd396e 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -847,21 +847,43 @@ func parseResolvConf(reader io.Reader) (nameservers []string, searches []string, return nameservers, searches, nil } -func (kl *Kubelet) pullImage(img string, ref *api.ObjectReference) error { +// Pull the image for the specified pod and container. +func (kl *Kubelet) pullImage(pod *api.Pod, container *api.Container) error { + if container.ImagePullPolicy == api.PullNever { + return nil + } + start := time.Now() defer func() { metrics.ImagePullLatency.Observe(metrics.SinceInMicroseconds(start)) }() - if err := kl.containerManager.Pull(img); err != nil { + ref, err := kubecontainer.GenerateContainerRef(pod, container) + if err != nil { + glog.Errorf("Couldn't make a ref to pod %v, container %v: '%v'", pod.Name, container.Name, err) + } + present, err := kl.containerManager.IsImagePresent(container.Image) + if err != nil { if ref != nil { - kl.recorder.Eventf(ref, "failed", "Failed to pull image %q: %v", img, err) + kl.recorder.Eventf(ref, "failed", "Failed to inspect image %q: %v", container.Image, err) } + glog.Errorf("Failed to inspect image %q: %v; skipping pod %q container %q", container.Image, err, kubecontainer.GetPodFullName(pod), container.Name) return err } - if ref != nil { - kl.recorder.Eventf(ref, "pulled", "Successfully pulled image %q", img) + + if container.ImagePullPolicy == api.PullAlways || + (container.ImagePullPolicy == api.PullIfNotPresent && (!present)) { + if err := kl.containerManager.Pull(container.Image); err != nil { + if ref != nil { + kl.recorder.Eventf(ref, "failed", "Failed to pull image %q: %v", container.Image, err) + } + return err + } + if ref != nil { + kl.recorder.Eventf(ref, "pulled", "Successfully pulled image %q", container.Image) + } } + return nil } @@ -959,33 +981,15 @@ func shouldContainerBeRestarted(container *api.Container, pod *api.Pod, podStatu // Attempts to start a container pulling the image before that if necessary. It returns DockerID of a started container // if it was successful, and a non-nil error otherwise. func (kl *Kubelet) pullImageAndRunContainer(pod *api.Pod, container *api.Container, podInfraContainerID dockertools.DockerID) (dockertools.DockerID, error) { - podFullName := kubecontainer.GetPodFullName(pod) - ref, err := kubecontainer.GenerateContainerRef(pod, container) - if err != nil { - glog.Errorf("Couldn't make a ref to pod %v, container %v: '%v'", pod.Name, container.Name, err) - } - if container.ImagePullPolicy != api.PullNever { - present, err := kl.containerManager.IsImagePresent(container.Image) - if err != nil { - if ref != nil { - kl.recorder.Eventf(ref, "failed", "Failed to inspect image %q: %v", container.Image, err) - } - glog.Errorf("Failed to inspect image %q: %v; skipping pod %q container %q", container.Image, err, podFullName, container.Name) - return "", err - } - if container.ImagePullPolicy == api.PullAlways || - (container.ImagePullPolicy == api.PullIfNotPresent && (!present)) { - if err := kl.pullImage(container.Image, ref); err != nil { - return "", err - } - } + if err := kl.pullImage(pod, container); err != nil { + return "", err } // TODO(dawnchen): Check RestartPolicy.DelaySeconds before restart a container namespaceMode := fmt.Sprintf("container:%v", podInfraContainerID) containerID, err := kl.containerManager.RunContainer(pod, container, kl, kl.handlerRunner, namespaceMode, namespaceMode) if err != nil { // TODO(bburns) : Perhaps blacklist a container after N failures? - glog.Errorf("Error running pod %q container %q: %v", podFullName, container.Name, err) + glog.Errorf("Error running pod %q container %q: %v", kubecontainer.GetPodFullName(pod), container.Name, err) return "", err } return containerID, nil