Skip to content

Commit

Permalink
Check if kubelet does know such Pod before querying status.
Browse files Browse the repository at this point in the history
  • Loading branch information
dchen1107 committed Feb 11, 2015
1 parent 2c2a595 commit f5f2b6f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 33 deletions.
1 change: 1 addition & 0 deletions cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func podsOnMinions(c *client.Client, pods api.PodList) wait.ConditionFunc {
return false, nil
}
if _, err := podInfo.GetPodStatus(host, namespace, id); err != nil {
glog.Infof("GetPodStatus error: %v", err)
return false, nil
}
}
Expand Down
18 changes: 14 additions & 4 deletions pkg/kubelet/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ func (kl *Kubelet) syncPod(pod *api.BoundPod, dockerContainers dockertools.Docke

podStatus, err := kl.GetPodStatus(podFullName, uid)
if err != nil {
glog.Errorf("Unable to get pod with name %q and uid %q info, health checks may be invalid", podFullName, uid)
glog.Errorf("Unable to get pod with name %q and uid %q info with error(%v)", podFullName, uid, err)
}

for _, container := range pod.Spec.Containers {
Expand Down Expand Up @@ -1539,24 +1539,34 @@ func getPodReadyCondition(spec *api.PodSpec, info api.PodInfo) []api.PodConditio
// GetPodStatus returns information from Docker about the containers in a pod
func (kl *Kubelet) GetPodStatus(podFullName string, uid types.UID) (api.PodStatus, error) {
var spec api.PodSpec
var podStatus api.PodStatus
found := false
for _, pod := range kl.pods {
if GetPodFullName(&pod) == podFullName {
spec = pod.Spec
found = true
break
}
}
if !found {
return podStatus, fmt.Errorf("Couldn't find spec for pod %s", podFullName)
}

info, err := dockertools.GetDockerPodInfo(kl.dockerClient, spec, podFullName, uid)

if err != nil {
glog.Infof("Query docker container info failed with error: %v", err)
return podStatus, err
}

podStatus.Phase = getPhase(&spec, info)
for _, c := range spec.Containers {
containerStatus := info[c.Name]
containerStatus.Ready = kl.readiness.IsReady(containerStatus)
info[c.Name] = containerStatus
}

var podStatus api.PodStatus
podStatus.Phase = getPhase(&spec, info)
podStatus.Conditions = append(podStatus.Conditions, getPodReadyCondition(&spec, info)...)

netContainerInfo, found := info[dockertools.PodInfraContainerName]
if found {
podStatus.PodIP = netContainerInfo.PodIP
Expand Down
72 changes: 43 additions & 29 deletions pkg/kubelet/kubelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func TestSyncPodsDoesNothing(t *testing.T) {
ID: "9876",
},
}
err := kubelet.SyncPods([]api.BoundPod{
kubelet.pods = []api.BoundPod{
{
ObjectMeta: api.ObjectMeta{
UID: "12345678",
Expand All @@ -379,7 +379,8 @@ func TestSyncPodsDoesNothing(t *testing.T) {
},
},
},
})
}
err := kubelet.SyncPods(kubelet.pods)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand All @@ -394,7 +395,7 @@ func TestSyncPodsWithTerminationLog(t *testing.T) {
TerminationMessagePath: "/dev/somepath",
}
fakeDocker.ContainerList = []docker.APIContainers{}
err := kubelet.SyncPods([]api.BoundPod{
kubelet.pods = []api.BoundPod{
{
ObjectMeta: api.ObjectMeta{
UID: "12345678",
Expand All @@ -408,13 +409,14 @@ func TestSyncPodsWithTerminationLog(t *testing.T) {
},
},
},
})
}
err := kubelet.SyncPods(kubelet.pods)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
kubelet.drainWorkers()
verifyCalls(t, fakeDocker, []string{
"list", "create", "start", "list", "inspect_container", "list", "create", "start"})
"list", "create", "start", "list", "inspect_container", "inspect_image", "list", "create", "start"})

fakeDocker.Lock()
parts := strings.Split(fakeDocker.Container.HostConfig.Binds[0], ":")
Expand Down Expand Up @@ -452,7 +454,7 @@ func TestSyncPodsCreatesNetAndContainer(t *testing.T) {
kubelet, fakeDocker := newTestKubelet(t)
kubelet.podInfraContainerImage = "custom_image_name"
fakeDocker.ContainerList = []docker.APIContainers{}
err := kubelet.SyncPods([]api.BoundPod{
kubelet.pods = []api.BoundPod{
{
ObjectMeta: api.ObjectMeta{
UID: "12345678",
Expand All @@ -466,14 +468,15 @@ func TestSyncPodsCreatesNetAndContainer(t *testing.T) {
},
},
},
})
}
err := kubelet.SyncPods(kubelet.pods)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
kubelet.drainWorkers()

verifyCalls(t, fakeDocker, []string{
"list", "create", "start", "list", "inspect_container", "list", "create", "start"})
"list", "create", "start", "list", "inspect_container", "inspect_image", "list", "create", "start"})

