From d625e0e4686a700d3ed02ae483e505a6ca4ba0ae Mon Sep 17 00:00:00 2001 From: Ryan Cole Date: Thu, 20 Jul 2017 13:05:12 -0400 Subject: [PATCH 1/4] move containerState to/from disk to libkpod Signed-off-by: Ryan Cole --- libkpod/containerserver.go | 31 +++++++++++++++++++++++++++++++ server/container_create.go | 2 +- server/container_start.go | 2 +- server/container_status.go | 2 +- server/container_stop.go | 2 +- server/sandbox_run.go | 2 +- server/sandbox_status.go | 2 +- server/sandbox_stop.go | 2 +- server/server.go | 30 ++---------------------------- 9 files changed, 40 insertions(+), 35 deletions(-) diff --git a/libkpod/containerserver.go b/libkpod/containerserver.go index c9580b78..f2abfe2f 100644 --- a/libkpod/containerserver.go +++ b/libkpod/containerserver.go @@ -1,6 +1,7 @@ package libkpod import ( + "encoding/json" "sync" "github.com/containers/image/types" @@ -9,6 +10,7 @@ import ( "github.com/docker/docker/pkg/truncindex" "github.com/kubernetes-incubator/cri-o/oci" "github.com/kubernetes-incubator/cri-o/pkg/storage" + "github.com/moby/moby/pkg/ioutils" ) // ContainerServer implements the ImageServer @@ -70,6 +72,35 @@ func New(runtime *oci.Runtime, store cstorage.Store, storageImageServer storage. } } +// ContainerStateFromDisk retrieves information on the state of a running container +// from the disk +func (c *ContainerServer) ContainerStateFromDisk(ctr *oci.Container) error { + if err := ctr.FromDisk(); err != nil { + return err + } + // ignore errors, this is a best effort to have up-to-date info about + // a given container before its state gets stored + c.runtime.UpdateStatus(ctr) + + return nil +} + +// ContainerStateToDisk writes the container's state information to a JSON file +// on disk +func (c *ContainerServer) ContainerStateToDisk(ctr *oci.Container) error { + // ignore errors, this is a best effort to have up-to-date info about + // a given container before its state gets stored + c.Runtime().UpdateStatus(ctr) + + jsonSource, err := ioutils.NewAtomicFileWriter(ctr.StatePath(), 0644) + if err != nil { + return err + } + defer jsonSource.Close() + enc := json.NewEncoder(jsonSource) + return enc.Encode(c.runtime.ContainerStatus(ctr)) +} + type containerServerState struct { containers oci.ContainerStorer } diff --git a/server/container_create.go b/server/container_create.go index ebcd607f..c2d4abc7 100644 --- a/server/container_create.go +++ b/server/container_create.go @@ -335,7 +335,7 @@ func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerReq return nil, err } - s.containerStateToDisk(container) + s.ContainerStateToDisk(container) resp := &pb.CreateContainerResponse{ ContainerId: containerID, diff --git a/server/container_start.go b/server/container_start.go index d3d97bc5..b8c7fdb0 100644 --- a/server/container_start.go +++ b/server/container_start.go @@ -29,7 +29,7 @@ func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerReque if err != nil { s.Runtime().SetStartFailed(c, err) } - s.containerStateToDisk(c) + s.ContainerStateToDisk(c) }() err = s.Runtime().StartContainer(c) diff --git a/server/container_status.go b/server/container_status.go index ffa315bb..ece493eb 100644 --- a/server/container_status.go +++ b/server/container_status.go @@ -29,7 +29,7 @@ func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusReq if err = s.Runtime().UpdateStatus(c); err != nil { return nil, err } - s.containerStateToDisk(c) + s.ContainerStateToDisk(c) containerID := c.ID() resp := &pb.ContainerStatusResponse{ diff --git a/server/container_stop.go b/server/container_stop.go index d069e603..ccbde3cc 100644 --- a/server/container_stop.go +++ b/server/container_stop.go @@ -30,7 +30,7 @@ func (s *Server) StopContainer(ctx context.Context, req *pb.StopContainerRequest } } - s.containerStateToDisk(c) + s.ContainerStateToDisk(c) resp := &pb.StopContainerResponse{} logrus.Debugf("StopContainerResponse: %+v", resp) diff --git a/server/sandbox_run.go b/server/sandbox_run.go index bfff47d2..8f8abe8b 100644 --- a/server/sandbox_run.go +++ b/server/sandbox_run.go @@ -500,7 +500,7 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest return nil, err } - s.containerStateToDisk(container) + s.ContainerStateToDisk(container) resp = &pb.RunPodSandboxResponse{PodSandboxId: id} logrus.Debugf("RunPodSandboxResponse: %+v", resp) diff --git a/server/sandbox_status.go b/server/sandbox_status.go index ba00563d..48eb6695 100644 --- a/server/sandbox_status.go +++ b/server/sandbox_status.go @@ -19,7 +19,7 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR if err = s.Runtime().UpdateStatus(podInfraContainer); err != nil { return nil, err } - s.containerStateToDisk(podInfraContainer) + s.ContainerStateToDisk(podInfraContainer) cState := s.Runtime().ContainerStatus(podInfraContainer) diff --git a/server/sandbox_stop.go b/server/sandbox_stop.go index 1f863fc2..1689d87b 100644 --- a/server/sandbox_stop.go +++ b/server/sandbox_stop.go @@ -85,7 +85,7 @@ func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxReque logrus.Warnf("failed to stop container %s in pod sandbox %s: %v", c.Name(), sb.ID(), err) } } - s.containerStateToDisk(c) + s.ContainerStateToDisk(c) } if err := label.ReleaseLabel(sb.ProcessLabel()); err != nil { diff --git a/server/server.go b/server/server.go index 71cb960b..28ce262f 100644 --- a/server/server.go +++ b/server/server.go @@ -13,7 +13,6 @@ import ( "github.com/Sirupsen/logrus" "github.com/containers/image/types" cstorage "github.com/containers/storage" - "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" @@ -158,37 +157,12 @@ func (s *Server) loadContainer(id string) error { return err } - s.containerStateFromDisk(ctr) + s.ContainerStateFromDisk(ctr) s.addContainer(ctr) return s.CtrIDIndex().Add(id) } -func (s *Server) containerStateFromDisk(c *oci.Container) error { - if err := c.FromDisk(); err != nil { - return err - } - // ignore errors, this is a best effort to have up-to-date info about - // a given container before its state gets stored - s.Runtime().UpdateStatus(c) - - return nil -} - -func (s *Server) containerStateToDisk(c *oci.Container) error { - // ignore errors, this is a best effort to have up-to-date info about - // a given container before its state gets stored - s.Runtime().UpdateStatus(c) - - jsonSource, err := ioutils.NewAtomicFileWriter(c.StatePath(), 0644) - if err != nil { - return err - } - defer jsonSource.Close() - enc := json.NewEncoder(jsonSource) - return enc.Encode(s.Runtime().ContainerStatus(c)) -} - func configNetNsPath(spec rspec.Spec) (string, error) { for _, ns := range spec.Linux.Namespaces { if ns.Type != rspec.NetworkNamespace { @@ -302,7 +276,7 @@ func (s *Server) loadSandbox(id string) error { return err } - s.containerStateFromDisk(scontainer) + s.ContainerStateFromDisk(scontainer) if err = label.ReserveLabel(processLabel); err != nil { return err From 49ed4ab7109c339a5998f14a47a01842cd7e25a5 Mon Sep 17 00:00:00 2001 From: Ryan Cole Date: Thu, 20 Jul 2017 13:10:16 -0400 Subject: [PATCH 2/4] move reserve/release container name into libkpod Signed-off-by: Ryan Cole --- libkpod/containerserver.go | 24 ++++++++++++++++++++++++ server/container_create.go | 2 +- server/container_remove.go | 2 +- server/naming.go | 4 ++-- server/sandbox_remove.go | 4 ++-- server/sandbox_run.go | 2 +- server/server.go | 31 ++++++------------------------- 7 files changed, 37 insertions(+), 32 deletions(-) diff --git a/libkpod/containerserver.go b/libkpod/containerserver.go index f2abfe2f..c13cd36f 100644 --- a/libkpod/containerserver.go +++ b/libkpod/containerserver.go @@ -2,8 +2,10 @@ package libkpod import ( "encoding/json" + "fmt" "sync" + "github.com/Sirupsen/logrus" "github.com/containers/image/types" cstorage "github.com/containers/storage" "github.com/docker/docker/pkg/registrar" @@ -101,6 +103,28 @@ func (c *ContainerServer) ContainerStateToDisk(ctr *oci.Container) error { return enc.Encode(c.runtime.ContainerStatus(ctr)) } +// ReserveContainerName holds a name for a container that is being created +func (c *ContainerServer) ReserveContainerName(id, name string) (string, error) { + if err := c.ctrNameIndex.Reserve(name, id); err != nil { + if err == registrar.ErrNameReserved { + id, err := c.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 +} + +// ReleaseContainerName releases a container name from the index so that it can +// be used by other containers +func (c *ContainerServer) ReleaseContainerName(name string) { + c.ctrNameIndex.Release(name) +} + type containerServerState struct { containers oci.ContainerStorer } diff --git a/server/container_create.go b/server/container_create.go index c2d4abc7..fafd5940 100644 --- a/server/container_create.go +++ b/server/container_create.go @@ -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) } }() diff --git a/server/container_remove.go b/server/container_remove.go index c32e4fa0..8e330e60 100644 --- a/server/container_remove.go +++ b/server/container_remove.go @@ -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 diff --git a/server/naming.go b/server/naming.go index 0175158b..881b2510 100644 --- a/server/naming.go +++ b/server/naming.go @@ -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 } diff --git a/server/sandbox_remove.go b/server/sandbox_remove.go index d9efe7e6..0b22629b 100644 --- a/server/sandbox_remove.go +++ b/server/sandbox_remove.go @@ -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) } diff --git a/server/sandbox_run.go b/server/sandbox_run.go index 8f8abe8b..ab04cf2a 100644 --- a/server/sandbox_run.go +++ b/server/sandbox_run.go @@ -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) } }() diff --git a/server/server.go b/server/server.go index 28ce262f..4e9dbef1 100644 --- a/server/server.go +++ b/server/server.go @@ -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) From 4fe17ee16dd09a6a3a69ff92380e8e39d8a877ef Mon Sep 17 00:00:00 2001 From: Ryan Cole Date: Thu, 20 Jul 2017 15:43:01 -0400 Subject: [PATCH 3/4] Update libkpod New() update libkpod's New() function to use a config struct, and update server.New() to call into libkpod.New() Signed-off-by: Ryan Cole --- libkpod/containerserver.go | 45 +++++++++++++++++++++++++++++++------- server/server.go | 26 +++------------------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/libkpod/containerserver.go b/libkpod/containerserver.go index c13cd36f..15993bb6 100644 --- a/libkpod/containerserver.go +++ b/libkpod/containerserver.go @@ -3,7 +3,9 @@ package libkpod import ( "encoding/json" "fmt" + "os" "sync" + "time" "github.com/Sirupsen/logrus" "github.com/containers/image/types" @@ -11,8 +13,10 @@ import ( "github.com/docker/docker/pkg/registrar" "github.com/docker/docker/pkg/truncindex" "github.com/kubernetes-incubator/cri-o/oci" + "github.com/kubernetes-incubator/cri-o/pkg/annotations" "github.com/kubernetes-incubator/cri-o/pkg/storage" "github.com/moby/moby/pkg/ioutils" + rspec "github.com/opencontainers/runtime-spec/specs-go" ) // ContainerServer implements the ImageServer @@ -23,6 +27,7 @@ type ContainerServer struct { ctrNameIndex *registrar.Registrar ctrIDIndex *truncindex.TruncIndex imageContext *types.SystemContext + updateLock *sync.RWMutex stateLock sync.Locker state *containerServerState } @@ -57,21 +62,45 @@ func (c *ContainerServer) ImageContext() *types.SystemContext { return c.imageContext } -// New creates a new ContainerServer -func New(runtime *oci.Runtime, store cstorage.Store, storageImageServer storage.ImageServer, ctrNameIndex *registrar.Registrar, ctrIDIndex *truncindex.TruncIndex, imageContext *types.SystemContext) *ContainerServer { +// New creates a new ContainerServer with options provided +func New(config *server.Config) (*ContainerServer, error) { + store, err := cstorage.GetStore(cstorage.StoreOptions{ + RunRoot: config.RunRoot, + GraphRoot: config.Root, + GraphDriverName: config.Storage, + GraphDriverOptions: config.StorageOptions, + }) + if err != nil { + return nil, err + } + + imageService, err := storage.GetImageService(store, config.DefaultTransport, config.InsecureRegistries) + if err != nil { + return nil, err + } + + r, err := oci.New(config.Runtime, config.RuntimeUntrustedWorkload, config.DefaultWorkloadTrust, config.Conmon, config.ConmonEnv, config.CgroupManager) + if err != nil { + return nil, err + } + + // Create new container server struct here containers := oci.NewMemoryStore() - return &ContainerServer{ - runtime: runtime, + c := &ContainerServer{ + runtime: r, store: store, - storageImageServer: storageImageServer, - ctrNameIndex: ctrNameIndex, - ctrIDIndex: ctrIDIndex, - imageContext: imageContext, + storageImageServer: imageService, + ctrNameIndex: registrar.NewRegistrar(), + ctrIDIndex: truncindex.NewTruncIndex([]string{}), + imageContext: &types.SystemContext{SignaturePolicyPath: config.ImageConfig.SignaturePolicyPath}, stateLock: new(sync.Mutex), state: &containerServerState{ containers: containers, }, } + + logrus.Debugf("containers: %v", c.ListContainers()) + return c, nil } // ContainerStateFromDisk retrieves information on the state of a running container diff --git a/server/server.go b/server/server.go index 4e9dbef1..d2e7e6ce 100644 --- a/server/server.go +++ b/server/server.go @@ -11,8 +11,6 @@ import ( "time" "github.com/Sirupsen/logrus" - "github.com/containers/image/types" - cstorage "github.com/containers/storage" "github.com/docker/docker/pkg/registrar" "github.com/docker/docker/pkg/truncindex" "github.com/kubernetes-incubator/cri-o/libkpod" @@ -477,28 +475,17 @@ func (s *Server) Shutdown() error { // notice this won't trigger just on system halt but also on normal // crio.service restart!!! s.cleanupSandboxesOnShutdown() - _, err := s.Store().Shutdown(false) - return err + return s.ContainerServer.Shutdown() } // New creates a new Server with options provided func New(config *Config) (*Server, error) { - store, err := cstorage.GetStore(cstorage.StoreOptions{ - RunRoot: config.RunRoot, - GraphRoot: config.Root, - GraphDriverName: config.Storage, - GraphDriverOptions: config.StorageOptions, - }) + containerServer, err := libkpod.New(config) if err != nil { return nil, err } - imageService, err := storage.GetImageService(store, config.DefaultTransport, config.InsecureRegistries) - if err != nil { - return nil, err - } - - storageRuntimeService := storage.GetRuntimeService(imageService, config.PauseImage) + storageRuntimeService := storage.GetRuntimeService(containerServer.StorageImageServer(), config.PauseImage) if err != nil { return nil, err } @@ -507,11 +494,6 @@ func New(config *Config) (*Server, error) { return nil, err } - r, err := oci.New(config.Runtime, config.RuntimeUntrustedWorkload, config.DefaultWorkloadTrust, config.Conmon, config.ConmonEnv, config.CgroupManager) - if err != nil { - return nil, err - } - sandboxes := make(map[string]*sandbox.Sandbox) netPlugin, err := ocicni.InitCNI(config.NetworkDir, config.PluginDir) if err != nil { @@ -521,7 +503,6 @@ func New(config *Config) (*Server, error) { iptInterface.EnsureChain(utiliptables.TableNAT, iptablesproxy.KubeMarkMasqChain) hostportManager := hostport.NewHostportManager() - containerServer := libkpod.New(r, store, imageService, registrar.NewRegistrar(), truncindex.NewTruncIndex([]string{}), &types.SystemContext{SignaturePolicyPath: config.ImageConfig.SignaturePolicyPath}) s := &Server{ ContainerServer: *containerServer, storageRuntimeServer: storageRuntimeService, @@ -588,7 +569,6 @@ func New(config *Config) (*Server, error) { }() logrus.Debugf("sandboxes: %v", s.state.sandboxes) - logrus.Debugf("containers: %v", s.ContainerServer.ListContainers()) return s, nil } From 314d55f284c6ce153f2eb7256349ac4e0a6a0736 Mon Sep 17 00:00:00 2001 From: Ryan Cole Date: Thu, 20 Jul 2017 14:53:40 -0400 Subject: [PATCH 4/4] move shutdown function into libkpod and have server call in to it Signed-off-by: Ryan Cole --- libkpod/containerserver.go | 48 ++++++++++---------------------------- server/server.go | 22 +++++++++++++++-- 2 files changed, 32 insertions(+), 38 deletions(-) diff --git a/libkpod/containerserver.go b/libkpod/containerserver.go index 15993bb6..5f26fefd 100644 --- a/libkpod/containerserver.go +++ b/libkpod/containerserver.go @@ -3,20 +3,16 @@ package libkpod import ( "encoding/json" "fmt" - "os" "sync" - "time" "github.com/Sirupsen/logrus" "github.com/containers/image/types" cstorage "github.com/containers/storage" + "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/registrar" "github.com/docker/docker/pkg/truncindex" "github.com/kubernetes-incubator/cri-o/oci" - "github.com/kubernetes-incubator/cri-o/pkg/annotations" "github.com/kubernetes-incubator/cri-o/pkg/storage" - "github.com/moby/moby/pkg/ioutils" - rspec "github.com/opencontainers/runtime-spec/specs-go" ) // ContainerServer implements the ImageServer @@ -27,7 +23,6 @@ type ContainerServer struct { ctrNameIndex *registrar.Registrar ctrIDIndex *truncindex.TruncIndex imageContext *types.SystemContext - updateLock *sync.RWMutex stateLock sync.Locker state *containerServerState } @@ -63,44 +58,19 @@ func (c *ContainerServer) ImageContext() *types.SystemContext { } // New creates a new ContainerServer with options provided -func New(config *server.Config) (*ContainerServer, error) { - store, err := cstorage.GetStore(cstorage.StoreOptions{ - RunRoot: config.RunRoot, - GraphRoot: config.Root, - GraphDriverName: config.Storage, - GraphDriverOptions: config.StorageOptions, - }) - if err != nil { - return nil, err - } - - imageService, err := storage.GetImageService(store, config.DefaultTransport, config.InsecureRegistries) - if err != nil { - return nil, err - } - - r, err := oci.New(config.Runtime, config.RuntimeUntrustedWorkload, config.DefaultWorkloadTrust, config.Conmon, config.ConmonEnv, config.CgroupManager) - if err != nil { - return nil, err - } - - // Create new container server struct here - containers := oci.NewMemoryStore() - c := &ContainerServer{ - runtime: r, +func New(runtime *oci.Runtime, store cstorage.Store, imageService storage.ImageServer, signaturePolicyPath string) *ContainerServer { + return &ContainerServer{ + runtime: runtime, store: store, storageImageServer: imageService, ctrNameIndex: registrar.NewRegistrar(), ctrIDIndex: truncindex.NewTruncIndex([]string{}), - imageContext: &types.SystemContext{SignaturePolicyPath: config.ImageConfig.SignaturePolicyPath}, + imageContext: &types.SystemContext{SignaturePolicyPath: signaturePolicyPath}, stateLock: new(sync.Mutex), state: &containerServerState{ - containers: containers, + containers: oci.NewMemoryStore(), }, } - - logrus.Debugf("containers: %v", c.ListContainers()) - return c, nil } // ContainerStateFromDisk retrieves information on the state of a running container @@ -154,6 +124,12 @@ func (c *ContainerServer) ReleaseContainerName(name string) { c.ctrNameIndex.Release(name) } +// Shutdown attempts to shut down the server's storage cleanly +func (c *ContainerServer) Shutdown() error { + _, err := c.store.Shutdown(false) + return err +} + type containerServerState struct { containers oci.ContainerStorer } diff --git a/server/server.go b/server/server.go index d2e7e6ce..1229ae8a 100644 --- a/server/server.go +++ b/server/server.go @@ -11,6 +11,7 @@ import ( "time" "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" @@ -480,12 +481,27 @@ func (s *Server) Shutdown() error { // New creates a new Server with options provided func New(config *Config) (*Server, error) { - containerServer, err := libkpod.New(config) + store, err := cstorage.GetStore(cstorage.StoreOptions{ + RunRoot: config.RunRoot, + GraphRoot: config.Root, + GraphDriverName: config.Storage, + GraphDriverOptions: config.StorageOptions, + }) if err != nil { return nil, err } - storageRuntimeService := storage.GetRuntimeService(containerServer.StorageImageServer(), config.PauseImage) + imageService, err := storage.GetImageService(store, config.DefaultTransport, config.InsecureRegistries) + if err != nil { + return nil, err + } + + r, err := oci.New(config.Runtime, config.RuntimeUntrustedWorkload, config.DefaultWorkloadTrust, config.Conmon, config.ConmonEnv, config.CgroupManager) + if err != nil { + return nil, err + } + + storageRuntimeService := storage.GetRuntimeService(imageService, config.PauseImage) if err != nil { return nil, err } @@ -494,6 +510,8 @@ func New(config *Config) (*Server, error) { return nil, err } + 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 {