Merge pull request #445 from mikebrow/debug-container

adding some verbose debug
This commit is contained in:
Lantao Liu 2017-11-30 11:15:34 -08:00 committed by GitHub
commit dc7066d23f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 3 deletions

View File

@ -17,6 +17,7 @@ limitations under the License.
package server
import (
"encoding/json"
"fmt"
"golang.org/x/net/context"
@ -52,9 +53,12 @@ func (c *criContainerdService) ContainerStatus(ctx context.Context, r *runtime.C
// Based on the CRI definition, this field will be consumed by user.
imageRef = image.RepoDigests[0]
}
status := toCRIContainerStatus(container, spec, imageRef)
info := toCRIContainerInfo(ctx, container, r.GetVerbose())
return &runtime.ContainerStatusResponse{
Status: toCRIContainerStatus(container, spec, imageRef),
Status: status,
Info: info,
}, nil
}
@ -70,6 +74,7 @@ func toCRIContainerStatus(container containerstore.Container, spec *runtime.Imag
reason = errorExitReason
}
}
return &runtime.ContainerStatus{
Id: meta.ID,
Metadata: meta.Config.GetMetadata(),
@ -88,3 +93,45 @@ func toCRIContainerStatus(container containerstore.Container, spec *runtime.Imag
LogPath: meta.LogPath,
}
}
// 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 {
if !verbose {
return 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)
oldSpec, err := container.Container.Spec(ctx)
if err == nil {
info["runtimeSpec"] = marshallToString(oldSpec)
} else {
info["runtimeSpec"] = err.Error()
}
ctrInfo, err := container.Container.Info(ctx)
if err == nil {
info["snapshotKey"] = marshallToString(ctrInfo.SnapshotKey)
info["snapshotter"] = marshallToString(ctrInfo.Snapshotter)
} else {
info["snapshotKey"] = err.Error()
info["snapshotter"] = err.Error()
}
return info
}
func marshallToString(v interface{}) string {
m, err := json.Marshal(v)
if err == nil {
return string(m)
}
return err.Error()
}

View File

@ -137,12 +137,28 @@ func TestToCRIContainerStatus(t *testing.T) {
expected.FinishedAt = test.finishedAt
expected.ExitCode = test.exitCode
expected.Message = test.message
assert.Equal(t, expected, toCRIContainerStatus(container,
containerStatus := toCRIContainerStatus(container,
&runtime.ImageSpec{Image: image.RepoTags[0]},
image.RepoDigests[0]), desc)
image.RepoDigests[0])
assert.Equal(t, expected, containerStatus, desc)
}
}
// TODO(mikebrow): add a fake containerd container.Container.Spec client api so we can test verbose is true option
func TestToCRIContainerInfo(t *testing.T) {
metadata, status, _, _ := getContainerStatusTestData()
container, err := containerstore.NewContainer(
*metadata,
containerstore.WithFakeStatus(*status),
)
assert.NoError(t, err)
info := toCRIContainerInfo(context.Background(),
container,
false)
assert.Nil(t, info)
}
func TestContainerStatus(t *testing.T) {
for desc, test := range map[string]struct {
exist bool