Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nil pod in pod format #89286

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/kubelet/util/format/pod.go
Expand Up @@ -30,6 +30,9 @@ type podHandler func(*v1.Pod) string
// Pod returns a string representing a pod in a consistent human readable format,
// with pod UID as part of the string.
func Pod(pod *v1.Pod) string {
if pod == nil {
return "<nil>"
}
return PodDesc(pod.Name, pod.Namespace, pod.UID)
}

Expand All @@ -44,6 +47,9 @@ func PodDesc(podName, podNamespace string, podUID types.UID) string {
// PodWithDeletionTimestamp is the same as Pod. In addition, it prints the
// deletion timestamp of the pod if it's not nil.
func PodWithDeletionTimestamp(pod *v1.Pod) string {
if pod == nil {
return "<nil>"
}
var deletionTimestamp string
if pod.DeletionTimestamp != nil {
deletionTimestamp = ":DeletionTimestamp=" + pod.DeletionTimestamp.UTC().Format(time.RFC3339)
Expand Down
12 changes: 10 additions & 2 deletions pkg/kubelet/util/format/pod_test.go
Expand Up @@ -56,6 +56,7 @@ func TestPod(t *testing.T) {
}{
{"field_empty_case", fakeCreatePod("", "", ""), "_()"},
{"field_normal_case", fakeCreatePod("test-pod", metav1.NamespaceDefault, "551f5a43-9f2f-11e7-a589-fa163e148d75"), "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
{"nil_pod_case", nil, "<nil>"},
}

for _, testCase := range testCases {
Expand Down Expand Up @@ -87,12 +88,14 @@ func TestPodWithDeletionTimestamp(t *testing.T) {

testCases := []struct {
caseName string
isPodNil bool
isdeletionTimestampNil bool
deletionTimestamp metav1.Time
expectedValue string
}{
{"timestamp_is_nil_case", true, normalDeletionTime, "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
{"timestamp_is_normal_case", false, normalDeletionTime, "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75):DeletionTimestamp=2017-09-26T14:37:50Z"},
{"timestamp_is_nil_case", false, true, normalDeletionTime, "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
{"timestamp_is_normal_case", false, false, normalDeletionTime, "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75):DeletionTimestamp=2017-09-26T14:37:50Z"},
{"pod_is_nil_case", true, false, normalDeletionTime, "<nil>"},
}

for _, testCase := range testCases {
Expand All @@ -101,6 +104,9 @@ func TestPodWithDeletionTimestamp(t *testing.T) {
if testCase.isdeletionTimestampNil {
fakePod.SetDeletionTimestamp(nil)
}
if testCase.isPodNil {
fakePod = nil
}

realPodWithDeletionTimestamp := PodWithDeletionTimestamp(fakePod)
assert.Equalf(t, testCase.expectedValue, realPodWithDeletionTimestamp, "Failed to test: %s", testCase.caseName)
Expand All @@ -120,6 +126,7 @@ func TestPods(t *testing.T) {
{"input_empty_case", []*v1.Pod{}, ""},
{"input_length_one_case", []*v1.Pod{pod1}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
{"input_length_more_than_one_case", []*v1.Pod{pod1, pod2}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75), pod2_default(e84a99bf-d1f9-43c2-9fa5-044ac85f794b)"},
{"input_include_nil_case", []*v1.Pod{pod1, nil}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75), <nil>"},
}

for _, testCase := range testCases {
Expand All @@ -142,6 +149,7 @@ func TestPodsWithDeletionTimestamps(t *testing.T) {
{"input_empty_case", []*v1.Pod{}, ""},
{"input_length_one_case", []*v1.Pod{pod1}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75):DeletionTimestamp=2017-09-26T14:37:50Z"},
{"input_length_more_than_one_case", []*v1.Pod{pod1, pod2}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75):DeletionTimestamp=2017-09-26T14:37:50Z, pod2_default(e84a99bf-d1f9-43c2-9fa5-044ac85f794b):DeletionTimestamp=2017-09-26T14:37:50Z"},
{"input_include_nil_case", []*v1.Pod{pod1, nil}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75):DeletionTimestamp=2017-09-26T14:37:50Z, <nil>"},
}

for _, testCase := range testCases {
Expand Down