Merge pull request #667 from 14rcole/containerserver-state

move container state to libkpod
This commit is contained in:
Antonio Murdaca 2017-07-20 16:42:31 +02:00 committed by GitHub
commit ab1fef9e1f
4 changed files with 51 additions and 12 deletions

View file

@ -1,6 +1,8 @@
package libkpod package libkpod
import ( import (
"sync"
"github.com/containers/image/types" "github.com/containers/image/types"
cstorage "github.com/containers/storage" cstorage "github.com/containers/storage"
"github.com/docker/docker/pkg/registrar" "github.com/docker/docker/pkg/registrar"
@ -17,6 +19,8 @@ type ContainerServer struct {
ctrNameIndex *registrar.Registrar ctrNameIndex *registrar.Registrar
ctrIDIndex *truncindex.TruncIndex ctrIDIndex *truncindex.TruncIndex
imageContext *types.SystemContext imageContext *types.SystemContext
stateLock sync.Locker
state *containerServerState
} }
// Runtime returns the oci runtime for the ContainerServer // Runtime returns the oci runtime for the ContainerServer
@ -51,6 +55,7 @@ func (c *ContainerServer) ImageContext() *types.SystemContext {
// New creates a new ContainerServer // New creates a new ContainerServer
func New(runtime *oci.Runtime, store cstorage.Store, storageImageServer storage.ImageServer, ctrNameIndex *registrar.Registrar, ctrIDIndex *truncindex.TruncIndex, imageContext *types.SystemContext) *ContainerServer { func New(runtime *oci.Runtime, store cstorage.Store, storageImageServer storage.ImageServer, ctrNameIndex *registrar.Registrar, ctrIDIndex *truncindex.TruncIndex, imageContext *types.SystemContext) *ContainerServer {
containers := oci.NewMemoryStore()
return &ContainerServer{ return &ContainerServer{
runtime: runtime, runtime: runtime,
store: store, store: store,
@ -58,5 +63,41 @@ func New(runtime *oci.Runtime, store cstorage.Store, storageImageServer storage.
ctrNameIndex: ctrNameIndex, ctrNameIndex: ctrNameIndex,
ctrIDIndex: ctrIDIndex, ctrIDIndex: ctrIDIndex,
imageContext: imageContext, imageContext: imageContext,
stateLock: new(sync.Mutex),
state: &containerServerState{
containers: containers,
},
} }
} }
type containerServerState struct {
containers oci.ContainerStorer
}
// AddContainer adds a container to the container state store
func (c *ContainerServer) AddContainer(ctr *oci.Container) {
c.stateLock.Lock()
defer c.stateLock.Unlock()
c.state.containers.Add(ctr.ID(), ctr)
}
// GetContainer returns a container by its ID
func (c *ContainerServer) GetContainer(id string) *oci.Container {
c.stateLock.Lock()
defer c.stateLock.Unlock()
return c.state.containers.Get(id)
}
// RemoveContainer removes a container from the container state store
func (c *ContainerServer) RemoveContainer(ctr *oci.Container) {
c.stateLock.Lock()
defer c.stateLock.Unlock()
c.state.containers.Delete(ctr.ID())
}
// ListContainers returns a list of all containers stored by the server state
func (c *ContainerServer) ListContainers() []*oci.Container {
c.stateLock.Lock()
defer c.stateLock.Unlock()
return c.state.containers.List()
}

View file

@ -16,7 +16,7 @@ func (s *Server) getContainerFromRequest(cid string) (*oci.Container, error) {
return nil, fmt.Errorf("container with ID starting with %s not found: %v", cid, err) return nil, fmt.Errorf("container with ID starting with %s not found: %v", cid, err)
} }
c := s.state.containers.Get(containerID) c := s.ContainerServer.GetContainer(containerID)
if c == nil { if c == nil {
return nil, fmt.Errorf("specified container not found: %s", containerID) return nil, fmt.Errorf("specified container not found: %s", containerID)
} }

View file

@ -31,7 +31,7 @@ func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersReque
logrus.Debugf("ListContainersRequest %+v", req) logrus.Debugf("ListContainersRequest %+v", req)
var ctrs []*pb.Container var ctrs []*pb.Container
filter := req.Filter filter := req.Filter
ctrList := s.state.containers.List() ctrList := s.ContainerServer.ListContainers()
// Filter using container id and pod id first. // Filter using container id and pod id first.
if filter != nil { if filter != nil {
@ -40,7 +40,7 @@ func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersReque
if err != nil { if err != nil {
return nil, err return nil, err
} }
c := s.state.containers.Get(id) c := s.ContainerServer.GetContainer(id)
if c != nil { if c != nil {
if filter.PodSandboxId != "" { if filter.PodSandboxId != "" {
if c.Sandbox() == filter.PodSandboxId { if c.Sandbox() == filter.PodSandboxId {

View file

@ -568,8 +568,8 @@ func New(config *Config) (*Server, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
sandboxes := make(map[string]*Sandbox) sandboxes := make(map[string]*Sandbox)
containers := oci.NewMemoryStore()
netPlugin, err := ocicni.InitCNI(config.NetworkDir, config.PluginDir) netPlugin, err := ocicni.InitCNI(config.NetworkDir, config.PluginDir)
if err != nil { if err != nil {
return nil, err return nil, err
@ -587,8 +587,7 @@ func New(config *Config) (*Server, error) {
hostportManager: hostportManager, hostportManager: hostportManager,
config: *config, config: *config,
state: &serverState{ state: &serverState{
sandboxes: sandboxes, sandboxes: sandboxes,
containers: containers,
}, },
seccompEnabled: seccomp.IsEnabled(), seccompEnabled: seccomp.IsEnabled(),
appArmorEnabled: apparmor.IsEnabled(), appArmorEnabled: apparmor.IsEnabled(),
@ -646,13 +645,12 @@ func New(config *Config) (*Server, error) {
}() }()
logrus.Debugf("sandboxes: %v", s.state.sandboxes) logrus.Debugf("sandboxes: %v", s.state.sandboxes)
logrus.Debugf("containers: %v", s.state.containers) logrus.Debugf("containers: %v", s.ContainerServer.ListContainers())
return s, nil return s, nil
} }
type serverState struct { type serverState struct {
sandboxes map[string]*Sandbox sandboxes map[string]*Sandbox
containers oci.ContainerStorer
} }
func (s *Server) addSandbox(sb *Sandbox) { func (s *Server) addSandbox(sb *Sandbox) {
@ -686,13 +684,13 @@ func (s *Server) addContainer(c *oci.Container) {
sandbox := s.state.sandboxes[c.Sandbox()] sandbox := s.state.sandboxes[c.Sandbox()]
// TODO(runcom): handle !ok above!!! otherwise it panics! // TODO(runcom): handle !ok above!!! otherwise it panics!
sandbox.AddContainer(c) sandbox.AddContainer(c)
s.state.containers.Add(c.ID(), c) s.ContainerServer.AddContainer(c)
s.stateLock.Unlock() s.stateLock.Unlock()
} }
func (s *Server) getContainer(id string) *oci.Container { func (s *Server) getContainer(id string) *oci.Container {
s.stateLock.Lock() s.stateLock.Lock()
c := s.state.containers.Get(id) c := s.ContainerServer.GetContainer(id)
s.stateLock.Unlock() s.stateLock.Unlock()
return c return c
} }
@ -712,6 +710,6 @@ func (s *Server) removeContainer(c *oci.Container) {
s.stateLock.Lock() s.stateLock.Lock()
sandbox := s.state.sandboxes[c.Sandbox()] sandbox := s.state.sandboxes[c.Sandbox()]
sandbox.RemoveContainer(c) sandbox.RemoveContainer(c)
s.state.containers.Delete(c.ID()) s.ContainerServer.RemoveContainer(c)
s.stateLock.Unlock() s.stateLock.Unlock()
} }