2016-07-08 19:04:00 +00:00
|
|
|
package server
|
|
|
|
|
2016-07-29 22:35:10 +00:00
|
|
|
import (
|
2016-09-18 14:35:17 +00:00
|
|
|
"encoding/json"
|
2016-08-01 23:50:45 +00:00
|
|
|
"fmt"
|
2016-09-18 14:35:17 +00:00
|
|
|
"io/ioutil"
|
2016-08-04 14:34:30 +00:00
|
|
|
"os"
|
2016-09-17 14:10:35 +00:00
|
|
|
"sync"
|
2016-08-01 23:50:45 +00:00
|
|
|
|
2016-09-18 14:35:17 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-10-18 14:48:33 +00:00
|
|
|
"github.com/containers/image/types"
|
|
|
|
sstorage "github.com/containers/storage/storage"
|
2016-09-20 08:27:11 +00:00
|
|
|
"github.com/docker/docker/pkg/registrar"
|
|
|
|
"github.com/docker/docker/pkg/truncindex"
|
2016-09-26 23:55:12 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2017-01-19 21:36:21 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/pkg/ocicni"
|
2016-10-18 14:48:33 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/pkg/storage"
|
2016-11-29 12:34:15 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/server/apparmor"
|
2016-11-23 09:41:48 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/server/seccomp"
|
2016-10-05 13:29:30 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2016-09-20 08:27:11 +00:00
|
|
|
rspec "github.com/opencontainers/runtime-spec/specs-go"
|
2016-10-26 11:23:53 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
2016-07-29 22:35:10 +00:00
|
|
|
)
|
|
|
|
|
2016-07-19 18:53:57 +00:00
|
|
|
const (
|
|
|
|
runtimeAPIVersion = "v1alpha1"
|
2016-07-08 19:04:00 +00:00
|
|
|
)
|
|
|
|
|
2016-07-19 18:53:57 +00:00
|
|
|
// Server implements the RuntimeService and ImageService
|
|
|
|
type Server struct {
|
2016-10-10 09:57:40 +00:00
|
|
|
config Config
|
2016-09-20 08:27:11 +00:00
|
|
|
runtime *oci.Runtime
|
2016-10-18 14:48:33 +00:00
|
|
|
store sstorage.Store
|
|
|
|
images storage.ImageServer
|
|
|
|
storage storage.RuntimeServer
|
2016-09-20 08:27:11 +00:00
|
|
|
stateLock sync.Mutex
|
|
|
|
state *serverState
|
|
|
|
netPlugin ocicni.CNIPlugin
|
|
|
|
podNameIndex *registrar.Registrar
|
|
|
|
podIDIndex *truncindex.TruncIndex
|
2016-10-04 23:00:04 +00:00
|
|
|
ctrNameIndex *registrar.Registrar
|
|
|
|
ctrIDIndex *truncindex.TruncIndex
|
2016-10-18 14:48:33 +00:00
|
|
|
imageContext *types.SystemContext
|
2016-11-23 09:41:48 +00:00
|
|
|
|
|
|
|
seccompEnabled bool
|
|
|
|
seccompProfile seccomp.Seccomp
|
2016-11-29 12:34:15 +00:00
|
|
|
|
|
|
|
appArmorEnabled bool
|
2016-11-30 08:19:36 +00:00
|
|
|
appArmorProfile string
|
2016-07-08 19:04:00 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 14:20:04 +00:00
|
|
|
func (s *Server) loadContainer(id string) error {
|
2016-10-18 14:48:33 +00:00
|
|
|
config, err := s.store.GetFromContainerDirectory(id, "config.json")
|
2016-10-07 14:20:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var m rspec.Spec
|
|
|
|
if err = json.Unmarshal(config, &m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
labels := make(map[string]string)
|
|
|
|
if err = json.Unmarshal([]byte(m.Annotations["ocid/labels"]), &labels); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name := m.Annotations["ocid/name"]
|
|
|
|
name, err = s.reserveContainerName(id, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-22 10:26:19 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
s.releaseContainerName(name)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-10-26 11:23:53 +00:00
|
|
|
var metadata pb.ContainerMetadata
|
|
|
|
if err = json.Unmarshal([]byte(m.Annotations["ocid/metadata"]), &metadata); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-07 14:20:04 +00:00
|
|
|
sb := s.getSandbox(m.Annotations["ocid/sandbox_id"])
|
|
|
|
if sb == nil {
|
2016-12-22 10:26:19 +00:00
|
|
|
return fmt.Errorf("could not get sandbox with id %s, skipping", m.Annotations["ocid/sandbox_id"])
|
2016-10-07 14:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var tty bool
|
|
|
|
if v := m.Annotations["ocid/tty"]; v == "true" {
|
|
|
|
tty = true
|
|
|
|
}
|
2016-10-18 14:48:33 +00:00
|
|
|
containerPath, err := s.store.GetContainerRunDirectory(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-07 14:20:04 +00:00
|
|
|
|
2016-12-12 10:12:03 +00:00
|
|
|
var img *pb.ImageSpec
|
|
|
|
image, ok := m.Annotations["ocid/image"]
|
|
|
|
if ok {
|
|
|
|
img = &pb.ImageSpec{
|
2017-02-03 14:41:28 +00:00
|
|
|
Image: image,
|
2016-12-12 10:12:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
annotations := make(map[string]string)
|
|
|
|
if err = json.Unmarshal([]byte(m.Annotations["ocid/annotations"]), &annotations); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-23 17:16:21 +00:00
|
|
|
ctr, err := oci.NewContainer(id, name, containerPath, m.Annotations["ocid/log_path"], sb.netNs(), labels, annotations, img, &metadata, sb.id, tty)
|
2016-10-07 14:20:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = s.runtime.UpdateStatus(ctr); err != nil {
|
2016-12-22 10:26:19 +00:00
|
|
|
return fmt.Errorf("error updating status for container %s: %v", ctr.ID(), err)
|
2016-10-07 14:20:04 +00:00
|
|
|
}
|
2016-12-22 10:26:19 +00:00
|
|
|
s.addContainer(ctr)
|
2016-10-07 14:20:04 +00:00
|
|
|
if err = s.ctrIDIndex.Add(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-23 17:16:21 +00:00
|
|
|
func configNetNsPath(spec rspec.Spec) (string, error) {
|
|
|
|
for _, ns := range spec.Linux.Namespaces {
|
|
|
|
if ns.Type != rspec.NetworkNamespace {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ns.Path == "" {
|
|
|
|
return "", fmt.Errorf("empty networking namespace")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ns.Path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("missing networking namespace")
|
|
|
|
}
|
|
|
|
|
2016-09-19 07:39:13 +00:00
|
|
|
func (s *Server) loadSandbox(id string) error {
|
2016-10-18 14:48:33 +00:00
|
|
|
config, err := s.store.GetFromContainerDirectory(id, "config.json")
|
2016-09-19 07:39:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-20 08:27:11 +00:00
|
|
|
var m rspec.Spec
|
|
|
|
if err = json.Unmarshal(config, &m); err != nil {
|
2016-09-19 07:39:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-09-20 08:27:11 +00:00
|
|
|
labels := make(map[string]string)
|
|
|
|
if err = json.Unmarshal([]byte(m.Annotations["ocid/labels"]), &labels); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name := m.Annotations["ocid/name"]
|
2016-09-20 08:16:59 +00:00
|
|
|
name, err = s.reservePodName(id, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-22 10:26:19 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
s.releasePodName(name)
|
|
|
|
}
|
|
|
|
}()
|
2016-10-26 11:23:53 +00:00
|
|
|
var metadata pb.PodSandboxMetadata
|
|
|
|
if err = json.Unmarshal([]byte(m.Annotations["ocid/metadata"]), &metadata); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-05 13:29:30 +00:00
|
|
|
|
|
|
|
processLabel, mountLabel, err := label.InitLabels(label.DupSecOpt(m.Process.SelinuxLabel))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-24 12:01:01 +00:00
|
|
|
|
2016-11-01 05:49:21 +00:00
|
|
|
annotations := make(map[string]string)
|
|
|
|
if err = json.Unmarshal([]byte(m.Annotations["ocid/annotations"]), &annotations); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-24 12:01:01 +00:00
|
|
|
sb := &sandbox{
|
2016-10-05 13:29:30 +00:00
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
logDir: m.Annotations["ocid/log_path"],
|
|
|
|
labels: labels,
|
|
|
|
containers: oci.NewMemoryStore(),
|
|
|
|
processLabel: processLabel,
|
|
|
|
mountLabel: mountLabel,
|
2016-11-01 05:49:21 +00:00
|
|
|
annotations: annotations,
|
2016-10-26 11:23:53 +00:00
|
|
|
metadata: &metadata,
|
2016-12-08 23:32:17 +00:00
|
|
|
shmPath: m.Annotations["ocid/shm_path"],
|
2016-10-24 12:01:01 +00:00
|
|
|
}
|
2016-11-23 17:16:21 +00:00
|
|
|
|
|
|
|
// We add a netNS only if we can load a permanent one.
|
|
|
|
// Otherwise, the sandbox will live in the host namespace.
|
|
|
|
netNsPath, err := configNetNsPath(m)
|
|
|
|
if err == nil {
|
2016-12-12 15:37:03 +00:00
|
|
|
netNS, nsErr := netNsGet(netNsPath, sb.name)
|
2016-11-23 17:16:21 +00:00
|
|
|
// If we can't load the networking namespace
|
|
|
|
// because it's closed, we just set the sb netns
|
|
|
|
// pointer to nil. Otherwise we return an error.
|
|
|
|
if nsErr != nil && nsErr != errSandboxClosedNetNS {
|
|
|
|
return nsErr
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.netns = netNS
|
|
|
|
}
|
|
|
|
|
2016-10-24 12:01:01 +00:00
|
|
|
s.addSandbox(sb)
|
|
|
|
|
2016-12-22 10:26:19 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
s.removeSandbox(sb.id)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-10-18 14:48:33 +00:00
|
|
|
sandboxPath, err := s.store.GetContainerRunDirectory(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-05 13:29:30 +00:00
|
|
|
|
2016-10-07 14:20:04 +00:00
|
|
|
cname, err := s.reserveContainerName(m.Annotations["ocid/container_id"], m.Annotations["ocid/container_name"])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-22 10:26:19 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
s.releaseContainerName(cname)
|
|
|
|
}
|
|
|
|
}()
|
2016-11-23 17:16:21 +00:00
|
|
|
scontainer, err := oci.NewContainer(m.Annotations["ocid/container_id"], cname, sandboxPath, sandboxPath, sb.netNs(), labels, annotations, nil, nil, id, false)
|
2016-09-19 07:39:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = s.runtime.UpdateStatus(scontainer); err != nil {
|
2016-12-22 10:26:19 +00:00
|
|
|
return fmt.Errorf("error updating status for pod sandbox infra container %s: %v", scontainer.ID(), err)
|
2016-10-07 14:20:04 +00:00
|
|
|
}
|
2016-12-22 10:26:19 +00:00
|
|
|
if err = label.ReserveLabel(processLabel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sb.infraContainer = scontainer
|
2016-10-07 14:20:04 +00:00
|
|
|
if err = s.ctrIDIndex.Add(scontainer.ID()); err != nil {
|
|
|
|
return err
|
2016-09-19 07:39:13 +00:00
|
|
|
}
|
2016-09-20 08:16:59 +00:00
|
|
|
if err = s.podIDIndex.Add(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-19 07:39:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-07 14:20:04 +00:00
|
|
|
func (s *Server) restore() {
|
2016-10-18 14:48:33 +00:00
|
|
|
containers, err := s.store.Containers()
|
2016-10-08 12:36:14 +00:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2016-10-18 14:48:33 +00:00
|
|
|
logrus.Warnf("could not read containers and sandboxes: %v", err)
|
|
|
|
}
|
|
|
|
pods := map[string]*storage.RuntimeContainerMetadata{}
|
|
|
|
podContainers := map[string]*storage.RuntimeContainerMetadata{}
|
|
|
|
for _, container := range containers {
|
|
|
|
metadata, err2 := s.storage.GetContainerMetadata(container.ID)
|
|
|
|
if err2 != nil {
|
|
|
|
logrus.Warnf("error parsing metadata for %s: %v, ignoring", container.ID, err2)
|
2016-10-07 14:20:04 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-10-18 14:48:33 +00:00
|
|
|
if metadata.Pod {
|
|
|
|
pods[container.ID] = &metadata
|
|
|
|
} else {
|
|
|
|
podContainers[container.ID] = &metadata
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for containerID, metadata := range pods {
|
|
|
|
if err = s.loadSandbox(containerID); err != nil {
|
|
|
|
logrus.Warnf("could not restore sandbox %s container %s: %v", metadata.PodID, containerID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for containerID := range podContainers {
|
|
|
|
if err := s.loadContainer(containerID); err != nil {
|
|
|
|
logrus.Warnf("could not restore container %s: %v", containerID, err)
|
2016-10-07 14:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-18 14:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update makes changes to the server's state (lists of pods and containers) to
|
|
|
|
// reflect the list of pods and containers that are stored on disk, possibly
|
|
|
|
// having been modified by other parties
|
|
|
|
func (s *Server) Update() {
|
|
|
|
logrus.Debugf("updating sandbox and container information")
|
|
|
|
if err := s.update(); err != nil {
|
|
|
|
logrus.Errorf("error updating sandbox and container information: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) update() error {
|
|
|
|
containers, err := s.store.Containers()
|
2016-10-08 12:36:14 +00:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2016-10-18 14:48:33 +00:00
|
|
|
logrus.Warnf("could not read containers and sandboxes: %v", err)
|
|
|
|
return err
|
2016-10-07 14:20:04 +00:00
|
|
|
}
|
2016-10-18 14:48:33 +00:00
|
|
|
newPods := map[string]*storage.RuntimeContainerMetadata{}
|
|
|
|
oldPods := map[string]string{}
|
|
|
|
removedPods := map[string]string{}
|
|
|
|
newPodContainers := map[string]*storage.RuntimeContainerMetadata{}
|
|
|
|
oldPodContainers := map[string]string{}
|
|
|
|
removedPodContainers := map[string]string{}
|
|
|
|
for _, container := range containers {
|
|
|
|
if s.hasSandbox(container.ID) {
|
|
|
|
// FIXME: do we need to reload/update any info about the sandbox?
|
|
|
|
oldPods[container.ID] = container.ID
|
|
|
|
oldPodContainers[container.ID] = container.ID
|
2016-10-07 14:20:04 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-10-18 14:48:33 +00:00
|
|
|
if s.getContainer(container.ID) != nil {
|
|
|
|
// FIXME: do we need to reload/update any info about the container?
|
|
|
|
oldPodContainers[container.ID] = container.ID
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// not previously known, so figure out what it is
|
|
|
|
metadata, err2 := s.storage.GetContainerMetadata(container.ID)
|
|
|
|
if err2 != nil {
|
|
|
|
logrus.Errorf("error parsing metadata for %s: %v, ignoring", container.ID, err2)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if metadata.Pod {
|
|
|
|
newPods[container.ID] = &metadata
|
|
|
|
} else {
|
|
|
|
newPodContainers[container.ID] = &metadata
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.ctrIDIndex.Iterate(func(id string) {
|
|
|
|
if _, ok := oldPodContainers[id]; !ok {
|
|
|
|
// this container's ID wasn't in the updated list -> removed
|
|
|
|
removedPodContainers[id] = id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
for removedPodContainer := range removedPodContainers {
|
|
|
|
// forget this container
|
|
|
|
c := s.getContainer(removedPodContainer)
|
2016-12-22 10:03:32 +00:00
|
|
|
if c == nil {
|
|
|
|
logrus.Warnf("bad state when getting container removed %+v", removedPodContainer)
|
|
|
|
continue
|
|
|
|
}
|
2016-10-18 14:48:33 +00:00
|
|
|
s.releaseContainerName(c.Name())
|
|
|
|
s.removeContainer(c)
|
|
|
|
if err = s.ctrIDIndex.Delete(c.ID()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Debugf("forgetting removed pod container %s", c.ID())
|
|
|
|
}
|
|
|
|
s.podIDIndex.Iterate(func(id string) {
|
|
|
|
if _, ok := oldPods[id]; !ok {
|
|
|
|
// this pod's ID wasn't in the updated list -> removed
|
|
|
|
removedPods[id] = id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
for removedPod := range removedPods {
|
|
|
|
// forget this pod
|
|
|
|
sb := s.getSandbox(removedPod)
|
2016-12-21 21:11:51 +00:00
|
|
|
if sb == nil {
|
|
|
|
logrus.Warnf("bad state when getting pod to remove %+v", removedPod)
|
|
|
|
continue
|
|
|
|
}
|
2016-10-18 14:48:33 +00:00
|
|
|
podInfraContainer := sb.infraContainer
|
|
|
|
s.releaseContainerName(podInfraContainer.Name())
|
|
|
|
s.removeContainer(podInfraContainer)
|
|
|
|
if err = s.ctrIDIndex.Delete(podInfraContainer.ID()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sb.infraContainer = nil
|
|
|
|
s.releasePodName(sb.name)
|
|
|
|
s.removeSandbox(sb.id)
|
|
|
|
if err = s.podIDIndex.Delete(sb.id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Debugf("forgetting removed pod %s", sb.id)
|
|
|
|
}
|
|
|
|
for sandboxID := range newPods {
|
|
|
|
// load this pod
|
|
|
|
if err = s.loadSandbox(sandboxID); err != nil {
|
|
|
|
logrus.Warnf("could not load new pod sandbox %s: %v, ignoring", sandboxID, err)
|
|
|
|
} else {
|
|
|
|
logrus.Debugf("loaded new pod sandbox %s", sandboxID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for containerID := range newPodContainers {
|
|
|
|
// load this container
|
|
|
|
if err = s.loadContainer(containerID); err != nil {
|
|
|
|
logrus.Warnf("could not load new sandbox container %s: %v, ignoring", containerID, err)
|
|
|
|
} else {
|
|
|
|
logrus.Debugf("loaded new pod container %s", containerID, err)
|
2016-09-18 14:35:17 +00:00
|
|
|
}
|
2016-09-19 07:39:13 +00:00
|
|
|
}
|
2016-10-18 14:48:33 +00:00
|
|
|
return nil
|
2016-09-18 14:35:17 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 08:27:11 +00:00
|
|
|
func (s *Server) reservePodName(id, name string) (string, error) {
|
|
|
|
if err := s.podNameIndex.Reserve(name, id); err != nil {
|
|
|
|
if err == registrar.ErrNameReserved {
|
|
|
|
id, err := s.podNameIndex.Get(name)
|
|
|
|
if err != nil {
|
2016-12-11 16:46:37 +00:00
|
|
|
logrus.Warnf("conflict, pod name %q already reserved", name)
|
2016-09-20 08:27:11 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2016-12-11 16:46:37 +00:00
|
|
|
return "", fmt.Errorf("conflict, name %q already reserved for pod %q", name, id)
|
2016-09-20 08:27:11 +00:00
|
|
|
}
|
2016-12-11 16:46:37 +00:00
|
|
|
return "", fmt.Errorf("error reserving pod name %q", name)
|
2016-09-20 08:27:11 +00:00
|
|
|
}
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
2016-09-26 22:35:34 +00:00
|
|
|
func (s *Server) releasePodName(name string) {
|
|
|
|
s.podNameIndex.Release(name)
|
|
|
|
}
|
|
|
|
|
2016-10-04 23:00:04 +00:00
|
|
|
func (s *Server) reserveContainerName(id, name string) (string, error) {
|
|
|
|
if err := s.ctrNameIndex.Reserve(name, id); err != nil {
|
|
|
|
if err == registrar.ErrNameReserved {
|
|
|
|
id, err := s.ctrNameIndex.Get(name)
|
|
|
|
if err != nil {
|
2016-12-11 16:46:37 +00:00
|
|
|
logrus.Warnf("conflict, ctr name %q already reserved", name)
|
2016-10-04 23:00:04 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2016-12-11 16:46:37 +00:00
|
|
|
return "", fmt.Errorf("conflict, name %q already reserved for ctr %q", name, id)
|
2016-10-04 23:00:04 +00:00
|
|
|
}
|
2016-12-11 16:46:37 +00:00
|
|
|
return "", fmt.Errorf("error reserving ctr name %s", name)
|
2016-10-04 23:00:04 +00:00
|
|
|
}
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) releaseContainerName(name string) {
|
|
|
|
s.ctrNameIndex.Release(name)
|
|
|
|
}
|
|
|
|
|
2016-10-18 14:48:33 +00:00
|
|
|
// Shutdown attempts to shut down the server's storage cleanly
|
|
|
|
func (s *Server) Shutdown() error {
|
|
|
|
_, err := s.store.Shutdown(false)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-19 18:53:57 +00:00
|
|
|
// New creates a new Server with options provided
|
2016-10-10 09:57:40 +00:00
|
|
|
func New(config *Config) (*Server, error) {
|
2016-10-18 14:48:33 +00:00
|
|
|
store, err := sstorage.GetStore(sstorage.StoreOptions{
|
|
|
|
RunRoot: config.RunRoot,
|
|
|
|
GraphRoot: config.Root,
|
|
|
|
GraphDriverName: config.Storage,
|
|
|
|
GraphDriverOptions: config.StorageOptions,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imageService, err := storage.GetImageService(store, config.DefaultTransport)
|
|
|
|
if err != nil {
|
2016-08-04 14:34:30 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-16 18:19:44 +00:00
|
|
|
storageRuntimeService := storage.GetRuntimeService(imageService, config.PauseImage)
|
2016-10-18 14:48:33 +00:00
|
|
|
if err != nil {
|
2016-09-18 15:00:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-18 14:48:33 +00:00
|
|
|
r, err := oci.New(config.Runtime, config.Conmon, config.ConmonEnv, config.CgroupManager)
|
2016-07-19 18:53:57 +00:00
|
|
|
if err != nil {
|
2016-07-29 22:35:10 +00:00
|
|
|
return nil, err
|
2016-07-19 18:53:57 +00:00
|
|
|
}
|
2016-08-01 23:05:37 +00:00
|
|
|
sandboxes := make(map[string]*sandbox)
|
2016-09-19 11:09:30 +00:00
|
|
|
containers := oci.NewMemoryStore()
|
2017-01-24 17:12:51 +00:00
|
|
|
netPlugin, err := ocicni.InitCNI(config.NetworkDir, config.PluginDir)
|
2016-09-02 19:38:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-18 14:35:17 +00:00
|
|
|
s := &Server{
|
2016-10-10 09:57:40 +00:00
|
|
|
runtime: r,
|
2016-10-18 14:48:33 +00:00
|
|
|
store: store,
|
|
|
|
images: imageService,
|
|
|
|
storage: storageRuntimeService,
|
2016-10-10 09:57:40 +00:00
|
|
|
netPlugin: netPlugin,
|
|
|
|
config: *config,
|
2016-08-01 23:05:37 +00:00
|
|
|
state: &serverState{
|
2016-08-01 17:39:42 +00:00
|
|
|
sandboxes: sandboxes,
|
|
|
|
containers: containers,
|
2016-08-01 23:05:37 +00:00
|
|
|
},
|
2017-02-22 00:39:31 +00:00
|
|
|
seccompEnabled: seccomp.IsEnabled(),
|
2016-11-29 12:34:15 +00:00
|
|
|
appArmorEnabled: apparmor.IsEnabled(),
|
2016-12-12 07:55:17 +00:00
|
|
|
appArmorProfile: config.ApparmorProfile,
|
2016-11-23 09:41:48 +00:00
|
|
|
}
|
2017-02-22 00:21:04 +00:00
|
|
|
if s.seccompEnabled {
|
|
|
|
seccompProfile, err := ioutil.ReadFile(config.SeccompProfile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("opening seccomp profile (%s) failed: %v", config.SeccompProfile, err)
|
|
|
|
}
|
|
|
|
var seccompConfig seccomp.Seccomp
|
|
|
|
if err := json.Unmarshal(seccompProfile, &seccompConfig); err != nil {
|
|
|
|
return nil, fmt.Errorf("decoding seccomp profile failed: %v", err)
|
|
|
|
}
|
|
|
|
s.seccompProfile = seccompConfig
|
2016-09-18 14:35:17 +00:00
|
|
|
}
|
2016-10-04 23:00:04 +00:00
|
|
|
|
2016-12-12 07:55:17 +00:00
|
|
|
if s.appArmorEnabled && s.appArmorProfile == apparmor.DefaultApparmorProfile {
|
|
|
|
if err := apparmor.EnsureDefaultApparmorProfile(); err != nil {
|
|
|
|
return nil, fmt.Errorf("ensuring the default apparmor profile is installed failed: %v", err)
|
|
|
|
}
|
2016-11-29 12:34:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 08:27:11 +00:00
|
|
|
s.podIDIndex = truncindex.NewTruncIndex([]string{})
|
|
|
|
s.podNameIndex = registrar.NewRegistrar()
|
2016-10-04 23:00:04 +00:00
|
|
|
s.ctrIDIndex = truncindex.NewTruncIndex([]string{})
|
|
|
|
s.ctrNameIndex = registrar.NewRegistrar()
|
2016-10-18 14:48:33 +00:00
|
|
|
s.imageContext = &types.SystemContext{
|
|
|
|
SignaturePolicyPath: config.ImageConfig.SignaturePolicyPath,
|
|
|
|
}
|
2016-10-04 23:00:04 +00:00
|
|
|
|
2016-10-07 14:20:04 +00:00
|
|
|
s.restore()
|
|
|
|
|
2016-09-18 14:35:17 +00:00
|
|
|
logrus.Debugf("sandboxes: %v", s.state.sandboxes)
|
|
|
|
logrus.Debugf("containers: %v", s.state.containers)
|
|
|
|
return s, nil
|
2016-07-08 19:04:00 +00:00
|
|
|
}
|
2016-07-20 01:30:05 +00:00
|
|
|
|
2016-08-01 23:05:37 +00:00
|
|
|
type serverState struct {
|
2016-08-01 17:39:42 +00:00
|
|
|
sandboxes map[string]*sandbox
|
2016-09-19 11:09:30 +00:00
|
|
|
containers oci.Store
|
2016-08-01 23:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) addSandbox(sb *sandbox) {
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Lock()
|
2016-09-20 08:27:11 +00:00
|
|
|
s.state.sandboxes[sb.id] = sb
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-09-20 08:27:11 +00:00
|
|
|
func (s *Server) getSandbox(id string) *sandbox {
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Lock()
|
2016-09-20 08:27:11 +00:00
|
|
|
sb := s.state.sandboxes[id]
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Unlock()
|
|
|
|
return sb
|
2016-08-01 23:05:37 +00:00
|
|
|
}
|
2016-08-01 17:39:42 +00:00
|
|
|
|
2016-09-20 08:27:11 +00:00
|
|
|
func (s *Server) hasSandbox(id string) bool {
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Lock()
|
2016-09-20 08:27:11 +00:00
|
|
|
_, ok := s.state.sandboxes[id]
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Unlock()
|
2016-08-01 17:39:42 +00:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2016-09-27 08:40:08 +00:00
|
|
|
func (s *Server) removeSandbox(id string) {
|
|
|
|
s.stateLock.Lock()
|
|
|
|
delete(s.state.sandboxes, id)
|
|
|
|
s.stateLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-08-01 17:39:42 +00:00
|
|
|
func (s *Server) addContainer(c *oci.Container) {
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Lock()
|
2016-08-01 17:39:42 +00:00
|
|
|
sandbox := s.state.sandboxes[c.Sandbox()]
|
2016-09-20 08:27:11 +00:00
|
|
|
// TODO(runcom): handle !ok above!!! otherwise it panics!
|
2016-08-01 17:39:42 +00:00
|
|
|
sandbox.addContainer(c)
|
2016-10-04 23:50:29 +00:00
|
|
|
s.state.containers.Add(c.ID(), c)
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-10-04 23:50:29 +00:00
|
|
|
func (s *Server) getContainer(id string) *oci.Container {
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Lock()
|
2016-10-04 23:50:29 +00:00
|
|
|
c := s.state.containers.Get(id)
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Unlock()
|
|
|
|
return c
|
2016-08-01 17:39:42 +00:00
|
|
|
}
|
2016-08-25 19:14:59 +00:00
|
|
|
|
|
|
|
func (s *Server) removeContainer(c *oci.Container) {
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Lock()
|
2016-08-25 19:14:59 +00:00
|
|
|
sandbox := s.state.sandboxes[c.Sandbox()]
|
|
|
|
sandbox.removeContainer(c)
|
2016-10-04 23:50:29 +00:00
|
|
|
s.state.containers.Delete(c.ID())
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Unlock()
|
2016-08-25 19:14:59 +00:00
|
|
|
}
|