2016-11-22 22:23:01 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-12-08 23:32:17 +00:00
|
|
|
"strconv"
|
|
|
|
"syscall"
|
2016-11-22 22:23:01 +00:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-10-18 14:48:33 +00:00
|
|
|
"github.com/containers/storage/storage"
|
2016-11-22 22:23:01 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
|
|
|
"github.com/opencontainers/runtime-tools/generate"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
|
|
|
)
|
|
|
|
|
2016-12-06 12:17:52 +00:00
|
|
|
func (s *Server) runContainer(container *oci.Container) error {
|
|
|
|
if err := s.runtime.CreateContainer(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.runtime.UpdateStatus(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.runtime.StartContainer(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.runtime.UpdateStatus(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-22 22:23:01 +00:00
|
|
|
// RunPodSandbox creates and runs a pod-level sandbox.
|
2016-11-23 17:16:21 +00:00
|
|
|
func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest) (resp *pb.RunPodSandboxResponse, err error) {
|
2016-11-22 22:23:01 +00:00
|
|
|
logrus.Debugf("RunPodSandboxRequest %+v", req)
|
2016-11-23 17:16:21 +00:00
|
|
|
var processLabel, mountLabel, netNsPath string
|
2016-11-22 22:23:01 +00:00
|
|
|
// process req.Name
|
|
|
|
name := req.GetConfig().GetMetadata().GetName()
|
|
|
|
if name == "" {
|
|
|
|
return nil, fmt.Errorf("PodSandboxConfig.Name should not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace := req.GetConfig().GetMetadata().GetNamespace()
|
|
|
|
attempt := req.GetConfig().GetMetadata().GetAttempt()
|
|
|
|
|
|
|
|
id, name, err := s.generatePodIDandName(name, namespace, attempt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-18 14:48:33 +00:00
|
|
|
_, containerName, err := s.generateContainerIDandName(name, "infra", attempt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-22 22:23:01 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
s.releasePodName(name)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err = s.podIDIndex.Add(id); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2016-10-18 14:48:33 +00:00
|
|
|
if err2 := s.podIDIndex.Delete(id); err2 != nil {
|
2016-11-22 22:23:01 +00:00
|
|
|
logrus.Warnf("couldn't delete pod id %s from idIndex", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-10-18 14:48:33 +00:00
|
|
|
podContainer, err := s.storage.CreatePodSandbox(s.imageContext,
|
|
|
|
name, id,
|
|
|
|
s.config.PauseImage, "",
|
|
|
|
containerName,
|
|
|
|
req.GetConfig().GetMetadata().GetName(),
|
|
|
|
req.GetConfig().GetMetadata().GetUid(),
|
|
|
|
namespace,
|
|
|
|
attempt,
|
|
|
|
nil)
|
|
|
|
if err == storage.ErrDuplicateName {
|
|
|
|
return nil, fmt.Errorf("pod sandbox with name %q already exists", name)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating pod sandbox with name %q: %v", name, err)
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2016-10-18 14:48:33 +00:00
|
|
|
if err2 := s.storage.RemovePodSandbox(id); err2 != nil {
|
|
|
|
logrus.Warnf("couldn't cleanup pod sandbox %q: %v", id, err2)
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-10-18 14:48:33 +00:00
|
|
|
// TODO: factor generating/updating the spec into something other projects can vendor
|
2016-11-22 22:23:01 +00:00
|
|
|
|
|
|
|
// creates a spec Generator with the default spec.
|
|
|
|
g := generate.New()
|
|
|
|
|
|
|
|
// setup defaults for the pod sandbox
|
|
|
|
g.SetRootReadonly(true)
|
2016-10-18 14:48:33 +00:00
|
|
|
if s.config.PauseCommand == "" {
|
|
|
|
if podContainer.Config != nil {
|
|
|
|
g.SetProcessArgs(podContainer.Config.Config.Cmd)
|
|
|
|
} else {
|
|
|
|
g.SetProcessArgs([]string{podInfraCommand})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
g.SetProcessArgs([]string{s.config.PauseCommand})
|
|
|
|
}
|
2016-11-22 22:23:01 +00:00
|
|
|
|
|
|
|
// set hostname
|
|
|
|
hostname := req.GetConfig().GetHostname()
|
|
|
|
if hostname != "" {
|
|
|
|
g.SetHostname(hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set log directory
|
|
|
|
logDir := req.GetConfig().GetLogDirectory()
|
|
|
|
if logDir == "" {
|
|
|
|
logDir = filepath.Join(s.config.LogDir, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set DNS options
|
|
|
|
dnsServers := req.GetConfig().GetDnsConfig().GetServers()
|
|
|
|
dnsSearches := req.GetConfig().GetDnsConfig().GetSearches()
|
|
|
|
dnsOptions := req.GetConfig().GetDnsConfig().GetOptions()
|
2016-10-18 14:48:33 +00:00
|
|
|
resolvPath := fmt.Sprintf("%s/resolv.conf", podContainer.RunDir)
|
2016-11-22 22:23:01 +00:00
|
|
|
err = parseDNSOptions(dnsServers, dnsSearches, dnsOptions, 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
|
|
|
|
}
|
|
|
|
|
2016-11-24 11:26:11 +00:00
|
|
|
g.AddBindMount(resolvPath, "/etc/resolv.conf", []string{"ro"})
|
2016-11-22 22:23:01 +00:00
|
|
|
|
|
|
|
// add metadata
|
|
|
|
metadata := req.GetConfig().GetMetadata()
|
|
|
|
metadataJSON, err := json.Marshal(metadata)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// add labels
|
|
|
|
labels := req.GetConfig().GetLabels()
|
|
|
|
labelsJSON, err := json.Marshal(labels)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// add annotations
|
|
|
|
annotations := req.GetConfig().GetAnnotations()
|
|
|
|
annotationsJSON, err := json.Marshal(annotations)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't use SELinux separation with Host Pid or IPC Namespace,
|
|
|
|
if !req.GetConfig().GetLinux().GetSecurityContext().GetNamespaceOptions().GetHostPid() && !req.GetConfig().GetLinux().GetSecurityContext().GetNamespaceOptions().GetHostIpc() {
|
|
|
|
processLabel, mountLabel, err = getSELinuxLabels(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
g.SetProcessSelinuxLabel(processLabel)
|
|
|
|
}
|
|
|
|
|
2016-12-08 23:32:17 +00:00
|
|
|
// create shm mount for the pod containers.
|
|
|
|
var shmPath string
|
|
|
|
if req.GetConfig().GetLinux().GetSecurityContext().GetNamespaceOptions().GetHostIpc() {
|
|
|
|
shmPath = "/dev/shm"
|
|
|
|
} else {
|
2016-10-18 14:48:33 +00:00
|
|
|
shmPath, err = setupShm(podContainer.RunDir, mountLabel)
|
2016-12-08 23:32:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if err2 := syscall.Unmount(shmPath, syscall.MNT_DETACH); err2 != nil {
|
|
|
|
logrus.Warnf("failed to unmount shm for pod: %v", err2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2016-10-18 14:48:33 +00:00
|
|
|
err = s.setPodSandboxMountLabel(id, mountLabel)
|
2016-11-22 22:23:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
s.releaseContainerName(containerName)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-10-18 14:48:33 +00:00
|
|
|
if err = s.ctrIDIndex.Add(id); err != nil {
|
2016-11-22 22:23:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2016-10-18 14:48:33 +00:00
|
|
|
if err2 := s.ctrIDIndex.Delete(id); err2 != nil {
|
|
|
|
logrus.Warnf("couldn't delete ctr id %s from idIndex", id)
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
g.AddAnnotation("ocid/metadata", string(metadataJSON))
|
|
|
|
g.AddAnnotation("ocid/labels", string(labelsJSON))
|
|
|
|
g.AddAnnotation("ocid/annotations", string(annotationsJSON))
|
|
|
|
g.AddAnnotation("ocid/log_path", logDir)
|
|
|
|
g.AddAnnotation("ocid/name", name)
|
2016-11-12 03:15:59 +00:00
|
|
|
g.AddAnnotation("ocid/container_type", containerTypeSandbox)
|
2016-10-18 14:48:33 +00:00
|
|
|
g.AddAnnotation("ocid/sandbox_id", id)
|
2016-11-22 22:23:01 +00:00
|
|
|
g.AddAnnotation("ocid/container_name", containerName)
|
2016-10-18 14:48:33 +00:00
|
|
|
g.AddAnnotation("ocid/container_id", id)
|
2016-12-08 23:32:17 +00:00
|
|
|
g.AddAnnotation("ocid/shm_path", shmPath)
|
2016-11-22 22:23:01 +00:00
|
|
|
|
|
|
|
sb := &sandbox{
|
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
logDir: logDir,
|
|
|
|
labels: labels,
|
|
|
|
annotations: annotations,
|
|
|
|
containers: oci.NewMemoryStore(),
|
|
|
|
processLabel: processLabel,
|
|
|
|
mountLabel: mountLabel,
|
|
|
|
metadata: metadata,
|
2016-12-08 23:32:17 +00:00
|
|
|
shmPath: shmPath,
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.addSandbox(sb)
|
|
|
|
|
|
|
|
for k, v := range annotations {
|
|
|
|
g.AddAnnotation(k, v)
|
|
|
|
}
|
|
|
|
|
2016-11-19 02:16:50 +00:00
|
|
|
// extract linux sysctls from annotations and pass down to oci runtime
|
|
|
|
safe, unsafe, err := SysctlsFromPodAnnotations(annotations)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, sysctl := range safe {
|
|
|
|
g.AddLinuxSysctl(sysctl.Name, sysctl.Value)
|
|
|
|
}
|
|
|
|
for _, sysctl := range unsafe {
|
|
|
|
g.AddLinuxSysctl(sysctl.Name, sysctl.Value)
|
|
|
|
}
|
|
|
|
|
2016-11-22 22:23:01 +00:00
|
|
|
// setup cgroup settings
|
|
|
|
cgroupParent := req.GetConfig().GetLinux().GetCgroupParent()
|
|
|
|
if cgroupParent != "" {
|
2016-12-19 23:06:27 +00:00
|
|
|
if s.config.CgroupManager == "systemd" {
|
2016-10-18 14:48:33 +00:00
|
|
|
cgPath := sb.cgroupParent + ":" + "ocid" + ":" + id
|
2016-12-19 23:06:27 +00:00
|
|
|
g.SetLinuxCgroupsPath(cgPath)
|
|
|
|
|
|
|
|
} else {
|
2016-10-18 14:48:33 +00:00
|
|
|
g.SetLinuxCgroupsPath(sb.cgroupParent + "/" + id)
|
2016-12-19 23:06:27 +00:00
|
|
|
|
|
|
|
}
|
2016-12-13 08:34:55 +00:00
|
|
|
sb.cgroupParent = cgroupParent
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-16 15:53:29 +00:00
|
|
|
hostNetwork := req.GetConfig().GetLinux().GetSecurityContext().GetNamespaceOptions().GetHostNetwork()
|
|
|
|
|
2016-11-22 22:23:01 +00:00
|
|
|
// set up namespaces
|
2017-01-16 15:53:29 +00:00
|
|
|
if hostNetwork {
|
2016-11-22 22:23:01 +00:00
|
|
|
err = g.RemoveLinuxNamespace("network")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-23 17:16:21 +00:00
|
|
|
|
|
|
|
netNsPath, err = hostNetNsPath()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Create the sandbox network namespace
|
|
|
|
if err = sb.netNsCreate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if netnsErr := sb.netNsRemove(); netnsErr != nil {
|
|
|
|
logrus.Warnf("Failed to remove networking namespace: %v", netnsErr)
|
|
|
|
}
|
2016-12-13 08:34:55 +00:00
|
|
|
}()
|
2016-11-23 17:16:21 +00:00
|
|
|
|
|
|
|
// Pass the created namespace path to the runtime
|
|
|
|
err = g.AddOrReplaceLinuxNamespace("network", sb.netNsPath())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
netNsPath = sb.netNsPath()
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetConfig().GetLinux().GetSecurityContext().GetNamespaceOptions().GetHostPid() {
|
|
|
|
err = g.RemoveLinuxNamespace("pid")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetConfig().GetLinux().GetSecurityContext().GetNamespaceOptions().GetHostIpc() {
|
|
|
|
err = g.RemoveLinuxNamespace("ipc")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-18 14:48:33 +00:00
|
|
|
saveOptions := generate.ExportOptions{}
|
|
|
|
mountPoint, err := s.storage.StartContainer(id)
|
2016-11-22 22:23:01 +00:00
|
|
|
if err != nil {
|
2016-10-18 14:48:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to mount container %s in pod sandbox %s(%s): %v", containerName, sb.name, id, err)
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
2016-10-18 14:48:33 +00:00
|
|
|
g.SetRootPath(mountPoint)
|
|
|
|
err = g.SaveToFile(filepath.Join(podContainer.Dir, "config.json"), saveOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to save template configuration for pod sandbox %s(%s): %v", sb.name, id, err)
|
|
|
|
}
|
|
|
|
if err = g.SaveToFile(filepath.Join(podContainer.RunDir, "config.json"), saveOptions); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to write runtime configuration for pod sandbox %s(%s): %v", sb.name, id, err)
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 14:48:33 +00:00
|
|
|
container, err := oci.NewContainer(id, containerName, podContainer.RunDir, logDir, sb.netNs(), labels, annotations, nil, nil, id, false)
|
2016-11-22 22:23:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.infraContainer = container
|
|
|
|
|
2016-11-25 15:24:23 +00:00
|
|
|
// setup the network
|
2017-01-16 15:53:29 +00:00
|
|
|
if !hostNetwork {
|
|
|
|
podNamespace := ""
|
|
|
|
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-11-25 15:24:23 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 12:17:52 +00:00
|
|
|
if err = s.runContainer(container); err != nil {
|
2016-11-22 22:23:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-23 17:16:21 +00:00
|
|
|
resp = &pb.RunPodSandboxResponse{PodSandboxId: &id}
|
2016-11-22 22:23:01 +00:00
|
|
|
logrus.Debugf("RunPodSandboxResponse: %+v", resp)
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2016-10-18 14:48:33 +00:00
|
|
|
func (s *Server) setPodSandboxMountLabel(id, mountLabel string) error {
|
|
|
|
storageMetadata, err := s.storage.GetContainerMetadata(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
storageMetadata.SetMountLabel(mountLabel)
|
|
|
|
err = s.storage.SetContainerMetadata(id, storageMetadata)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-22 22:23:01 +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))
|
|
|
|
}
|
2016-12-08 23:32:17 +00:00
|
|
|
|
2016-10-18 14:48:33 +00:00
|
|
|
func setupShm(podSandboxRunDir, mountLabel string) (shmPath string, err error) {
|
|
|
|
shmPath = filepath.Join(podSandboxRunDir, "shm")
|
2016-12-08 23:32:17 +00:00
|
|
|
if err = os.Mkdir(shmPath, 0700); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
shmOptions := "mode=1777,size=" + strconv.Itoa(defaultShmSize)
|
2016-12-09 17:37:47 +00:00
|
|
|
if err = syscall.Mount("shm", shmPath, "tmpfs", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV),
|
|
|
|
label.FormatMountLabel(shmOptions, mountLabel)); err != nil {
|
2016-12-08 23:32:17 +00:00
|
|
|
return "", fmt.Errorf("failed to mount shm tmpfs for pod: %v", err)
|
|
|
|
}
|
|
|
|
return shmPath, nil
|
|
|
|
}
|