From 0cf48bab2cee394ccefcc05e7025ade76d142bc4 Mon Sep 17 00:00:00 2001 From: Abel Feng Date: Thu, 19 Oct 2023 21:06:18 +0800 Subject: [PATCH] sandbox: podsandbox init its own client To break the cyclic dependency of cri plugin and podsandbox plugin, we define a new plugin type of SandboxesServicePlugin and when cri init it's own client, it will add the all the controllers by get them from the SandboxesServicePlugin. when podsandbox controller init it's client, it will not Require the SandboxesServicePlugin. Signed-off-by: Abel Feng --- client/client.go | 1 - client/services.go | 16 ++++++++++++--- pkg/cri/cri.go | 1 + pkg/cri/server/podsandbox/controller.go | 27 +++++++++++++++++-------- pkg/cri/server/service.go | 2 +- plugins/types.go | 2 ++ services/sandbox/controller_service.go | 4 ++-- services/sandbox/sandboxers.go | 2 +- 8 files changed, 39 insertions(+), 16 deletions(-) diff --git a/client/client.go b/client/client.go index 714c57c250..036e6e5ac4 100644 --- a/client/client.go +++ b/client/client.go @@ -720,7 +720,6 @@ func (c *Client) SandboxStore() sandbox.Store { // SandboxController returns the underlying sandbox controller client func (c *Client) SandboxController(name string) sandbox.Controller { - // default sandboxer is shim if c.sandboxers != nil { return c.sandboxers[name] } diff --git a/client/services.go b/client/services.go index 3ca357eb33..03da252464 100644 --- a/client/services.go +++ b/client/services.go @@ -218,9 +218,6 @@ func WithInMemoryServices(ic *plugin.InitContext) Opt { srv.SnapshotsService: func(s interface{}) ServicesOpt { return WithSnapshotters(s.(map[string]snapshots.Snapshotter)) }, - srv.SandboxControllersService: func(s interface{}) ServicesOpt { - return WithSandboxers(s.(map[string]sandbox.Controller)) - }, srv.ContainersService: func(s interface{}) ServicesOpt { return WithContainerClient(s.(containersapi.ContainersClient)) }, @@ -251,3 +248,16 @@ func WithInMemoryServices(ic *plugin.InitContext) Opt { return nil } } + +func WithSandboxersService(ic *plugin.InitContext) ClientOpt { + return func(c *clientOpts) error { + sandboxesPlugin, err := ic.GetByID(plugins.SandboxesServicePlugin, srv.SandboxControllersService) + if err != nil { + return err + } + + sbs := sandboxesPlugin.(map[string]sandbox.Controller) + c.services.sandboxers = sbs + return nil + } +} diff --git a/pkg/cri/cri.go b/pkg/cri/cri.go index e85547e683..8544230fbf 100644 --- a/pkg/cri/cri.go +++ b/pkg/cri/cri.go @@ -92,6 +92,7 @@ func initCRIService(ic *plugin.InitContext) (interface{}, error) { containerd.WithDefaultNamespace(constants.K8sContainerdNamespace), containerd.WithDefaultPlatform(platforms.Default()), containerd.WithInMemoryServices(ic), + containerd.WithSandboxersService(ic), ) if err != nil { return nil, fmt.Errorf("failed to create containerd client: %w", err) diff --git a/pkg/cri/server/podsandbox/controller.go b/pkg/cri/server/podsandbox/controller.go index 45b3faf7d1..ff3de7a0fc 100644 --- a/pkg/cri/server/podsandbox/controller.go +++ b/pkg/cri/server/podsandbox/controller.go @@ -29,6 +29,7 @@ import ( "github.com/containerd/containerd/v2/errdefs" "github.com/containerd/containerd/v2/oci" criconfig "github.com/containerd/containerd/v2/pkg/cri/config" + "github.com/containerd/containerd/v2/pkg/cri/constants" imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image" sandboxstore "github.com/containerd/containerd/v2/pkg/cri/store/sandbox" ctrdutil "github.com/containerd/containerd/v2/pkg/cri/util" @@ -43,13 +44,25 @@ import ( func init() { registry.Register(&plugin.Registration{ - Type: plugins.SandboxControllerPlugin, - ID: "podsandbox", - Requires: []plugin.Type{}, + Type: plugins.SandboxControllerPlugin, + ID: "podsandbox", + Requires: []plugin.Type{ + plugins.EventPlugin, + plugins.ServicePlugin, + }, InitFn: func(ic *plugin.InitContext) (interface{}, error) { - // register the global controller to containerd plugin manager, - // the global controller will be initialized when cri plugin is initializing - return &Controller{}, nil + c := Controller{} + client, err := containerd.New( + "", + containerd.WithDefaultNamespace(constants.K8sContainerdNamespace), + containerd.WithDefaultPlatform(platforms.Default()), + containerd.WithInMemoryServices(ic), + ) + if err != nil { + return nil, fmt.Errorf("unable to load CRI service base dependencies: %w", err) + } + c.client = client + return &c, nil }, }) } @@ -90,7 +103,6 @@ type Controller struct { func (c *Controller) Init( config criconfig.Config, - client *containerd.Client, sandboxStore *sandboxstore.Store, os osinterface.OS, cri CRIService, @@ -98,7 +110,6 @@ func (c *Controller) Init( baseOCISpecs map[string]*oci.Spec, ) { c.cri = cri - c.client = client c.config = config c.sandboxStore = sandboxStore c.os = os diff --git a/pkg/cri/server/service.go b/pkg/cri/server/service.go index 070c81be46..10c8a38822 100644 --- a/pkg/cri/server/service.go +++ b/pkg/cri/server/service.go @@ -218,7 +218,7 @@ func NewCRIService(config criconfig.Config, client *containerd.Client, nri *nri. } // Initialize pod sandbox controller - sbControllers[string(criconfig.ModePodSandbox)].(*podsandbox.Controller).Init(config, client, c.sandboxStore, c.os, c, c.imageService, c.baseOCISpecs) + sbControllers[string(criconfig.ModePodSandbox)].(*podsandbox.Controller).Init(config, c.sandboxStore, c.os, c, c.imageService, c.baseOCISpecs) c.nri = nri diff --git a/plugins/types.go b/plugins/types.go index 1ac4a27375..eea2095bca 100644 --- a/plugins/types.go +++ b/plugins/types.go @@ -67,6 +67,8 @@ const ( ImageVerifierPlugin plugin.Type = "io.containerd.image-verifier.v1" // WarningPlugin implements a warning service WarningPlugin plugin.Type = "io.containerd.warning.v1" + // SandboxesServicePlugin implements sandboxes service + SandboxesServicePlugin plugin.Type = "io.containerd.service.sandboxes.v1" ) const ( diff --git a/services/sandbox/controller_service.go b/services/sandbox/controller_service.go index dd8cc352eb..8ef2736e7c 100644 --- a/services/sandbox/controller_service.go +++ b/services/sandbox/controller_service.go @@ -42,11 +42,11 @@ func init() { Type: plugins.GRPCPlugin, ID: "sandbox-controllers", Requires: []plugin.Type{ - plugins.ServicePlugin, + plugins.SandboxesServicePlugin, plugins.EventPlugin, }, InitFn: func(ic *plugin.InitContext) (interface{}, error) { - i, err := ic.GetByID(plugins.ServicePlugin, services.SandboxControllersService) + i, err := ic.GetByID(plugins.SandboxesServicePlugin, services.SandboxControllersService) if err != nil { return nil, err } diff --git a/services/sandbox/sandboxers.go b/services/sandbox/sandboxers.go index 85db67018e..df09a800ae 100644 --- a/services/sandbox/sandboxers.go +++ b/services/sandbox/sandboxers.go @@ -26,7 +26,7 @@ import ( func init() { registry.Register(&plugin.Registration{ - Type: plugins.ServicePlugin, + Type: plugins.SandboxesServicePlugin, ID: services.SandboxControllersService, Requires: []plugin.Type{ plugins.SandboxControllerPlugin,