2016-09-19 07:21:14 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-20 08:27:11 +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-09-19 07:21:14 +00:00
|
|
|
"golang.org/x/net/context"
|
2016-09-20 14:15:22 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
2016-09-19 07:21:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type sandbox struct {
|
2016-10-05 13:29:30 +00:00
|
|
|
id string
|
|
|
|
name string
|
|
|
|
logDir string
|
|
|
|
labels map[string]string
|
|
|
|
annotations map[string]string
|
|
|
|
containers oci.Store
|
|
|
|
processLabel string
|
|
|
|
mountLabel string
|
2016-10-06 23:51:13 +00:00
|
|
|
metadata *pb.PodSandboxMetadata
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 22:29:41 +00:00
|
|
|
const (
|
2016-09-22 23:02:46 +00:00
|
|
|
podInfraRootfs = "/var/lib/ocid/graph/vfs/pause"
|
|
|
|
podDefaultNamespace = "default"
|
2016-09-20 22:29:41 +00:00
|
|
|
)
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
func (s *sandbox) addContainer(c *oci.Container) {
|
|
|
|
s.containers.Add(c.Name(), c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sandbox) getContainer(name string) *oci.Container {
|
|
|
|
return s.containers.Get(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sandbox) removeContainer(c *oci.Container) {
|
|
|
|
s.containers.Delete(c.Name())
|
|
|
|
}
|
|
|
|
|
2016-09-30 22:23:38 +00:00
|
|
|
func (s *Server) generatePodIDandName(name string, namespace string, attempt uint32) (string, string, error) {
|
2016-09-20 08:27:11 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
id = stringid.GenerateNonCryptoID()
|
|
|
|
)
|
2016-09-22 23:02:46 +00:00
|
|
|
if namespace == "" {
|
|
|
|
namespace = podDefaultNamespace
|
|
|
|
}
|
2016-09-30 22:23:38 +00:00
|
|
|
|
|
|
|
if name, err = s.reservePodName(id, fmt.Sprintf("%s-%s-%v", namespace, name, attempt)); err != nil {
|
2016-09-20 08:27:11 +00:00
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
return id, name, err
|
|
|
|
}
|
|
|
|
|
2016-10-06 22:32:01 +00:00
|
|
|
type podSandboxRequest interface {
|
|
|
|
GetPodSandboxId() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getPodSandboxFromRequest(req podSandboxRequest) (*sandbox, error) {
|
|
|
|
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)
|
|
|
|
if sb == nil {
|
|
|
|
return nil, fmt.Errorf("specified sandbox not found: %s", sandboxID)
|
|
|
|
}
|
|
|
|
return sb, nil
|
|
|
|
}
|
|
|
|
|
2016-09-26 08:07:46 +00:00
|
|
|
// RunPodSandbox creates and runs a pod-level sandbox.
|
|
|
|
func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest) (*pb.RunPodSandboxResponse, error) {
|
2016-10-05 13:29:30 +00:00
|
|
|
var processLabel, mountLabel string
|
2016-09-19 07:21:14 +00:00
|
|
|
// process req.Name
|
|
|
|
name := req.GetConfig().GetMetadata().GetName()
|
|
|
|
if name == "" {
|
|
|
|
return nil, fmt.Errorf("PodSandboxConfig.Name should not be empty")
|
|
|
|
}
|
|
|
|
|
2016-09-22 23:02:46 +00:00
|
|
|
namespace := req.GetConfig().GetMetadata().GetNamespace()
|
2016-09-30 22:23:38 +00:00
|
|
|
attempt := req.GetConfig().GetMetadata().GetAttempt()
|
2016-09-22 23:02:46 +00:00
|
|
|
|
2016-09-20 08:27:11 +00:00
|
|
|
var err error
|
2016-09-30 22:23:38 +00:00
|
|
|
id, name, err := s.generatePodIDandName(name, namespace, attempt)
|
2016-09-20 08:27:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
podSandboxDir := filepath.Join(s.sandboxDir, id)
|
|
|
|
if _, err = os.Stat(podSandboxDir); err == nil {
|
2016-09-19 07:21:14 +00:00
|
|
|
return nil, fmt.Errorf("pod sandbox (%s) already exists", podSandboxDir)
|
|
|
|
}
|
|
|
|
|
2016-09-20 08:27:11 +00:00
|
|
|
if err = os.MkdirAll(podSandboxDir, 0755); err != nil {
|
2016-09-19 07:21:14 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if err2 := os.RemoveAll(podSandboxDir); err2 != nil {
|
|
|
|
logrus.Warnf("couldn't cleanup podSandboxDir %s: %v", podSandboxDir, err2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// creates a spec Generator with the default spec.
|
|
|
|
g := generate.New()
|
|
|
|
|
2016-09-21 08:59:37 +00:00
|
|
|
podInfraRootfs := filepath.Join(s.root, "graph/vfs/pause")
|
2016-09-19 07:21:14 +00:00
|
|
|
// setup defaults for the pod sandbox
|
2016-09-20 22:29:41 +00:00
|
|
|
g.SetRootPath(filepath.Join(podInfraRootfs, "rootfs"))
|
2016-09-19 07:21:14 +00:00
|
|
|
g.SetRootReadonly(true)
|
|
|
|
g.SetProcessArgs([]string{"/pause"})
|
|
|
|
|
2016-09-23 21:51:59 +00:00
|
|
|
// set hostname
|
2016-09-19 07:21:14 +00:00
|
|
|
hostname := req.GetConfig().GetHostname()
|
|
|
|
if hostname != "" {
|
|
|
|
g.SetHostname(hostname)
|
|
|
|
}
|
|
|
|
|
2016-09-23 21:51:59 +00:00
|
|
|
// set log directory
|
2016-09-19 07:21:14 +00:00
|
|
|
logDir := req.GetConfig().GetLogDirectory()
|
|
|
|
if logDir == "" {
|
2016-09-20 08:27:11 +00:00
|
|
|
logDir = fmt.Sprintf("/var/log/ocid/pods/%s", id)
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 21:51:59 +00:00
|
|
|
// set DNS options
|
2016-09-19 07:21:14 +00:00
|
|
|
dnsServers := req.GetConfig().GetDnsOptions().GetServers()
|
|
|
|
dnsSearches := req.GetConfig().GetDnsOptions().GetSearches()
|
|
|
|
resolvPath := fmt.Sprintf("%s/resolv.conf", podSandboxDir)
|
|
|
|
err = parseDNSOptions(dnsServers, dnsSearches, resolvPath)
|
|
|
|
if err != nil {
|
|
|
|
err1 := removeFile(resolvPath)
|
|
|
|
if err1 != nil {
|
|
|
|
err = err1
|
|
|
|
return nil, fmt.Errorf("%v; failed to remove %s: %v", err, resolvPath, err1)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
g.AddBindMount(resolvPath, "/etc/resolv.conf", "ro")
|
|
|
|
|
2016-09-23 21:51:59 +00:00
|
|
|
// add labels
|
2016-09-19 07:21:14 +00:00
|
|
|
labels := req.GetConfig().GetLabels()
|
2016-09-20 08:27:11 +00:00
|
|
|
labelsJSON, err := json.Marshal(labels)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-30 22:48:31 +00:00
|
|
|
|
|
|
|
// add annotations
|
|
|
|
annotations := req.GetConfig().GetAnnotations()
|
|
|
|
annotationsJSON, err := json.Marshal(annotations)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-04 23:50:29 +00:00
|
|
|
|
2016-10-07 11:24:49 +00:00
|
|
|
// Don't use SELinux separation with Host Pid or IPC Namespace,
|
|
|
|
if !req.GetConfig().GetLinux().GetNamespaceOptions().GetHostPid() && !req.GetConfig().GetLinux().GetNamespaceOptions().GetHostIpc() {
|
|
|
|
processLabel, mountLabel, err = getSELinuxLabels(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-05 13:29:30 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 23:50:29 +00:00
|
|
|
containerID, containerName, err := s.generateContainerIDandName(name, "infra", 0)
|
2016-09-20 08:27:11 +00:00
|
|
|
g.AddAnnotation("ocid/labels", string(labelsJSON))
|
2016-09-30 22:48:31 +00:00
|
|
|
g.AddAnnotation("ocid/annotations", string(annotationsJSON))
|
2016-09-20 08:27:11 +00:00
|
|
|
g.AddAnnotation("ocid/log_path", logDir)
|
|
|
|
g.AddAnnotation("ocid/name", name)
|
|
|
|
g.AddAnnotation("ocid/container_name", containerName)
|
2016-10-04 23:50:29 +00:00
|
|
|
g.AddAnnotation("ocid/container_id", containerID)
|
2016-09-19 07:21:14 +00:00
|
|
|
s.addSandbox(&sandbox{
|
2016-10-05 13:29:30 +00:00
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
logDir: logDir,
|
|
|
|
labels: labels,
|
|
|
|
annotations: annotations,
|
|
|
|
containers: oci.NewMemoryStore(),
|
|
|
|
processLabel: processLabel,
|
|
|
|
mountLabel: mountLabel,
|
2016-10-06 23:51:13 +00:00
|
|
|
metadata: req.GetConfig().GetMetadata(),
|
2016-09-19 07:21:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
for k, v := range annotations {
|
|
|
|
g.AddAnnotation(k, v)
|
|
|
|
}
|
|
|
|
|
2016-09-23 21:51:59 +00:00
|
|
|
// setup cgroup settings
|
2016-09-19 07:21:14 +00:00
|
|
|
cgroupParent := req.GetConfig().GetLinux().GetCgroupParent()
|
|
|
|
if cgroupParent != "" {
|
|
|
|
g.SetLinuxCgroupsPath(cgroupParent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up namespaces
|
|
|
|
if req.GetConfig().GetLinux().GetNamespaceOptions().GetHostNetwork() {
|
|
|
|
err = g.RemoveLinuxNamespace("network")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetConfig().GetLinux().GetNamespaceOptions().GetHostPid() {
|
|
|
|
err = g.RemoveLinuxNamespace("pid")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetConfig().GetLinux().GetNamespaceOptions().GetHostIpc() {
|
|
|
|
err = g.RemoveLinuxNamespace("ipc")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = g.SaveToFile(filepath.Join(podSandboxDir, "config.json"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-20 22:29:41 +00:00
|
|
|
if _, err = os.Stat(podInfraRootfs); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// TODO: Replace by rootfs creation API when it is ready
|
2016-10-02 09:11:07 +00:00
|
|
|
if err = utils.CreateInfraRootfs(podInfraRootfs, s.pausePath); err != nil {
|
2016-09-20 22:29:41 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-04 23:50:29 +00:00
|
|
|
container, err := oci.NewContainer(containerID, containerName, podSandboxDir, podSandboxDir, labels, id, false)
|
2016-09-19 07:21:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = s.runtime.CreateContainer(container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = s.runtime.UpdateStatus(container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-23 21:51:59 +00:00
|
|
|
// setup the network
|
2016-09-19 07:21:14 +00:00
|
|
|
podNamespace := ""
|
|
|
|
netnsPath, err := container.NetNsPath()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-20 08:27:11 +00:00
|
|
|
if err = s.netPlugin.SetUpPod(netnsPath, podNamespace, id, containerName); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create network for container %s in sandbox %s: %v", containerName, id, err)
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = s.runtime.StartContainer(container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.addContainer(container)
|
|
|
|
|
2016-09-20 08:16:59 +00:00
|
|
|
if err = s.podIDIndex.Add(id); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-19 07:21:14 +00:00
|
|
|
|
|
|
|
if err = s.runtime.UpdateStatus(container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-26 08:07:46 +00:00
|
|
|
return &pb.RunPodSandboxResponse{PodSandboxId: &id}, nil
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StopPodSandbox stops the sandbox. If there are any running containers in the
|
|
|
|
// sandbox, they should be force terminated.
|
|
|
|
func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxRequest) (*pb.StopPodSandboxResponse, error) {
|
2016-10-06 22:32:47 +00:00
|
|
|
sb, err := s.getPodSandboxFromRequest(req)
|
2016-09-26 22:12:12 +00:00
|
|
|
if err != nil {
|
2016-10-06 22:32:47 +00:00
|
|
|
return nil, err
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 21:34:31 +00:00
|
|
|
podInfraContainer := sb.name + "-infra"
|
2016-09-19 07:21:14 +00:00
|
|
|
for _, c := range sb.containers.List() {
|
|
|
|
if podInfraContainer == c.Name() {
|
|
|
|
podNamespace := ""
|
|
|
|
netnsPath, err := c.NetNsPath()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-06 22:32:47 +00:00
|
|
|
if err := s.netPlugin.TearDownPod(netnsPath, podNamespace, sb.id, podInfraContainer); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to destroy network for container %s in sandbox %s: %v", c.Name(), sb.id, err)
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-19 19:34:10 +00:00
|
|
|
cStatus := s.runtime.ContainerStatus(c)
|
2016-10-06 22:16:21 +00:00
|
|
|
if cStatus.Status != oci.ContainerStateStopped {
|
2016-09-19 19:34:10 +00:00
|
|
|
if err := s.runtime.StopContainer(c); err != nil {
|
2016-10-06 22:32:47 +00:00
|
|
|
return nil, fmt.Errorf("failed to stop container %s in sandbox %s: %v", c.Name(), sb.id, err)
|
2016-09-19 19:34:10 +00:00
|
|
|
}
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.StopPodSandboxResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemovePodSandbox deletes the sandbox. If there are any running containers in the
|
|
|
|
// sandbox, they should be force deleted.
|
|
|
|
func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxRequest) (*pb.RemovePodSandboxResponse, error) {
|
2016-10-06 22:32:47 +00:00
|
|
|
sb, err := s.getPodSandboxFromRequest(req)
|
2016-09-26 22:12:12 +00:00
|
|
|
if err != nil {
|
2016-10-06 22:32:47 +00:00
|
|
|
return nil, err
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 19:23:48 +00:00
|
|
|
podInfraContainerName := sb.name + "-infra"
|
|
|
|
var podInfraContainer *oci.Container
|
2016-09-19 07:21:14 +00:00
|
|
|
|
|
|
|
// Delete all the containers in the sandbox
|
|
|
|
for _, c := range sb.containers.List() {
|
2016-09-28 19:40:51 +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:40:51 +00:00
|
|
|
if err := s.runtime.StopContainer(c); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to stop container %s: %v", c.Name(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
if err := s.runtime.DeleteContainer(c); err != nil {
|
2016-10-06 22:32:47 +00:00
|
|
|
return nil, fmt.Errorf("failed to delete container %s in sandbox %s: %v", c.Name(), sb.id, err)
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
2016-09-28 19:40:51 +00:00
|
|
|
|
2016-09-30 19:23:48 +00:00
|
|
|
if podInfraContainerName == c.Name() {
|
|
|
|
podInfraContainer = c
|
2016-09-19 07:21:14 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-09-30 19:23:48 +00:00
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
containerDir := filepath.Join(s.runtime.ContainerDir(), c.Name())
|
|
|
|
if err := os.RemoveAll(containerDir); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to remove container %s directory: %v", c.Name(), err)
|
|
|
|
}
|
2016-09-30 19:23:48 +00:00
|
|
|
|
2016-10-04 23:50:29 +00:00
|
|
|
s.releaseContainerName(c.Name())
|
2016-09-30 19:23:48 +00:00
|
|
|
s.removeContainer(c)
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the files related to the sandbox
|
2016-10-06 22:32:47 +00:00
|
|
|
podSandboxDir := filepath.Join(s.sandboxDir, sb.id)
|
2016-09-19 07:21:14 +00:00
|
|
|
if err := os.RemoveAll(podSandboxDir); err != nil {
|
2016-10-06 22:32:47 +00:00
|
|
|
return nil, fmt.Errorf("failed to remove sandbox %s directory: %v", sb.id, err)
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
2016-10-04 23:50:29 +00:00
|
|
|
s.releaseContainerName(podInfraContainer.Name())
|
2016-09-30 19:23:48 +00:00
|
|
|
s.removeContainer(podInfraContainer)
|
2016-09-19 07:21:14 +00:00
|
|
|
|
2016-09-26 22:42:08 +00:00
|
|
|
s.releasePodName(sb.name)
|
2016-10-06 22:32:47 +00:00
|
|
|
s.removeSandbox(sb.id)
|
2016-09-26 22:42:08 +00:00
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
return &pb.RemovePodSandboxResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PodSandboxStatus returns the Status of the PodSandbox.
|
|
|
|
func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusRequest) (*pb.PodSandboxStatusResponse, error) {
|
2016-10-06 22:32:47 +00:00
|
|
|
sb, err := s.getPodSandboxFromRequest(req)
|
2016-09-26 22:12:12 +00:00
|
|
|
if err != nil {
|
2016-10-06 22:32:47 +00:00
|
|
|
return nil, err
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 21:47:47 +00:00
|
|
|
podInfraContainerName := sb.name + "-infra"
|
2016-09-19 07:21:14 +00:00
|
|
|
podInfraContainer := sb.getContainer(podInfraContainerName)
|
2016-09-26 22:12:12 +00:00
|
|
|
if err = s.runtime.UpdateStatus(podInfraContainer); err != nil {
|
2016-09-26 19:20:59 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-19 07:21:14 +00:00
|
|
|
|
|
|
|
cState := s.runtime.ContainerStatus(podInfraContainer)
|
|
|
|
created := cState.Created.Unix()
|
|
|
|
|
|
|
|
netNsPath, err := podInfraContainer.NetNsPath()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
podNamespace := ""
|
2016-10-06 22:32:47 +00:00
|
|
|
ip, err := s.netPlugin.GetContainerNetworkStatus(netNsPath, podNamespace, sb.id, podInfraContainerName)
|
2016-09-19 07:21:14 +00:00
|
|
|
if err != nil {
|
|
|
|
// ignore the error on network status
|
|
|
|
ip = ""
|
|
|
|
}
|
|
|
|
|
2016-09-26 18:18:35 +00:00
|
|
|
rStatus := pb.PodSandBoxState_NOTREADY
|
2016-10-06 22:16:21 +00:00
|
|
|
if cState.Status == oci.ContainerStateRunning {
|
2016-09-26 18:18:35 +00:00
|
|
|
rStatus = pb.PodSandBoxState_READY
|
|
|
|
}
|
|
|
|
|
2016-10-06 22:32:47 +00:00
|
|
|
sandboxID := sb.id
|
2016-09-19 07:21:14 +00:00
|
|
|
return &pb.PodSandboxStatusResponse{
|
|
|
|
Status: &pb.PodSandboxStatus{
|
2016-10-06 22:32:47 +00:00
|
|
|
Id: &sandboxID,
|
2016-09-19 07:21:14 +00:00
|
|
|
CreatedAt: int64Ptr(created),
|
|
|
|
Linux: &pb.LinuxPodSandboxStatus{
|
|
|
|
Namespaces: &pb.Namespace{
|
|
|
|
Network: sPtr(netNsPath),
|
|
|
|
},
|
|
|
|
},
|
2016-10-06 23:51:13 +00:00
|
|
|
Network: &pb.PodSandboxNetworkStatus{Ip: &ip},
|
|
|
|
State: &rStatus,
|
|
|
|
Labels: sb.labels,
|
|
|
|
Annotations: sb.annotations,
|
|
|
|
Metadata: sb.metadata,
|
2016-09-19 07:21:14 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-09-26 19:20:59 +00:00
|
|
|
// ListPodSandbox returns a list of SandBoxes.
|
2016-09-19 07:21:14 +00:00
|
|
|
func (s *Server) ListPodSandbox(context.Context, *pb.ListPodSandboxRequest) (*pb.ListPodSandboxResponse, error) {
|
2016-09-26 19:20:59 +00:00
|
|
|
var pods []*pb.PodSandbox
|
|
|
|
for _, sb := range s.state.sandboxes {
|
|
|
|
podInfraContainerName := sb.name + "-infra"
|
|
|
|
podInfraContainer := sb.getContainer(podInfraContainerName)
|
2016-09-27 08:40:08 +00:00
|
|
|
if podInfraContainer == nil {
|
|
|
|
// this can't really happen, but if it does because of a bug
|
|
|
|
// it's better not to panic
|
|
|
|
continue
|
|
|
|
}
|
2016-09-26 19:20:59 +00:00
|
|
|
if err := s.runtime.UpdateStatus(podInfraContainer); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cState := s.runtime.ContainerStatus(podInfraContainer)
|
|
|
|
created := cState.Created.Unix()
|
|
|
|
rStatus := pb.PodSandBoxState_NOTREADY
|
2016-10-06 22:16:21 +00:00
|
|
|
if cState.Status == oci.ContainerStateRunning {
|
2016-09-26 19:20:59 +00:00
|
|
|
rStatus = pb.PodSandBoxState_READY
|
|
|
|
}
|
|
|
|
|
|
|
|
pod := &pb.PodSandbox{
|
2016-10-06 23:51:13 +00:00
|
|
|
Id: &sb.id,
|
|
|
|
CreatedAt: int64Ptr(created),
|
|
|
|
State: &rStatus,
|
|
|
|
Labels: sb.labels,
|
|
|
|
Annotations: sb.annotations,
|
|
|
|
Metadata: sb.metadata,
|
2016-09-26 19:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pods = append(pods, pod)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.ListPodSandboxResponse{
|
|
|
|
Items: pods,
|
|
|
|
}, nil
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
2016-10-05 13:29:30 +00:00
|
|
|
|
|
|
|
func getSELinuxLabels(selinuxOptions *pb.SELinuxOption) (processLabel string, mountLabel string, err error) {
|
|
|
|
processLabel = ""
|
|
|
|
if selinuxOptions != nil {
|
|
|
|
user := selinuxOptions.GetUser()
|
|
|
|
if user == "" {
|
|
|
|
return "", "", fmt.Errorf("SELinuxOption.User is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
role := selinuxOptions.GetRole()
|
|
|
|
if role == "" {
|
|
|
|
return "", "", fmt.Errorf("SELinuxOption.Role is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
t := selinuxOptions.GetType()
|
|
|
|
if t == "" {
|
|
|
|
return "", "", fmt.Errorf("SELinuxOption.Type is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
level := selinuxOptions.GetLevel()
|
|
|
|
if level == "" {
|
|
|
|
return "", "", fmt.Errorf("SELinuxOption.Level is empty")
|
|
|
|
}
|
|
|
|
processLabel = fmt.Sprintf("%s:%s:%s:%s", user, role, t, level)
|
|
|
|
}
|
|
|
|
return label.InitLabels(label.DupSecOpt(processLabel))
|
|
|
|
}
|