From 4fe17ee16dd09a6a3a69ff92380e8e39d8a877ef Mon Sep 17 00:00:00 2001 From: Ryan Cole Date: Thu, 20 Jul 2017 15:43:01 -0400 Subject: [PATCH] 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 }