Skip to content

Commit 497c493

Browse files
authored
Merge pull request #9214 from dmcgowan/generalize-plugin-library
Generalize plugin library
2 parents dcba353 + 10ab410 commit 497c493

7 files changed

Lines changed: 239 additions & 252 deletions

File tree

context.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,15 @@ package plugin
1919
import (
2020
"context"
2121
"fmt"
22-
"path/filepath"
2322

24-
"github.com/containerd/containerd/errdefs"
25-
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
23+
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
2624
)
2725

2826
// InitContext is used for plugin initialization
2927
type InitContext struct {
3028
Context context.Context
31-
Root string
32-
State string
29+
Properties map[string]string
3330
Config interface{}
34-
Address string
35-
TTRPCAddress string
3631
RegisterReadiness func() func()
3732

3833
// Meta is metadata plugins can fill in at init
@@ -42,11 +37,13 @@ type InitContext struct {
4237
}
4338

4439
// NewContext returns a new plugin InitContext
45-
func NewContext(ctx context.Context, r *Registration, plugins *Set, root, state string) *InitContext {
40+
func NewContext(ctx context.Context, plugins *Set, properties map[string]string) *InitContext {
41+
if properties == nil {
42+
properties = map[string]string{}
43+
}
4644
return &InitContext{
47-
Context: ctx,
48-
Root: filepath.Join(root, r.URI()),
49-
State: filepath.Join(state, r.URI()),
45+
Context: ctx,
46+
Properties: properties,
5047
Meta: &Meta{
5148
Exports: map[string]string{},
5249
},
@@ -62,16 +59,16 @@ func (i *InitContext) Get(t Type) (interface{}, error) {
6259
// Meta contains information gathered from the registration and initialization
6360
// process.
6461
type Meta struct {
65-
Platforms []ocispec.Platform // platforms supported by plugin
66-
Exports map[string]string // values exported by plugin
67-
Capabilities []string // feature switches for plugin
62+
Platforms []imagespec.Platform // platforms supported by plugin
63+
Exports map[string]string // values exported by plugin
64+
Capabilities []string // feature switches for plugin
6865
}
6966

7067
// Plugin represents an initialized plugin, used with an init context.
7168
type Plugin struct {
72-
Registration *Registration // registration, as initialized
73-
Config interface{} // config, as initialized
74-
Meta *Meta
69+
Registration Registration // registration, as initialized
70+
Config interface{} // config, as initialized
71+
Meta Meta
7572

7673
instance interface{}
7774
err error // will be set if there was an error initializing the plugin
@@ -115,7 +112,7 @@ func (ps *Set) Add(p *Plugin) error {
115112
} else if _, idok := byID[p.Registration.ID]; !idok {
116113
byID[p.Registration.ID] = p
117114
} else {
118-
return fmt.Errorf("plugin %v already initialized: %w", p.Registration.URI(), errdefs.ErrAlreadyExists)
115+
return fmt.Errorf("plugin add failed for %s: %w", p.Registration.URI(), ErrPluginInitialized)
119116
}
120117

121118
ps.ordered = append(ps.ordered, p)
@@ -127,7 +124,7 @@ func (ps *Set) Get(t Type) (interface{}, error) {
127124
for _, v := range ps.byTypeAndID[t] {
128125
return v.Instance()
129126
}
130-
return nil, fmt.Errorf("no plugins registered for %s: %w", t, errdefs.ErrNotFound)
127+
return nil, fmt.Errorf("no plugins registered for %s: %w", t, ErrPluginNotFound)
131128
}
132129

133130
// GetAll returns all initialized plugins
@@ -153,7 +150,7 @@ func (i *InitContext) GetByID(t Type, id string) (interface{}, error) {
153150
}
154151
p, ok := ps[id]
155152
if !ok {
156-
return nil, fmt.Errorf("no %s plugins with id %s: %w", t, id, errdefs.ErrNotFound)
153+
return nil, fmt.Errorf("no %s plugins with id %s: %w", t, id, ErrPluginNotFound)
157154
}
158155
return p.Instance()
159156
}
@@ -162,7 +159,7 @@ func (i *InitContext) GetByID(t Type, id string) (interface{}, error) {
162159
func (i *InitContext) GetByType(t Type) (map[string]*Plugin, error) {
163160
p, ok := i.plugins.byTypeAndID[t]
164161
if !ok {
165-
return nil, fmt.Errorf("no plugins registered for %s: %w", t, errdefs.ErrNotFound)
162+
return nil, fmt.Errorf("no plugins registered for %s: %w", t, ErrPluginNotFound)
166163
}
167164

168165
return p, nil

dynamic/dynamic.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package dynamic
18+
19+
import "fmt"
20+
21+
// Load loads all plugins at the provided path into containerd.
22+
//
23+
// Load is currently only implemented on non-static, non-gccgo builds for amd64
24+
// and arm64, and plugins must be built with the exact same version of Go as
25+
// containerd itself.
26+
func Load(path string) (err error) {
27+
defer func() {
28+
if v := recover(); v != nil {
29+
rerr, ok := v.(error)
30+
if !ok {
31+
rerr = fmt.Errorf("%s", v)
32+
}
33+
err = rerr
34+
}
35+
}()
36+
return loadPlugins(path)
37+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
limitations under the License.
1717
*/
1818

19-
package plugin
19+
package dynamic
2020

2121
import (
2222
"fmt"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
limitations under the License.
1717
*/
1818

19-
package plugin
19+
package dynamic
2020

2121
// loadPlugins is not supported;
2222
//

0 commit comments

Comments
 (0)