From aa04fc9d538653a3513d8e855e5d32453d86781e Mon Sep 17 00:00:00 2001 From: Brian Pursley Date: Sat, 27 Jun 2020 16:45:08 -0400 Subject: [PATCH] Change "failed to stop sandbox" error message to use state name instead of numeric value Signed-off-by: Brian Pursley --- pkg/store/sandbox/status.go | 20 +++++++++++++++++++- pkg/store/sandbox/status_test.go | 9 +++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pkg/store/sandbox/status.go b/pkg/store/sandbox/status.go index e7adcea4d..e9198eb97 100644 --- a/pkg/store/sandbox/status.go +++ b/pkg/store/sandbox/status.go @@ -17,8 +17,11 @@ package sandbox import ( + "strconv" "sync" "time" + + runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" ) // The sandbox state machine in the CRI plugin: @@ -63,7 +66,7 @@ type State uint32 const ( // StateReady is ready state, it means sandbox container // is running. - StateReady = iota + StateReady State = iota // StateNotReady is notready state, it ONLY means sandbox // container is not running. // StopPodSandbox should still be called for NOTREADY sandbox to @@ -75,6 +78,21 @@ const ( StateUnknown ) +// String returns the string representation of the state +func (s State) String() string { + switch s { + case StateReady: + return runtime.PodSandboxState_SANDBOX_READY.String() + case StateNotReady: + return runtime.PodSandboxState_SANDBOX_NOTREADY.String() + case StateUnknown: + // PodSandboxState doesn't have an unknown state, but State does, so return a string using the same convention + return "SANDBOX_UNKNOWN" + default: + return "invalid sandbox state value: " + strconv.Itoa(int(s)) + } +} + // Status is the status of a sandbox. type Status struct { // Pid is the init process id of the sandbox container. diff --git a/pkg/store/sandbox/status_test.go b/pkg/store/sandbox/status_test.go index c3a5dcf4c..7a93715d6 100644 --- a/pkg/store/sandbox/status_test.go +++ b/pkg/store/sandbox/status_test.go @@ -18,6 +18,7 @@ package sandbox import ( "errors" + "fmt" "testing" "time" @@ -59,3 +60,11 @@ func TestStatus(t *testing.T) { assert.NoError(err) assert.Equal(updateStatus, s.Get()) } + +func TestStateStringConversion(t *testing.T) { + assert := assertlib.New(t) + assert.Equal("SANDBOX_READY", fmt.Sprintf("%s", StateReady)) + assert.Equal("SANDBOX_NOTREADY", fmt.Sprintf("%s", StateNotReady)) + assert.Equal("SANDBOX_UNKNOWN", fmt.Sprintf("%s", StateUnknown)) + assert.Equal("invalid sandbox state value: 123", fmt.Sprintf("%s", State(123))) +}