2016-07-19 18:53:57 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-10-07 14:20:04 +00:00
|
|
|
"encoding/json"
|
2016-09-20 08:27:11 +00:00
|
|
|
"errors"
|
2016-07-20 01:30:05 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-07-19 18:53:57 +00:00
|
|
|
"path/filepath"
|
2016-10-05 13:29:30 +00:00
|
|
|
"syscall"
|
2016-07-19 18:53:57 +00:00
|
|
|
|
2016-08-29 18:49:26 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-10-04 23:01:22 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-09-26 23:55:12 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
|
|
|
"github.com/kubernetes-incubator/cri-o/utils"
|
2016-10-05 13:29:30 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2016-09-20 08:16:59 +00:00
|
|
|
"github.com/opencontainers/runtime-tools/generate"
|
2016-07-19 18:53:57 +00:00
|
|
|
"golang.org/x/net/context"
|
2016-10-14 00:01:31 +00:00
|
|
|
"k8s.io/kubernetes/pkg/fields"
|
2016-09-20 14:15:22 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
2016-07-19 18:53:57 +00:00
|
|
|
)
|
|
|
|
|
2016-10-04 23:01:22 +00:00
|
|
|
func (s *Server) generateContainerIDandName(podName string, name string, attempt uint32) (string, string, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
id = stringid.GenerateNonCryptoID()
|
|
|
|
)
|
2016-10-04 23:50:29 +00:00
|
|
|
nameStr := fmt.Sprintf("%s-%s-%v", podName, name, attempt)
|
2016-10-04 23:01:22 +00:00
|
|
|
if name == "infra" {
|
2016-10-04 23:50:29 +00:00
|
|
|
nameStr = fmt.Sprintf("%s-%s", podName, name)
|
2016-10-04 23:01:22 +00:00
|
|
|
}
|
|
|
|
if name, err = s.reserveContainerName(id, nameStr); err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
return id, name, err
|
|
|
|
}
|
|
|
|
|
2016-10-06 19:49:04 +00:00
|
|
|
type containerRequest interface {
|
|
|
|
GetContainerId() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getContainerFromRequest(req containerRequest) (*oci.Container, error) {
|
|
|
|
ctrID := req.GetContainerId()
|
|
|
|
if ctrID == "" {
|
|
|
|
return nil, fmt.Errorf("container ID should not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
containerID, err := s.ctrIDIndex.Get(ctrID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("container with ID starting with %s not found: %v", ctrID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := s.state.containers.Get(containerID)
|
|
|
|
if c == nil {
|
|
|
|
return nil, fmt.Errorf("specified container not found: %s", containerID)
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2016-07-19 18:53:57 +00:00
|
|
|
// CreateContainer creates a new container in specified PodSandbox
|
2016-10-06 18:03:05 +00:00
|
|
|
func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerRequest) (res *pb.CreateContainerResponse, err error) {
|
2016-09-26 22:24:33 +00:00
|
|
|
sbID := req.GetPodSandboxId()
|
|
|
|
if sbID == "" {
|
|
|
|
return nil, fmt.Errorf("PodSandboxId should not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
sandboxID, err := s.podIDIndex.Get(sbID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("PodSandbox with ID starting with %s not found: %v", sbID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sb := s.getSandbox(sandboxID)
|
2016-09-20 08:27:11 +00:00
|
|
|
if sb == nil {
|
2016-09-26 22:24:33 +00:00
|
|
|
return nil, fmt.Errorf("specified sandbox not found: %s", sandboxID)
|
2016-08-01 17:39:42 +00:00
|
|
|
}
|
2016-09-26 22:24:33 +00:00
|
|
|
|
2016-08-01 17:39:42 +00:00
|
|
|
// The config of the container
|
|
|
|
containerConfig := req.GetConfig()
|
|
|
|
if containerConfig == nil {
|
|
|
|
return nil, fmt.Errorf("CreateContainerRequest.ContainerConfig is nil")
|
|
|
|
}
|
|
|
|
|
2016-08-30 22:27:31 +00:00
|
|
|
name := containerConfig.GetMetadata().GetName()
|
2016-08-01 17:39:42 +00:00
|
|
|
if name == "" {
|
|
|
|
return nil, fmt.Errorf("CreateContainerRequest.ContainerConfig.Name is empty")
|
|
|
|
}
|
|
|
|
|
2016-10-04 23:50:29 +00:00
|
|
|
attempt := containerConfig.GetMetadata().GetAttempt()
|
|
|
|
containerID, containerName, err := s.generateContainerIDandName(sb.name, name, attempt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-01 17:39:42 +00:00
|
|
|
// containerDir is the dir for the container bundle.
|
2016-10-05 19:18:56 +00:00
|
|
|
containerDir := filepath.Join(s.runtime.ContainerDir(), containerID)
|
2016-10-06 18:03:05 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2016-10-11 12:19:51 +00:00
|
|
|
s.releaseContainerName(containerName)
|
2016-10-06 18:03:05 +00:00
|
|
|
err1 := os.RemoveAll(containerDir)
|
|
|
|
if err1 != nil {
|
|
|
|
logrus.Warnf("Failed to cleanup container directory: %v")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-08-01 17:39:42 +00:00
|
|
|
|
2016-09-26 22:24:33 +00:00
|
|
|
if _, err = os.Stat(containerDir); err == nil {
|
2016-08-01 17:39:42 +00:00
|
|
|
return nil, fmt.Errorf("container (%s) already exists", containerDir)
|
|
|
|
}
|
|
|
|
|
2016-09-26 22:24:33 +00:00
|
|
|
if err = os.MkdirAll(containerDir, 0755); err != nil {
|
2016-08-01 17:39:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-04 23:50:29 +00:00
|
|
|
container, err := s.createSandboxContainer(containerID, containerName, sb, req.GetSandboxConfig(), containerDir, containerConfig)
|
2016-09-19 07:21:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-08-01 17:39:42 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 18:03:05 +00:00
|
|
|
if err = s.runtime.CreateContainer(container); err != nil {
|
2016-09-19 07:21:14 +00:00
|
|
|
return nil, err
|
2016-08-01 17:39:42 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 18:03:05 +00:00
|
|
|
if err = s.runtime.UpdateStatus(container); err != nil {
|
2016-09-19 07:21:14 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.addContainer(container)
|
|
|
|
|
2016-10-06 18:03:05 +00:00
|
|
|
if err = s.ctrIDIndex.Add(containerID); err != nil {
|
|
|
|
s.removeContainer(container)
|
2016-10-05 18:31:41 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
return &pb.CreateContainerResponse{
|
2016-10-04 23:50:29 +00:00
|
|
|
ContainerId: &containerID,
|
2016-09-19 07:21:14 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-10-04 23:50:29 +00:00
|
|
|
func (s *Server) createSandboxContainer(containerID string, containerName string, sb *sandbox, SandboxConfig *pb.PodSandboxConfig, containerDir string, containerConfig *pb.ContainerConfig) (*oci.Container, error) {
|
2016-09-20 08:27:11 +00:00
|
|
|
if sb == nil {
|
|
|
|
return nil, errors.New("createSandboxContainer needs a sandbox")
|
|
|
|
}
|
2016-08-01 17:39:42 +00:00
|
|
|
// creates a spec Generator with the default spec.
|
|
|
|
specgen := generate.New()
|
|
|
|
|
|
|
|
// by default, the root path is an empty string.
|
|
|
|
// here set it to be "rootfs".
|
|
|
|
specgen.SetRootPath("rootfs")
|
|
|
|
|
|
|
|
args := containerConfig.GetArgs()
|
|
|
|
if args == nil {
|
|
|
|
args = []string{"/bin/sh"}
|
|
|
|
}
|
|
|
|
specgen.SetProcessArgs(args)
|
|
|
|
|
|
|
|
cwd := containerConfig.GetWorkingDir()
|
|
|
|
if cwd == "" {
|
|
|
|
cwd = "/"
|
|
|
|
}
|
|
|
|
specgen.SetProcessCwd(cwd)
|
|
|
|
|
|
|
|
envs := containerConfig.GetEnvs()
|
|
|
|
if envs != nil {
|
|
|
|
for _, item := range envs {
|
|
|
|
key := item.GetKey()
|
|
|
|
value := item.GetValue()
|
|
|
|
if key == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
env := fmt.Sprintf("%s=%s", key, value)
|
|
|
|
specgen.AddProcessEnv(env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mounts := containerConfig.GetMounts()
|
|
|
|
for _, mount := range mounts {
|
|
|
|
dest := mount.GetContainerPath()
|
|
|
|
if dest == "" {
|
|
|
|
return nil, fmt.Errorf("Mount.ContainerPath is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
src := mount.GetHostPath()
|
|
|
|
if src == "" {
|
|
|
|
return nil, fmt.Errorf("Mount.HostPath is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
options := "rw"
|
|
|
|
if mount.GetReadonly() {
|
|
|
|
options = "ro"
|
|
|
|
}
|
|
|
|
|
2016-10-05 13:29:30 +00:00
|
|
|
if mount.GetSelinuxRelabel() {
|
|
|
|
// Need a way in kubernetes to determine if the volume is shared or private
|
|
|
|
if err := label.Relabel(src, sb.mountLabel, true); err != nil && err != syscall.ENOTSUP {
|
|
|
|
return nil, fmt.Errorf("relabel failed %s: %v", src, err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-01 17:39:42 +00:00
|
|
|
|
|
|
|
specgen.AddBindMount(src, dest, options)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := containerConfig.GetLabels()
|
|
|
|
|
|
|
|
annotations := containerConfig.GetAnnotations()
|
|
|
|
if annotations != nil {
|
|
|
|
for k, v := range annotations {
|
|
|
|
specgen.AddAnnotation(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if containerConfig.GetPrivileged() {
|
|
|
|
specgen.SetupPrivileged(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if containerConfig.GetReadonlyRootfs() {
|
|
|
|
specgen.SetRootReadonly(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
logPath := containerConfig.GetLogPath()
|
|
|
|
|
|
|
|
if containerConfig.GetTty() {
|
|
|
|
specgen.SetProcessTerminal(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
linux := containerConfig.GetLinux()
|
|
|
|
if linux != nil {
|
|
|
|
resources := linux.GetResources()
|
|
|
|
if resources != nil {
|
|
|
|
cpuPeriod := resources.GetCpuPeriod()
|
|
|
|
if cpuPeriod != 0 {
|
|
|
|
specgen.SetLinuxResourcesCPUPeriod(uint64(cpuPeriod))
|
|
|
|
}
|
|
|
|
|
|
|
|
cpuQuota := resources.GetCpuQuota()
|
|
|
|
if cpuQuota != 0 {
|
|
|
|
specgen.SetLinuxResourcesCPUQuota(uint64(cpuQuota))
|
|
|
|
}
|
|
|
|
|
|
|
|
cpuShares := resources.GetCpuShares()
|
|
|
|
if cpuShares != 0 {
|
|
|
|
specgen.SetLinuxResourcesCPUShares(uint64(cpuShares))
|
|
|
|
}
|
|
|
|
|
|
|
|
memoryLimit := resources.GetMemoryLimitInBytes()
|
|
|
|
if memoryLimit != 0 {
|
|
|
|
specgen.SetLinuxResourcesMemoryLimit(uint64(memoryLimit))
|
|
|
|
}
|
|
|
|
|
|
|
|
oomScoreAdj := resources.GetOomScoreAdj()
|
|
|
|
specgen.SetLinuxResourcesOOMScoreAdj(int(oomScoreAdj))
|
|
|
|
}
|
|
|
|
|
|
|
|
capabilities := linux.GetCapabilities()
|
|
|
|
if capabilities != nil {
|
|
|
|
addCaps := capabilities.GetAddCapabilities()
|
|
|
|
if addCaps != nil {
|
|
|
|
for _, cap := range addCaps {
|
|
|
|
if err := specgen.AddProcessCapability(cap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dropCaps := capabilities.GetDropCapabilities()
|
|
|
|
if dropCaps != nil {
|
|
|
|
for _, cap := range dropCaps {
|
|
|
|
if err := specgen.DropProcessCapability(cap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 13:29:30 +00:00
|
|
|
specgen.SetProcessSelinuxLabel(sb.processLabel)
|
|
|
|
specgen.SetLinuxMountLabel(sb.mountLabel)
|
2016-08-01 17:39:42 +00:00
|
|
|
|
|
|
|
user := linux.GetUser()
|
|
|
|
if user != nil {
|
|
|
|
uid := user.GetUid()
|
|
|
|
specgen.SetProcessUID(uint32(uid))
|
|
|
|
|
|
|
|
gid := user.GetGid()
|
|
|
|
specgen.SetProcessGID(uint32(gid))
|
|
|
|
|
|
|
|
groups := user.GetAdditionalGids()
|
|
|
|
if groups != nil {
|
|
|
|
for _, group := range groups {
|
|
|
|
specgen.AddProcessAdditionalGid(uint32(group))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-29 19:00:53 +00:00
|
|
|
// Join the namespace paths for the pod sandbox container.
|
2016-09-20 08:27:11 +00:00
|
|
|
podContainerName := sb.name + "-infra"
|
2016-10-04 23:50:29 +00:00
|
|
|
podInfraContainer := sb.getContainer(podContainerName)
|
2016-08-29 18:49:26 +00:00
|
|
|
podInfraState := s.runtime.ContainerStatus(podInfraContainer)
|
|
|
|
|
|
|
|
logrus.Infof("pod container state %v", podInfraState)
|
|
|
|
|
2016-08-29 19:00:53 +00:00
|
|
|
for nsType, nsFile := range map[string]string{
|
|
|
|
"ipc": "ipc",
|
|
|
|
"uts": "uts",
|
|
|
|
"network": "net",
|
|
|
|
} {
|
|
|
|
nsPath := fmt.Sprintf("/proc/%d/ns/%s", podInfraState.Pid, nsFile)
|
2016-09-20 14:15:22 +00:00
|
|
|
if err := specgen.AddOrReplaceLinuxNamespace(nsType, nsPath); err != nil {
|
2016-09-19 07:21:14 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-29 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 14:20:04 +00:00
|
|
|
specgen.AddAnnotation("ocid/name", containerName)
|
|
|
|
specgen.AddAnnotation("ocid/sandbox_id", sb.id)
|
|
|
|
specgen.AddAnnotation("ocid/log_path", logPath)
|
|
|
|
specgen.AddAnnotation("ocid/tty", fmt.Sprintf("%v", containerConfig.GetTty()))
|
|
|
|
labelsJSON, err := json.Marshal(labels)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
specgen.AddAnnotation("ocid/labels", string(labelsJSON))
|
|
|
|
|
|
|
|
if err = specgen.SaveToFile(filepath.Join(containerDir, "config.json")); err != nil {
|
2016-08-01 17:39:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
imageSpec := containerConfig.GetImage()
|
|
|
|
if imageSpec == nil {
|
|
|
|
return nil, fmt.Errorf("CreateContainerRequest.ContainerConfig.Image is nil")
|
2016-08-01 17:39:42 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
image := imageSpec.GetImage()
|
|
|
|
if image == "" {
|
|
|
|
return nil, fmt.Errorf("CreateContainerRequest.ContainerConfig.Image.Image is empty")
|
2016-08-01 17:39:42 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
// TODO: copy the rootfs into the bundle.
|
|
|
|
// Currently, utils.CreateFakeRootfs is used to populate the rootfs.
|
2016-10-07 14:20:04 +00:00
|
|
|
if err = utils.CreateFakeRootfs(containerDir, image); err != nil {
|
2016-08-01 17:39:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-04 23:50:29 +00:00
|
|
|
container, err := oci.NewContainer(containerID, containerName, containerDir, logPath, labels, sb.id, containerConfig.GetTty())
|
2016-09-19 07:21:14 +00:00
|
|
|
if err != nil {
|
2016-09-13 20:51:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
return container, nil
|
2016-07-19 18:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartContainer starts the container.
|
2016-08-18 22:52:50 +00:00
|
|
|
func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerRequest) (*pb.StartContainerResponse, error) {
|
2016-10-06 19:49:59 +00:00
|
|
|
c, err := s.getContainerFromRequest(req)
|
2016-10-05 18:55:45 +00:00
|
|
|
if err != nil {
|
2016-10-06 19:49:59 +00:00
|
|
|
return nil, err
|
2016-08-18 22:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.runtime.StartContainer(c); err != nil {
|
2016-10-06 19:49:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to start container %s: %v", c.ID(), err)
|
2016-08-18 22:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.StartContainerResponse{}, nil
|
2016-07-19 18:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StopContainer stops a running container with a grace period (i.e., timeout).
|
2016-08-22 23:43:35 +00:00
|
|
|
func (s *Server) StopContainer(ctx context.Context, req *pb.StopContainerRequest) (*pb.StopContainerResponse, error) {
|
2016-10-06 19:49:59 +00:00
|
|
|
c, err := s.getContainerFromRequest(req)
|
2016-10-05 18:55:45 +00:00
|
|
|
if err != nil {
|
2016-10-06 19:49:59 +00:00
|
|
|
return nil, err
|
2016-08-22 23:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.runtime.StopContainer(c); err != nil {
|
2016-10-06 19:49:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to stop container %s: %v", c.ID(), err)
|
2016-08-22 23:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.StopContainerResponse{}, nil
|
2016-07-19 18:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveContainer removes the container. If the container is running, the container
|
|
|
|
// should be force removed.
|
2016-08-22 23:47:11 +00:00
|
|
|
func (s *Server) RemoveContainer(ctx context.Context, req *pb.RemoveContainerRequest) (*pb.RemoveContainerResponse, error) {
|
2016-10-06 19:49:59 +00:00
|
|
|
c, err := s.getContainerFromRequest(req)
|
2016-10-05 18:55:45 +00:00
|
|
|
if err != nil {
|
2016-10-06 19:49:59 +00:00
|
|
|
return nil, err
|
2016-08-22 23:47:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-28 19:24:12 +00:00
|
|
|
if err := s.runtime.UpdateStatus(c); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to update container state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cState := s.runtime.ContainerStatus(c)
|
2016-10-06 22:16:21 +00:00
|
|
|
if cState.Status == oci.ContainerStateCreated || cState.Status == oci.ContainerStateRunning {
|
2016-09-28 19:24:12 +00:00
|
|
|
if err := s.runtime.StopContainer(c); err != nil {
|
2016-10-06 19:49:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to stop container %s: %v", c.ID(), err)
|
2016-09-28 19:24:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 23:47:11 +00:00
|
|
|
if err := s.runtime.DeleteContainer(c); err != nil {
|
2016-10-06 19:49:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to delete container %s: %v", c.ID(), err)
|
2016-08-22 23:47:11 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 19:49:59 +00:00
|
|
|
containerDir := filepath.Join(s.runtime.ContainerDir(), c.ID())
|
2016-08-25 18:18:08 +00:00
|
|
|
if err := os.RemoveAll(containerDir); err != nil {
|
2016-10-06 19:49:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to remove container %s directory: %v", c.ID(), err)
|
2016-08-25 18:18:08 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 23:50:29 +00:00
|
|
|
s.releaseContainerName(c.Name())
|
2016-08-25 19:15:20 +00:00
|
|
|
s.removeContainer(c)
|
|
|
|
|
2016-10-05 18:31:41 +00:00
|
|
|
if err := s.ctrIDIndex.Delete(c.ID()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-22 23:47:11 +00:00
|
|
|
return &pb.RemoveContainerResponse{}, nil
|
2016-07-19 18:53:57 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 17:07:36 +00:00
|
|
|
// filterContainer returns whether passed container matches filtering criteria
|
|
|
|
func filterContainer(c *pb.Container, filter *pb.ContainerFilter) bool {
|
|
|
|
if filter != nil {
|
|
|
|
if filter.State != nil {
|
|
|
|
if *c.State != *filter.State {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2016-10-14 00:01:31 +00:00
|
|
|
if filter.LabelSelector != nil {
|
|
|
|
sel := fields.SelectorFromSet(filter.LabelSelector)
|
|
|
|
if !sel.Matches(fields.Set(c.Labels)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2016-10-13 17:07:36 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-07-19 18:53:57 +00:00
|
|
|
// ListContainers lists all containers by filters.
|
2016-09-27 22:17:41 +00:00
|
|
|
func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersRequest) (*pb.ListContainersResponse, error) {
|
|
|
|
var ctrs []*pb.Container
|
2016-10-13 17:07:36 +00:00
|
|
|
filter := req.Filter
|
|
|
|
ctrList := s.state.containers.List()
|
|
|
|
|
|
|
|
// Filter using container id and pod id first.
|
|
|
|
if filter != nil {
|
|
|
|
if filter.Id != nil {
|
|
|
|
c := s.state.containers.Get(*filter.Id)
|
|
|
|
if c != nil {
|
|
|
|
if filter.PodSandboxId != nil {
|
|
|
|
if c.Sandbox() == *filter.PodSandboxId {
|
|
|
|
ctrList = []*oci.Container{c}
|
|
|
|
} else {
|
|
|
|
ctrList = []*oci.Container{}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ctrList = []*oci.Container{c}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if filter.PodSandboxId != nil {
|
|
|
|
pod := s.state.sandboxes[*filter.PodSandboxId]
|
|
|
|
if pod == nil {
|
|
|
|
ctrList = []*oci.Container{}
|
|
|
|
} else {
|
|
|
|
ctrList = pod.containers.List()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ctr := range ctrList {
|
2016-09-27 22:17:41 +00:00
|
|
|
if err := s.runtime.UpdateStatus(ctr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
podSandboxID := ctr.Sandbox()
|
|
|
|
cState := s.runtime.ContainerStatus(ctr)
|
|
|
|
created := cState.Created.Unix()
|
|
|
|
rState := pb.ContainerState_UNKNOWN
|
2016-10-08 12:55:25 +00:00
|
|
|
cID := ctr.ID()
|
2016-09-27 22:17:41 +00:00
|
|
|
|
|
|
|
c := &pb.Container{
|
2016-10-08 12:55:25 +00:00
|
|
|
Id: &cID,
|
2016-09-27 22:17:41 +00:00
|
|
|
PodSandboxId: &podSandboxID,
|
|
|
|
CreatedAt: int64Ptr(created),
|
2016-10-14 00:01:31 +00:00
|
|
|
Labels: ctr.Labels(),
|
2016-09-27 22:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch cState.Status {
|
2016-10-06 22:16:21 +00:00
|
|
|
case oci.ContainerStateCreated:
|
2016-09-27 22:17:41 +00:00
|
|
|
rState = pb.ContainerState_CREATED
|
2016-10-06 22:16:21 +00:00
|
|
|
case oci.ContainerStateRunning:
|
2016-09-27 22:17:41 +00:00
|
|
|
rState = pb.ContainerState_RUNNING
|
2016-10-06 22:16:21 +00:00
|
|
|
case oci.ContainerStateStopped:
|
2016-09-27 22:17:41 +00:00
|
|
|
rState = pb.ContainerState_EXITED
|
|
|
|
}
|
|
|
|
c.State = &rState
|
|
|
|
|
2016-10-13 17:07:36 +00:00
|
|
|
// Filter by other criteria such as state and labels.
|
|
|
|
if filterContainer(c, req.Filter) {
|
|
|
|
ctrs = append(ctrs, c)
|
|
|
|
}
|
2016-09-27 22:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.ListContainersResponse{
|
|
|
|
Containers: ctrs,
|
|
|
|
}, nil
|
2016-07-19 18:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerStatus returns status of the container.
|
2016-09-12 22:03:03 +00:00
|
|
|
func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusRequest) (*pb.ContainerStatusResponse, error) {
|
2016-10-06 19:49:59 +00:00
|
|
|
c, err := s.getContainerFromRequest(req)
|
2016-10-05 18:55:45 +00:00
|
|
|
if err != nil {
|
2016-10-06 19:49:59 +00:00
|
|
|
return nil, err
|
2016-09-12 22:03:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.runtime.UpdateStatus(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-06 19:49:59 +00:00
|
|
|
containerID := c.ID()
|
2016-09-12 22:43:30 +00:00
|
|
|
csr := &pb.ContainerStatusResponse{
|
2016-09-12 22:03:03 +00:00
|
|
|
Status: &pb.ContainerStatus{
|
2016-10-04 23:50:29 +00:00
|
|
|
Id: &containerID,
|
2016-09-12 22:03:03 +00:00
|
|
|
},
|
2016-09-12 22:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cState := s.runtime.ContainerStatus(c)
|
|
|
|
rStatus := pb.ContainerState_UNKNOWN
|
|
|
|
|
|
|
|
switch cState.Status {
|
2016-10-06 22:16:21 +00:00
|
|
|
case oci.ContainerStateCreated:
|
2016-09-12 22:43:30 +00:00
|
|
|
rStatus = pb.ContainerState_CREATED
|
|
|
|
created := cState.Created.Unix()
|
|
|
|
csr.Status.CreatedAt = int64Ptr(created)
|
2016-10-06 22:16:21 +00:00
|
|
|
case oci.ContainerStateRunning:
|
2016-09-12 22:43:30 +00:00
|
|
|
rStatus = pb.ContainerState_RUNNING
|
|
|
|
created := cState.Created.Unix()
|
|
|
|
csr.Status.CreatedAt = int64Ptr(created)
|
|
|
|
started := cState.Started.Unix()
|
|
|
|
csr.Status.StartedAt = int64Ptr(started)
|
2016-10-06 22:16:21 +00:00
|
|
|
case oci.ContainerStateStopped:
|
2016-09-12 22:43:30 +00:00
|
|
|
rStatus = pb.ContainerState_EXITED
|
|
|
|
created := cState.Created.Unix()
|
|
|
|
csr.Status.CreatedAt = int64Ptr(created)
|
|
|
|
started := cState.Started.Unix()
|
|
|
|
csr.Status.StartedAt = int64Ptr(started)
|
2016-09-16 23:32:19 +00:00
|
|
|
finished := cState.Finished.Unix()
|
|
|
|
csr.Status.FinishedAt = int64Ptr(finished)
|
|
|
|
csr.Status.ExitCode = int32Ptr(cState.ExitCode)
|
2016-09-12 22:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
csr.Status.State = &rStatus
|
|
|
|
|
|
|
|
return csr, nil
|
2016-07-19 18:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exec executes the command in the container.
|
|
|
|
func (s *Server) Exec(pb.RuntimeService_ExecServer) error {
|
|
|
|
return nil
|
|
|
|
}
|