Skip to content

Commit 180d34e

Browse files
author
k8s-merge-robot
committed
Merge pull request kubernetes#21120 from Random-Liu/minor-probe-manager-refactor
Auto commit by PR queue bot
2 parents 9dacc5b + 77b6f14 commit 180d34e

File tree

9 files changed

+52
-40
lines changed

9 files changed

+52
-40
lines changed

pkg/kubelet/dockertools/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ func TestSyncPodsUnhealthy(t *testing.T) {
867867
ID: infraContainerID,
868868
Name: "/k8s_POD." + strconv.FormatUint(generatePodInfraContainerHash(pod), 16) + "_foo_new_12345678_42",
869869
}})
870-
dm.livenessManager.Set(kubecontainer.DockerID(unhealthyContainerID).ContainerID(), proberesults.Failure, nil)
870+
dm.livenessManager.Set(kubecontainer.DockerID(unhealthyContainerID).ContainerID(), proberesults.Failure, pod)
871871

872872
runSyncPod(t, dm, fakeDocker, pod, nil, false)
873873

pkg/kubelet/kubelet.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,11 +2349,7 @@ func (kl *Kubelet) syncLoopIteration(updates <-chan kubetypes.PodUpdate, handler
23492349
if update.Result == proberesults.Failure {
23502350
// We should not use the pod from livenessManager, because it is never updated after
23512351
// initialization.
2352-
// TODO(random-liu): This is just a quick fix. We should:
2353-
// * Just pass pod UID in probe updates to make this less confusing.
2354-
// * Maybe probe manager should rely on pod manager, or at least the pod in probe manager
2355-
// should be updated.
2356-
pod, ok := kl.podManager.GetPodByUID(update.Pod.UID)
2352+
pod, ok := kl.podManager.GetPodByUID(update.PodUID)
23572353
if !ok {
23582354
// If the pod no longer exists, ignore the update.
23592355
glog.V(4).Infof("SyncLoop (container unhealthy): ignore irrelevant update: %#v", update)

pkg/kubelet/prober/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333

3434
// Manager manages pod probing. It creates a probe "worker" for every container that specifies a
3535
// probe (AddPod). The worker periodically probes its assigned container and caches the results. The
36-
// manager usse the cached probe results to set the appropriate Ready state in the PodStatus when
36+
// manager use the cached probe results to set the appropriate Ready state in the PodStatus when
3737
// requested (UpdatePodStatus). Updating probe parameters is not currently supported.
3838
// TODO: Move liveness probing out of the runtime, to here.
3939
type Manager interface {
@@ -234,5 +234,5 @@ func (m *manager) updateReadiness() {
234234
update := <-m.readinessManager.Updates()
235235

236236
ready := update.Result == results.Success
237-
m.statusManager.SetContainerReadiness(update.Pod, update.ContainerID, ready)
237+
m.statusManager.SetContainerReadiness(update.PodUID, update.ContainerID, ready)
238238
}

pkg/kubelet/prober/manager_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ func TestUpdatePodStatus(t *testing.T) {
280280
}
281281

282282
func TestUpdateReadiness(t *testing.T) {
283-
testPod := getTestPod(readiness, api.Probe{})
283+
testPod := getTestPod()
284+
setTestProbe(testPod, readiness, api.Probe{})
284285
m := newTestManager()
285286
defer cleanup(t, m)
286287

@@ -297,9 +298,9 @@ func TestUpdateReadiness(t *testing.T) {
297298
exec.set(probe.Success, nil)
298299
m.prober.exec = &exec
299300

300-
m.statusManager.SetPodStatus(&testPod, getTestRunningStatus())
301+
m.statusManager.SetPodStatus(testPod, getTestRunningStatus())
301302

302-
m.AddPod(&testPod)
303+
m.AddPod(testPod)
303304
probePaths := []probeKey{{testPodUID, testContainerName, readiness}}
304305
if err := expectProbes(m, probePaths); err != nil {
305306
t.Error(err)

pkg/kubelet/prober/results/results_manager.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"k8s.io/kubernetes/pkg/api"
2323
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
24+
"k8s.io/kubernetes/pkg/types"
2425
)
2526

2627
// Manager provides a probe results cache and channel of updates.
@@ -61,7 +62,7 @@ func (r Result) String() string {
6162
type Update struct {
6263
ContainerID kubecontainer.ContainerID
6364
Result Result
64-
Pod *api.Pod
65+
PodUID types.UID
6566
}
6667

6768
// Manager implementation.
@@ -93,7 +94,7 @@ func (m *manager) Get(id kubecontainer.ContainerID) (Result, bool) {
9394

9495
func (m *manager) Set(id kubecontainer.ContainerID, result Result, pod *api.Pod) {
9596
if m.setInternal(id, result) {
96-
m.updates <- Update{id, result, pod}
97+
m.updates <- Update{id, result, pod.UID}
9798
}
9899
}
99100

pkg/kubelet/prober/results/results_manager_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestCacheOperations(t *testing.T) {
3535
_, found := m.Get(unsetID)
3636
assert.False(t, found, "unset result found")
3737

38-
m.Set(setID, Success, nil)
38+
m.Set(setID, Success, &api.Pod{})
3939
result, found := m.Get(setID)
4040
assert.True(t, result == Success, "set result")
4141
assert.True(t, found, "set result found")
@@ -77,10 +77,10 @@ func TestUpdates(t *testing.T) {
7777

7878
// New result should always push an update.
7979
m.Set(fooID, Success, pod)
80-
expectUpdate(Update{fooID, Success, pod}, "new success")
80+
expectUpdate(Update{fooID, Success, pod.UID}, "new success")
8181

8282
m.Set(barID, Failure, pod)
83-
expectUpdate(Update{barID, Failure, pod}, "new failure")
83+
expectUpdate(Update{barID, Failure, pod.UID}, "new failure")
8484

8585
// Unchanged results should not send an update.
8686
m.Set(fooID, Success, pod)
@@ -91,8 +91,8 @@ func TestUpdates(t *testing.T) {
9191

9292
// Changed results should send an update.
9393
m.Set(fooID, Failure, pod)
94-
expectUpdate(Update{fooID, Failure, pod}, "changed foo")
94+
expectUpdate(Update{fooID, Failure, pod.UID}, "changed foo")
9595

9696
m.Set(barID, Success, pod)
97-
expectUpdate(Update{barID, Success, pod}, "changed bar")
97+
expectUpdate(Update{barID, Success, pod.UID}, "changed bar")
9898
}

pkg/kubelet/prober/testing.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,22 @@ func getTestRunningStatus() api.PodStatus {
5252
return podStatus
5353
}
5454

55-
func getTestPod(probeType probeType, probeSpec api.Probe) api.Pod {
55+
func getTestPod() *api.Pod {
5656
container := api.Container{
5757
Name: testContainerName,
5858
}
59+
pod := api.Pod{
60+
Spec: api.PodSpec{
61+
Containers: []api.Container{container},
62+
RestartPolicy: api.RestartPolicyNever,
63+
},
64+
}
65+
pod.Name = "testPod"
66+
pod.UID = testPodUID
67+
return &pod
68+
}
5969

70+
func setTestProbe(pod *api.Pod, probeType probeType, probeSpec api.Probe) {
6071
// All tests rely on the fake exec prober.
6172
probeSpec.Handler = api.Handler{
6273
Exec: &api.ExecAction{},
@@ -78,26 +89,20 @@ func getTestPod(probeType probeType, probeSpec api.Probe) api.Pod {
7889

7990
switch probeType {
8091
case readiness:
81-
container.ReadinessProbe = &probeSpec
92+
pod.Spec.Containers[0].ReadinessProbe = &probeSpec
8293
case liveness:
83-
container.LivenessProbe = &probeSpec
94+
pod.Spec.Containers[0].LivenessProbe = &probeSpec
8495
}
85-
pod := api.Pod{
86-
Spec: api.PodSpec{
87-
Containers: []api.Container{container},
88-
RestartPolicy: api.RestartPolicyNever,
89-
},
90-
}
91-
pod.Name = "testPod"
92-
pod.UID = testPodUID
93-
return pod
9496
}
9597

9698
func newTestManager() *manager {
9799
refManager := kubecontainer.NewRefManager()
98100
refManager.SetRef(testContainerID, &api.ObjectReference{}) // Suppress prober warnings.
101+
podManager := kubepod.NewBasicPodManager(nil)
102+
// Add test pod to pod manager, so that status manager can get the pod from pod manager if needed.
103+
podManager.AddPod(getTestPod())
99104
m := NewManager(
100-
status.NewManager(&fake.Clientset{}, kubepod.NewBasicPodManager(nil)),
105+
status.NewManager(&fake.Clientset{}, podManager),
101106
results.NewManager(),
102107
nil, // runner
103108
refManager,
@@ -109,8 +114,9 @@ func newTestManager() *manager {
109114
}
110115

111116
func newTestWorker(m *manager, probeType probeType, probeSpec api.Probe) *worker {
112-
pod := getTestPod(probeType, probeSpec)
113-
return newWorker(m, probeType, &pod, pod.Spec.Containers[0])
117+
pod := getTestPod()
118+
setTestProbe(pod, probeType, probeSpec)
119+
return newWorker(m, probeType, pod, pod.Spec.Containers[0])
114120
}
115121

116122
type fakeExecProber struct {

pkg/kubelet/status/manager.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ type Manager interface {
8181

8282
// SetContainerReadiness updates the cached container status with the given readiness, and
8383
// triggers a status update.
84-
SetContainerReadiness(pod *api.Pod, containerID kubecontainer.ContainerID, ready bool)
84+
SetContainerReadiness(podUID types.UID, containerID kubecontainer.ContainerID, ready bool)
8585

8686
// TerminatePods resets the container status for the provided pods to terminated and triggers
8787
// a status update. This function may not enqueue all the provided pods, in which case it will
@@ -150,10 +150,16 @@ func (m *manager) SetPodStatus(pod *api.Pod, status api.PodStatus) {
150150
m.updateStatusInternal(pod, status)
151151
}
152152

153-
func (m *manager) SetContainerReadiness(pod *api.Pod, containerID kubecontainer.ContainerID, ready bool) {
153+
func (m *manager) SetContainerReadiness(podUID types.UID, containerID kubecontainer.ContainerID, ready bool) {
154154
m.podStatusesLock.Lock()
155155
defer m.podStatusesLock.Unlock()
156156

157+
pod, ok := m.podManager.GetPodByUID(podUID)
158+
if !ok {
159+
glog.V(4).Infof("Pod %q has been deleted, no need to update readiness", string(podUID))
160+
return
161+
}
162+
157163
oldStatus, found := m.podStatuses[pod.UID]
158164
if !found {
159165
glog.Warningf("Container readiness changed before pod has synced: %q - %q",

pkg/kubelet/status/manager_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,11 @@ func TestSetContainerReadiness(t *testing.T) {
578578
}
579579

580580
m := newTestManager(&fake.Clientset{})
581+
// Add test pod because the container spec has been changed.
582+
m.podManager.AddPod(pod)
581583

582584
t.Log("Setting readiness before status should fail.")
583-
m.SetContainerReadiness(pod, cID1, true)
585+
m.SetContainerReadiness(pod.UID, cID1, true)
584586
verifyUpdates(t, m, 0)
585587
if status, ok := m.GetPodStatus(pod.UID); ok {
586588
t.Errorf("Unexpected PodStatus: %+v", status)
@@ -593,25 +595,25 @@ func TestSetContainerReadiness(t *testing.T) {
593595
verifyReadiness("initial", &status, false, false, false)
594596

595597
t.Log("Setting unchanged readiness should do nothing.")
596-
m.SetContainerReadiness(pod, cID1, false)
598+
m.SetContainerReadiness(pod.UID, cID1, false)
597599
verifyUpdates(t, m, 0)
598600
status = expectPodStatus(t, m, pod)
599601
verifyReadiness("unchanged", &status, false, false, false)
600602

601603
t.Log("Setting container readiness should generate update but not pod readiness.")
602-
m.SetContainerReadiness(pod, cID1, true)
604+
m.SetContainerReadiness(pod.UID, cID1, true)
603605
verifyUpdates(t, m, 1)
604606
status = expectPodStatus(t, m, pod)
605607
verifyReadiness("c1 ready", &status, true, false, false)
606608

607609
t.Log("Setting both containers to ready should update pod readiness.")
608-
m.SetContainerReadiness(pod, cID2, true)
610+
m.SetContainerReadiness(pod.UID, cID2, true)
609611
verifyUpdates(t, m, 1)
610612
status = expectPodStatus(t, m, pod)
611613
verifyReadiness("all ready", &status, true, true, true)
612614

613615
t.Log("Setting non-existant container readiness should fail.")
614-
m.SetContainerReadiness(pod, kubecontainer.ContainerID{"test", "foo"}, true)
616+
m.SetContainerReadiness(pod.UID, kubecontainer.ContainerID{"test", "foo"}, true)
615617
verifyUpdates(t, m, 0)
616618
status = expectPodStatus(t, m, pod)
617619
verifyReadiness("ignore non-existant", &status, true, true, true)

0 commit comments

Comments
 (0)