Move Pod ID and Name indexes into libkpod
Signed-off-by: Matthew Heon <mheon@redhat.com>
This commit is contained in:
parent
ece055327a
commit
158893bd1a
7 changed files with 58 additions and 47 deletions
|
@ -23,9 +23,12 @@ type ContainerServer struct {
|
|||
storageImageServer storage.ImageServer
|
||||
ctrNameIndex *registrar.Registrar
|
||||
ctrIDIndex *truncindex.TruncIndex
|
||||
imageContext *types.SystemContext
|
||||
stateLock sync.Locker
|
||||
state *containerServerState
|
||||
podNameIndex *registrar.Registrar
|
||||
podIDIndex *truncindex.TruncIndex
|
||||
|
||||
imageContext *types.SystemContext
|
||||
stateLock sync.Locker
|
||||
state *containerServerState
|
||||
}
|
||||
|
||||
// Runtime returns the oci runtime for the ContainerServer
|
||||
|
@ -53,6 +56,16 @@ func (c *ContainerServer) CtrIDIndex() *truncindex.TruncIndex {
|
|||
return c.ctrIDIndex
|
||||
}
|
||||
|
||||
// PodNameIndex returns the index of pod names
|
||||
func (c *ContainerServer) PodNameIndex() *registrar.Registrar {
|
||||
return c.podNameIndex
|
||||
}
|
||||
|
||||
// PodIDIndex returns the index of pod IDs
|
||||
func (c *ContainerServer) PodIDIndex() *truncindex.TruncIndex {
|
||||
return c.podIDIndex
|
||||
}
|
||||
|
||||
// ImageContext returns the SystemContext for the ContainerServer
|
||||
func (c *ContainerServer) ImageContext() *types.SystemContext {
|
||||
return c.imageContext
|
||||
|
@ -66,6 +79,8 @@ func New(runtime *oci.Runtime, store cstorage.Store, imageService storage.ImageS
|
|||
storageImageServer: imageService,
|
||||
ctrNameIndex: registrar.NewRegistrar(),
|
||||
ctrIDIndex: truncindex.NewTruncIndex([]string{}),
|
||||
podNameIndex: registrar.NewRegistrar(),
|
||||
podIDIndex: truncindex.NewTruncIndex([]string{}),
|
||||
imageContext: &types.SystemContext{SignaturePolicyPath: signaturePolicyPath},
|
||||
stateLock: new(sync.Mutex),
|
||||
state: &containerServerState{
|
||||
|
@ -126,6 +141,28 @@ func (c *ContainerServer) ReleaseContainerName(name string) {
|
|||
c.ctrNameIndex.Release(name)
|
||||
}
|
||||
|
||||
// ReservePodName holds a name for a pod that is being created
|
||||
func (c *ContainerServer) ReservePodName(id, name string) (string, error) {
|
||||
if err := c.podNameIndex.Reserve(name, id); err != nil {
|
||||
if err == registrar.ErrNameReserved {
|
||||
id, err := c.podNameIndex.Get(name)
|
||||
if err != nil {
|
||||
logrus.Warnf("conflict, pod name %q already reserved", name)
|
||||
return "", err
|
||||
}
|
||||
return "", fmt.Errorf("conflict, name %q already reserved for pod %q", name, id)
|
||||
}
|
||||
return "", fmt.Errorf("error reserving pod name %q", name)
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
// ReleasePodName releases a pod name from the index so it can be used by other
|
||||
// pods
|
||||
func (c *ContainerServer) ReleasePodName(name string) {
|
||||
c.podNameIndex.Release(name)
|
||||
}
|
||||
|
||||
// Shutdown attempts to shut down the server's storage cleanly
|
||||
func (c *ContainerServer) Shutdown() error {
|
||||
_, err := c.store.Shutdown(false)
|
||||
|
|
|
@ -275,7 +275,7 @@ func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerReq
|
|||
return nil, fmt.Errorf("PodSandboxId should not be empty")
|
||||
}
|
||||
|
||||
sandboxID, err := s.podIDIndex.Get(sbID)
|
||||
sandboxID, err := s.PodIDIndex().Get(sbID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("PodSandbox with ID starting with %s not found: %v", sbID, err)
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ func (s *Server) generatePodIDandName(sandboxConfig *pb.PodSandboxConfig) (strin
|
|||
if sandboxConfig.Metadata.Namespace == "" {
|
||||
return "", "", fmt.Errorf("cannot generate pod ID without namespace")
|
||||
}
|
||||
name, err := s.reservePodName(id, makeSandboxName(sandboxConfig))
|
||||
name, err := s.ReservePodName(id, makeSandboxName(sandboxConfig))
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ func (s *Server) ListPodSandbox(ctx context.Context, req *pb.ListPodSandboxReque
|
|||
// Filter by pod id first.
|
||||
if filter != nil {
|
||||
if filter.Id != "" {
|
||||
id, err := s.podIDIndex.Get(filter.Id)
|
||||
id, err := s.PodIDIndex().Get(filter.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -87,9 +87,9 @@ func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxR
|
|||
return nil, fmt.Errorf("failed to delete infra container %s in pod sandbox %s from index: %v", podInfraContainer.ID(), sb.ID(), err)
|
||||
}
|
||||
|
||||
s.releasePodName(sb.Name())
|
||||
s.ReleasePodName(sb.Name())
|
||||
s.removeSandbox(sb.ID())
|
||||
if err := s.podIDIndex.Delete(sb.ID()); err != nil {
|
||||
if err := s.PodIDIndex().Delete(sb.ID()); err != nil {
|
||||
return nil, fmt.Errorf("failed to delete pod sandbox %s from index: %v", sb.ID(), err)
|
||||
}
|
||||
|
||||
|
|
|
@ -138,7 +138,7 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
|
|||
|
||||
defer func() {
|
||||
if err != nil {
|
||||
s.releasePodName(name)
|
||||
s.ReleasePodName(name)
|
||||
}
|
||||
}()
|
||||
|
||||
|
@ -358,13 +358,13 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
|
|||
}
|
||||
}()
|
||||
|
||||
if err = s.podIDIndex.Add(id); err != nil {
|
||||
if err = s.PodIDIndex().Add(id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err != nil {
|
||||
if err := s.podIDIndex.Delete(id); err != nil {
|
||||
if err := s.PodIDIndex().Delete(id); err != nil {
|
||||
logrus.Warnf("couldn't delete pod id %s from idIndex", id)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,8 +12,6 @@ import (
|
|||
|
||||
"github.com/Sirupsen/logrus"
|
||||
cstorage "github.com/containers/storage"
|
||||
"github.com/docker/docker/pkg/registrar"
|
||||
"github.com/docker/docker/pkg/truncindex"
|
||||
"github.com/kubernetes-incubator/cri-o/libkpod"
|
||||
"github.com/kubernetes-incubator/cri-o/libkpod/sandbox"
|
||||
"github.com/kubernetes-incubator/cri-o/oci"
|
||||
|
@ -59,8 +57,6 @@ type Server struct {
|
|||
updateLock sync.RWMutex
|
||||
netPlugin ocicni.CNIPlugin
|
||||
hostportManager hostport.HostPortManager
|
||||
podNameIndex *registrar.Registrar
|
||||
podIDIndex *truncindex.TruncIndex
|
||||
|
||||
seccompEnabled bool
|
||||
seccompProfile seccomp.Seccomp
|
||||
|
@ -190,13 +186,13 @@ func (s *Server) loadSandbox(id string) error {
|
|||
return err
|
||||
}
|
||||
name := m.Annotations[annotations.Name]
|
||||
name, err = s.reservePodName(id, name)
|
||||
name, err = s.ReservePodName(id, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
s.releasePodName(name)
|
||||
s.ReleasePodName(name)
|
||||
}
|
||||
}()
|
||||
var metadata pb.PodSandboxMetadata
|
||||
|
@ -282,7 +278,7 @@ func (s *Server) loadSandbox(id string) error {
|
|||
if err = s.CtrIDIndex().Add(scontainer.ID()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = s.podIDIndex.Add(id); err != nil {
|
||||
if err = s.PodIDIndex().Add(id); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
@ -388,7 +384,7 @@ func (s *Server) update() error {
|
|||
}
|
||||
logrus.Debugf("forgetting removed pod container %s", c.ID())
|
||||
}
|
||||
s.podIDIndex.Iterate(func(id string) {
|
||||
s.PodIDIndex().Iterate(func(id string) {
|
||||
if _, ok := oldPods[id]; !ok {
|
||||
// this pod's ID wasn't in the updated list -> removed
|
||||
removedPods[id] = id
|
||||
|
@ -408,9 +404,9 @@ func (s *Server) update() error {
|
|||
return err
|
||||
}
|
||||
sb.RemoveInfraContainer()
|
||||
s.releasePodName(sb.Name())
|
||||
s.ReleasePodName(sb.Name())
|
||||
s.removeSandbox(sb.ID())
|
||||
if err = s.podIDIndex.Delete(sb.ID()); err != nil {
|
||||
if err = s.PodIDIndex().Delete(sb.ID()); err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Debugf("forgetting removed pod %s", sb.ID())
|
||||
|
@ -434,25 +430,6 @@ func (s *Server) update() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) reservePodName(id, name string) (string, error) {
|
||||
if err := s.podNameIndex.Reserve(name, id); err != nil {
|
||||
if err == registrar.ErrNameReserved {
|
||||
id, err := s.podNameIndex.Get(name)
|
||||
if err != nil {
|
||||
logrus.Warnf("conflict, pod name %q already reserved", name)
|
||||
return "", err
|
||||
}
|
||||
return "", fmt.Errorf("conflict, name %q already reserved for pod %q", name, id)
|
||||
}
|
||||
return "", fmt.Errorf("error reserving pod name %q", name)
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
func (s *Server) releasePodName(name string) {
|
||||
s.podNameIndex.Release(name)
|
||||
}
|
||||
|
||||
// cleanupSandboxesOnShutdown Remove all running Sandboxes on system shutdown
|
||||
func (s *Server) cleanupSandboxesOnShutdown() {
|
||||
_, err := os.Stat(shutdownFile)
|
||||
|
@ -524,9 +501,9 @@ func New(config *Config) (*Server, error) {
|
|||
netPlugin: netPlugin,
|
||||
hostportManager: hostportManager,
|
||||
config: *config,
|
||||
seccompEnabled: seccomp.IsEnabled(),
|
||||
appArmorEnabled: apparmor.IsEnabled(),
|
||||
appArmorProfile: config.ApparmorProfile,
|
||||
seccompEnabled: seccomp.IsEnabled(),
|
||||
appArmorEnabled: apparmor.IsEnabled(),
|
||||
appArmorProfile: config.ApparmorProfile,
|
||||
}
|
||||
if s.seccompEnabled {
|
||||
seccompProfile, fileErr := ioutil.ReadFile(config.SeccompProfile)
|
||||
|
@ -546,9 +523,6 @@ func New(config *Config) (*Server, error) {
|
|||
}
|
||||
}
|
||||
|
||||
s.podIDIndex = truncindex.NewTruncIndex([]string{})
|
||||
s.podNameIndex = registrar.NewRegistrar()
|
||||
|
||||
s.restore()
|
||||
s.cleanupSandboxesOnShutdown()
|
||||
|
||||
|
@ -626,7 +600,7 @@ func (s *Server) getPodSandboxFromRequest(podSandboxID string) (*sandbox.Sandbox
|
|||
return nil, sandbox.ErrIDEmpty
|
||||
}
|
||||
|
||||
sandboxID, err := s.podIDIndex.Get(podSandboxID)
|
||||
sandboxID, err := s.PodIDIndex().Get(podSandboxID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("PodSandbox with ID starting with %s not found: %v", podSandboxID, err)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue