Skip to content

Commit

Permalink
Merge pull request #6782 from jbartosik/nice-format-blocking-pod-reason
Browse files Browse the repository at this point in the history
Format BlockingPodReason in human readbale way with %v
  • Loading branch information
k8s-ci-robot authored May 7, 2024
2 parents daaf243 + 4e42fe7 commit 442c353
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 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
55 changes: 55 additions & 0 deletions cluster-autoscaler/utils/drain/drain_test.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"
"testing"
"time"

Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit 442c353

Please sign in to comment.