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

Commit

Permalink
Improve container and sandbox status.
Browse files Browse the repository at this point in the history
Signed-off-by: Lantao Liu <[email protected]>
  • Loading branch information
Random-Liu committed Dec 6, 2017
1 parent 77a8ccb commit 2e29ca9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 41 deletions.
13 changes: 10 additions & 3 deletions pkg/server/container_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ func toCRIContainerStatus(container containerstore.Container, spec *runtime.Imag
}

type containerInfo struct {
// TODO(random-liu): Add sandboxID in CRI container status.
SandboxID string `json:"sandboxID"`
Path string `json:"path"`
Args []string `json:"args"`
Pid uint32 `json:"pid"`
Removing bool `json:"removing"`
SnapshotKey string `json:"snapshotKey"`
Expand All @@ -115,7 +118,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 All @@ -129,6 +131,10 @@ func toCRIContainerInfo(ctx context.Context, container containerstore.Container,

spec, err := container.Container.Spec(ctx)
if err == nil {
if spec.Process != nil && len(spec.Process.Args) > 0 {
ci.Path = spec.Process.Args[0]
ci.Args = spec.Process.Args[1:]
}
ci.RuntimeSpec = spec
} else {
glog.Errorf("Failed to get container %q spec: %v", container.ID, err)
Expand All @@ -146,6 +152,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
}
88 changes: 50 additions & 38 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
}
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())
if err != nil {
return nil, fmt.Errorf("failed to get verbose sandbox container info: %v", err)
}
Expand All @@ -82,62 +86,70 @@ func (c *criContainerdService) PodSandboxStatus(ctx context.Context, r *runtime.

// 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"`
Pid uint32 `json:"pid"`
Status string `json:"processStatus"`
NetNS string `json:"netNamespace"`
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"`
}

// 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) {
// 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
}

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)
if err != nil {
return nil, fmt.Errorf("failed to get sandbox container task status: %v", err)
si := &sandboxInfo{
Pid: pid,
Status: string(processStatus),
NetNS: sandbox.NetNSPath,
Config: sandbox.Config,
}

info := make(map[string]string)
pid := task.Pid()
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"
}

oldSpec, err := container.Spec(ctx)
if err != nil {
glog.Errorf("Failed to get sandbox container runtime spec: %v", err)
if sandbox.NetNSPath != "" {
// Add network closed information if sandbox is not using host network.
si.NetNSClosed = (sandbox.NetNS == nil || sandbox.NetNS.Closed())
}

var snapshotkey, snapshotter string
ctrInfo, err := container.Info(ctx)
container := sandbox.Container

spec, err := container.Spec(ctx)
if err == nil {
snapshotkey = ctrInfo.SnapshotKey
snapshotter = ctrInfo.Snapshotter
si.RuntimeSpec = spec
} 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,
glog.Errorf("Failed to get sandbox container %q runtime spec: %v", sandbox.ID, err)
}

m, err := json.Marshal(si)
ctrInfo, err := container.Info(ctx)
if err == nil {
info["info"] = string(m)
// 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 marshal info %v: %v", si, err)
info["info"] = err.Error()
glog.Errorf("Failed to get sandbox contaienr %q info: %v", sandbox.ID, err)
}

return info, nil
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
}

func (c *criContainerdService) getIP(sandbox sandboxstore.Sandbox) (string, error) {
Expand Down

0 comments on commit 2e29ca9

Please sign in to comment.