Skip to content

Commit

Permalink
kubelet: Introduce PodInfraContainerChanged().
Browse files Browse the repository at this point in the history
This functions computes in ahead whether we need to restart the pod
infra container.
  • Loading branch information
Yifan Gu committed Apr 9, 2015
1 parent 43949b4 commit 4d28d87
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 20 deletions.
5 changes: 3 additions & 2 deletions pkg/kubelet/dockertools/docker.go
Expand Up @@ -40,8 +40,9 @@ import (
)

const (
PodInfraContainerName = leaky.PodInfraContainerName
DockerPrefix = "docker://"
PodInfraContainerName = leaky.PodInfraContainerName
DockerPrefix = "docker://"
PodInfraContainerImage = "gcr.io/google_containers/pause:0.8.0"
)

const (
Expand Down
37 changes: 37 additions & 0 deletions pkg/kubelet/dockertools/manager.go
Expand Up @@ -452,3 +452,40 @@ func makeCapabilites(capAdd []api.CapabilityType, capDrop []api.CapabilityType)
}
return addCaps, dropCaps
}

// PodInfraContainer returns true if the pod infra container has changed.
func (self *DockerManager) PodInfraContainerChanged(pod *api.Pod, podInfraContainer *kubecontainer.Container) (bool, error) {
// Use host networking if specified and allowed.
networkMode := ""
var ports []api.ContainerPort

dockerPodInfraContainer, err := self.client.InspectContainer(string(podInfraContainer.ID))
if err != nil {
return false, err
}

// Check network mode.
if dockerPodInfraContainer.HostConfig != nil {
networkMode = dockerPodInfraContainer.HostConfig.NetworkMode
}
if pod.Spec.HostNetwork && networkMode != "host" {
return true, nil
} else {
// Docker only exports ports from the pod infra container. Let's
// collect all of the relevant ports and export them.
for _, container := range pod.Spec.Containers {
ports = append(ports, container.Ports...)
}
}
container := &api.Container{
Name: PodInfraContainerName,
Image: PodInfraContainerImage,
Ports: ports,
}

expectedHash := HashContainer(container)
if podInfraContainer.Hash != expectedHash {
return true, nil
}
return false, nil
}
28 changes: 10 additions & 18 deletions pkg/kubelet/kubelet.go
Expand Up @@ -854,10 +854,6 @@ func (kl *Kubelet) killContainerByID(ID string) error {
return err
}

const (
PodInfraContainerImage = "gcr.io/google_containers/pause:0.8.0"
)

// Determined whether the specified pod is allowed to use host networking
func allowHostNetwork(pod *api.Pod) (bool, error) {
podSource, err := getPodSource(pod)
Expand Down Expand Up @@ -1116,8 +1112,15 @@ func (kl *Kubelet) computePodContainerChanges(pod *api.Pod, runningPod kubeconta
podInfraContainer := runningPod.FindContainerByName(dockertools.PodInfraContainerName)
if podInfraContainer != nil {
glog.V(4).Infof("Found infra pod for %q", podFullName)
podInfraContainerID = dockertools.DockerID(podInfraContainer.ID)
containersToKeep[podInfraContainerID] = -1
changed, err := kl.containerManager.PodInfraContainerChanged(pod, podInfraContainer)
if err != nil {
return podContainerChangesSpec{}, err
}
if !changed {
glog.V(4).Infof("Infra pod is good, keeps it %q", podFullName)
podInfraContainerID = dockertools.DockerID(podInfraContainer.ID)
containersToKeep[podInfraContainerID] = -1
}

} else {
glog.V(2).Infof("No Infra Container for %q found. All containers will be restarted.", podFullName)
Expand Down Expand Up @@ -1163,21 +1166,10 @@ func (kl *Kubelet) computePodContainerChanges(pod *api.Pod, runningPod kubeconta
continue
}
glog.Infof("pod %q container %q is unhealthy (probe result: %v). Container will be killed and re-created.", podFullName, container.Name, result)
containersToStart[index] = empty{}
} else {
glog.Infof("pod %q container %q hash changed (%d vs %d). Pod will be killed and re-created.", podFullName, container.Name, hash, expectedHash)
createPodInfraContainer = true
delete(containersToKeep, podInfraContainerID)
// If we are to restart Infra Container then we move containersToKeep into containersToStart
// if RestartPolicy allows restarting failed containers.
if pod.Spec.RestartPolicy != api.RestartPolicyNever {
for _, v := range containersToKeep {
containersToStart[v] = empty{}
}
}
containersToStart[index] = empty{}
containersToKeep = make(map[dockertools.DockerID]int)
}
containersToStart[index] = empty{}
} else { // createPodInfraContainer == true and Container exists
// If we're creating infra containere everything will be killed anyway
// If RestartPolicy is Always or OnFailure we restart containers that were running before we
Expand Down
12 changes: 12 additions & 0 deletions pkg/kubelet/kubelet_test.go
Expand Up @@ -455,6 +455,18 @@ func TestSyncPodsDoesNothing(t *testing.T) {
ID: "9876",
},
}
fakeDocker.ContainerMap = map[string]*docker.Container{
"1234": {
ID: "1234",
HostConfig: &docker.HostConfig{},
Config: &docker.Config{},
},
"9876": {
ID: "9876",
HostConfig: &docker.HostConfig{},
Config: &docker.Config{},
},
}
pods := []api.Pod{
{
ObjectMeta: api.ObjectMeta{
Expand Down

0 comments on commit 4d28d87

Please sign in to comment.