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

Use one big info struct before we change info to an array. #475

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
58 changes: 36 additions & 22 deletions pkg/server/container_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"encoding/json"
"fmt"

"github.com/golang/glog"
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/net/context"
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"

Expand Down Expand Up @@ -54,7 +56,10 @@ func (c *criContainerdService) ContainerStatus(ctx context.Context, r *runtime.C
imageRef = image.RepoDigests[0]
}
status := toCRIContainerStatus(container, spec, imageRef)
info := toCRIContainerInfo(ctx, container, r.GetVerbose())
info, err := toCRIContainerInfo(ctx, container, r.GetVerbose())
if err != nil {
return nil, fmt.Errorf("failed to generate container info: %v", err)
}

return &runtime.ContainerStatusResponse{
Status: status,
Expand Down Expand Up @@ -94,44 +99,53 @@ func toCRIContainerStatus(container containerstore.Container, spec *runtime.Imag
}
}

type containerInfo struct {
Copy link
Member

Choose a reason for hiding this comment

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

move the todo here for all this stuff to be specified in CRI (containerInfo, "info") etc.

SandboxID string `json:"sandboxID"`
Pid uint32 `json:"pid"`
Removing bool `json:"removing"`
SnapshotKey string `json:"snapshotKey"`
Snapshotter string `json:"snapshotter"`
Config *runtime.ContainerConfig `json:"config"`
RuntimeSpec *runtimespec.Spec `json:"runtimeSpec"`
}

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

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

// TODO (mikebrow): discuss predefining constants for some or all of these key names in CRI for these info map values
info["pid"] = marshallToString(status.Pid)
info["containerConfig"] = marshallToString(meta.Config)
info["removingState"] = marshallToString(status.Removing)
// TODO(random-liu): Change CRI status info to use array instead of map.
ci := &containerInfo{
SandboxID: container.SandboxID,
Pid: status.Pid,
Removing: status.Removing,
Config: meta.Config,
}

oldSpec, err := container.Container.Spec(ctx)
spec, err := container.Container.Spec(ctx)
if err == nil {
info["runtimeSpec"] = marshallToString(oldSpec)
ci.RuntimeSpec = spec
} else {
info["runtimeSpec"] = err.Error()
glog.Errorf("Failed to get container %q spec: %v", container.ID, err)
}

ctrInfo, err := container.Container.Info(ctx)
if err == nil {
info["snapshotKey"] = marshallToString(ctrInfo.SnapshotKey)
info["snapshotter"] = marshallToString(ctrInfo.Snapshotter)
ci.SnapshotKey = ctrInfo.SnapshotKey
ci.Snapshotter = ctrInfo.Snapshotter
} else {
info["snapshotKey"] = err.Error()
info["snapshotter"] = err.Error()
glog.Errorf("Failed to get container %q info: %v", container.ID, err)
}

return info
}

func marshallToString(v interface{}) string {
m, err := json.Marshal(v)
if err == nil {
return string(m)
infoBytes, err := json.Marshal(ci)
if err != nil {
return nil, fmt.Errorf("failed to marshal info %v: %v", ci, err)
}
return err.Error()
info["info"] = string(infoBytes)
return info, nil
}
3 changes: 2 additions & 1 deletion pkg/server/container_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ func TestToCRIContainerInfo(t *testing.T) {
)
assert.NoError(t, err)

info := toCRIContainerInfo(context.Background(),
info, err := toCRIContainerInfo(context.Background(),
container,
false)
assert.NoError(t, err)
assert.Nil(t, info)
}

Expand Down