Move platformInit and plugin load to server
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
a6e77432df
commit
003ad67375
@ -69,6 +69,9 @@ func TestMain(m *testing.M) {
|
|||||||
|
|
||||||
client, err := waitForDaemonStart(ctx, address)
|
client, err := waitForDaemonStart(ctx, address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if cmd.Process != nil {
|
||||||
|
cmd.Process.Kill()
|
||||||
|
}
|
||||||
cmd.Wait()
|
cmd.Wait()
|
||||||
fmt.Fprintf(os.Stderr, "%s: %s", err, buf.String())
|
fmt.Fprintf(os.Stderr, "%s: %s", err, buf.String())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -6,17 +6,13 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
|
||||||
gocontext "golang.org/x/net/context"
|
gocontext "golang.org/x/net/context"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/containerd/containerd/content"
|
|
||||||
"github.com/containerd/containerd/log"
|
"github.com/containerd/containerd/log"
|
||||||
"github.com/containerd/containerd/plugin"
|
|
||||||
"github.com/containerd/containerd/server"
|
"github.com/containerd/containerd/server"
|
||||||
"github.com/containerd/containerd/sys"
|
"github.com/containerd/containerd/sys"
|
||||||
"github.com/containerd/containerd/version"
|
"github.com/containerd/containerd/version"
|
||||||
@ -89,18 +85,9 @@ func main() {
|
|||||||
if address == "" {
|
if address == "" {
|
||||||
return errors.New("grpc address cannot be empty")
|
return errors.New("grpc address cannot be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := platformInit(ctx, config); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
log.G(ctx).Info("starting containerd boot...")
|
log.G(ctx).Info("starting containerd boot...")
|
||||||
|
|
||||||
plugins, err := loadPlugins(config)
|
server, err := server.New(ctx, config)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
server, err := server.New(ctx, config, plugins)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -186,41 +173,6 @@ func setLevel(context *cli.Context, config *server.Config) error {
|
|||||||
return nil
|
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() {
|
func dumpStacks() {
|
||||||
var (
|
var (
|
||||||
buf []byte
|
buf []byte
|
||||||
|
@ -9,7 +9,6 @@ import (
|
|||||||
"github.com/containerd/containerd/log"
|
"github.com/containerd/containerd/log"
|
||||||
"github.com/containerd/containerd/reaper"
|
"github.com/containerd/containerd/reaper"
|
||||||
"github.com/containerd/containerd/server"
|
"github.com/containerd/containerd/server"
|
||||||
"github.com/containerd/containerd/sys"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultConfigPath = "/etc/containerd/config.toml"
|
const defaultConfigPath = "/etc/containerd/config.toml"
|
||||||
@ -21,22 +20,6 @@ var handledSignals = []os.Signal{
|
|||||||
unix.SIGCHLD,
|
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 {
|
func handleSignals(ctx context.Context, signals chan os.Signal, server *server.Server) error {
|
||||||
for s := range signals {
|
for s := range signals {
|
||||||
log.G(ctx).WithField("signal", s).Debug("received signal")
|
log.G(ctx).WithField("signal", s).Debug("received signal")
|
||||||
|
@ -22,10 +22,6 @@ var handledSignals = []os.Signal{
|
|||||||
unix.SIGCHLD,
|
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 {
|
func handleSignals(ctx context.Context, signals chan os.Signal, server *server.Server) error {
|
||||||
for s := range signals {
|
for s := range signals {
|
||||||
log.G(ctx).WithField("signal", s).Debug("received signal")
|
log.G(ctx).WithField("signal", s).Debug("received signal")
|
||||||
|
@ -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 {
|
func handleSignals(ctx context.Context, signals chan os.Signal, server *server.Server) error {
|
||||||
for s := range signals {
|
for s := range signals {
|
||||||
log.G(ctx).WithField("signal", s).Debug("received signal")
|
log.G(ctx).WithField("signal", s).Debug("received signal")
|
||||||
|
@ -7,7 +7,9 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/pprof"
|
"net/http/pprof"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/boltdb/bolt"
|
||||||
containers "github.com/containerd/containerd/api/services/containers/v1"
|
containers "github.com/containerd/containerd/api/services/containers/v1"
|
||||||
content "github.com/containerd/containerd/api/services/content/v1"
|
content "github.com/containerd/containerd/api/services/content/v1"
|
||||||
diff "github.com/containerd/containerd/api/services/diff/v1"
|
diff "github.com/containerd/containerd/api/services/diff/v1"
|
||||||
@ -16,6 +18,7 @@ import (
|
|||||||
snapshot "github.com/containerd/containerd/api/services/snapshot/v1"
|
snapshot "github.com/containerd/containerd/api/services/snapshot/v1"
|
||||||
tasks "github.com/containerd/containerd/api/services/tasks/v1"
|
tasks "github.com/containerd/containerd/api/services/tasks/v1"
|
||||||
version "github.com/containerd/containerd/api/services/version/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/events"
|
||||||
"github.com/containerd/containerd/log"
|
"github.com/containerd/containerd/log"
|
||||||
"github.com/containerd/containerd/plugin"
|
"github.com/containerd/containerd/plugin"
|
||||||
@ -28,13 +31,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// New creates and initializes a new containerd server
|
// 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 == "" {
|
if config.Root == "" {
|
||||||
return nil, errors.New("root must be specified")
|
return nil, errors.New("root must be specified")
|
||||||
}
|
}
|
||||||
if err := os.MkdirAll(config.Root, 0700); err != nil {
|
if err := os.MkdirAll(config.Root, 0700); err != nil {
|
||||||
return nil, err
|
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(
|
rpc := grpc.NewServer(
|
||||||
grpc.UnaryInterceptor(interceptor),
|
grpc.UnaryInterceptor(interceptor),
|
||||||
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
|
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
|
||||||
@ -131,6 +141,34 @@ func (s *Server) Stop() {
|
|||||||
s.rpc.GracefulStop()
|
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 {
|
func shouldLoadPlugin(p *plugin.Registration, config *Config) bool {
|
||||||
switch p.Type {
|
switch p.Type {
|
||||||
case plugin.SnapshotPlugin:
|
case plugin.SnapshotPlugin:
|
||||||
|
26
server/server_linux.go
Normal file
26
server/server_linux.go
Normal file
@ -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
|
||||||
|
}
|
9
server/server_unsupported.go
Normal file
9
server/server_unsupported.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// +build !linux
|
||||||
|
|
||||||
|
package server
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
func apply(_ context.Context, _ *Config) error {
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user