kubelet/dockertools: Refactor image pulling for pod infra container.

Replace the trunk of pull image code with dockerManagner.pullImage().
Also add tests to verify the image pulling/pulled events.
This commit is contained in:
Yifan Gu
2015-06-08 17:53:24 -07:00
parent eb0fb43453
commit 053db8dba7
4 changed files with 77 additions and 52 deletions

View File

@@ -69,6 +69,9 @@ const (
// DockerManager implements the Runtime interface.
var _ kubecontainer.Runtime = &DockerManager{}
// TODO: make this a TTL based pull (if image older than X policy, pull)
var podInfraContainerImagePullPolicy = api.PullIfNotPresent
type DockerManager struct {
client DockerInterface
recorder record.EventRecorder
@@ -832,9 +835,10 @@ func (dm *DockerManager) podInfraContainerChanged(pod *api.Pod, podInfraContaine
}
}
expectedPodInfraContainer := &api.Container{
Name: PodInfraContainerName,
Image: dm.podInfraContainerImage,
Ports: ports,
Name: PodInfraContainerName,
Image: dm.podInfraContainerImage,
Ports: ports,
ImagePullPolicy: podInfraContainerImagePullPolicy,
}
return podInfraContainer.Hash != kubecontainer.HashContainer(expectedPodInfraContainer), nil
}
@@ -1313,38 +1317,16 @@ func (dm *DockerManager) createPodInfraContainer(pod *api.Pod) (kubeletTypes.Doc
}
container := &api.Container{
Name: PodInfraContainerName,
Image: dm.podInfraContainerImage,
Ports: ports,
Name: PodInfraContainerName,
Image: dm.podInfraContainerImage,
Ports: ports,
ImagePullPolicy: podInfraContainerImagePullPolicy,
}
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)
}
spec := kubecontainer.ImageSpec{container.Image}
// TODO: make this a TTL based pull (if image older than X policy, pull)
ok, err := dm.IsImagePresent(spec)
if err != nil {
if ref != nil {
dm.recorder.Eventf(ref, "failed", "Failed to inspect image %q: %v", container.Image, err)
}
// No pod secrets for the infra container.
if err := dm.pullImage(pod, container, nil); err != nil {
return "", err
}
if ok {
if ref != nil {
dm.recorder.Eventf(ref, "pulled", "Pod container image %q already present on machine", container.Image)
}
} else {
dm.runtimeHooks.ReportImagePulling(pod, container)
err := dm.PullImage(spec, nil /* no pod secrets for the infra container */)
dm.runtimeHooks.ReportImagePulled(pod, container, err)
if err != nil {
return "", err
}
if ref != nil {
dm.recorder.Eventf(ref, "pulled", "Successfully pulled Pod container image %q", container.Image)
}
}
id, err := dm.runContainerInPod(pod, container, netNamespace, "")
if err != nil {
@@ -1501,20 +1483,22 @@ func (dm *DockerManager) clearReasonCache(pod *api.Pod, container *api.Container
// Pull the image for the specified pod and container.
func (dm *DockerManager) pullImage(pod *api.Pod, container *api.Container, pullSecrets []api.Secret) error {
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)
}
spec := kubecontainer.ImageSpec{container.Image}
present, err := dm.IsImagePresent(spec)
if 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)
}
if ref != nil {
dm.recorder.Eventf(ref, "failed", "Failed to inspect image %q: %v", container.Image, err)
}
return fmt.Errorf("failed to inspect image %q: %v", container.Image, err)
}
if !dm.runtimeHooks.ShouldPullImage(pod, container, present) {
if present && ref != nil {
dm.recorder.Eventf(ref, "pulled", "Container image %q already present on machine", container.Image)
}
return nil
}