fakeDocker.Lock()

Expand Down Expand Up @@ -501,7 +504,7 @@ func TestSyncPodsCreatesNetAndContainerPullsImage(t *testing.T) {
puller.HasImages = []string{}
kubelet.podInfraContainerImage = "custom_image_name"
fakeDocker.ContainerList = []docker.APIContainers{}
err := kubelet.SyncPods([]api.BoundPod{
kubelet.pods = []api.BoundPod{
{
ObjectMeta: api.ObjectMeta{
UID: "12345678",
Expand All @@ -515,14 +518,15 @@ func TestSyncPodsCreatesNetAndContainerPullsImage(t *testing.T) {
},
},
},
})
}
err := kubelet.SyncPods(kubelet.pods)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
kubelet.drainWorkers()

verifyCalls(t, fakeDocker, []string{
"list", "create", "start", "list", "inspect_container", "list", "create", "start"})
"list", "create", "start", "list", "inspect_container", "inspect_image", "list", "create", "start"})

fakeDocker.Lock()

Expand All @@ -547,7 +551,7 @@ func TestSyncPodsWithNetCreatesContainer(t *testing.T) {
ID: "9876",
},
}
err := kubelet.SyncPods([]api.BoundPod{
kubelet.pods = []api.BoundPod{
{
ObjectMeta: api.ObjectMeta{
UID: "12345678",
Expand All @@ -561,14 +565,15 @@ func TestSyncPodsWithNetCreatesContainer(t *testing.T) {
},
},
},
})
}
err := kubelet.SyncPods(kubelet.pods)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
kubelet.drainWorkers()

verifyCalls(t, fakeDocker, []string{
"list", "list", "inspect_container", "list", "create", "start"})
"list", "list", "inspect_container", "inspect_image", "list", "create", "start"})

fakeDocker.Lock()
if len(fakeDocker.Created) != 1 ||
Expand All @@ -589,7 +594,7 @@ func TestSyncPodsWithNetCreatesContainerCallsHandler(t *testing.T) {
ID: "9876",
},
}
err := kubelet.SyncPods([]api.BoundPod{
kubelet.pods = []api.BoundPod{
{
ObjectMeta: api.ObjectMeta{
UID: "12345678",
Expand All @@ -614,14 +619,15 @@ func TestSyncPodsWithNetCreatesContainerCallsHandler(t *testing.T) {
},
},
},
})
}
err := kubelet.SyncPods(kubelet.pods)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
kubelet.drainWorkers()

verifyCalls(t, fakeDocker, []string{
"list", "list", "inspect_container", "list", "create", "start"})
"list", "list", "inspect_container", "inspect_image", "list", "create", "start"})

