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 <fshb1988@gmail.com>
This commit is contained in:
parent
7deb68fbf4
commit
0cf48bab2c
@ -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]
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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 (
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user