From 314d55f284c6ce153f2eb7256349ac4e0a6a0736 Mon Sep 17 00:00:00 2001 From: Ryan Cole Date: Thu, 20 Jul 2017 14:53:40 -0400 Subject: [PATCH] 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 {