Cleanup shim manager configuration

Keep platforms configuration on task manager and add environment config
for shims.

Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
Derek McGowan 2024-05-17 17:31:57 -07:00
parent 28b77e33ad
commit ca59fb0b41
No known key found for this signature in database
GPG Key ID: F58C5D0A4405ACDB
6 changed files with 59 additions and 37 deletions

View File

@ -39,7 +39,7 @@ type shimBinaryConfig struct {
runtime string runtime string
address string address string
ttrpcAddress string ttrpcAddress string
schedCore bool env []string
} }
func shimBinary(bundle *Bundle, config shimBinaryConfig) *binary { func shimBinary(bundle *Bundle, config shimBinaryConfig) *binary {
@ -48,7 +48,7 @@ func shimBinary(bundle *Bundle, config shimBinaryConfig) *binary {
runtime: config.runtime, runtime: config.runtime,
containerdAddress: config.address, containerdAddress: config.address,
containerdTTRPCAddress: config.ttrpcAddress, containerdTTRPCAddress: config.ttrpcAddress,
schedCore: config.schedCore, env: config.env,
} }
} }
@ -56,8 +56,8 @@ type binary struct {
runtime string runtime string
containerdAddress string containerdAddress string
containerdTTRPCAddress string containerdTTRPCAddress string
schedCore bool
bundle *Bundle bundle *Bundle
env []string
} }
func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_ *shim, err error) { func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_ *shim, err error) {
@ -77,7 +77,7 @@ func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_
Path: b.bundle.Path, Path: b.bundle.Path,
Opts: opts, Opts: opts,
Args: args, Args: args,
SchedCore: b.schedCore, Env: b.env,
}) })
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -154,7 +154,7 @@ func (m *ShimManager) loadShim(ctx context.Context, bundle *Bundle) error {
runtime: runtime, runtime: runtime,
address: m.containerdAddress, address: m.containerdAddress,
ttrpcAddress: m.containerdTTRPCAddress, ttrpcAddress: m.containerdTTRPCAddress,
schedCore: m.schedCore, env: m.env,
}) })
// TODO: It seems we can only call loadShim here if it is a sandbox shim? // TODO: It seems we can only call loadShim here if it is a sandbox shim?
shim, err := loadShimTask(ctx, bundle, func() { shim, err := loadShimTask(ctx, bundle, func() {

View File

@ -27,7 +27,6 @@ import (
"sync" "sync"
"github.com/containerd/log" "github.com/containerd/log"
"github.com/containerd/platforms"
"github.com/containerd/plugin" "github.com/containerd/plugin"
"github.com/containerd/plugin/registry" "github.com/containerd/plugin/registry"
@ -45,12 +44,10 @@ import (
"github.com/containerd/containerd/v2/version" "github.com/containerd/containerd/v2/version"
) )
// Config for the shim // ShimConfig for the shim
type Config struct { type ShimConfig struct {
// Supported platforms // Env is environment variables added to shim processes
Platforms []string `toml:"platforms"` Env []string `toml:"env"`
// SchedCore enabled linux core scheduling
SchedCore bool `toml:"sched_core"`
} }
func init() { func init() {
@ -59,21 +56,14 @@ func init() {
// so we make it an independent plugin // so we make it an independent plugin
registry.Register(&plugin.Registration{ registry.Register(&plugin.Registration{
Type: plugins.ShimPlugin, Type: plugins.ShimPlugin,
ID: "shim", ID: "manager",
Requires: []plugin.Type{ Requires: []plugin.Type{
plugins.EventPlugin, plugins.EventPlugin,
plugins.MetadataPlugin, plugins.MetadataPlugin,
}, },
Config: &Config{ Config: &ShimConfig{},
Platforms: defaultPlatforms(),
},
InitFn: func(ic *plugin.InitContext) (interface{}, error) { InitFn: func(ic *plugin.InitContext) (interface{}, error) {
config := ic.Config.(*Config) config := ic.Config.(*ShimConfig)
supportedPlatforms, err := platforms.ParseAll(config.Platforms)
if err != nil {
return nil, err
}
ic.Meta.Platforms = supportedPlatforms
m, err := ic.GetSingle(plugins.MetadataPlugin) m, err := ic.GetSingle(plugins.MetadataPlugin)
if err != nil { if err != nil {
@ -91,13 +81,13 @@ func init() {
TTRPCAddress: ic.Properties[plugins.PropertyTTRPCAddress], TTRPCAddress: ic.Properties[plugins.PropertyTTRPCAddress],
Events: events, Events: events,
Store: cs, Store: cs,
SchedCore: config.SchedCore, ShimEnv: config.Env,
SandboxStore: ss, SandboxStore: ss,
}) })
}, },
ConfigMigration: func(ctx context.Context, configVersion int, pluginConfigs map[string]interface{}) error { ConfigMigration: func(ctx context.Context, configVersion int, pluginConfigs map[string]interface{}) error {
// Migrate configurations from io.containerd.runtime.v2.task // Migrate configurations from io.containerd.runtime.v2.task
// if the configVersion >= 3 please make sure the config is under io.containerd.shim.v1.shim. // if the configVersion >= 3 please make sure the config is under io.containerd.shim.v1.manager.
if configVersion >= version.ConfigVersion { if configVersion >= version.ConfigVersion {
return nil return nil
} }
@ -106,9 +96,22 @@ func init() {
if !ok { if !ok {
return nil return nil
} }
const newPluginName = string(plugins.ShimPlugin) + ".shim" src := original.(map[string]interface{})
pluginConfigs[originalPluginName] = nil dest := map[string]interface{}{}
pluginConfigs[newPluginName] = original
if v, ok := src["sched_core"]; ok {
if schedCore, ok := v.(bool); schedCore {
dest["env"] = []string{"SCHED_CORE=1"}
} else if !ok {
log.G(ctx).Warnf("skipping migration for non-boolean 'sched_core' value %v", v)
}
delete(src, "sched_core")
}
const newPluginName = string(plugins.ShimPlugin) + ".manager"
pluginConfigs[originalPluginName] = src
pluginConfigs[newPluginName] = dest
return nil return nil
}, },
}) })
@ -119,8 +122,8 @@ type ManagerConfig struct {
Events *exchange.Exchange Events *exchange.Exchange
Address string Address string
TTRPCAddress string TTRPCAddress string
SchedCore bool
SandboxStore sandbox.Store SandboxStore sandbox.Store
ShimEnv []string
} }
// NewShimManager creates a manager for v2 shims // NewShimManager creates a manager for v2 shims
@ -131,7 +134,7 @@ func NewShimManager(config *ManagerConfig) (*ShimManager, error) {
shims: runtime.NewNSMap[ShimInstance](), shims: runtime.NewNSMap[ShimInstance](),
events: config.Events, events: config.Events,
containers: config.Store, containers: config.Store,
schedCore: config.SchedCore, env: config.ShimEnv,
sandboxStore: config.SandboxStore, sandboxStore: config.SandboxStore,
} }
@ -145,7 +148,7 @@ func NewShimManager(config *ManagerConfig) (*ShimManager, error) {
type ShimManager struct { type ShimManager struct {
containerdAddress string containerdAddress string
containerdTTRPCAddress string containerdTTRPCAddress string
schedCore bool env []string
shims *runtime.NSMap[ShimInstance] shims *runtime.NSMap[ShimInstance]
events *exchange.Exchange events *exchange.Exchange
containers containers.Store containers containers.Store
@ -156,7 +159,7 @@ type ShimManager struct {
// ID of the shim manager // ID of the shim manager
func (m *ShimManager) ID() string { func (m *ShimManager) ID() string {
return plugins.ShimPlugin.String() + ".shim" return plugins.ShimPlugin.String() + ".manager"
} }
// Start launches a new shim instance // Start launches a new shim instance
@ -251,7 +254,7 @@ func (m *ShimManager) startShim(ctx context.Context, bundle *Bundle, id string,
runtime: runtimePath, runtime: runtimePath,
address: m.containerdAddress, address: m.containerdAddress,
ttrpcAddress: m.containerdTTRPCAddress, ttrpcAddress: m.containerdTTRPCAddress,
schedCore: m.schedCore, env: m.env,
}) })
shim, err := b.Start(ctx, protobuf.FromAny(topts), func() { shim, err := b.Start(ctx, protobuf.FromAny(topts), func() {
log.G(ctx).WithField("id", id).Info("shim disconnected") log.G(ctx).WithField("id", id).Info("shim disconnected")

View File

@ -26,6 +26,7 @@ import (
"slices" "slices"
"github.com/containerd/errdefs" "github.com/containerd/errdefs"
"github.com/containerd/platforms"
"github.com/containerd/plugin" "github.com/containerd/plugin"
"github.com/containerd/plugin/registry" "github.com/containerd/plugin/registry"
"github.com/containerd/typeurl/v2" "github.com/containerd/typeurl/v2"
@ -33,6 +34,7 @@ import (
"github.com/opencontainers/runtime-spec/specs-go/features" "github.com/opencontainers/runtime-spec/specs-go/features"
apitypes "github.com/containerd/containerd/api/types" apitypes "github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/v2/core/runtime" "github.com/containerd/containerd/v2/core/runtime"
"github.com/containerd/containerd/v2/internal/cleanup" "github.com/containerd/containerd/v2/internal/cleanup"
"github.com/containerd/containerd/v2/pkg/protobuf/proto" "github.com/containerd/containerd/v2/pkg/protobuf/proto"
@ -40,6 +42,12 @@ import (
"github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/plugins"
) )
// TaskConfig for the runtime task manager
type TaskConfig struct {
// Supported platforms
Platforms []string `toml:"platforms"`
}
func init() { func init() {
registry.Register(&plugin.Registration{ registry.Register(&plugin.Registration{
Type: plugins.RuntimePluginV2, Type: plugins.RuntimePluginV2,
@ -47,8 +55,19 @@ func init() {
Requires: []plugin.Type{ Requires: []plugin.Type{
plugins.ShimPlugin, plugins.ShimPlugin,
}, },
Config: &TaskConfig{
Platforms: defaultPlatforms(),
},
InitFn: func(ic *plugin.InitContext) (interface{}, error) { InitFn: func(ic *plugin.InitContext) (interface{}, error) {
shimManagerI, err := ic.GetByID(plugins.ShimPlugin, "shim") config := ic.Config.(*TaskConfig)
supportedPlatforms, err := platforms.ParseAll(config.Platforms)
if err != nil {
return nil, err
}
ic.Meta.Platforms = supportedPlatforms
shimManagerI, err := ic.GetSingle(plugins.ShimPlugin)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -44,9 +44,9 @@ type CommandConfig struct {
Address string Address string
TTRPCAddress string TTRPCAddress string
Path string Path string
SchedCore bool
Args []string Args []string
Opts *types.Any Opts *types.Any
Env []string
} }
// Command returns the shim command with the provided args and configuration // Command returns the shim command with the provided args and configuration
@ -75,8 +75,8 @@ func Command(ctx context.Context, config *CommandConfig) (*exec.Cmd, error) {
fmt.Sprintf("%s=%s", grpcAddressEnv, config.Address), fmt.Sprintf("%s=%s", grpcAddressEnv, config.Address),
fmt.Sprintf("%s=%s", namespaceEnv, ns), fmt.Sprintf("%s=%s", namespaceEnv, ns),
) )
if config.SchedCore { if len(config.Env) > 0 {
cmd.Env = append(cmd.Env, "SCHED_CORE=1") cmd.Env = append(cmd.Env, config.Env...)
} }
cmd.SysProcAttr = getSysProcAttr() cmd.SysProcAttr = getSysProcAttr()
if config.Opts != nil { if config.Opts != nil {

View File

@ -49,7 +49,7 @@ func init() {
plugins.EventPlugin, plugins.EventPlugin,
}, },
InitFn: func(ic *plugin.InitContext) (interface{}, error) { InitFn: func(ic *plugin.InitContext) (interface{}, error) {
shimPlugin, err := ic.GetByID(plugins.ShimPlugin, "shim") shimPlugin, err := ic.GetSingle(plugins.ShimPlugin)
if err != nil { if err != nil {
return nil, err return nil, err
} }