2016-11-22 22:05:37 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-10-18 14:48:33 +00:00
|
|
|
"fmt"
|
2018-02-14 12:16:55 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-11-09 10:10:35 +00:00
|
|
|
"time"
|
2016-10-18 14:48:33 +00:00
|
|
|
|
2017-05-17 17:18:35 +00:00
|
|
|
"github.com/containers/storage"
|
2017-11-28 00:34:55 +00:00
|
|
|
pkgstorage "github.com/kubernetes-incubator/cri-o/pkg/storage"
|
2017-08-02 15:17:45 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-08-05 11:40:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-11-22 22:05:37 +00:00
|
|
|
"golang.org/x/net/context"
|
2018-02-12 20:13:07 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
|
2016-11-22 22:05:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImageStatus returns the status of the image.
|
2017-11-09 10:10:35 +00:00
|
|
|
func (s *Server) ImageStatus(ctx context.Context, req *pb.ImageStatusRequest) (resp *pb.ImageStatusResponse, err error) {
|
|
|
|
const operation = "image_status"
|
|
|
|
defer func() {
|
|
|
|
recordOperation(operation, time.Now())
|
|
|
|
recordError(operation, err)
|
|
|
|
}()
|
|
|
|
|
2016-10-18 14:48:33 +00:00
|
|
|
logrus.Debugf("ImageStatusRequest: %+v", req)
|
|
|
|
image := ""
|
|
|
|
img := req.GetImage()
|
|
|
|
if img != nil {
|
2017-02-03 14:41:28 +00:00
|
|
|
image = img.Image
|
2016-10-18 14:48:33 +00:00
|
|
|
}
|
|
|
|
if image == "" {
|
|
|
|
return nil, fmt.Errorf("no image specified")
|
|
|
|
}
|
2017-07-20 08:01:23 +00:00
|
|
|
images, err := s.StorageImageServer().ResolveNames(image)
|
|
|
|
if err != nil {
|
2017-11-28 00:34:55 +00:00
|
|
|
if err == pkgstorage.ErrCannotParseImageID {
|
2017-07-20 08:01:23 +00:00
|
|
|
images = append(images, image)
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-02-14 12:16:55 +00:00
|
|
|
var (
|
|
|
|
notfound bool
|
|
|
|
lastErr error
|
|
|
|
)
|
|
|
|
for _, image := range images {
|
|
|
|
status, err := s.StorageImageServer().ImageStatus(s.ImageContext(), image)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Cause(err) == storage.ErrImageUnknown {
|
|
|
|
logrus.Warnf("imageStatus: can't find %s", image)
|
|
|
|
notfound = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logrus.Warnf("imageStatus: error getting status from %s: %v", image, err)
|
|
|
|
lastErr = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
resp = &pb.ImageStatusResponse{
|
|
|
|
Image: &pb.Image{
|
|
|
|
Id: status.ID,
|
|
|
|
RepoTags: status.RepoTags,
|
|
|
|
RepoDigests: status.RepoDigests,
|
|
|
|
Size_: *status.Size,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
uid, username := getUserFromImage(status.User)
|
|
|
|
if uid != nil {
|
|
|
|
resp.Image.Uid = &pb.Int64Value{Value: *uid}
|
2016-12-21 20:54:15 +00:00
|
|
|
}
|
2018-02-14 12:16:55 +00:00
|
|
|
resp.Image.Username = username
|
|
|
|
break
|
2016-10-18 14:48:33 +00:00
|
|
|
}
|
2018-02-14 12:16:55 +00:00
|
|
|
if lastErr != nil && resp == nil {
|
|
|
|
return nil, lastErr
|
|
|
|
}
|
|
|
|
if notfound && resp == nil {
|
|
|
|
return &pb.ImageStatusResponse{}, nil
|
2016-10-18 14:48:33 +00:00
|
|
|
}
|
|
|
|
logrus.Debugf("ImageStatusResponse: %+v", resp)
|
|
|
|
return resp, nil
|
2016-11-22 22:05:37 +00:00
|
|
|
}
|
2018-02-14 12:16:55 +00:00
|
|
|
|
|
|
|
// getUserFromImage gets uid or user name of the image user.
|
|
|
|
// If user is numeric, it will be treated as uid; or else, it is treated as user name.
|
|
|
|
func getUserFromImage(user string) (*int64, string) {
|
|
|
|
// return both empty if user is not specified in the image.
|
|
|
|
if user == "" {
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
// split instances where the id may contain user:group
|
|
|
|
user = strings.Split(user, ":")[0]
|
|
|
|
// user could be either uid or user name. Try to interpret as numeric uid.
|
|
|
|
uid, err := strconv.ParseInt(user, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
// If user is non numeric, assume it's user name.
|
|
|
|
return nil, user
|
|
|
|
}
|
|
|
|
// If user is a numeric uid.
|
|
|
|
return &uid, ""
|
|
|
|
}
|