diff --git a/cluster-autoscaler/utils/drain/drain.go b/cluster-autoscaler/utils/drain/drain.go index 81eef9ad858a..00f66e673fb5 100644 --- a/cluster-autoscaler/utils/drain/drain.go +++ b/cluster-autoscaler/utils/drain/drain.go @@ -17,6 +17,7 @@ limitations under the License. package drain import ( + "fmt" "strings" "time" @@ -68,6 +69,31 @@ const ( UnexpectedError ) +func (e BlockingPodReason) String() string { + switch e { + case NoReason: + return "NoReason" + case ControllerNotFound: + return "ControllerNotFound" + case MinReplicasReached: + return "MinReplicasReached" + case NotReplicated: + return "NotReplicated" + case LocalStorageRequested: + return "LocalStorageRequested" + case NotSafeToEvictAnnotation: + return "NotSafeToEvictAnnotation" + case UnmovableKubeSystemPod: + return "UnmovableKubeSystemPod" + case NotEnoughPdb: + return "NotEnoughPdb" + case UnexpectedError: + return "UnexpectedError" + default: + return fmt.Sprintf("unrecognized reason: %d", int(e)) + } +} + // ControllerRef returns the OwnerReference to pod's controller. func ControllerRef(pod *apiv1.Pod) *metav1.OwnerReference { return metav1.GetControllerOf(pod) diff --git a/cluster-autoscaler/utils/drain/drain_test.go b/cluster-autoscaler/utils/drain/drain_test.go index 95176fdf55d0..ba65b3a8f38c 100644 --- a/cluster-autoscaler/utils/drain/drain_test.go +++ b/cluster-autoscaler/utils/drain/drain_test.go @@ -17,6 +17,7 @@ limitations under the License. package drain import ( + "fmt" "testing" "time" @@ -128,3 +129,57 @@ func TestIsPodLongTerminating(t *testing.T) { }) } } + +func TestBlockingPodReasonFormatting(t *testing.T) { + for _, tc := range []struct { + bpr BlockingPodReason + want string + }{ + { + bpr: NoReason, + want: "NoReason", + }, + { + bpr: ControllerNotFound, + want: "ControllerNotFound", + }, + { + bpr: ControllerNotFound, + want: "ControllerNotFound", + }, + { + bpr: NotReplicated, + want: "NotReplicated", + }, + { + bpr: LocalStorageRequested, + want: "LocalStorageRequested", + }, + { + bpr: NotSafeToEvictAnnotation, + want: "NotSafeToEvictAnnotation", + }, + { + bpr: UnmovableKubeSystemPod, + want: "UnmovableKubeSystemPod", + }, + { + bpr: NotEnoughPdb, + want: "NotEnoughPdb", + }, + { + bpr: UnexpectedError, + want: "UnexpectedError", + }, + { + bpr: BlockingPodReason(9), + want: "unrecognized reason: 9", + }, + } { + t.Run(tc.want, func(t *testing.T) { + if got := fmt.Sprintf("%v", tc.bpr); got != tc.want { + t.Errorf("got: %s, want %s", got, tc.want) + } + }) + } +}