fakeDocker.Lock()
if len(fakeDocker.Created) != 1 ||
Expand All @@ -643,7 +649,7 @@ func TestSyncPodsDeletesWithNoNetContainer(t *testing.T) {
ID: "1234",
},
}
err := kubelet.SyncPods([]api.BoundPod{
kubelet.pods = []api.BoundPod{
{
ObjectMeta: api.ObjectMeta{
UID: "12345678",
Expand All @@ -657,14 +663,15 @@ func TestSyncPodsDeletesWithNoNetContainer(t *testing.T) {
},
},
},
})
}
err := kubelet.SyncPods(kubelet.pods)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
kubelet.drainWorkers()

verifyCalls(t, fakeDocker, []string{
"list", "stop", "create", "start", "list", "list", "inspect_container", "list", "create", "start"})
"list", "stop", "create", "start", "list", "list", "inspect_container", "inspect_image", "list", "create", "start"})

// A map iteration is used to delete containers, so must not depend on
// order here.
Expand Down Expand Up @@ -844,7 +851,7 @@ func TestSyncPodDeletesDuplicate(t *testing.T) {
ID: "2304",
},
}
err := kubelet.syncPod(&api.BoundPod{
bound := api.BoundPod{
ObjectMeta: api.ObjectMeta{
UID: "12345678",
Name: "bar",
Expand All @@ -856,13 +863,14 @@ func TestSyncPodDeletesDuplicate(t *testing.T) {
{Name: "foo"},
},
},
}, dockerContainers)
}
kubelet.pods = append(kubelet.pods, bound)
err := kubelet.syncPod(&bound, dockerContainers)
if err != nil {
t.Errorf("unexpected error: %v", err)
}

verifyCalls(t, fakeDocker, []string{"list", "stop"})

// Expect one of the duplicates to be killed.
if len(fakeDocker.Stopped) != 1 || (fakeDocker.Stopped[0] != "1234" && fakeDocker.Stopped[0] != "4567") {
t.Errorf("Wrong containers were stopped: %v", fakeDocker.Stopped)
Expand All @@ -883,7 +891,7 @@ func TestSyncPodBadHash(t *testing.T) {
ID: "9876",
},
}
err := kubelet.syncPod(&api.BoundPod{
bound := api.BoundPod{
ObjectMeta: api.ObjectMeta{
UID: "12345678",
Name: "foo",
Expand All @@ -895,7 +903,9 @@ func TestSyncPodBadHash(t *testing.T) {
{Name: "bar"},
},
},
}, dockerContainers)
}
kubelet.pods = append(kubelet.pods, bound)
err := kubelet.syncPod(&bound, dockerContainers)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -929,7 +939,7 @@ func TestSyncPodUnhealthy(t *testing.T) {
ID: "9876",
},
}
err := kubelet.syncPod(&api.BoundPod{
bound := api.BoundPod{
ObjectMeta: api.ObjectMeta{
UID: "12345678",
Name: "foo",
Expand All @@ -945,7 +955,9 @@ func TestSyncPodUnhealthy(t *testing.T) {
},
},
},
}, dockerContainers)
}
kubelet.pods = append(kubelet.pods, bound)
err := kubelet.syncPod(&bound, dockerContainers)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -1580,7 +1592,7 @@ func TestSyncPodEventHandlerFails(t *testing.T) {
ID: "9876",
},
}
err := kubelet.syncPod(&api.BoundPod{
bound := api.BoundPod{
ObjectMeta: api.ObjectMeta{
UID: "12345678",
Name: "foo",
Expand All @@ -1602,7 +1614,9 @@ func TestSyncPodEventHandlerFails(t *testing.T) {
},
},
},
}, dockerContainers)
}
kubelet.pods = append(kubelet.pods, bound)
err := kubelet.syncPod(&bound, dockerContainers)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand Down

0 comments on commit f5f2b6f

Please sign in to comment.