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-18 14:35:17 +00:00
|
|
|
"path/filepath"
|
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-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"
|
|
|
|
"github.com/kubernetes-incubator/cri-o/utils"
|
2016-09-20 08:27:11 +00:00
|
|
|
rspec "github.com/opencontainers/runtime-spec/specs-go"
|
2016-09-02 19:38:42 +00:00
|
|
|
"github.com/rajatchopra/ocicni"
|
2016-07-29 22:35:10 +00:00
|
|
|
)
|
|
|
|
|
2016-07-19 18:53:57 +00:00
|
|
|
const (
|
|
|
|
runtimeAPIVersion = "v1alpha1"
|
2016-08-04 14:34:30 +00:00
|
|
|
imageStore = "/var/lib/ocid/images"
|
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-09-21 08:59:37 +00:00
|
|
|
root string
|
2016-09-20 08:27:11 +00:00
|
|
|
runtime *oci.Runtime
|
|
|
|
sandboxDir string
|
2016-10-02 09:11:07 +00:00
|
|
|
pausePath string
|
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-07-08 19:04:00 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 07:39:13 +00:00
|
|
|
func (s *Server) loadSandbox(id string) error {
|
2016-09-20 08:27:11 +00:00
|
|
|
config, err := ioutil.ReadFile(filepath.Join(s.sandboxDir, 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-09-19 07:39:13 +00:00
|
|
|
s.addSandbox(&sandbox{
|
2016-09-20 08:27:11 +00:00
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
logDir: m.Annotations["ocid/log_path"],
|
|
|
|
labels: labels,
|
2016-09-19 07:39:13 +00:00
|
|
|
containers: oci.NewMemoryStore(),
|
|
|
|
})
|
|
|
|
sandboxPath := filepath.Join(s.sandboxDir, id)
|
2016-09-20 08:27:11 +00:00
|
|
|
scontainer, err := oci.NewContainer(m.Annotations["ocid/container_name"], sandboxPath, sandboxPath, labels, id, false)
|
2016-09-19 07:39:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.addContainer(scontainer)
|
|
|
|
if err = s.runtime.UpdateStatus(scontainer); err != nil {
|
|
|
|
logrus.Warnf("error updating status for container %s: %v", scontainer, err)
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) restore() error {
|
|
|
|
dir, err := ioutil.ReadDir(s.sandboxDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, v := range dir {
|
|
|
|
if err := s.loadSandbox(v.Name()); err != nil {
|
2016-09-18 14:35:17 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-09-19 07:39:13 +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 {
|
|
|
|
logrus.Warnf("name %s already reserved for %s", name, id)
|
|
|
|
return "", err
|
|
|
|
}
|
2016-09-26 22:31:06 +00:00
|
|
|
return "", fmt.Errorf("conflict, name %s already reserved", name)
|
2016-09-20 08:27:11 +00:00
|
|
|
}
|
|
|
|
return "", fmt.Errorf("error reserving name %s", name)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
logrus.Warnf("name %s already reserved for %s", name, id)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("conflict, name %s already reserved", name)
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("error reserving name %s", name)
|
|
|
|
}
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) releaseContainerName(name string) {
|
|
|
|
s.ctrNameIndex.Release(name)
|
|
|
|
}
|
|
|
|
|
2016-07-19 18:53:57 +00:00
|
|
|
// New creates a new Server with options provided
|
2016-10-02 09:11:07 +00:00
|
|
|
func New(runtimePath, root, sandboxDir, containerDir, conmonPath, pausePath string) (*Server, error) {
|
2016-08-01 23:50:45 +00:00
|
|
|
// TODO: This will go away later when we have wrapper process or systemd acting as
|
|
|
|
// subreaper.
|
|
|
|
if err := utils.SetSubreaper(1); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to set server as subreaper: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:57:41 +00:00
|
|
|
utils.StartReaper()
|
|
|
|
|
2016-08-04 14:34:30 +00:00
|
|
|
if err := os.MkdirAll(imageStore, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-18 15:00:37 +00:00
|
|
|
if err := os.MkdirAll(sandboxDir, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-28 19:49:46 +00:00
|
|
|
r, err := oci.New(runtimePath, containerDir, conmonPath)
|
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()
|
2016-09-02 19:38:42 +00:00
|
|
|
netPlugin, err := ocicni.InitCNI("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-18 14:35:17 +00:00
|
|
|
s := &Server{
|
2016-09-21 08:59:37 +00:00
|
|
|
root: root,
|
2016-08-01 23:05:37 +00:00
|
|
|
runtime: r,
|
2016-09-02 19:38:42 +00:00
|
|
|
netPlugin: netPlugin,
|
2016-08-01 23:05:37 +00:00
|
|
|
sandboxDir: sandboxDir,
|
2016-10-02 09:11:07 +00:00
|
|
|
pausePath: pausePath,
|
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
|
|
|
},
|
2016-09-18 14:35:17 +00:00
|
|
|
}
|
2016-10-04 23:00:04 +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-09-19 07:39:13 +00:00
|
|
|
if err := s.restore(); err != nil {
|
|
|
|
logrus.Warnf("couldn't restore: %v", err)
|
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-09-19 11:09:30 +00:00
|
|
|
s.state.containers.Add(c.Name(), c)
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getContainer(name string) *oci.Container {
|
|
|
|
s.stateLock.Lock()
|
2016-09-19 11:09:30 +00:00
|
|
|
c := s.state.containers.Get(name)
|
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-09-19 11:09:30 +00:00
|
|
|
s.state.containers.Delete(c.Name())
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Unlock()
|
2016-08-25 19:14:59 +00:00
|
|
|
}
|