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-09 21:48:39 +00:00
|
|
|
"github.com/kubernetes-incubator/ocid/oci"
|
|
|
|
"github.com/kubernetes-incubator/ocid/utils"
|
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-08-01 23:05:37 +00:00
|
|
|
runtime *oci.Runtime
|
|
|
|
sandboxDir string
|
2016-09-17 14:10:35 +00:00
|
|
|
stateLock sync.Mutex
|
2016-08-01 23:05:37 +00:00
|
|
|
state *serverState
|
2016-09-02 19:38:42 +00:00
|
|
|
netPlugin ocicni.CNIPlugin
|
2016-07-08 19:04:00 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 14:35:17 +00:00
|
|
|
func (s *Server) loadSandboxes() error {
|
2016-09-19 07:21:14 +00:00
|
|
|
err := filepath.Walk(s.sandboxDir, func(path string, info os.FileInfo, err error) error {
|
2016-09-18 14:35:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if path == s.sandboxDir {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
metaJSON, err := ioutil.ReadFile(filepath.Join(path, "metadata.json"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var m metadata
|
2016-09-19 07:21:14 +00:00
|
|
|
if err2 := json.Unmarshal(metaJSON, &m); err2 != nil {
|
|
|
|
return err2
|
2016-09-18 14:35:17 +00:00
|
|
|
}
|
|
|
|
sname, err := filepath.Rel(s.sandboxDir, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.addSandbox(&sandbox{
|
|
|
|
name: sname,
|
|
|
|
logDir: m.LogDir,
|
|
|
|
labels: m.Labels,
|
2016-09-19 07:21:14 +00:00
|
|
|
containers: oci.NewMemoryStore(),
|
2016-09-18 14:35:17 +00:00
|
|
|
})
|
|
|
|
scontainer, err := oci.NewContainer(m.ContainerName, path, path, m.Labels, sname, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.addContainer(scontainer)
|
|
|
|
if err = s.runtime.UpdateStatus(scontainer); err != nil {
|
2016-09-18 16:37:29 +00:00
|
|
|
logrus.Warnf("error updating status for container %s: %v", scontainer, err)
|
2016-09-18 14:35:17 +00:00
|
|
|
}
|
|
|
|
return nil
|
2016-09-19 07:21:14 +00:00
|
|
|
})
|
|
|
|
return err
|
2016-09-18 14:35:17 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 18:53:57 +00:00
|
|
|
// New creates a new Server with options provided
|
2016-08-01 22:08:21 +00:00
|
|
|
func New(runtimePath, sandboxDir, containerDir 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-08-01 23:05:37 +00:00
|
|
|
r, err := oci.New(runtimePath, containerDir)
|
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-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,
|
|
|
|
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
|
|
|
}
|
|
|
|
if err := s.loadSandboxes(); err != nil {
|
|
|
|
logrus.Warnf("couldn't get sandboxes: %v", err)
|
|
|
|
}
|
|
|
|
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-08-01 23:05:37 +00:00
|
|
|
s.state.sandboxes[sb.name] = sb
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getSandbox(name string) *sandbox {
|
|
|
|
s.stateLock.Lock()
|
|
|
|
sb := s.state.sandboxes[name]
|
|
|
|
s.stateLock.Unlock()
|
|
|
|
return sb
|
2016-08-01 23:05:37 +00:00
|
|
|
}
|
2016-08-01 17:39:42 +00:00
|
|
|
|
|
|
|
func (s *Server) hasSandbox(name string) bool {
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Lock()
|
2016-08-01 17:39:42 +00:00
|
|
|
_, ok := s.state.sandboxes[name]
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Unlock()
|
2016-08-01 17:39:42 +00:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
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()]
|
|
|
|
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
|
|
|
}
|