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/ioutils"
|
||||||
"github.com/docker/docker/pkg/registrar"
|
"github.com/docker/docker/pkg/registrar"
|
||||||
"github.com/docker/docker/pkg/truncindex"
|
"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/oci"
|
||||||
"github.com/kubernetes-incubator/cri-o/pkg/storage"
|
"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),
|
stateLock: new(sync.Mutex),
|
||||||
state: &containerServerState{
|
state: &containerServerState{
|
||||||
containers: oci.NewMemoryStore(),
|
containers: oci.NewMemoryStore(),
|
||||||
|
sandboxes: make(map[string]*sandbox.Sandbox),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,12 +134,15 @@ func (c *ContainerServer) Shutdown() error {
|
||||||
|
|
||||||
type containerServerState struct {
|
type containerServerState struct {
|
||||||
containers oci.ContainerStorer
|
containers oci.ContainerStorer
|
||||||
|
sandboxes map[string]*sandbox.Sandbox
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddContainer adds a container to the container state store
|
// AddContainer adds a container to the container state store
|
||||||
func (c *ContainerServer) AddContainer(ctr *oci.Container) {
|
func (c *ContainerServer) AddContainer(ctr *oci.Container) {
|
||||||
c.stateLock.Lock()
|
c.stateLock.Lock()
|
||||||
defer c.stateLock.Unlock()
|
defer c.stateLock.Unlock()
|
||||||
|
sandbox := c.state.sandboxes[ctr.Sandbox()]
|
||||||
|
sandbox.AddContainer(ctr)
|
||||||
c.state.containers.Add(ctr.ID(), 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)
|
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
|
// RemoveContainer removes a container from the container state store
|
||||||
func (c *ContainerServer) RemoveContainer(ctr *oci.Container) {
|
func (c *ContainerServer) RemoveContainer(ctr *oci.Container) {
|
||||||
c.stateLock.Lock()
|
c.stateLock.Lock()
|
||||||
defer c.stateLock.Unlock()
|
defer c.stateLock.Unlock()
|
||||||
|
sbID := ctr.Sandbox()
|
||||||
|
sb := c.state.sandboxes[sbID]
|
||||||
|
sb.RemoveContainer(ctr)
|
||||||
c.state.containers.Delete(ctr.ID())
|
c.state.containers.Delete(ctr.ID())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,3 +177,55 @@ func (c *ContainerServer) ListContainers() []*oci.Container {
|
||||||
defer c.stateLock.Unlock()
|
defer c.stateLock.Unlock()
|
||||||
return c.state.containers.List()
|
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 {
|
} else {
|
||||||
if filter.PodSandboxId != "" {
|
if filter.PodSandboxId != "" {
|
||||||
pod := s.state.sandboxes[filter.PodSandboxId]
|
pod := s.ContainerServer.GetSandbox(filter.PodSandboxId)
|
||||||
if pod == nil {
|
if pod == nil {
|
||||||
ctrList = []*oci.Container{}
|
ctrList = []*oci.Container{}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -32,7 +32,7 @@ func (s *Server) ListPodSandbox(ctx context.Context, req *pb.ListPodSandboxReque
|
||||||
logrus.Debugf("ListPodSandboxRequest %+v", req)
|
logrus.Debugf("ListPodSandboxRequest %+v", req)
|
||||||
var pods []*pb.PodSandbox
|
var pods []*pb.PodSandbox
|
||||||
var podList []*sandbox.Sandbox
|
var podList []*sandbox.Sandbox
|
||||||
for _, sb := range s.state.sandboxes {
|
for _, sb := range s.ContainerServer.ListSandboxes() {
|
||||||
podList = append(podList, sb)
|
podList = append(podList, sb)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -120,7 +120,7 @@ func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxReque
|
||||||
// StopAllPodSandboxes removes all pod sandboxes
|
// StopAllPodSandboxes removes all pod sandboxes
|
||||||
func (s *Server) StopAllPodSandboxes() {
|
func (s *Server) StopAllPodSandboxes() {
|
||||||
logrus.Debugf("StopAllPodSandboxes")
|
logrus.Debugf("StopAllPodSandboxes")
|
||||||
for _, sb := range s.state.sandboxes {
|
for _, sb := range s.ContainerServer.ListSandboxes() {
|
||||||
pod := &pb.StopPodSandboxRequest{
|
pod := &pb.StopPodSandboxRequest{
|
||||||
PodSandboxId: sb.ID(),
|
PodSandboxId: sb.ID(),
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,9 +56,7 @@ type Server struct {
|
||||||
config Config
|
config Config
|
||||||
|
|
||||||
storageRuntimeServer storage.RuntimeServer
|
storageRuntimeServer storage.RuntimeServer
|
||||||
stateLock sync.Locker
|
|
||||||
updateLock sync.RWMutex
|
updateLock sync.RWMutex
|
||||||
state *serverState
|
|
||||||
netPlugin ocicni.CNIPlugin
|
netPlugin ocicni.CNIPlugin
|
||||||
hostportManager hostport.HostPortManager
|
hostportManager hostport.HostPortManager
|
||||||
podNameIndex *registrar.Registrar
|
podNameIndex *registrar.Registrar
|
||||||
|
@ -512,7 +510,6 @@ func New(config *Config) (*Server, error) {
|
||||||
|
|
||||||
containerServer := libkpod.New(r, store, imageService, config.SignaturePolicyPath)
|
containerServer := libkpod.New(r, store, imageService, config.SignaturePolicyPath)
|
||||||
|
|
||||||
sandboxes := make(map[string]*sandbox.Sandbox)
|
|
||||||
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
|
||||||
|
@ -524,13 +521,9 @@ func New(config *Config) (*Server, error) {
|
||||||
s := &Server{
|
s := &Server{
|
||||||
ContainerServer: *containerServer,
|
ContainerServer: *containerServer,
|
||||||
storageRuntimeServer: storageRuntimeService,
|
storageRuntimeServer: storageRuntimeService,
|
||||||
stateLock: new(sync.Mutex),
|
|
||||||
netPlugin: netPlugin,
|
netPlugin: netPlugin,
|
||||||
hostportManager: hostportManager,
|
hostportManager: hostportManager,
|
||||||
config: *config,
|
config: *config,
|
||||||
state: &serverState{
|
|
||||||
sandboxes: sandboxes,
|
|
||||||
},
|
|
||||||
seccompEnabled: seccomp.IsEnabled(),
|
seccompEnabled: seccomp.IsEnabled(),
|
||||||
appArmorEnabled: apparmor.IsEnabled(),
|
appArmorEnabled: apparmor.IsEnabled(),
|
||||||
appArmorProfile: config.ApparmorProfile,
|
appArmorProfile: config.ApparmorProfile,
|
||||||
|
@ -586,60 +579,37 @@ func New(config *Config) (*Server, error) {
|
||||||
s.stream.streamServer.Start(true)
|
s.stream.streamServer.Start(true)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
logrus.Debugf("sandboxes: %v", s.state.sandboxes)
|
logrus.Debugf("sandboxes: %v", s.ContainerServer.ListSandboxes())
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type serverState struct {
|
|
||||||
sandboxes map[string]*sandbox.Sandbox
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) addSandbox(sb *sandbox.Sandbox) {
|
func (s *Server) addSandbox(sb *sandbox.Sandbox) {
|
||||||
s.stateLock.Lock()
|
s.ContainerServer.AddSandbox(sb)
|
||||||
s.state.sandboxes[sb.ID()] = sb
|
|
||||||
s.stateLock.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) getSandbox(id string) *sandbox.Sandbox {
|
func (s *Server) getSandbox(id string) *sandbox.Sandbox {
|
||||||
s.stateLock.Lock()
|
return s.ContainerServer.GetSandbox(id)
|
||||||
sb := s.state.sandboxes[id]
|
|
||||||
s.stateLock.Unlock()
|
|
||||||
return sb
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) hasSandbox(id string) bool {
|
func (s *Server) hasSandbox(id string) bool {
|
||||||
s.stateLock.Lock()
|
return s.ContainerServer.HasSandbox(id)
|
||||||
_, ok := s.state.sandboxes[id]
|
|
||||||
s.stateLock.Unlock()
|
|
||||||
return ok
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) removeSandbox(id string) {
|
func (s *Server) removeSandbox(id string) {
|
||||||
s.stateLock.Lock()
|
s.ContainerServer.RemoveSandbox(id)
|
||||||
delete(s.state.sandboxes, id)
|
|
||||||
s.stateLock.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) addContainer(c *oci.Container) {
|
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.ContainerServer.AddContainer(c)
|
||||||
s.stateLock.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) getContainer(id string) *oci.Container {
|
func (s *Server) getContainer(id string) *oci.Container {
|
||||||
s.stateLock.Lock()
|
return s.ContainerServer.GetContainer(id)
|
||||||
c := s.ContainerServer.GetContainer(id)
|
|
||||||
s.stateLock.Unlock()
|
|
||||||
return c
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSandboxContainer returns the infra container for a given sandbox
|
// GetSandboxContainer returns the infra container for a given sandbox
|
||||||
func (s *Server) GetSandboxContainer(id string) *oci.Container {
|
func (s *Server) GetSandboxContainer(id string) *oci.Container {
|
||||||
sb := s.getSandbox(id)
|
return s.ContainerServer.GetSandboxContainer(id)
|
||||||
return sb.InfraContainer()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetContainer returns a container by its 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) {
|
func (s *Server) removeContainer(c *oci.Container) {
|
||||||
s.stateLock.Lock()
|
|
||||||
sandbox := s.state.sandboxes[c.Sandbox()]
|
|
||||||
sandbox.RemoveContainer(c)
|
|
||||||
s.ContainerServer.RemoveContainer(c)
|
s.ContainerServer.RemoveContainer(c)
|
||||||
s.stateLock.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) getPodSandboxFromRequest(podSandboxID string) (*sandbox.Sandbox, error) {
|
func (s *Server) getPodSandboxFromRequest(podSandboxID string) (*sandbox.Sandbox, error) {
|
||||||
|
|
Loading…
Reference in a new issue