Generalize the plugin package

Remove containerd specific parts of the plugin package to prepare its
move out of the main repository. Separate the plugin registration
singleton into a separate package.

Separating out the plugin package and registration makes it easier to
implement external plugins without creating a dependency loop.

Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
Derek McGowan
2023-10-12 15:36:02 -07:00
parent a80606bc2d
commit 7b2a918213
65 changed files with 363 additions and 299 deletions

View File

@@ -239,11 +239,10 @@ type ProxyPlugin struct {
}
// Decode unmarshals a plugin specific configuration by plugin id
func (c *Config) Decode(ctx context.Context, p *plugin.Registration) (interface{}, error) {
id := p.URI()
func (c *Config) Decode(ctx context.Context, id string, config interface{}) (interface{}, error) {
data, ok := c.Plugins[id]
if !ok {
return p.Config, nil
return config, nil
}
b, err := toml.Marshal(data)
@@ -251,7 +250,7 @@ func (c *Config) Decode(ctx context.Context, p *plugin.Registration) (interface{
return nil, err
}
if err := toml.NewDecoder(bytes.NewReader(b)).DisallowUnknownFields().Decode(p.Config); err != nil {
if err := toml.NewDecoder(bytes.NewReader(b)).DisallowUnknownFields().Decode(config); err != nil {
var serr *toml.StrictMissingError
if errors.As(err, &serr) {
for _, derr := range serr.Errors {
@@ -260,7 +259,7 @@ func (c *Config) Decode(ctx context.Context, p *plugin.Registration) (interface{
"key": strings.Join(derr.Key(), " "),
}).WithError(err).Warn("Ignoring unknown key in TOML for plugin")
}
err = toml.Unmarshal(b, p.Config)
err = toml.Unmarshal(b, config)
}
if err != nil {
return nil, err
@@ -268,7 +267,7 @@ func (c *Config) Decode(ctx context.Context, p *plugin.Registration) (interface{
}
return p.Config, nil
return config, nil
}
// LoadConfig loads the containerd server config from the provided path

View File

@@ -25,7 +25,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/containerd/containerd/plugin"
"github.com/containerd/log/logtest"
)
@@ -201,7 +200,7 @@ func TestDecodePlugin(t *testing.T) {
ctx := logtest.WithT(context.Background(), t)
data := `
version = 2
[plugins."io.containerd.runtime.v1.linux"]
[plugins."io.containerd.runtime.v2.task"]
shim_debug = true
`
@@ -216,7 +215,7 @@ version = 2
assert.NoError(t, err)
pluginConfig := map[string]interface{}{}
_, err = out.Decode(ctx, &plugin.Registration{Type: "io.containerd.runtime.v1", ID: "linux", Config: &pluginConfig})
_, err = out.Decode(ctx, "io.containerd.runtime.v2.task", &pluginConfig)
assert.NoError(t, err)
assert.Equal(t, true, pluginConfig["shim_debug"])
}
@@ -226,7 +225,7 @@ version = 2
func TestDecodePluginInV1Config(t *testing.T) {
ctx := logtest.WithT(context.Background(), t)
data := `
[plugins.linux]
[plugins.task]
shim_debug = true
`
@@ -244,7 +243,7 @@ func TestDecodePluginInV1Config(t *testing.T) {
assert.Equal(t, 3, out.Version)
pluginConfig := map[string]interface{}{}
_, err = out.Decode(ctx, &plugin.Registration{Type: "io.containerd.runtime.v1", ID: "linux", Config: &pluginConfig})
_, err = out.Decode(ctx, "io.containerd.runtime.v2.task", &pluginConfig)
assert.NoError(t, err)
assert.Equal(t, true, pluginConfig["shim_debug"])
}