2016-07-08 19:04:00 +00:00
|
|
|
package server
|
|
|
|
|
2016-07-29 22:35:10 +00:00
|
|
|
import (
|
2016-08-01 23:50:45 +00:00
|
|
|
"fmt"
|
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-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-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-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-08-01 17:39:42 +00:00
|
|
|
containers := make(map[string]*oci.Container)
|
2016-09-02 19:38:42 +00:00
|
|
|
netPlugin, err := ocicni.InitCNI("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-29 22:35:10 +00:00
|
|
|
return &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-07-29 22:35:10 +00:00
|
|
|
}, 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
|
|
|
|
containers map[string]*oci.Container
|
2016-08-01 23:05:37 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 01:30:05 +00:00
|
|
|
type sandbox struct {
|
2016-09-17 14:10:35 +00:00
|
|
|
name string
|
|
|
|
logDir string
|
|
|
|
labels map[string]string
|
|
|
|
containersLock sync.Mutex
|
|
|
|
containers map[string]*oci.Container
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sandbox) addContainer(c *oci.Container) {
|
|
|
|
s.containersLock.Lock()
|
|
|
|
s.containers[c.Name()] = c
|
|
|
|
s.containersLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sandbox) getContainer(name string) *oci.Container {
|
|
|
|
s.containersLock.Lock()
|
|
|
|
c := s.containers[name]
|
|
|
|
s.containersLock.Unlock()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sandbox) removeContainer(c *oci.Container) {
|
|
|
|
s.containersLock.Lock()
|
|
|
|
delete(s.containers, c.Name())
|
|
|
|
s.containersLock.Unlock()
|
2016-07-20 01:30:05 +00:00
|
|
|
}
|
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)
|
|
|
|
s.state.containers[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()
|
|
|
|
c := s.state.containers[name]
|
|
|
|
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)
|
|
|
|
delete(s.state.containers, c.Name())
|
2016-09-17 14:10:35 +00:00
|
|
|
s.stateLock.Unlock()
|
2016-08-25 19:14:59 +00:00
|
|
|
}
|