From 003ad673757832076175831b5e5cc6ab4dc89487 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Thu, 22 Jun 2017 13:34:37 -0700 Subject: [PATCH] Move platformInit and plugin load to server Signed-off-by: Michael Crosby --- client_test.go | 3 ++ cmd/containerd/main.go | 50 +--------------------------------- cmd/containerd/main_linux.go | 17 ------------ cmd/containerd/main_unix.go | 4 --- cmd/containerd/main_windows.go | 4 --- server/server.go | 40 ++++++++++++++++++++++++++- server/server_linux.go | 26 ++++++++++++++++++ server/server_unsupported.go | 9 ++++++ 8 files changed, 78 insertions(+), 75 deletions(-) create mode 100644 server/server_linux.go create mode 100644 server/server_unsupported.go diff --git a/client_test.go b/client_test.go index 5bed570ad..1703f1c58 100644 --- a/client_test.go +++ b/client_test.go @@ -69,6 +69,9 @@ func TestMain(m *testing.M) { client, err := waitForDaemonStart(ctx, address) if err != nil { + if cmd.Process != nil { + cmd.Process.Kill() + } cmd.Wait() fmt.Fprintf(os.Stderr, "%s: %s", err, buf.String()) os.Exit(1) diff --git a/cmd/containerd/main.go b/cmd/containerd/main.go index bfdf4fc69..7248751ec 100644 --- a/cmd/containerd/main.go +++ b/cmd/containerd/main.go @@ -6,17 +6,13 @@ import ( "net" "os" "os/signal" - "path/filepath" "runtime" "time" - "github.com/boltdb/bolt" gocontext "golang.org/x/net/context" "github.com/Sirupsen/logrus" - "github.com/containerd/containerd/content" "github.com/containerd/containerd/log" - "github.com/containerd/containerd/plugin" "github.com/containerd/containerd/server" "github.com/containerd/containerd/sys" "github.com/containerd/containerd/version" @@ -89,18 +85,9 @@ func main() { if address == "" { return errors.New("grpc address cannot be empty") } - - if err := platformInit(ctx, config); err != nil { - return err - } log.G(ctx).Info("starting containerd boot...") - plugins, err := loadPlugins(config) - if err != nil { - return err - } - - server, err := server.New(ctx, config, plugins) + server, err := server.New(ctx, config) if err != nil { return err } @@ -186,41 +173,6 @@ func setLevel(context *cli.Context, config *server.Config) error { return nil } -func loadPlugins(config *server.Config) ([]*plugin.Registration, error) { - // load all plugins into containerd - if err := plugin.Load(filepath.Join(config.Root, "plugins")); err != nil { - return nil, err - } - // load additional plugins that don't automatically register themselves - registerContentStore() - registerMetaDB() - // return the ordered graph for plugins - return plugin.Graph(), nil -} - -func registerContentStore() { - plugin.Register(&plugin.Registration{ - Type: plugin.ContentPlugin, - ID: "content", - Init: func(ic *plugin.InitContext) (interface{}, error) { - return content.NewStore(ic.Root) - }, - }) -} - -func registerMetaDB() { - plugin.Register(&plugin.Registration{ - Type: plugin.MetadataPlugin, - ID: "bolt", - Init: func(ic *plugin.InitContext) (interface{}, error) { - if err := os.MkdirAll(ic.Root, 0700); err != nil { - return nil, err - } - return bolt.Open(filepath.Join(ic.Root, "meta.db"), 0644, nil) - }, - }) -} - func dumpStacks() { var ( buf []byte diff --git a/cmd/containerd/main_linux.go b/cmd/containerd/main_linux.go index f17be7a86..3f8527258 100644 --- a/cmd/containerd/main_linux.go +++ b/cmd/containerd/main_linux.go @@ -9,7 +9,6 @@ import ( "github.com/containerd/containerd/log" "github.com/containerd/containerd/reaper" "github.com/containerd/containerd/server" - "github.com/containerd/containerd/sys" ) const defaultConfigPath = "/etc/containerd/config.toml" @@ -21,22 +20,6 @@ var handledSignals = []os.Signal{ unix.SIGCHLD, } -func platformInit(ctx context.Context, config *server.Config) error { - if config.Subreaper { - log.G(ctx).Info("setting subreaper...") - if err := sys.SetSubreaper(1); err != nil { - return err - } - } - if config.OOMScore != 0 { - log.G(ctx).Infof("changing OOM score to %d", config.OOMScore) - if err := sys.SetOOMScore(os.Getpid(), config.OOMScore); err != nil { - return err - } - } - return nil -} - func handleSignals(ctx context.Context, signals chan os.Signal, server *server.Server) error { for s := range signals { log.G(ctx).WithField("signal", s).Debug("received signal") diff --git a/cmd/containerd/main_unix.go b/cmd/containerd/main_unix.go index f90fbc584..09bb41645 100644 --- a/cmd/containerd/main_unix.go +++ b/cmd/containerd/main_unix.go @@ -22,10 +22,6 @@ var handledSignals = []os.Signal{ unix.SIGCHLD, } -func platformInit(context context.Context, config *server.Config) error { - return nil -} - func handleSignals(ctx context.Context, signals chan os.Signal, server *server.Server) error { for s := range signals { log.G(ctx).WithField("signal", s).Debug("received signal") diff --git a/cmd/containerd/main_windows.go b/cmd/containerd/main_windows.go index d5df67281..f1206bc09 100644 --- a/cmd/containerd/main_windows.go +++ b/cmd/containerd/main_windows.go @@ -18,10 +18,6 @@ var ( } ) -func platformInit(context context.Context, config *server.Config) error { - return nil -} - func handleSignals(ctx context.Context, signals chan os.Signal, server *server.Server) error { for s := range signals { log.G(ctx).WithField("signal", s).Debug("received signal") diff --git a/server/server.go b/server/server.go index cfc34c5c2..7e5df8f9f 100644 --- a/server/server.go +++ b/server/server.go @@ -7,7 +7,9 @@ import ( "net/http" "net/http/pprof" "os" + "path/filepath" + "github.com/boltdb/bolt" containers "github.com/containerd/containerd/api/services/containers/v1" content "github.com/containerd/containerd/api/services/content/v1" diff "github.com/containerd/containerd/api/services/diff/v1" @@ -16,6 +18,7 @@ import ( snapshot "github.com/containerd/containerd/api/services/snapshot/v1" tasks "github.com/containerd/containerd/api/services/tasks/v1" version "github.com/containerd/containerd/api/services/version/v1" + store "github.com/containerd/containerd/content" "github.com/containerd/containerd/events" "github.com/containerd/containerd/log" "github.com/containerd/containerd/plugin" @@ -28,13 +31,20 @@ import ( ) // New creates and initializes a new containerd server -func New(ctx context.Context, config *Config, plugins []*plugin.Registration) (*Server, error) { +func New(ctx context.Context, config *Config) (*Server, error) { if config.Root == "" { return nil, errors.New("root must be specified") } if err := os.MkdirAll(config.Root, 0700); err != nil { return nil, err } + if err := apply(ctx, config); err != nil { + return nil, err + } + plugins, err := loadPlugins(config) + if err != nil { + return nil, err + } rpc := grpc.NewServer( grpc.UnaryInterceptor(interceptor), grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor), @@ -131,6 +141,34 @@ func (s *Server) Stop() { s.rpc.GracefulStop() } +func loadPlugins(config *Config) ([]*plugin.Registration, error) { + // load all plugins into containerd + if err := plugin.Load(filepath.Join(config.Root, "plugins")); err != nil { + return nil, err + } + // load additional plugins that don't automatically register themselves + plugin.Register(&plugin.Registration{ + Type: plugin.ContentPlugin, + ID: "content", + Init: func(ic *plugin.InitContext) (interface{}, error) { + return store.NewStore(ic.Root) + }, + }) + plugin.Register(&plugin.Registration{ + Type: plugin.MetadataPlugin, + ID: "bolt", + Init: func(ic *plugin.InitContext) (interface{}, error) { + if err := os.MkdirAll(ic.Root, 0700); err != nil { + return nil, err + } + return bolt.Open(filepath.Join(ic.Root, "meta.db"), 0644, nil) + }, + }) + + // return the ordered graph for plugins + return plugin.Graph(), nil +} + func shouldLoadPlugin(p *plugin.Registration, config *Config) bool { switch p.Type { case plugin.SnapshotPlugin: diff --git a/server/server_linux.go b/server/server_linux.go new file mode 100644 index 000000000..ad63f49ac --- /dev/null +++ b/server/server_linux.go @@ -0,0 +1,26 @@ +package server + +import ( + "context" + "os" + + "github.com/containerd/containerd/log" + "github.com/containerd/containerd/sys" +) + +// apply sets config settings on the server process +func apply(ctx context.Context, config *Config) error { + if config.Subreaper { + log.G(ctx).Info("setting subreaper...") + if err := sys.SetSubreaper(1); err != nil { + return err + } + } + if config.OOMScore != 0 { + log.G(ctx).Infof("changing OOM score to %d", config.OOMScore) + if err := sys.SetOOMScore(os.Getpid(), config.OOMScore); err != nil { + return err + } + } + return nil +} diff --git a/server/server_unsupported.go b/server/server_unsupported.go new file mode 100644 index 000000000..da43ff49d --- /dev/null +++ b/server/server_unsupported.go @@ -0,0 +1,9 @@ +// +build !linux + +package server + +import "context" + +func apply(_ context.Context, _ *Config) error { + return nil +}