2017-08-30 01:12:10 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-09-12 10:17:44 +00:00
|
|
|
"errors"
|
2017-08-30 01:12:10 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
Return image references from the storage package
The image's canonical reference is a name with a digest of the image's
manifest, so in imageService.ImageStatus() and
imageService.ListImages(), divide the image's name list into tagged and
digested values, and if we have names, add canonical versions.
In Server.ContainerStatus(), return the image name as it was given to us
as the image, and the image digested reference as the image reference.
In Server.ListImages(), be sure to only return tagged names in the
RepoTags field. In Server.ImageStatus(), also return canonical
references in the RepoDigests field.
In Server.PullImage(), be sure that we consistently return the same
image reference for an image, whether we ended up pulling it or not.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2017-07-12 16:41:38 +00:00
|
|
|
cimage "github.com/containers/image/types"
|
2017-08-30 01:12:10 +00:00
|
|
|
"github.com/go-zoo/bone"
|
2017-11-30 15:46:11 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/lib/sandbox"
|
2017-09-12 10:17:44 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2017-09-22 21:56:10 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/types"
|
2017-09-12 10:17:44 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-08-30 01:12:10 +00:00
|
|
|
)
|
|
|
|
|
2017-09-22 21:56:10 +00:00
|
|
|
func (s *Server) getInfo() types.CrioInfo {
|
|
|
|
return types.CrioInfo{
|
2017-09-12 10:17:44 +00:00
|
|
|
StorageDriver: s.config.Config.Storage,
|
|
|
|
StorageRoot: s.config.Config.Root,
|
|
|
|
CgroupDriver: s.config.Config.CgroupManager,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
errCtrNotFound = errors.New("container not found")
|
|
|
|
errCtrStateNil = errors.New("container state is nil")
|
|
|
|
errSandboxNotFound = errors.New("sandbox for container not found")
|
|
|
|
)
|
|
|
|
|
2017-09-22 21:56:10 +00:00
|
|
|
func (s *Server) getContainerInfo(id string, getContainerFunc func(id string) *oci.Container, getInfraContainerFunc func(id string) *oci.Container, getSandboxFunc func(id string) *sandbox.Sandbox) (types.ContainerInfo, error) {
|
2017-09-12 10:17:44 +00:00
|
|
|
ctr := getContainerFunc(id)
|
|
|
|
if ctr == nil {
|
|
|
|
ctr = getInfraContainerFunc(id)
|
|
|
|
if ctr == nil {
|
2017-09-22 21:56:10 +00:00
|
|
|
return types.ContainerInfo{}, errCtrNotFound
|
2017-09-12 10:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO(mrunalp): should we call UpdateStatus()?
|
|
|
|
ctrState := ctr.State()
|
|
|
|
if ctrState == nil {
|
2017-09-22 21:56:10 +00:00
|
|
|
return types.ContainerInfo{}, errCtrStateNil
|
2017-09-12 10:17:44 +00:00
|
|
|
}
|
|
|
|
sb := getSandboxFunc(ctr.Sandbox())
|
|
|
|
if sb == nil {
|
|
|
|
logrus.Debugf("can't find sandbox %s for container %s", ctr.Sandbox(), id)
|
2017-09-22 21:56:10 +00:00
|
|
|
return types.ContainerInfo{}, errSandboxNotFound
|
2017-09-12 10:17:44 +00:00
|
|
|
}
|
Return image references from the storage package
The image's canonical reference is a name with a digest of the image's
manifest, so in imageService.ImageStatus() and
imageService.ListImages(), divide the image's name list into tagged and
digested values, and if we have names, add canonical versions.
In Server.ContainerStatus(), return the image name as it was given to us
as the image, and the image digested reference as the image reference.
In Server.ListImages(), be sure to only return tagged names in the
RepoTags field. In Server.ImageStatus(), also return canonical
references in the RepoDigests field.
In Server.PullImage(), be sure that we consistently return the same
image reference for an image, whether we ended up pulling it or not.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2017-07-12 16:41:38 +00:00
|
|
|
image := ctr.Image()
|
|
|
|
if s.ContainerServer != nil && s.ContainerServer.StorageImageServer() != nil {
|
|
|
|
if status, err := s.ContainerServer.StorageImageServer().ImageStatus(&cimage.SystemContext{}, ctr.ImageRef()); err == nil {
|
|
|
|
image = status.Name
|
|
|
|
}
|
|
|
|
}
|
2017-09-22 21:56:10 +00:00
|
|
|
return types.ContainerInfo{
|
2017-09-22 23:44:02 +00:00
|
|
|
Name: ctr.Name(),
|
|
|
|
Pid: ctrState.Pid,
|
Return image references from the storage package
The image's canonical reference is a name with a digest of the image's
manifest, so in imageService.ImageStatus() and
imageService.ListImages(), divide the image's name list into tagged and
digested values, and if we have names, add canonical versions.
In Server.ContainerStatus(), return the image name as it was given to us
as the image, and the image digested reference as the image reference.
In Server.ListImages(), be sure to only return tagged names in the
RepoTags field. In Server.ImageStatus(), also return canonical
references in the RepoDigests field.
In Server.PullImage(), be sure that we consistently return the same
image reference for an image, whether we ended up pulling it or not.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2017-07-12 16:41:38 +00:00
|
|
|
Image: image,
|
|
|
|
ImageRef: ctr.ImageRef(),
|
2017-09-22 23:44:02 +00:00
|
|
|
CreatedTime: ctrState.Created.UnixNano(),
|
|
|
|
Labels: ctr.Labels(),
|
|
|
|
Annotations: ctr.Annotations(),
|
|
|
|
CrioAnnotations: ctr.CrioAnnotations(),
|
|
|
|
Root: ctr.MountPoint(),
|
|
|
|
LogPath: ctr.LogPath(),
|
|
|
|
Sandbox: ctr.Sandbox(),
|
|
|
|
IP: sb.IP(),
|
2017-09-12 10:17:44 +00:00
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-01 10:37:10 +00:00
|
|
|
// GetInfoMux returns the mux used to serve info requests
|
|
|
|
func (s *Server) GetInfoMux() *bone.Mux {
|
2017-08-30 01:12:10 +00:00
|
|
|
mux := bone.New()
|
|
|
|
|
2017-08-30 23:25:48 +00:00
|
|
|
mux.Get("/info", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
2017-09-12 10:17:44 +00:00
|
|
|
ci := s.getInfo()
|
2017-08-30 23:25:48 +00:00
|
|
|
js, err := json.Marshal(ci)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(js)
|
|
|
|
}))
|
|
|
|
|
2017-08-30 01:12:10 +00:00
|
|
|
mux.Get("/containers/:id", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
containerID := bone.GetValue(req, "id")
|
2017-09-12 10:17:44 +00:00
|
|
|
ci, err := s.getContainerInfo(containerID, s.GetContainer, s.getInfraContainer, s.getSandbox)
|
|
|
|
if err != nil {
|
|
|
|
switch err {
|
|
|
|
case errCtrNotFound:
|
|
|
|
http.Error(w, fmt.Sprintf("can't find the container with id %s", containerID), http.StatusNotFound)
|
|
|
|
case errCtrStateNil:
|
|
|
|
http.Error(w, fmt.Sprintf("can't find container state for container with id %s", containerID), http.StatusInternalServerError)
|
|
|
|
case errSandboxNotFound:
|
|
|
|
http.Error(w, fmt.Sprintf("can't find the sandbox for container id %s", containerID), http.StatusNotFound)
|
|
|
|
default:
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2017-09-01 15:50:17 +00:00
|
|
|
}
|
2017-09-04 16:11:32 +00:00
|
|
|
return
|
|
|
|
}
|
2017-08-30 01:12:10 +00:00
|
|
|
js, err := json.Marshal(ci)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(js)
|
|
|
|
}))
|
|
|
|
|
2017-09-01 10:37:10 +00:00
|
|
|
return mux
|
2017-08-30 01:12:10 +00:00
|
|
|
}
|