Skip to content

Commit 9795677

Browse files
committed
Move cri base plugin to CRI runtime service
Create new plugin type for CRI runtime and image services. Signed-off-by: Derek McGowan <[email protected]>
1 parent 22e1a2e commit 9795677

File tree

20 files changed

+205
-104
lines changed

20 files changed

+205
-104
lines changed

cmd/containerd/builtins/cri.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ package builtins
2121
import (
2222
_ "github.com/containerd/containerd/v2/pkg/cri"
2323
_ "github.com/containerd/containerd/v2/plugins/cri/images"
24+
_ "github.com/containerd/containerd/v2/plugins/cri/runtime"
2425
)

contrib/fuzz/builtins.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
_ "github.com/containerd/containerd/v2/pkg/events/plugin"
2424
_ "github.com/containerd/containerd/v2/pkg/nri/plugin"
2525
_ "github.com/containerd/containerd/v2/plugins/cri/images"
26+
_ "github.com/containerd/containerd/v2/plugins/cri/runtime"
2627
_ "github.com/containerd/containerd/v2/plugins/diff/walking/plugin"
2728
_ "github.com/containerd/containerd/v2/plugins/gc"
2829
_ "github.com/containerd/containerd/v2/plugins/imageverifier"

contrib/fuzz/cri_server_fuzzer.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/containerd/containerd/v2/pkg/cri/server"
3030
"github.com/containerd/containerd/v2/pkg/cri/server/images"
3131
"github.com/containerd/containerd/v2/pkg/oci"
32+
"github.com/containerd/errdefs"
3233
)
3334

