File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1717package sandbox
1818
1919import (
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
6366const (
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.
7997type Status struct {
8098 // Pid is the init process id of the sandbox container.
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ package sandbox
1818
1919import (
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+ }
You can’t perform that action at this time.
0 commit comments