Skip to content

Commit 0a0621b

Browse files
committed
Move plugin context events into separate plugin
Signed-off-by: Derek McGowan <derek@mcg.dev>
1 parent 7d48917 commit 0a0621b

File tree

19 files changed

+137
-16
lines changed

19 files changed

+137
-16
lines changed

cmd/containerd/builtins.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919
// register containerd builtins here
2020
import (
2121
_ "github.com/containerd/containerd/diff/walking/plugin"
22+
_ "github.com/containerd/containerd/events/plugin"
2223
_ "github.com/containerd/containerd/gc/scheduler"
2324
_ "github.com/containerd/containerd/runtime/restart/monitor"
2425
_ "github.com/containerd/containerd/services/containers"

events/plugin/plugin.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 plugin
18+
19+
import (
20+
"github.com/containerd/containerd/plugin"
21+
)
22+
23+
func init() {
24+
plugin.Register(&plugin.Registration{
25+
Type: plugin.EventPlugin,
26+
ID: "exchange",
27+
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
28+
// TODO: In 2.0, create exchange since ic.Events will be removed
29+
return ic.Events, nil
30+
},
31+
})
32+
}

metrics/cgroups/cgroups.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package cgroups
2020

2121
import (
2222
"github.com/containerd/cgroups"
23+
"github.com/containerd/containerd/events"
2324
v1 "github.com/containerd/containerd/metrics/cgroups/v1"
2425
v2 "github.com/containerd/containerd/metrics/cgroups/v2"
2526
"github.com/containerd/containerd/platforms"
@@ -38,6 +39,9 @@ func init() {
3839
Type: plugin.TaskMonitorPlugin,
3940
ID: "cgroups",
4041
InitFn: New,
42+
Requires: []plugin.Type{
43+
plugin.EventPlugin,
44+
},
4145
Config: &Config{},
4246
})
4347
}
@@ -53,10 +57,16 @@ func New(ic *plugin.InitContext) (interface{}, error) {
5357
tm runtime.TaskMonitor
5458
err error
5559
)
60+
61+
ep, err := ic.Get(plugin.EventPlugin)
62+
if err != nil {
63+
return nil, err
64+
}
65+
5666
if cgroups.Mode() == cgroups.Unified {
57-
tm, err = v2.NewTaskMonitor(ic.Context, ic.Events, ns)
67+
tm, err = v2.NewTaskMonitor(ic.Context, ep.(events.Publisher), ns)
5868
} else {
59-
tm, err = v1.NewTaskMonitor(ic.Context, ic.Events, ns)
69+
tm, err = v1.NewTaskMonitor(ic.Context, ep.(events.Publisher), ns)
6070
}
6171
if err != nil {
6272
return nil, err

pkg/cri/cri.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func init() {
5353
ID: "cri",
5454
Config: &config,
5555
Requires: []plugin.Type{
56+
plugin.EventPlugin,
5657
plugin.ServicePlugin,
5758
},
5859
InitFn: initCRIService,
@@ -118,8 +119,13 @@ func getServicesOpts(ic *plugin.InitContext) ([]containerd.ServicesOpt, error) {
118119
return nil, errors.Wrap(err, "failed to get service plugin")
119120
}
120121

122+
ep, err := ic.Get(plugin.EventPlugin)
123+
if err != nil {
124+
return nil, errors.Wrap(err, "failed to get event plugin")
125+
}
126+
121127
opts := []containerd.ServicesOpt{
122-
containerd.WithEventService(ic.Events),
128+
containerd.WithEventService(ep.(containerd.EventService)),
123129
}
124130
for s, fn := range map[string]func(interface{}) containerd.ServicesOpt{
125131
services.ContentService: func(s interface{}) containerd.ServicesOpt {

plugin/context.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ type InitContext struct {
3434
Config interface{}
3535
Address string
3636
TTRPCAddress string
37-
Events *exchange.Exchange
37+
38+
// deprecated: will be removed in 2.0, use plugin.EventType
39+
Events *exchange.Exchange
3840

3941
Meta *Meta // plugins can fill in metadata at init.
4042

@@ -135,6 +137,19 @@ func (i *InitContext) GetAll() []*Plugin {
135137
return i.plugins.ordered
136138
}
137139

140+
// GetByID returns the plugin of the given type and ID
141+
func (i *InitContext) GetByID(t Type, id string) (interface{}, error) {
142+
ps, err := i.GetByType(t)
143+
if err != nil {
144+
return nil, err
145+
}
146+
p, ok := ps[id]
147+
if !ok {
148+
return nil, errors.Wrapf(errdefs.ErrNotFound, "no %s plugins with id %s", t, id)
149+
}
150+
return p.Instance()
151+
}
152+
138153
// GetByType returns all plugins with the specific type.
139154
func (i *InitContext) GetByType(t Type) (map[string]*Plugin, error) {
140155
p, ok := i.plugins.byTypeAndID[t]

plugin/plugin.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ const (
7575
ContentPlugin Type = "io.containerd.content.v1"
7676
// GCPlugin implements garbage collection policy
7777
GCPlugin Type = "io.containerd.gc.v1"
78+
// EventPlugin implements event handling
79+
EventPlugin Type = "io.containerd.event.v1"
7880
)
7981

8082
const (

runtime/restart/monitor/monitor.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func init() {
6363
plugin.Register(&plugin.Registration{
6464
Type: plugin.InternalPlugin,
6565
Requires: []plugin.Type{
66+
plugin.EventPlugin,
6667
plugin.ServicePlugin,
6768
},
6869
ID: "restart",
@@ -95,8 +96,14 @@ func getServicesOpts(ic *plugin.InitContext) ([]containerd.ServicesOpt, error) {
9596
if err != nil {
9697
return nil, errors.Wrap(err, "failed to get service plugin")
9798
}
99+
100+
ep, err := ic.Get(plugin.EventPlugin)
101+
if err != nil {
102+
return nil, errors.Wrap(err, "failed to get event plugin")
103+
}
104+
98105
opts := []containerd.ServicesOpt{
99-
containerd.WithEventService(ic.Events),
106+
containerd.WithEventService(ep.(containerd.EventService)),
100107
}
101108
for s, fn := range map[string]func(interface{}) containerd.ServicesOpt{
102109
services.ContentService: func(s interface{}) containerd.ServicesOpt {

runtime/v1/linux/runtime.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func init() {
7373
ID: "linux",
7474
InitFn: New,
7575
Requires: []plugin.Type{
76+
plugin.EventPlugin,
7677
plugin.MetadataPlugin,
7778
},
7879
Config: &Config{
@@ -112,14 +113,20 @@ func New(ic *plugin.InitContext) (interface{}, error) {
112113
if err != nil {
113114
return nil, err
114115
}
116+
117+
ep, err := ic.GetByID(plugin.EventPlugin, "exchange")
118+
if err != nil {
119+
return nil, err
120+
}
121+
115122
cfg := ic.Config.(*Config)
116123
r := &Runtime{
117124
root: ic.Root,
118125
state: ic.State,
119126
tasks: runtime.NewTaskList(),
120127
containers: metadata.NewContainerStore(m.(*metadata.DB)),
121128
address: ic.Address,
122-
events: ic.Events,
129+
events: ep.(*exchange.Exchange),
123130
config: cfg,
124131
}
125132
tasks, err := r.restoreTasks(ic.Context)

runtime/v2/manager.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func init() {
4949
Type: plugin.RuntimePluginV2,
5050
ID: "task",
5151
Requires: []plugin.Type{
52+
plugin.EventPlugin,
5253
plugin.MetadataPlugin,
5354
},
5455
Config: &Config{
@@ -71,9 +72,14 @@ func init() {
7172
if err != nil {
7273
return nil, err
7374
}
75+
ep, err := ic.GetByID(plugin.EventPlugin, "exchange")
76+
if err != nil {
77+
return nil, err
78+
}
7479
cs := metadata.NewContainerStore(m.(*metadata.DB))
80+
events := ep.(*exchange.Exchange)
7581

76-
return New(ic.Context, ic.Root, ic.State, ic.Address, ic.TTRPCAddress, ic.Events, cs)
82+
return New(ic.Context, ic.Root, ic.State, ic.Address, ic.TTRPCAddress, events, cs)
7783
},
7884
})
7985
}

services/containers/local.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,24 @@ func init() {
4141
Type: plugin.ServicePlugin,
4242
ID: services.ContainersService,
4343
Requires: []plugin.Type{
44+
plugin.EventPlugin,
4445
plugin.MetadataPlugin,
4546
},
4647
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
4748
m, err := ic.Get(plugin.MetadataPlugin)
4849
if err != nil {
4950
return nil, err
5051
}
52+
ep, err := ic.Get(plugin.EventPlugin)
53+
if err != nil {
54+
return nil, err
55+
}
5156

5257
db := m.(*metadata.DB)
5358
return &local{
5459
Store: metadata.NewContainerStore(db),
5560
db: db,
56-
publisher: ic.Events,
61+
publisher: ep.(events.Publisher),
5762
}, nil
5863
},
5964
})

0 commit comments

Comments
 (0)