move reserve/release container name into libkpod

Signed-off-by: Ryan Cole <rcyoalne@gmail.com>
This commit is contained in:
Ryan Cole 2017-07-20 13:10:16 -04:00
parent d625e0e468
commit 49ed4ab710
7 changed files with 37 additions and 32 deletions

View file

@ -303,7 +303,7 @@ func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerReq
defer func() {
if err != nil {
s.releaseContainerName(containerName)
s.ReleaseContainerName(containerName)
}
}()

View file

@ -42,7 +42,7 @@ func (s *Server) RemoveContainer(ctx context.Context, req *pb.RemoveContainerReq
return nil, fmt.Errorf("failed to delete storage for container %s: %v", c.ID(), err)
}
s.releaseContainerName(c.Name())
s.ReleaseContainerName(c.Name())
if err := s.CtrIDIndex().Delete(c.ID()); err != nil {
return nil, err

View file

@ -66,7 +66,7 @@ func (s *Server) generateContainerIDandNameForSandbox(sandboxConfig *pb.PodSandb
err error
id = stringid.GenerateNonCryptoID()
)
name, err := s.reserveContainerName(id, makeSandboxContainerName(sandboxConfig))
name, err := s.ReserveContainerName(id, makeSandboxContainerName(sandboxConfig))
if err != nil {
return "", "", err
}
@ -78,7 +78,7 @@ func (s *Server) generateContainerIDandName(sandboxMetadata *pb.PodSandboxMetada
err error
id = stringid.GenerateNonCryptoID()
)
name, err := s.reserveContainerName(id, makeContainerName(sandboxMetadata, containerConfig))
name, err := s.ReserveContainerName(id, makeContainerName(sandboxMetadata, containerConfig))
if err != nil {
return "", "", err
}

View file

@ -65,7 +65,7 @@ func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxR
return nil, fmt.Errorf("failed to delete container %s in pod sandbox %s: %v", c.Name(), sb.ID(), err)
}
s.releaseContainerName(c.Name())
s.ReleaseContainerName(c.Name())
s.removeContainer(c)
if err := s.CtrIDIndex().Delete(c.ID()); err != nil {
return nil, fmt.Errorf("failed to delete container %s in pod sandbox %s from index: %v", c.Name(), sb.ID(), err)
@ -82,7 +82,7 @@ func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxR
return nil, fmt.Errorf("failed to remove pod sandbox %s: %v", sb.ID(), err)
}
s.releaseContainerName(podInfraContainer.Name())
s.ReleaseContainerName(podInfraContainer.Name())
if err := s.CtrIDIndex().Delete(podInfraContainer.ID()); err != nil {
return nil, fmt.Errorf("failed to delete infra container %s in pod sandbox %s from index: %v", podInfraContainer.ID(), sb.ID(), err)
}

View file

@ -149,7 +149,7 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
defer func() {
if err != nil {
s.releaseContainerName(containerName)
s.ReleaseContainerName(containerName)
}
}()

View file

@ -103,14 +103,14 @@ func (s *Server) loadContainer(id string) error {
return err
}
name := m.Annotations[annotations.Name]
name, err = s.reserveContainerName(id, name)
name, err = s.ReserveContainerName(id, name)
if err != nil {
return err
}
defer func() {
if err != nil {
s.releaseContainerName(name)
s.ReleaseContainerName(name)
}
}()
@ -256,13 +256,13 @@ func (s *Server) loadSandbox(id string) error {
return err
}
cname, err := s.reserveContainerName(m.Annotations[annotations.ContainerID], m.Annotations[annotations.ContainerName])
cname, err := s.ReserveContainerName(m.Annotations[annotations.ContainerID], m.Annotations[annotations.ContainerName])
if err != nil {
return err
}
defer func() {
if err != nil {
s.releaseContainerName(cname)
s.ReleaseContainerName(cname)
}
}()
@ -384,7 +384,7 @@ func (s *Server) update() error {
logrus.Warnf("bad state when getting container removed %+v", removedPodContainer)
continue
}
s.releaseContainerName(c.Name())
s.ReleaseContainerName(c.Name())
s.removeContainer(c)
if err = s.CtrIDIndex().Delete(c.ID()); err != nil {
return err
@ -405,7 +405,7 @@ func (s *Server) update() error {
continue
}
podInfraContainer := sb.InfraContainer()
s.releaseContainerName(podInfraContainer.Name())
s.ReleaseContainerName(podInfraContainer.Name())
s.removeContainer(podInfraContainer)
if err = s.CtrIDIndex().Delete(podInfraContainer.ID()); err != nil {
return err
@ -456,25 +456,6 @@ func (s *Server) releasePodName(name string) {
s.podNameIndex.Release(name)
}
func (s *Server) reserveContainerName(id, name string) (string, error) {
if err := s.CtrNameIndex().Reserve(name, id); err != nil {
if err == registrar.ErrNameReserved {
id, err := s.CtrNameIndex().Get(name)
if err != nil {
logrus.Warnf("conflict, ctr name %q already reserved", name)
return "", err
}
return "", fmt.Errorf("conflict, name %q already reserved for ctr %q", name, id)
}
return "", fmt.Errorf("error reserving ctr name %s", name)
}
return name, nil
}
func (s *Server) releaseContainerName(name string) {
s.CtrNameIndex().Release(name)
}
// cleanupSandboxesOnShutdown Remove all running Sandboxes on system shutdown
func (s *Server) cleanupSandboxesOnShutdown() {
_, err := os.Stat(shutdownFile)