PodGC should not add DisruptionTarget condition for pods which are in terminal phase
This commit is contained in:
@@ -20,6 +20,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -39,16 +41,36 @@ import (
|
||||
func TestPodGcOrphanedPodsWithFinalizer(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
enablePodDisruptionConditions bool
|
||||
phase v1.PodPhase
|
||||
wantPhase v1.PodPhase
|
||||
wantDisruptionTarget *v1.PodCondition
|
||||
}{
|
||||
"PodDisruptionConditions enabled": {
|
||||
enablePodDisruptionConditions: true,
|
||||
phase: v1.PodPending,
|
||||
wantPhase: v1.PodFailed,
|
||||
wantDisruptionTarget: &v1.PodCondition{
|
||||
Type: v1.DisruptionTarget,
|
||||
Status: v1.ConditionTrue,
|
||||
Reason: "DeletionByPodGC",
|
||||
Message: "PodGC: node no longer exists",
|
||||
},
|
||||
},
|
||||
"PodDisruptionConditions disabled": {
|
||||
enablePodDisruptionConditions: false,
|
||||
phase: v1.PodPending,
|
||||
wantPhase: v1.PodPending,
|
||||
},
|
||||
"PodDisruptionConditions enabled; succeeded pod": {
|
||||
enablePodDisruptionConditions: true,
|
||||
phase: v1.PodSucceeded,
|
||||
wantPhase: v1.PodSucceeded,
|
||||
},
|
||||
"PodDisruptionConditions enabled; failed pod": {
|
||||
enablePodDisruptionConditions: true,
|
||||
phase: v1.PodFailed,
|
||||
wantPhase: v1.PodFailed,
|
||||
},
|
||||
}
|
||||
|
||||
for name, test := range tests {
|
||||
@@ -97,6 +119,11 @@ func TestPodGcOrphanedPodsWithFinalizer(t *testing.T) {
|
||||
}
|
||||
defer testutils.RemovePodFinalizers(testCtx.ClientSet, t, []*v1.Pod{pod})
|
||||
|
||||
pod.Status.Phase = test.phase
|
||||
if _, err := testCtx.ClientSet.CoreV1().Pods(testCtx.NS.Name).UpdateStatus(testCtx.Ctx, pod, metav1.UpdateOptions{}); err != nil {
|
||||
t.Fatalf("Error %v, while setting phase %v for pod: %v", err, test.phase, klog.KObj(pod))
|
||||
}
|
||||
|
||||
// we delete the node to orphan the pod
|
||||
err = cs.CoreV1().Nodes().Delete(testCtx.Ctx, pod.Spec.NodeName, metav1.DeleteOptions{})
|
||||
if err != nil {
|
||||
@@ -110,11 +137,9 @@ func TestPodGcOrphanedPodsWithFinalizer(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Error: '%v' while updating pod info: '%v'", err, klog.KObj(pod))
|
||||
}
|
||||
_, cond := podutil.GetPodCondition(&pod.Status, v1.DisruptionTarget)
|
||||
if test.enablePodDisruptionConditions == true && cond == nil {
|
||||
t.Errorf("Pod %q does not have the expected condition: %q", klog.KObj(pod), v1.DisruptionTarget)
|
||||
} else if test.enablePodDisruptionConditions == false && cond != nil {
|
||||
t.Errorf("Pod %q has an unexpected condition: %q", klog.KObj(pod), v1.DisruptionTarget)
|
||||
_, gotDisruptionTarget := podutil.GetPodCondition(&pod.Status, v1.DisruptionTarget)
|
||||
if diff := cmp.Diff(test.wantDisruptionTarget, gotDisruptionTarget, cmpopts.IgnoreFields(v1.PodCondition{}, "LastTransitionTime")); diff != "" {
|
||||
t.Errorf("Pod %v has unexpected DisruptionTarget condition: %s", klog.KObj(pod), diff)
|
||||
}
|
||||
if pod.Status.Phase != test.wantPhase {
|
||||
t.Errorf("Unexpected phase for pod %q. Got: %q, want: %q", klog.KObj(pod), pod.Status.Phase, test.wantPhase)
|
||||
|
Reference in New Issue
Block a user