Merge pull request #1147 from dchen1107/restart

Introduce the simplest RestartPolicy and handling.
This commit is contained in:
Tim Hockin
2014-09-09 14:44:42 -07:00
9 changed files with 171 additions and 54 deletions

View File

@@ -52,7 +52,8 @@ func CreateValidPod(name, namespace string) kubelet.Pod {
Name: name,
Namespace: namespace,
Manifest: api.ContainerManifest{
Version: "v1beta1",
Version: "v1beta1",
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{}},
},
}
}

View File

@@ -105,8 +105,12 @@ func TestExtractFromHTTP(t *testing.T) {
manifests: api.ContainerManifest{Version: "v1beta1", ID: "foo"},
expected: CreatePodUpdate(kubelet.SET,
kubelet.Pod{
Name: "foo",
Manifest: api.ContainerManifest{Version: "v1beta1", ID: "foo"},
Name: "foo",
Manifest: api.ContainerManifest{
Version: "v1beta1",
ID: "foo",
RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{}},
},
}),
},
{

View File

@@ -153,6 +153,33 @@ func getKubeletDockerContainers(client DockerInterface) (DockerContainers, error
return result, nil
}
// getRecentDockerContainersWithName returns a list of dead docker containers which matches the name
// and uuid given.
func getRecentDockerContainersWithNameAndUUID(client DockerInterface, podFullName, uuid, containerName string) ([]*docker.Container, error) {
var result []*docker.Container
containers, err := client.ListContainers(docker.ListContainersOptions{All: true})
if err != nil {
return nil, err
}
for _, dockerContainer := range containers {
dockerPodName, dockerUUID, dockerContainerName, _ := parseDockerName(dockerContainer.Names[0])
if dockerPodName != podFullName {
continue
}
if uuid != "" && dockerUUID != uuid {
continue
}
if dockerContainerName != containerName {
continue
}
inspectResult, _ := client.InspectContainer(dockerContainer.ID)
if inspectResult != nil && !inspectResult.State.Running && !inspectResult.State.Paused {
result = append(result, inspectResult)
}
}
return result, nil
}
// ErrNoContainersInPod is returned when there are no running containers for a given pod
var ErrNoContainersInPod = errors.New("no containers exist for this pod")

View File

@@ -516,11 +516,35 @@ func (kl *Kubelet) syncPod(pod *Pod, dockerContainers DockerContainers) error {
killedContainers[containerID] = empty{}
}
glog.Infof("Container doesn't exist, creating %#v", container)
// Check RestartPolicy for container
recentContainers, err := getRecentDockerContainersWithNameAndUUID(kl.dockerClient, podFullName, uuid, container.Name)
if err != nil {
glog.Errorf("Error listing recent containers with name and uuid:%s--%s--%s", podFullName, uuid, container.Name)
// TODO(dawnchen): error handling here?
}
if len(recentContainers) > 0 && pod.Manifest.RestartPolicy.Always == nil {
if pod.Manifest.RestartPolicy.Never != nil {
glog.Infof("Already ran container with name %s--%s--%s, do nothing",
podFullName, uuid, container.Name)
continue
}
if pod.Manifest.RestartPolicy.OnFailure != nil {
// Check the exit code of last run
if recentContainers[0].State.ExitCode == 0 {
glog.Infof("Already successfully ran container with name %s--%s--%s, do nothing",
podFullName, uuid, container.Name)
continue
}
}
}
glog.Infof("Container with name %s--%s--%s doesn't exist, creating %#v", podFullName, uuid, container.Name, container)
if err := kl.dockerPuller.Pull(container.Image); err != nil {
glog.Errorf("Failed to pull image %s: %v skipping pod %s container %s.", container.Image, err, podFullName, container.Name)
continue
}
// TODO(dawnchen): Check RestartPolicy.DelaySeconds before restart a container
containerID, err := kl.runContainer(pod, &container, podVolumes, "container:"+string(netID))
if err != nil {
// TODO(bburns) : Perhaps blacklist a container after N failures?

View File

@@ -300,7 +300,7 @@ func TestSyncPodsCreatesNetAndContainer(t *testing.T) {
kubelet.drainWorkers()
verifyCalls(t, fakeDocker, []string{
"list", "list", "create", "start", "list", "inspect", "create", "start"})
"list", "list", "create", "start", "list", "inspect", "list", "create", "start"})
fakeDocker.lock.Lock()
if len(fakeDocker.Created) != 2 ||
@@ -338,7 +338,7 @@ func TestSyncPodsWithNetCreatesContainer(t *testing.T) {
kubelet.drainWorkers()
verifyCalls(t, fakeDocker, []string{
"list", "list", "list", "inspect", "create", "start"})
"list", "list", "list", "inspect", "list", "create", "start"})
fakeDocker.lock.Lock()
if len(fakeDocker.Created) != 1 ||
@@ -388,7 +388,7 @@ func TestSyncPodsWithNetCreatesContainerCallsHandler(t *testing.T) {
kubelet.drainWorkers()
verifyCalls(t, fakeDocker, []string{
"list", "list", "list", "inspect", "create", "start"})
"list", "list", "list", "inspect", "list", "create", "start"})
fakeDocker.lock.Lock()
if len(fakeDocker.Created) != 1 ||
@@ -428,7 +428,7 @@ func TestSyncPodsDeletesWithNoNetContainer(t *testing.T) {
kubelet.drainWorkers()
verifyCalls(t, fakeDocker, []string{
"list", "list", "stop", "create", "start", "list", "list", "inspect", "create", "start"})
"list", "list", "stop", "create", "start", "list", "list", "inspect", "list", "create", "start"})
// A map iteration is used to delete containers, so must not depend on
// order here.
@@ -561,7 +561,7 @@ func TestSyncPodBadHash(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}
verifyCalls(t, fakeDocker, []string{"list", "stop", "create", "start"})
verifyCalls(t, fakeDocker, []string{"list", "stop", "list", "create", "start"})
// A map interation is used to delete containers, so must not depend on
// order here.
@@ -608,7 +608,7 @@ func TestSyncPodUnhealthy(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}
verifyCalls(t, fakeDocker, []string{"list", "stop", "create", "start"})
verifyCalls(t, fakeDocker, []string{"list", "stop", "list", "create", "start"})
// A map interation is used to delete containers, so must not depend on
// order here.
@@ -1329,7 +1329,7 @@ func TestSyncPodEventHandlerFails(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}
verifyCalls(t, fakeDocker, []string{"list", "create", "start", "stop"})
verifyCalls(t, fakeDocker, []string{"list", "list", "create", "start", "stop"})
if len(fakeDocker.stopped) != 1 {
t.Errorf("Wrong containers were stopped: %v", fakeDocker.stopped)