Move Sandbox state into libkpod
Signed-off-by: Matthew Heon <mheon@redhat.com>
This commit is contained in:
parent
8fd089c570
commit
ece055327a
5 changed files with 78 additions and 44 deletions
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/docker/docker/pkg/ioutils"
|
||||
"github.com/docker/docker/pkg/registrar"
|
||||
"github.com/docker/docker/pkg/truncindex"
|
||||
"github.com/kubernetes-incubator/cri-o/libkpod/sandbox"
|
||||
"github.com/kubernetes-incubator/cri-o/oci"
|
||||
"github.com/kubernetes-incubator/cri-o/pkg/storage"
|
||||
)
|
||||
|
@ -69,6 +70,7 @@ func New(runtime *oci.Runtime, store cstorage.Store, imageService storage.ImageS
|
|||
stateLock: new(sync.Mutex),
|
||||
state: &containerServerState{
|
||||
containers: oci.NewMemoryStore(),
|
||||
sandboxes: make(map[string]*sandbox.Sandbox),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -132,12 +134,15 @@ func (c *ContainerServer) Shutdown() error {
|
|||
|
||||
type containerServerState struct {
|
||||
containers oci.ContainerStorer
|
||||
sandboxes map[string]*sandbox.Sandbox
|
||||
}
|
||||
|
||||
// AddContainer adds a container to the container state store
|
||||
func (c *ContainerServer) AddContainer(ctr *oci.Container) {
|
||||
c.stateLock.Lock()
|
||||
defer c.stateLock.Unlock()
|
||||
sandbox := c.state.sandboxes[ctr.Sandbox()]
|
||||
sandbox.AddContainer(ctr)
|
||||
c.state.containers.Add(ctr.ID(), ctr)
|
||||
}
|
||||
|
||||
|
@ -148,10 +153,21 @@ func (c *ContainerServer) GetContainer(id string) *oci.Container {
|
|||
return c.state.containers.Get(id)
|
||||
}
|
||||
|
||||
// HasContainer checks if a container exists in the state
|
||||
func (c *ContainerServer) HasContainer(id string) bool {
|
||||
c.stateLock.Lock()
|
||||
defer c.stateLock.Unlock()
|
||||
ctr := c.state.containers.Get(id)
|
||||
return ctr != nil
|
||||
}
|
||||
|
||||
// RemoveContainer removes a container from the container state store
|
||||
func (c *ContainerServer) RemoveContainer(ctr *oci.Container) {
|
||||
c.stateLock.Lock()
|
||||
defer c.stateLock.Unlock()
|
||||
sbID := ctr.Sandbox()
|
||||
sb := c.state.sandboxes[sbID]
|
||||
sb.RemoveContainer(ctr)
|
||||
c.state.containers.Delete(ctr.ID())
|
||||
}
|
||||
|
||||
|
@ -161,3 +177,55 @@ func (c *ContainerServer) ListContainers() []*oci.Container {
|
|||
defer c.stateLock.Unlock()
|
||||
return c.state.containers.List()
|
||||
}
|
||||
|
||||
// AddSandbox adds a sandbox to the sandbox state store
|
||||
func (c *ContainerServer) AddSandbox(sb *sandbox.Sandbox) {
|
||||
c.stateLock.Lock()
|
||||
defer c.stateLock.Unlock()
|
||||
c.state.sandboxes[sb.ID()] = sb
|
||||
}
|
||||
|
||||
// GetSandbox returns a sandbox by its ID
|
||||
func (c *ContainerServer) GetSandbox(id string) *sandbox.Sandbox {
|
||||
c.stateLock.Lock()
|
||||
defer c.stateLock.Unlock()
|
||||
return c.state.sandboxes[id]
|
||||
}
|
||||
|
||||
// GetSandboxContainer returns a sandbox's infra container
|
||||
func (c *ContainerServer) GetSandboxContainer(id string) *oci.Container {
|
||||
c.stateLock.Lock()
|
||||
defer c.stateLock.Unlock()
|
||||
sb, ok := c.state.sandboxes[id]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return sb.InfraContainer()
|
||||
}
|
||||
|
||||
// HasSandbox checks if a sandbox exists in the state
|
||||
func (c *ContainerServer) HasSandbox(id string) bool {
|
||||
c.stateLock.Lock()
|
||||
defer c.stateLock.Unlock()
|
||||
_, ok := c.state.sandboxes[id]
|
||||
return ok
|
||||
}
|
||||
|
||||
// RemoveSandbox removes a sandbox from the state store
|
||||
func (c *ContainerServer) RemoveSandbox(id string) {
|
||||
c.stateLock.Lock()
|
||||
defer c.stateLock.Unlock()
|
||||
delete(c.state.sandboxes, id)
|
||||
}
|
||||
|
||||
// ListSandboxes lists all sandboxes in the state store
|
||||
func (c *ContainerServer) ListSandboxes() []*sandbox.Sandbox {
|
||||
c.stateLock.Lock()
|
||||
defer c.stateLock.Unlock()
|
||||
sbArray := make([]*sandbox.Sandbox, 0, len(c.state.sandboxes))
|
||||
for _, sb := range c.state.sandboxes {
|
||||
sbArray = append(sbArray, sb)
|
||||
}
|
||||
|
||||
return sbArray
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersReque
|
|||
}
|
||||
} else {
|
||||
if filter.PodSandboxId != "" {
|
||||
pod := s.state.sandboxes[filter.PodSandboxId]
|
||||
pod := s.ContainerServer.GetSandbox(filter.PodSandboxId)
|
||||
if pod == nil {
|
||||
ctrList = []*oci.Container{}
|
||||
} else {
|
||||
|
|
|
@ -32,7 +32,7 @@ func (s *Server) ListPodSandbox(ctx context.Context, req *pb.ListPodSandboxReque
|
|||
logrus.Debugf("ListPodSandboxRequest %+v", req)
|
||||
var pods []*pb.PodSandbox
|
||||
var podList []*sandbox.Sandbox
|
||||
for _, sb := range s.state.sandboxes {
|
||||
for _, sb := range s.ContainerServer.ListSandboxes() {
|
||||
podList = append(podList, sb)
|
||||
}
|
||||
|
||||
|
|
|
@ -120,7 +120,7 @@ func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxReque
|
|||
// StopAllPodSandboxes removes all pod sandboxes
|
||||
func (s *Server) StopAllPodSandboxes() {
|
||||
logrus.Debugf("StopAllPodSandboxes")
|
||||
for _, sb := range s.state.sandboxes {
|
||||
for _, sb := range s.ContainerServer.ListSandboxes() {
|
||||
pod := &pb.StopPodSandboxRequest{
|
||||
PodSandboxId: sb.ID(),
|
||||
}
|
||||
|
|
|
@ -56,9 +56,7 @@ type Server struct {
|
|||
config Config
|
||||
|
||||
storageRuntimeServer storage.RuntimeServer
|
||||
stateLock sync.Locker
|
||||
updateLock sync.RWMutex
|
||||
state *serverState
|
||||
netPlugin ocicni.CNIPlugin
|
||||
hostportManager hostport.HostPortManager
|
||||
podNameIndex *registrar.Registrar
|
||||
|
@ -512,7 +510,6 @@ func New(config *Config) (*Server, error) {
|
|||
|
||||
containerServer := libkpod.New(r, store, imageService, config.SignaturePolicyPath)
|
||||
|
||||
sandboxes := make(map[string]*sandbox.Sandbox)
|
||||
netPlugin, err := ocicni.InitCNI(config.NetworkDir, config.PluginDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -524,13 +521,9 @@ func New(config *Config) (*Server, error) {
|
|||
s := &Server{
|
||||
ContainerServer: *containerServer,
|
||||
storageRuntimeServer: storageRuntimeService,
|
||||
stateLock: new(sync.Mutex),
|
||||
netPlugin: netPlugin,
|
||||
hostportManager: hostportManager,
|
||||
config: *config,
|
||||
state: &serverState{
|
||||
sandboxes: sandboxes,
|
||||
},
|
||||
seccompEnabled: seccomp.IsEnabled(),
|
||||
appArmorEnabled: apparmor.IsEnabled(),
|
||||
appArmorProfile: config.ApparmorProfile,
|
||||
|
@ -586,60 +579,37 @@ func New(config *Config) (*Server, error) {
|
|||
s.stream.streamServer.Start(true)
|
||||
}()
|
||||
|
||||
logrus.Debugf("sandboxes: %v", s.state.sandboxes)
|
||||
logrus.Debugf("sandboxes: %v", s.ContainerServer.ListSandboxes())
|
||||
return s, nil
|
||||
}
|
||||
|
||||
type serverState struct {
|
||||
sandboxes map[string]*sandbox.Sandbox
|
||||
}
|
||||
|
||||
func (s *Server) addSandbox(sb *sandbox.Sandbox) {
|
||||
s.stateLock.Lock()
|
||||
s.state.sandboxes[sb.ID()] = sb
|
||||
s.stateLock.Unlock()
|
||||
s.ContainerServer.AddSandbox(sb)
|
||||
}
|
||||
|
||||
func (s *Server) getSandbox(id string) *sandbox.Sandbox {
|
||||
s.stateLock.Lock()
|
||||
sb := s.state.sandboxes[id]
|
||||
s.stateLock.Unlock()
|
||||
return sb
|
||||
return s.ContainerServer.GetSandbox(id)
|
||||
}
|
||||
|
||||
func (s *Server) hasSandbox(id string) bool {
|
||||
s.stateLock.Lock()
|
||||
_, ok := s.state.sandboxes[id]
|
||||
s.stateLock.Unlock()
|
||||
return ok
|
||||
return s.ContainerServer.HasSandbox(id)
|
||||
}
|
||||
|
||||
func (s *Server) removeSandbox(id string) {
|
||||
s.stateLock.Lock()
|
||||
delete(s.state.sandboxes, id)
|
||||
s.stateLock.Unlock()
|
||||
s.ContainerServer.RemoveSandbox(id)
|
||||
}
|
||||
|
||||
func (s *Server) addContainer(c *oci.Container) {
|
||||
s.stateLock.Lock()
|
||||
sandbox := s.state.sandboxes[c.Sandbox()]
|
||||
// TODO(runcom): handle !ok above!!! otherwise it panics!
|
||||
sandbox.AddContainer(c)
|
||||
s.ContainerServer.AddContainer(c)
|
||||
s.stateLock.Unlock()
|
||||
}
|
||||
|
||||
func (s *Server) getContainer(id string) *oci.Container {
|
||||
s.stateLock.Lock()
|
||||
c := s.ContainerServer.GetContainer(id)
|
||||
s.stateLock.Unlock()
|
||||
return c
|
||||
return s.ContainerServer.GetContainer(id)
|
||||
}
|
||||
|
||||
// GetSandboxContainer returns the infra container for a given sandbox
|
||||
func (s *Server) GetSandboxContainer(id string) *oci.Container {
|
||||
sb := s.getSandbox(id)
|
||||
return sb.InfraContainer()
|
||||
return s.ContainerServer.GetSandboxContainer(id)
|
||||
}
|
||||
|
||||
// GetContainer returns a container by its ID
|
||||
|
@ -648,11 +618,7 @@ func (s *Server) GetContainer(id string) *oci.Container {
|
|||
}
|
||||
|
||||
func (s *Server) removeContainer(c *oci.Container) {
|
||||
s.stateLock.Lock()
|
||||
sandbox := s.state.sandboxes[c.Sandbox()]
|
||||
sandbox.RemoveContainer(c)
|
||||
s.ContainerServer.RemoveContainer(c)
|
||||
s.stateLock.Unlock()
|
||||
}
|
||||
|
||||
func (s *Server) getPodSandboxFromRequest(podSandboxID string) (*sandbox.Sandbox, error) {
|
||||
|
|
Loading…
Reference in a new issue