Skip to content

Commit c2191fd

Browse files
authored
Merge pull request #1513 from brianpursley/state-name
Change "failed to stop sandbox" error message to use state name instead of numeric value
2 parents 09d6426 + aa04fc9 commit c2191fd

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

pkg/store/sandbox/status.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
package sandbox
1818

1919
import (
20+
"strconv"
2021
"sync"
2122
"time"
23+
24+
runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
2225
)
2326

2427
// The sandbox state machine in the CRI plugin:
@@ -63,7 +66,7 @@ type State uint32
6366
const (
6467
// StateReady is ready state, it means sandbox container
6568
// is running.
66-
StateReady = iota
69+
StateReady State = iota
6770
// StateNotReady is notready state, it ONLY means sandbox
6871
// container is not running.
6972
// StopPodSandbox should still be called for NOTREADY sandbox to
@@ -75,6 +78,21 @@ const (
7578
StateUnknown
7679
)
7780

81+
// String returns the string representation of the state
82+
func (s State) String() string {
83+
switch s {
84+
case StateReady:
85+
return runtime.PodSandboxState_SANDBOX_READY.String()
86+
case StateNotReady:
87+
return runtime.PodSandboxState_SANDBOX_NOTREADY.String()
88+
case StateUnknown:
89+
// PodSandboxState doesn't have an unknown state, but State does, so return a string using the same convention
90+
return "SANDBOX_UNKNOWN"
91+
default:
92+
return "invalid sandbox state value: " + strconv.Itoa(int(s))
93+
}
94+
}
95+
7896
// Status is the status of a sandbox.
7997
type Status struct {
8098
// Pid is the init process id of the sandbox container.

pkg/store/sandbox/status_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package sandbox
1818

1919
import (
2020
"errors"
21+
"fmt"
2122
"testing"
2223
"time"
2324

@@ -59,3 +60,11 @@ func TestStatus(t *testing.T) {
5960
assert.NoError(err)
6061
assert.Equal(updateStatus, s.Get())
6162
}
63+
64+
func TestStateStringConversion(t *testing.T) {
65+
assert := assertlib.New(t)
66+
assert.Equal("SANDBOX_READY", fmt.Sprintf("%s", StateReady))
67+
assert.Equal("SANDBOX_NOTREADY", fmt.Sprintf("%s", StateNotReady))
68+
assert.Equal("SANDBOX_UNKNOWN", fmt.Sprintf("%s", StateUnknown))
69+
assert.Equal("invalid sandbox state value: 123", fmt.Sprintf("%s", State(123)))
70+
}

0 commit comments

Comments
 (0)