adding info map for verbose image status

Signed-off-by: Mike Brown <brownwm@us.ibm.com>
This commit is contained in:
Mike Brown 2017-12-04 15:13:04 -06:00
parent 5003c61849
commit 03ac989644

View File

@ -17,10 +17,16 @@ limitations under the License.
package server package server
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/golang/glog"
"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"
content "github.com/containerd/containerd/content"
imagestore "github.com/kubernetes-incubator/cri-containerd/pkg/store/image"
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
) )
// ImageStatus returns the status of the image, returns nil if the image isn't present. // ImageStatus returns the status of the image, returns nil if the image isn't present.
@ -37,6 +43,21 @@ func (c *criContainerdService) ImageStatus(ctx context.Context, r *runtime.Image
} }
// TODO(random-liu): [P0] Make sure corresponding snapshot exists. What if snapshot // TODO(random-liu): [P0] Make sure corresponding snapshot exists. What if snapshot
// doesn't exist? // doesn't exist?
runtimeImage := toCRIRuntimeImage(image)
info, err := c.toCRIImageInfo(ctx, image, r.GetVerbose())
if err != nil {
return nil, fmt.Errorf("failed to generate image info: %v", err)
}
return &runtime.ImageStatusResponse{
Image: runtimeImage,
Info: info,
}, nil
}
// toCRIRuntimeImage converts internal image object to CRI runtime.Image.
func toCRIRuntimeImage(image *imagestore.Image) *runtime.Image {
runtimeImage := &runtime.Image{ runtimeImage := &runtime.Image{
Id: image.ID, Id: image.ID,
RepoTags: image.RepoTags, RepoTags: image.RepoTags,
@ -49,6 +70,59 @@ func (c *criContainerdService) ImageStatus(ctx context.Context, r *runtime.Image
} }
runtimeImage.Username = username runtimeImage.Username = username
// TODO(mikebrow): write a ImageMetadata to runtime.Image converter return runtimeImage
return &runtime.ImageStatusResponse{Image: runtimeImage}, nil }
// TODO (mikebrow): discuss moving this struct and / or constants for info map for some or all of these fields to CRI
type verboseImageInfo struct {
Config *imagespec.ImageConfig `json:"config"`
ConfigDescriptor imagespec.Descriptor `json:"configDescriptor"`
ManifestDescriptor imagespec.Descriptor `json:"manifestDescriptor"`
LayerInfo []content.Info `json:"layerInfo"`
}
// toCRIImageInfo converts internal image object information to CRI image status response info map.
func (c *criContainerdService) toCRIImageInfo(ctx context.Context, image *imagestore.Image, verbose bool) (map[string]string, error) {
if !verbose {
return nil, nil
}
info := make(map[string]string)
i := image.Image
descriptor, err := i.Config(ctx)
if err != nil {
glog.Errorf("Failed to get image config %q: %v", image.ID, err)
} // fallthrough
targetDescriptor := i.Target()
var dia []content.Info
digests, err := i.RootFS(ctx)
if err != nil {
glog.Errorf("Failed to get target digests %q: %v", i.Name(), err)
} else {
dia = make([]content.Info, len(digests))
for i, d := range digests {
di, err := c.client.ContentStore().Info(ctx, d)
if err == nil {
dia[i] = di
}
}
}
imi := &verboseImageInfo{
Config: image.Config,
ConfigDescriptor: descriptor,
ManifestDescriptor: targetDescriptor,
LayerInfo: dia,
}
m, err := json.Marshal(imi)
if err == nil {
info["info"] = string(m)
} else {
glog.Errorf("failed to marshal info %v: %v", imi, err)
info["info"] = err.Error()
}
return info, nil
} }