Add v2 server config support with plugin URIs

Closes #3210

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby
2019-06-04 17:55:58 +00:00
parent 42f4bb98ac
commit 9547d269a1
10 changed files with 147 additions and 33 deletions

View File

@@ -17,13 +17,18 @@
package config
import (
"strings"
"github.com/BurntSushi/toml"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/plugin"
"github.com/pkg/errors"
)
// Config provides containerd configuration data for the server
type Config struct {
// Version of the config file
Version int `toml:"version"`
// Root is the path to a directory where containerd will store persistent data
Root string `toml:"root"`
// State is the path to a directory where containerd will store transient data
@@ -54,6 +59,42 @@ type Config struct {
md toml.MetaData
}
// GetVersion returns the config file's version
func (c *Config) GetVersion() int {
if c.Version == 0 {
return 1
}
return c.Version
}
// ValidateV2 validates the config for a v2 file
func (c *Config) ValidateV2() error {
if c.GetVersion() != 2 {
return nil
}
for _, p := range c.DisabledPlugins {
if len(strings.Split(p, ".")) < 4 {
return errors.Errorf("invalid disabled plugin URI %q expect io.containerd.x.vx", p)
}
}
for _, p := range c.RequiredPlugins {
if len(strings.Split(p, ".")) < 4 {
return errors.Errorf("invalid required plugin URI %q expect io.containerd.x.vx", p)
}
}
for p := range c.Plugins {
if len(strings.Split(p, ".")) < 4 {
return errors.Errorf("invalid plugin key URI %q expect io.containerd.x.vx", p)
}
}
for p := range c.ProxyPlugins {
if len(strings.Split(p, ".")) < 4 {
return errors.Errorf("invalid proxy plugin key URI %q expect io.containerd.x.vx", p)
}
}
return nil
}
// GRPCConfig provides GRPC configuration for the socket
type GRPCConfig struct {
Address string `toml:"address"`
@@ -130,15 +171,19 @@ func (bc *BoltConfig) Validate() error {
}
// Decode unmarshals a plugin specific configuration by plugin id
func (c *Config) Decode(id string, v interface{}) (interface{}, error) {
func (c *Config) Decode(p *plugin.Registration) (interface{}, error) {
id := p.URI()
if c.GetVersion() == 1 {
id = p.ID
}
data, ok := c.Plugins[id]
if !ok {
return v, nil
return p.Config, nil
}
if err := c.md.PrimitiveDecode(data, v); err != nil {
if err := c.md.PrimitiveDecode(data, p.Config); err != nil {
return nil, err
}
return v, nil
return p.Config, nil
}
// LoadConfig loads the containerd server config from the provided path
@@ -151,5 +196,29 @@ func LoadConfig(path string, v *Config) error {
return err
}
v.md = md
return nil
return v.ValidateV2()
}
// V1DisabledFilter matches based on ID
func V1DisabledFilter(list []string) plugin.DisableFilter {
set := make(map[string]struct{}, len(list))
for _, l := range list {
set[l] = struct{}{}
}
return func(r *plugin.Registration) bool {
_, ok := set[r.ID]
return ok
}
}
// V2DisabledFilter matches based on URI
func V2DisabledFilter(list []string) plugin.DisableFilter {
set := make(map[string]struct{}, len(list))
for _, l := range list {
set[l] = struct{}{}
}
return func(r *plugin.Registration) bool {
_, ok := set[r.URI()]
return ok
}
}

View File

@@ -126,6 +126,10 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) {
}
for _, p := range plugins {
id := p.URI()
reqID := id
if config.GetVersion() == 1 {
reqID = p.ID
}
log.G(ctx).WithField("type", p.Type).Infof("loading plugin %q...", id)
initContext := plugin.NewContext(
@@ -140,11 +144,11 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) {
// load the plugin specific configuration if it is provided
if p.Config != nil {
pluginConfig, err := config.Decode(p.ID, p.Config)
pc, err := config.Decode(p)
if err != nil {
return nil, err
}
initContext.Config = pluginConfig
initContext.Config = pc
}
result := p.Init(initContext)
if err := initialized.Add(result); err != nil {
@@ -158,12 +162,13 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) {
} else {
log.G(ctx).WithError(err).Warnf("failed to load plugin %s", id)
}
if _, ok := required[p.ID]; ok {
if _, ok := required[reqID]; ok {
return nil, errors.Wrapf(err, "load required plugin %s", id)
}
continue
}
delete(required, p.ID)
delete(required, reqID)
// check for grpc services that should be registered with the server
if src, ok := instance.(plugin.Service); ok {
grpcServices = append(grpcServices, src)
@@ -266,7 +271,7 @@ func (s *Server) Stop() {
p := s.plugins[i]
instance, err := p.Instance()
if err != nil {
log.L.WithError(err).WithField("id", p.Registration.ID).
log.L.WithError(err).WithField("id", p.Registration.URI()).
Errorf("could not get plugin instance")
continue
}
@@ -275,7 +280,7 @@ func (s *Server) Stop() {
continue
}
if err := closer.Close(); err != nil {
log.L.WithError(err).WithField("id", p.Registration.ID).
log.L.WithError(err).WithField("id", p.Registration.URI()).
Errorf("failed to close plugin")
}
}
@@ -415,8 +420,12 @@ func LoadPlugins(ctx context.Context, config *srvconfig.Config) ([]*plugin.Regis
}
filter := srvconfig.V2DisabledFilter
if config.GetVersion() == 1 {
filter = srvconfig.V1DisabledFilter
}
// return the ordered graph for plugins
return plugin.Graph(config.DisabledPlugins), nil
return plugin.Graph(filter(config.DisabledPlugins)), nil
}
type proxyClients struct {