id and name indexes for pods
Signed-off-by: Antonio Murdaca <runcom@redhat.com>
This commit is contained in:
parent
361fc8fde7
commit
32029aaba6
4 changed files with 95 additions and 61 deletions
|
@ -9,8 +9,11 @@ import (
|
|||
"sync"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/docker/docker/pkg/registrar"
|
||||
"github.com/docker/docker/pkg/truncindex"
|
||||
"github.com/kubernetes-incubator/ocid/oci"
|
||||
"github.com/kubernetes-incubator/ocid/utils"
|
||||
rspec "github.com/opencontainers/runtime-spec/specs-go"
|
||||
"github.com/rajatchopra/ocicni"
|
||||
)
|
||||
|
||||
|
@ -21,30 +24,38 @@ const (
|
|||
|
||||
// Server implements the RuntimeService and ImageService
|
||||
type Server struct {
|
||||
runtime *oci.Runtime
|
||||
sandboxDir string
|
||||
stateLock sync.Mutex
|
||||
state *serverState
|
||||
netPlugin ocicni.CNIPlugin
|
||||
runtime *oci.Runtime
|
||||
sandboxDir string
|
||||
stateLock sync.Mutex
|
||||
state *serverState
|
||||
netPlugin ocicni.CNIPlugin
|
||||
podNameIndex *registrar.Registrar
|
||||
podIDIndex *truncindex.TruncIndex
|
||||
}
|
||||
|
||||
func (s *Server) loadSandbox(id string) error {
|
||||
metaJSON, err := ioutil.ReadFile(filepath.Join(s.sandboxDir, id, "metadata.json"))
|
||||
config, err := ioutil.ReadFile(filepath.Join(s.sandboxDir, id, "config.json"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var m metadata
|
||||
if err = json.Unmarshal(metaJSON, &m); err != nil {
|
||||
var m rspec.Spec
|
||||
if err = json.Unmarshal(config, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
labels := make(map[string]string)
|
||||
if err = json.Unmarshal([]byte(m.Annotations["ocid/labels"]), &labels); err != nil {
|
||||
return err
|
||||
}
|
||||
name := m.Annotations["ocid/name"]
|
||||
s.addSandbox(&sandbox{
|
||||
name: id,
|
||||
logDir: m.LogDir,
|
||||
labels: m.Labels,
|
||||
id: id,
|
||||
name: name,
|
||||
logDir: m.Annotations["ocid/log_path"],
|
||||
labels: labels,
|
||||
containers: oci.NewMemoryStore(),
|
||||
})
|
||||
sandboxPath := filepath.Join(s.sandboxDir, id)
|
||||
scontainer, err := oci.NewContainer(m.ContainerName, sandboxPath, sandboxPath, m.Labels, id, false)
|
||||
scontainer, err := oci.NewContainer(m.Annotations["ocid/container_name"], sandboxPath, sandboxPath, labels, id, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -52,6 +63,8 @@ func (s *Server) loadSandbox(id string) error {
|
|||
if err = s.runtime.UpdateStatus(scontainer); err != nil {
|
||||
logrus.Warnf("error updating status for container %s: %v", scontainer, err)
|
||||
}
|
||||
s.podIDIndex.Add(id)
|
||||
s.reservePodName(id, name)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -68,6 +81,21 @@ func (s *Server) restore() 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("name %s already reserved for %s", name, id)
|
||||
return "", err
|
||||
}
|
||||
return "", fmt.Errorf("conflict, name %s already reserver", name)
|
||||
}
|
||||
return "", fmt.Errorf("error reserving name %s", name)
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
// New creates a new Server with options provided
|
||||
func New(runtimePath, sandboxDir, containerDir string) (*Server, error) {
|
||||
// TODO: This will go away later when we have wrapper process or systemd acting as
|
||||
|
@ -105,6 +133,8 @@ func New(runtimePath, sandboxDir, containerDir string) (*Server, error) {
|
|||
containers: containers,
|
||||
},
|
||||
}
|
||||
s.podIDIndex = truncindex.NewTruncIndex([]string{})
|
||||
s.podNameIndex = registrar.NewRegistrar()
|
||||
if err := s.restore(); err != nil {
|
||||
logrus.Warnf("couldn't restore: %v", err)
|
||||
}
|
||||
|
@ -120,20 +150,20 @@ type serverState struct {
|
|||
|
||||
func (s *Server) addSandbox(sb *sandbox) {
|
||||
s.stateLock.Lock()
|
||||
s.state.sandboxes[sb.name] = sb
|
||||
s.state.sandboxes[sb.id] = sb
|
||||
s.stateLock.Unlock()
|
||||
}
|
||||
|
||||
func (s *Server) getSandbox(name string) *sandbox {
|
||||
func (s *Server) getSandbox(id string) *sandbox {
|
||||
s.stateLock.Lock()
|
||||
sb := s.state.sandboxes[name]
|
||||
sb := s.state.sandboxes[id]
|
||||
s.stateLock.Unlock()
|
||||
return sb
|
||||
}
|
||||
|
||||
func (s *Server) hasSandbox(name string) bool {
|
||||
func (s *Server) hasSandbox(id string) bool {
|
||||
s.stateLock.Lock()
|
||||
_, ok := s.state.sandboxes[name]
|
||||
_, ok := s.state.sandboxes[id]
|
||||
s.stateLock.Unlock()
|
||||
return ok
|
||||
}
|
||||
|
@ -141,6 +171,7 @@ func (s *Server) hasSandbox(name string) bool {
|
|||
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.state.containers.Add(c.Name(), c)
|
||||
s.stateLock.Unlock()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue