Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Improve container sandbox status #479

Merged
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
7 changes: 4 additions & 3 deletions pkg/server/container_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func toCRIContainerStatus(container containerstore.Container, spec *runtime.Imag
}

type containerInfo struct {
// TODO(random-liu): Add sandboxID in CRI container status.
SandboxID string `json:"sandboxID"`
Pid uint32 `json:"pid"`
Removing bool `json:"removing"`
Expand All @@ -115,7 +116,6 @@ func toCRIContainerInfo(ctx context.Context, container containerstore.Container,
return nil, nil
}

info := make(map[string]string)
meta := container.Metadata
status := container.Status.Get()

Expand Down Expand Up @@ -146,6 +146,7 @@ func toCRIContainerInfo(ctx context.Context, container containerstore.Container,
if err != nil {
return nil, fmt.Errorf("failed to marshal info %v: %v", ci, err)
}
info["info"] = string(infoBytes)
return info, nil
return map[string]string{
"info": string(infoBytes),
}, nil
}
131 changes: 70 additions & 61 deletions pkg/server/sandbox_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func (c *criContainerdService) PodSandboxStatus(ctx context.Context, r *runtime.
return nil, fmt.Errorf("failed to get sandbox container task: %v", err)
}

var pid uint32
var processStatus containerd.ProcessStatus
// Set sandbox state to NOTREADY by default.
state := runtime.PodSandboxState_SANDBOX_NOTREADY
// If the sandbox container is running, treat it as READY.
Expand All @@ -56,6 +58,8 @@ func (c *criContainerdService) PodSandboxStatus(ctx context.Context, r *runtime.
if taskStatus.Status == containerd.Running {
state = runtime.PodSandboxState_SANDBOX_READY
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better that putting as below into toCRISandboxStatus?
We get the original info at the outside, processing it inside.

if taskStatus.Status == containerd.Running {
 			state = runtime.PodSandboxState_SANDBOX_READY

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will send another PR to do that. Thanks!

}
pid = task.Pid()
processStatus = taskStatus.Status
}

ip, err := c.getIP(sandbox)
Expand All @@ -69,7 +73,7 @@ func (c *criContainerdService) PodSandboxStatus(ctx context.Context, r *runtime.
}
createdAt := ctrInfo.CreatedAt
status := toCRISandboxStatus(sandbox.Metadata, state, createdAt, ip)
info, err := toCRISandboxContainerInfo(ctx, sandbox.Container, r.GetVerbose())
info, err := toCRISandboxInfo(ctx, sandbox, pid, processStatus, r.GetVerbose())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preferred it as it was..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not only "container info", right? e.g. PodSandboxConfig

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the parameters not the name :-)

if err != nil {
return nil, fmt.Errorf("failed to get verbose sandbox container info: %v", err)
}
Expand All @@ -80,66 +84,6 @@ func (c *criContainerdService) PodSandboxStatus(ctx context.Context, r *runtime.
}, nil
}

// TODO (mikebrow): discuss predefining constants structures for some or all of these field names in CRI
type sandboxInfo struct {
Pid uint32 `json:"pid"`
State string `json:"state"`
SnapshotKey string `json:"snapshotKey"`
Snapshotter string `json:"snapshotter"`
RuntimeSpec *runtimespec.Spec `json:"runtimeSpec"`
}

// toCRISandboxContainerInfo converts internal container object information to CRI sandbox container status response info map.
func toCRISandboxContainerInfo(ctx context.Context, container containerd.Container, verbose bool) (map[string]string, error) {
if !verbose {
return nil, nil
}

task, err := container.Task(ctx, nil)
if err != nil {
return nil, fmt.Errorf("failed to get sandbox container task: %v", err)
}
status, err := task.Status(ctx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change?

if err != nil {
return nil, fmt.Errorf("failed to get sandbox container task status: %v", err)
}

info := make(map[string]string)
pid := task.Pid()

oldSpec, err := container.Spec(ctx)
if err != nil {
glog.Errorf("Failed to get sandbox container runtime spec: %v", err)
}

var snapshotkey, snapshotter string
ctrInfo, err := container.Info(ctx)
if err == nil {
snapshotkey = ctrInfo.SnapshotKey
snapshotter = ctrInfo.Snapshotter
} else {
glog.Errorf("Failed to get target shapshot info: %v", err)
}

si := &sandboxInfo{
Pid: pid,
State: string(status.Status),
SnapshotKey: snapshotkey,
Snapshotter: snapshotter,
RuntimeSpec: oldSpec,
}

m, err := json.Marshal(si)
if err == nil {
info["info"] = string(m)
} else {
glog.Errorf("failed to marshal info %v: %v", si, err)
info["info"] = err.Error()
}

return info, nil
}

func (c *criContainerdService) getIP(sandbox sandboxstore.Sandbox) (string, error) {
config := sandbox.Config

Expand Down Expand Up @@ -200,3 +144,68 @@ func toCRISandboxStatus(meta sandboxstore.Metadata, state runtime.PodSandboxStat
Annotations: meta.Config.GetAnnotations(),
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why move?

// TODO (mikebrow): discuss predefining constants structures for some or all of these field names in CRI
type sandboxInfo struct {
Pid uint32 `json:"pid"`
Status string `json:"processStatus"`
NetNSClosed bool `json:"netNamespaceClosed"`
Image string `json:"image"`
SnapshotKey string `json:"snapshotKey"`
Snapshotter string `json:"snapshotter"`
Config *runtime.PodSandboxConfig `json:"config"`
RuntimeSpec *runtimespec.Spec `json:"runtimeSpec"`
}

// toCRISandboxInfo converts internal container object information to CRI sandbox status response info map.
func toCRISandboxInfo(ctx context.Context, sandbox sandboxstore.Sandbox,
pid uint32, processStatus containerd.ProcessStatus, verbose bool) (map[string]string, error) {
if !verbose {
return nil, nil
}

si := &sandboxInfo{
Pid: pid,
Status: string(processStatus),
Config: sandbox.Config,
}

if si.Status == "" {
// If processStatus is empty, it means that the task is deleted. Apply "deleted"
// status which does not exist in containerd.
si.Status = "deleted"
}

if sandbox.NetNSPath != "" {
// Add network closed information if sandbox is not using host network.
si.NetNSClosed = (sandbox.NetNS == nil || sandbox.NetNS.Closed())
}

container := sandbox.Container
spec, err := container.Spec(ctx)
if err == nil {
si.RuntimeSpec = spec
} else {
glog.Errorf("Failed to get sandbox container %q runtime spec: %v", sandbox.ID, err)
}

ctrInfo, err := container.Info(ctx)
if err == nil {
// Do not use config.SandboxImage because the configuration might
// be changed during restart. It may not reflect the actual image
// used by the sandbox container.
si.Image = ctrInfo.Image
si.SnapshotKey = ctrInfo.SnapshotKey
si.Snapshotter = ctrInfo.Snapshotter
} else {
glog.Errorf("Failed to get sandbox container %q info: %v", sandbox.ID, err)
}

infoBytes, err := json.Marshal(si)
if err != nil {
return nil, fmt.Errorf("failed to marshal info %v: %v", si, err)
}
return map[string]string{
"info": string(infoBytes),
}, nil
}