3435
func FuzzCRIServer(data []byte) int {
@@ -42,7 +43,6 @@ func FuzzCRIServer(data []byte) int {
4243
}
4344
defer client.Close()
4445

45-
config := criconfig.Config{}
4646
imageConfig := criconfig.ImageConfig{}
4747

4848
imageService, err := images.NewService(imageConfig, &images.CRIImageServiceOptions{
@@ -52,10 +52,10 @@ func FuzzCRIServer(data []byte) int {
5252
panic(err)
5353
}
5454

55-
c, rs, err := server.NewCRIService(config, &server.CRIServiceOptions{
56-
ImageService: imageService,
57-
Client: client,
58-
BaseOCISpecs: map[string]*oci.Spec{},
55+
c, rs, err := server.NewCRIService(&server.CRIServiceOptions{
56+
RuntimeService: &fakeRuntimeService{},
57+
ImageService: imageService,
58+
Client: client,
5959
})
6060
if err != nil {
6161
panic(err)
@@ -68,6 +68,16 @@ func FuzzCRIServer(data []byte) int {
6868
})
6969
}
7070

71+
type fakeRuntimeService struct{}
72+
73+
func (fakeRuntimeService) Config() criconfig.Config {
74+
return criconfig.Config{}
75+
}
76+
77+
func (fakeRuntimeService) LoadOCISpec(string) (*oci.Spec, error) {
78+
return nil, errdefs.ErrNotFound
79+
}
80+
7181
type service struct {
7282
server.CRIService
7383
runtime.RuntimeServiceServer

integration/build_local_containerd_helper_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
_ "github.com/containerd/containerd/v2/core/runtime/v2/runc/options"
3939
_ "github.com/containerd/containerd/v2/pkg/events/plugin"
4040
_ "github.com/containerd/containerd/v2/plugins/cri/images"
41+
_ "github.com/containerd/containerd/v2/plugins/cri/runtime"
4142
_ "github.com/containerd/containerd/v2/plugins/diff/walking/plugin"
4243
_ "github.com/containerd/containerd/v2/plugins/gc"
4344
_ "github.com/containerd/containerd/v2/plugins/leases"

pkg/cri/config/config.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,6 @@ type PluginConfig struct {
319319
ContainerdConfig `toml:"containerd" json:"containerd"`
320320
// CniConfig contains config related to cni
321321
CniConfig `toml:"cni" json:"cni"`
322-
// DisableTCPService disables serving CRI on the TCP server.
323-
DisableTCPService bool `toml:"disable_tcp_service" json:"disableTCPService"`
324322
// StreamServerAddress is the ip address streaming server is listening on.
325323
StreamServerAddress string `toml:"stream_server_address" json:"streamServerAddress"`
326324
// StreamServerPort is the port streaming server is listening on.
@@ -433,6 +431,12 @@ type Config struct {
433431
StateDir string `json:"stateDir"`
434432
}
435433

434+
// ServiceConfig contains all the configuration for the CRI API server.
435+
type ServiceConfig struct {
436+
// DisableTCPService disables serving CRI on the TCP server.
437+
DisableTCPService bool `toml:"disable_tcp_service" json:"disableTCPService"`
438+
}
439+
436440
const (
437441
// RuntimeUntrusted is the implicit runtime defined for ContainerdConfig.UntrustedWorkloadRuntime
438442
RuntimeUntrusted = "untrusted"

pkg/cri/config/config_unix.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ func DefaultConfig() PluginConfig {
8989
},
9090
},
9191
},
92-
DisableTCPService: true,
9392
StreamServerAddress: "127.0.0.1",
9493
StreamServerPort: "0",
9594
StreamIdleTimeout: streaming.DefaultConfig.StreamIdleTimeout.String(), // 4 hour

pkg/cri/config/config_windows.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ func DefaultConfig() PluginConfig {
7878
},
7979
},
8080
},
81-
DisableTCPService: true,
8281
StreamServerAddress: "127.0.0.1",
8382
StreamServerPort: "0",
8483
StreamIdleTimeout: streaming.DefaultConfig.StreamIdleTimeout.String(), // 4 hour

pkg/cri/cri.go

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package cri
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"io"
2223

@@ -25,13 +26,13 @@ import (
2526
"github.com/containerd/plugin/registry"
2627

2728
containerd "github.com/containerd/containerd/v2/client"
29+
srvconfig "github.com/containerd/containerd/v2/cmd/containerd/server/config"
2830
"github.com/containerd/containerd/v2/core/sandbox"
2931
criconfig "github.com/containerd/containerd/v2/pkg/cri/config"
3032
"github.com/containerd/containerd/v2/pkg/cri/constants"
3133
"github.com/containerd/containerd/v2/pkg/cri/instrument"
3234
"github.com/containerd/containerd/v2/pkg/cri/nri"
3335
"github.com/containerd/containerd/v2/pkg/cri/server"
34-
"github.com/containerd/containerd/v2/pkg/cri/server/base"
3536
nriservice "github.com/containerd/containerd/v2/pkg/nri"
3637
"github.com/containerd/containerd/v2/plugins"
3738
"github.com/containerd/platforms"
@@ -43,13 +44,11 @@ import (
4344

4445
// Register CRI service plugin
4546
func init() {
46-
4747
registry.Register(&plugin.Registration{
4848
Type: plugins.GRPCPlugin,
4949
ID: "cri",
5050
Requires: []plugin.Type{
51-
plugins.CRIImagePlugin,
52-
plugins.InternalPlugin,
51+
plugins.CRIServicePlugin,
5352
plugins.SandboxControllerPlugin,
5453
plugins.NRIApiPlugin,
5554
plugins.EventPlugin,
@@ -58,23 +57,46 @@ func init() {
5857
plugins.SandboxStorePlugin,
5958
plugins.TransferPlugin,
6059
},
60+
Config: &criconfig.ServiceConfig{
61+
DisableTCPService: true,
62+
},
63+
ConfigMigration: func(ctx context.Context, version int, pluginConfigs map[string]interface{}) error {
64+
if version >= srvconfig.CurrentConfigVersion {
65+
return nil
66+
}
67+
const pluginName = string(plugins.GRPCPlugin) + ".cri"
68+
original, ok := pluginConfigs[pluginName]
69+
if !ok {
70+
return nil
71+
}
72+
src := original.(map[string]interface{})
73+
74+
// Currently only a single key migrated
75+
if val, ok := src["disable_tcp_service"]; ok {
76+
pluginConfigs[pluginName] = map[string]interface{}{
77+
"disable_tcp_service": val,
78+
}
79+
} else {
80+
delete(pluginConfigs, pluginName)
81+
}
82+
return nil
83+
},
6184
InitFn: initCRIService,
6285
})
6386
}
6487

6588
func initCRIService(ic *plugin.InitContext) (interface{}, error) {
6689
ctx := ic.Context
90+
config := ic.Config.(*criconfig.ServiceConfig)
6791

68-
// Get base CRI dependencies.
69-
criBasePlugin, err := ic.GetByID(plugins.InternalPlugin, "cri")
92+
// Get runtime service.
93+
criRuntimePlugin, err := ic.GetByID(plugins.CRIServicePlugin, "runtime")
7094
if err != nil {
71-
return nil, fmt.Errorf("unable to load CRI service base dependencies: %w", err)
95+
return nil, fmt.Errorf("unable to load CRI runtime service plugin dependency: %w", err)
7296
}
73-
criBase := criBasePlugin.(*base.CRIBase)
74-
c := criBase.Config
7597

7698
// Get image service.
77-
criImagePlugin, err := ic.GetSingle(plugins.CRIImagePlugin)
99+
criImagePlugin, err := ic.GetByID(plugins.CRIServicePlugin, "images")
78100
if err != nil {
79101
return nil, fmt.Errorf("unable to load CRI image service plugin dependency: %w", err)
80102
}
@@ -98,15 +120,16 @@ func initCRIService(ic *plugin.InitContext) (interface{}, error) {
98120
}
99121

100122
options := &server.CRIServiceOptions{
123+
RuntimeService: criRuntimePlugin.(server.RuntimeService),
101124
ImageService: criImagePlugin.(server.ImageService),
102125
NRI: getNRIAPI(ic),
103126
Client: client,
104127
SandboxControllers: sbControllers,
105-
BaseOCISpecs: criBase.BaseOCISpecs,
106128
}
107129
is := criImagePlugin.(imageService).GRPCService()
108130

109-
s, rs, err := server.NewCRIService(criBase.Config, options)
131+
// TODO: More options specifically for grpc service?
132+
s, rs, err := server.NewCRIService(options)
110133
if err != nil {
111134
return nil, fmt.Errorf("failed to create CRI service: %w", err)
112135
}
@@ -127,7 +150,7 @@ func initCRIService(ic *plugin.InitContext) (interface{}, error) {
127150
initializer: s,
128151
}
129152

130-
if c.DisableTCPService {
153+
if config.DisableTCPService {
131154
return service, nil
132155
}
133156

pkg/cri/server/container_create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,9 @@ func (c *criService) runtimeSpec(id string, platform platforms.Platform, baseSpe
394394
container := &containers.Container{ID: id}
395395

396396
if baseSpecFile != "" {
397-
baseSpec, ok := c.baseOCISpecs[baseSpecFile]
398-
if !ok {
399-
return nil, fmt.Errorf("can't find base OCI spec %q", baseSpecFile)
397+
baseSpec, err := c.LoadOCISpec(baseSpecFile)
398+
if err != nil {
399+
return nil, fmt.Errorf("can't load base OCI spec %q: %w", baseSpecFile, err)
400400
}
401401

402402
spec := oci.Spec{}

pkg/cri/server/container_create_linux_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,23 +1680,24 @@ func TestPrivilegedDevices(t *testing.T) {
16801680
}
16811681

16821682
func TestBaseOCISpec(t *testing.T) {
1683-
c := newTestCRIService()
16841683
baseLimit := int64(100)
1685-
c.baseOCISpecs = map[string]*oci.Spec{
1686-
"/etc/containerd/cri-base.json": {
1687-
Process: &runtimespec.Process{
1688-
User: runtimespec.User{AdditionalGids: []uint32{9999}},
1689-
Capabilities: &runtimespec.LinuxCapabilities{
1690-
Permitted: []string{"CAP_SETUID"},
1684+
c := newTestCRIService(withRuntimeService(&fakeRuntimeService{
1685+
ocispecs: map[string]*oci.Spec{
1686+
"/etc/containerd/cri-base.json": {
1687+
Process: &runtimespec.Process{
1688+
User: runtimespec.User{AdditionalGids: []uint32{9999}},
1689+
Capabilities: &runtimespec.LinuxCapabilities{
1690+
Permitted: []string{"CAP_SETUID"},
1691+
},
16911692
},
1692-
},
1693-
Linux: &runtimespec.Linux{
1694-
Resources: &runtimespec.LinuxResources{
1695-
Memory: &runtimespec.LinuxMemory{Limit: &baseLimit}, // Will be overwritten by `getCreateContainerTestData`
1693+
Linux: &runtimespec.Linux{
1694+
Resources: &runtimespec.LinuxResources{
1695+
Memory: &runtimespec.LinuxMemory{Limit: &baseLimit}, // Will be overwritten by `getCreateContainerTestData`
1696+
},
16961697
},
16971698
},
16981699
},
1699-
}
1700+
}))
17001701

17011702
ociRuntime := config.Runtime{}
17021703
ociRuntime.BaseRuntimeSpec = "/etc/containerd/cri-base.json"

0 commit comments

Comments
 (0)