Change "failed to stop sandbox" error message to use state name instead of numeric value

Signed-off-by: Brian Pursley <bpursley@cinlogic.com>
This commit is contained in:
Brian Pursley 2020-06-27 16:45:08 -04:00
parent 682d158399
commit aa04fc9d53
2 changed files with 28 additions and 1 deletions

View File

@ -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.

View File

@ -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)))
}