Merge pull request #475 from Random-Liu/order-container-status-fields
Use one big info struct before we change info to an array.
This commit is contained in:
commit
11eb24c26f
@ -20,6 +20,8 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
|
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
||||||
|
|
||||||
@ -54,7 +56,10 @@ func (c *criContainerdService) ContainerStatus(ctx context.Context, r *runtime.C
|
|||||||
imageRef = image.RepoDigests[0]
|
imageRef = image.RepoDigests[0]
|
||||||
}
|
}
|
||||||
status := toCRIContainerStatus(container, spec, imageRef)
|
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{
|
return &runtime.ContainerStatusResponse{
|
||||||
Status: status,
|
Status: status,
|
||||||
@ -94,44 +99,53 @@ func toCRIContainerStatus(container containerstore.Container, spec *runtime.Imag
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type containerInfo struct {
|
||||||
|
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.
|
// 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 {
|
if !verbose {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
info := make(map[string]string)
|
info := make(map[string]string)
|
||||||
meta := container.Metadata
|
meta := container.Metadata
|
||||||
status := container.Status.Get()
|
status := container.Status.Get()
|
||||||
|
|
||||||
// TODO (mikebrow): discuss predefining constants for some or all of these key names in CRI for these info map values
|
// TODO(random-liu): Change CRI status info to use array instead of map.
|
||||||
info["pid"] = marshallToString(status.Pid)
|
ci := &containerInfo{
|
||||||
info["containerConfig"] = marshallToString(meta.Config)
|
SandboxID: container.SandboxID,
|
||||||
info["removingState"] = marshallToString(status.Removing)
|
Pid: status.Pid,
|
||||||
|
Removing: status.Removing,
|
||||||
|
Config: meta.Config,
|
||||||
|
}
|
||||||
|
|
||||||
oldSpec, err := container.Container.Spec(ctx)
|
spec, err := container.Container.Spec(ctx)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
info["runtimeSpec"] = marshallToString(oldSpec)
|
ci.RuntimeSpec = spec
|
||||||
} else {
|
} else {
|
||||||
info["runtimeSpec"] = err.Error()
|
glog.Errorf("Failed to get container %q spec: %v", container.ID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctrInfo, err := container.Container.Info(ctx)
|
ctrInfo, err := container.Container.Info(ctx)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
info["snapshotKey"] = marshallToString(ctrInfo.SnapshotKey)
|
ci.SnapshotKey = ctrInfo.SnapshotKey
|
||||||
info["snapshotter"] = marshallToString(ctrInfo.Snapshotter)
|
ci.Snapshotter = ctrInfo.Snapshotter
|
||||||
} else {
|
} else {
|
||||||
info["snapshotKey"] = err.Error()
|
glog.Errorf("Failed to get container %q info: %v", container.ID, err)
|
||||||
info["snapshotter"] = err.Error()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return info
|
infoBytes, err := json.Marshal(ci)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to marshal info %v: %v", ci, err)
|
||||||
}
|
}
|
||||||
|
info["info"] = string(infoBytes)
|
||||||
func marshallToString(v interface{}) string {
|
return info, nil
|
||||||
m, err := json.Marshal(v)
|
|
||||||
if err == nil {
|
|
||||||
return string(m)
|
|
||||||
}
|
|
||||||
return err.Error()
|
|
||||||
}
|
}
|
||||||
|
@ -153,9 +153,10 @@ func TestToCRIContainerInfo(t *testing.T) {
|
|||||||
)
|
)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
info := toCRIContainerInfo(context.Background(),
|
info, err := toCRIContainerInfo(context.Background(),
|
||||||
container,
|
container,
|
||||||
false)
|
false)
|
||||||
|
assert.NoError(t, err)
|
||||||
assert.Nil(t, info)
|
assert.Nil(t, info)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user