Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions internal/lib/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,16 @@ func New(id, namespace, name, kubeName, logDir string, labels, annotations map[s
}

func (s *Sandbox) CRISandbox() *types.PodSandbox {
// Return a deep copy so the State field doesn't get mutated mid-request,
// causing a proto panic.
cpy := *s.criSandbox
return &cpy
// If a protobuf message gets mutated mid-request, then the proto library panics.
// We would like to avoid deep copies when possible to avoid excessive garbage
// collection, but need to if the sandbox changes state.
newState := s.State()
if newState != s.criSandbox.State {
cpy := *s.criSandbox
cpy.State = newState
s.criSandbox = &cpy
}
return s.criSandbox
}

func (s *Sandbox) CreatedAt() int64 {
Expand Down
22 changes: 18 additions & 4 deletions internal/oci/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,24 @@ func NewSpoofedContainer(id, name string, labels map[string]string, sandbox stri
}

func (c *Container) CRIContainer() *types.Container {
// Return a deep copy so the State field doesn't get mutated mid-request,
// causing a proto panic.
cpy := *c.criContainer
return &cpy
// If a protobuf message gets mutated mid-request, then the proto library panics.
// We would like to avoid deep copies when possible to avoid excessive garbage
// collection, but need to if the container changes state.
newState := types.ContainerState_CONTAINER_UNKNOWN
switch c.StateNoLock().Status {
case ContainerStateCreated:
newState = types.ContainerState_CONTAINER_CREATED
case ContainerStateRunning, ContainerStatePaused:
newState = types.ContainerState_CONTAINER_RUNNING
case ContainerStateStopped:
newState = types.ContainerState_CONTAINER_EXITED
}
if newState != c.criContainer.State {
cpy := *c.criContainer
cpy.State = newState
c.criContainer = &cpy
}
return c.criContainer
}

// SetSpec loads the OCI spec in the container struct
Expand Down
13 changes: 0 additions & 13 deletions server/container_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,6 @@ func (s *Server) ListContainers(ctx context.Context, req *types.ListContainersRe
continue
}
c := ctr.CRIContainer()
cState := ctr.StateNoLock()

rState := types.ContainerState_CONTAINER_UNKNOWN
switch cState.Status {
case oci.ContainerStateCreated:
rState = types.ContainerState_CONTAINER_CREATED
case oci.ContainerStateRunning, oci.ContainerStatePaused:
rState = types.ContainerState_CONTAINER_RUNNING
case oci.ContainerStateStopped:
rState = types.ContainerState_CONTAINER_EXITED
}
c.State = rState

// Filter by other criteria such as state and labels.
if filterContainer(c, req.Filter) {
ctrs = append(ctrs, c)
Expand Down
2 changes: 0 additions & 2 deletions server/sandbox_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ func (s *Server) ListPodSandbox(ctx context.Context, req *types.ListPodSandboxRe
}

pod := sb.CRISandbox()
pod.State = sb.State()

// Filter by other criteria such as state and labels.
if filterSandbox(pod, req.Filter) {
respList = append(respList, pod)
Expand Down