2016-07-19 18:53:57 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-07-20 08:42:55 +00:00
|
|
|
"errors"
|
2016-09-19 07:21:14 +00:00
|
|
|
"io"
|
2016-07-20 08:42:55 +00:00
|
|
|
"os"
|
2016-08-04 14:34:30 +00:00
|
|
|
"path/filepath"
|
2016-07-20 08:42:55 +00:00
|
|
|
|
2016-10-20 20:13:41 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-07-20 08:42:55 +00:00
|
|
|
"github.com/containers/image/directory"
|
|
|
|
"github.com/containers/image/image"
|
|
|
|
"github.com/containers/image/transports"
|
2016-07-19 18:53:57 +00:00
|
|
|
"golang.org/x/net/context"
|
2016-09-26 23:55:12 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
2016-07-19 18:53:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListImages lists existing images.
|
|
|
|
func (s *Server) ListImages(ctx context.Context, req *pb.ListImagesRequest) (*pb.ListImagesResponse, error) {
|
2016-10-20 20:13:41 +00:00
|
|
|
logrus.Debugf("ListImages: %+v", req)
|
2016-07-20 08:42:55 +00:00
|
|
|
// TODO
|
|
|
|
// containers/storage will take care of this by looking inside /var/lib/ocid/images
|
|
|
|
// and listing images.
|
2016-10-20 20:13:41 +00:00
|
|
|
return &pb.ListImagesResponse{}, nil
|
2016-07-19 18:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ImageStatus returns the status of the image.
|
|
|
|
func (s *Server) ImageStatus(ctx context.Context, req *pb.ImageStatusRequest) (*pb.ImageStatusResponse, error) {
|
2016-10-20 20:13:41 +00:00
|
|
|
logrus.Debugf("ImageStatus: %+v", req)
|
2016-07-20 08:42:55 +00:00
|
|
|
// TODO
|
|
|
|
// containers/storage will take care of this by looking inside /var/lib/ocid/images
|
|
|
|
// and getting the image status
|
2016-10-20 20:13:41 +00:00
|
|
|
return &pb.ImageStatusResponse{}, nil
|
2016-07-19 18:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PullImage pulls a image with authentication config.
|
|
|
|
func (s *Server) PullImage(ctx context.Context, req *pb.PullImageRequest) (*pb.PullImageResponse, error) {
|
2016-10-20 20:13:41 +00:00
|
|
|
logrus.Debugf("PullImage: %+v", req)
|
2016-07-20 08:42:55 +00:00
|
|
|
img := req.GetImage().GetImage()
|
|
|
|
if img == "" {
|
|
|
|
return nil, errors.New("got empty imagespec name")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(runcom): deal with AuthConfig in req.GetAuth()
|
|
|
|
|
|
|
|
// TODO(mrunalp,runcom): why do we need the SandboxConfig here?
|
|
|
|
// how do we pull in a specified sandbox?
|
|
|
|
tr, err := transports.ParseImageName(img)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// TODO(runcom): figure out the ImageContext story in containers/image instead of passing ("", true)
|
2016-09-17 13:50:35 +00:00
|
|
|
src, err := tr.NewImageSource(nil, nil)
|
2016-07-20 08:42:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-17 13:50:35 +00:00
|
|
|
i := image.FromSource(src)
|
2016-07-20 08:42:55 +00:00
|
|
|
blobs, err := i.BlobDigests()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-04 14:34:30 +00:00
|
|
|
|
2016-10-21 09:50:49 +00:00
|
|
|
if err = os.Mkdir(filepath.Join(s.config.ImageDir, tr.StringWithinTransport()), 0755); err != nil {
|
2016-07-20 08:42:55 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-21 09:50:49 +00:00
|
|
|
dir, err := directory.NewReference(filepath.Join(s.config.ImageDir, tr.StringWithinTransport()))
|
2016-07-20 08:42:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// TODO(runcom): figure out the ImageContext story in containers/image instead of passing ("", true)
|
2016-09-17 13:50:35 +00:00
|
|
|
dest, err := dir.NewImageDestination(nil)
|
2016-07-20 08:42:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// save blobs (layer + config for docker v2s2, layers only for docker v2s1 [the config is in the manifest])
|
|
|
|
for _, b := range blobs {
|
|
|
|
// TODO(runcom,nalin): we need do-then-commit to later purge on error
|
2016-09-19 07:21:14 +00:00
|
|
|
var r io.ReadCloser
|
|
|
|
r, _, err = src.GetBlob(b)
|
2016-07-20 08:42:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-19 07:21:14 +00:00
|
|
|
if _, _, err = dest.PutBlob(r, b, -1); err != nil {
|
2016-07-20 08:42:55 +00:00
|
|
|
r.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r.Close()
|
|
|
|
}
|
|
|
|
// save manifest
|
|
|
|
m, _, err := i.Manifest()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := dest.PutManifest(m); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: what else do we need here? (Signatures when the story isn't just pulling from docker://)
|
|
|
|
|
|
|
|
return &pb.PullImageResponse{}, nil
|
2016-07-19 18:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveImage removes the image.
|
|
|
|
func (s *Server) RemoveImage(ctx context.Context, req *pb.RemoveImageRequest) (*pb.RemoveImageResponse, error) {
|
2016-10-20 20:13:41 +00:00
|
|
|
logrus.Debugf("RemoveImage: %+v", req)
|
|
|
|
return &pb.RemoveImageResponse{}, nil
|
2016-07-19 18:53:57 +00:00
|
|
|
}
|