Skip to content

Commit

Permalink
Format BlockingPodReason in human readbale way with %v
Browse files Browse the repository at this point in the history
Without this the enum is output as its int value, which is not readable
  • Loading branch information
jbartosik committed May 2, 2024
1 parent d8e1e6d commit 4e42fe7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
26 changes: 26 additions & 0 deletions cluster-autoscaler/utils/drain/drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package drain

import (
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions cluster-autoscaler/utils/drain/drain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,43 +137,43 @@ func TestBlockingPodReasonFormatting(t *testing.T) {
}{
{
bpr: NoReason,
want: "0",
want: "NoReason",
},
{
bpr: ControllerNotFound,
want: "1",
want: "ControllerNotFound",
},
{
bpr: MinReplicasReached,
want: "2",
bpr: ControllerNotFound,
want: "ControllerNotFound",
},
{
bpr: NotReplicated,
want: "3",
want: "NotReplicated",
},
{
bpr: LocalStorageRequested,
want: "4",
want: "LocalStorageRequested",
},
{
bpr: NotSafeToEvictAnnotation,
want: "5",
want: "NotSafeToEvictAnnotation",
},
{
bpr: UnmovableKubeSystemPod,
want: "6",
want: "UnmovableKubeSystemPod",
},
{
bpr: NotEnoughPdb,
want: "7",
want: "NotEnoughPdb",
},
{
bpr: UnexpectedError,
want: "8",
want: "UnexpectedError",
},
{
bpr: BlockingPodReason(9),
want: "9",
want: "unrecognized reason: 9",
},
} {
t.Run(tc.want, func(t *testing.T) {
Expand Down

0 comments on commit 4e42fe7

Please sign in to comment.