diff --git a/core/runtime/v2/binary.go b/core/runtime/v2/binary.go index d3dfcac07..c77fb494b 100644 --- a/core/runtime/v2/binary.go +++ b/core/runtime/v2/binary.go @@ -39,7 +39,7 @@ type shimBinaryConfig struct { runtime string address string ttrpcAddress string - schedCore bool + env []string } func shimBinary(bundle *Bundle, config shimBinaryConfig) *binary { @@ -48,7 +48,7 @@ func shimBinary(bundle *Bundle, config shimBinaryConfig) *binary { runtime: config.runtime, containerdAddress: config.address, containerdTTRPCAddress: config.ttrpcAddress, - schedCore: config.schedCore, + env: config.env, } } @@ -56,8 +56,8 @@ type binary struct { runtime string containerdAddress string containerdTTRPCAddress string - schedCore bool bundle *Bundle + env []string } 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, Opts: opts, Args: args, - SchedCore: b.schedCore, + Env: b.env, }) if err != nil { return nil, err diff --git a/core/runtime/v2/shim_load.go b/core/runtime/v2/shim_load.go index 951ab2c6f..c80bdebc2 100644 --- a/core/runtime/v2/shim_load.go +++ b/core/runtime/v2/shim_load.go @@ -154,7 +154,7 @@ func (m *ShimManager) loadShim(ctx context.Context, bundle *Bundle) error { runtime: runtime, address: m.containerdAddress, ttrpcAddress: m.containerdTTRPCAddress, - schedCore: m.schedCore, + env: m.env, }) // TODO: It seems we can only call loadShim here if it is a sandbox shim? shim, err := loadShimTask(ctx, bundle, func() { diff --git a/core/runtime/v2/shim_manager.go b/core/runtime/v2/shim_manager.go index 7ef0db1bc..31629e29d 100644 --- a/core/runtime/v2/shim_manager.go +++ b/core/runtime/v2/shim_manager.go @@ -27,7 +27,6 @@ import ( "sync" "github.com/containerd/log" - "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" @@ -45,12 +44,10 @@ import ( "github.com/containerd/containerd/v2/version" ) -// Config for the shim -type Config struct { - // Supported platforms - Platforms []string `toml:"platforms"` - // SchedCore enabled linux core scheduling - SchedCore bool `toml:"sched_core"` +// ShimConfig for the shim +type ShimConfig struct { + // Env is environment variables added to shim processes + Env []string `toml:"env"` } func init() { @@ -59,21 +56,14 @@ func init() { // so we make it an independent plugin registry.Register(&plugin.Registration{ Type: plugins.ShimPlugin, - ID: "shim", + ID: "manager", Requires: []plugin.Type{ plugins.EventPlugin, plugins.MetadataPlugin, }, - Config: &Config{ - Platforms: defaultPlatforms(), - }, + Config: &ShimConfig{}, InitFn: func(ic *plugin.InitContext) (interface{}, error) { - config := ic.Config.(*Config) - supportedPlatforms, err := platforms.ParseAll(config.Platforms) - if err != nil { - return nil, err - } - ic.Meta.Platforms = supportedPlatforms + config := ic.Config.(*ShimConfig) m, err := ic.GetSingle(plugins.MetadataPlugin) if err != nil { @@ -91,13 +81,13 @@ func init() { TTRPCAddress: ic.Properties[plugins.PropertyTTRPCAddress], Events: events, Store: cs, - SchedCore: config.SchedCore, + ShimEnv: config.Env, SandboxStore: ss, }) }, ConfigMigration: func(ctx context.Context, configVersion int, pluginConfigs map[string]interface{}) error { // 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 { return nil } @@ -106,9 +96,22 @@ func init() { if !ok { return nil } - const newPluginName = string(plugins.ShimPlugin) + ".shim" - pluginConfigs[originalPluginName] = nil - pluginConfigs[newPluginName] = original + src := original.(map[string]interface{}) + dest := map[string]interface{}{} + + 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 }, }) @@ -119,8 +122,8 @@ type ManagerConfig struct { Events *exchange.Exchange Address string TTRPCAddress string - SchedCore bool SandboxStore sandbox.Store + ShimEnv []string } // NewShimManager creates a manager for v2 shims @@ -131,7 +134,7 @@ func NewShimManager(config *ManagerConfig) (*ShimManager, error) { shims: runtime.NewNSMap[ShimInstance](), events: config.Events, containers: config.Store, - schedCore: config.SchedCore, + env: config.ShimEnv, sandboxStore: config.SandboxStore, } @@ -145,7 +148,7 @@ func NewShimManager(config *ManagerConfig) (*ShimManager, error) { type ShimManager struct { containerdAddress string containerdTTRPCAddress string - schedCore bool + env []string shims *runtime.NSMap[ShimInstance] events *exchange.Exchange containers containers.Store @@ -156,7 +159,7 @@ type ShimManager struct { // ID of the shim manager func (m *ShimManager) ID() string { - return plugins.ShimPlugin.String() + ".shim" + return plugins.ShimPlugin.String() + ".manager" } // Start launches a new shim instance @@ -251,7 +254,7 @@ func (m *ShimManager) startShim(ctx context.Context, bundle *Bundle, id string, runtime: runtimePath, address: m.containerdAddress, ttrpcAddress: m.containerdTTRPCAddress, - schedCore: m.schedCore, + env: m.env, }) shim, err := b.Start(ctx, protobuf.FromAny(topts), func() { log.G(ctx).WithField("id", id).Info("shim disconnected") diff --git a/core/runtime/v2/task_manager.go b/core/runtime/v2/task_manager.go index 375dec869..19219cb17 100644 --- a/core/runtime/v2/task_manager.go +++ b/core/runtime/v2/task_manager.go @@ -26,6 +26,7 @@ import ( "slices" "github.com/containerd/errdefs" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" "github.com/containerd/typeurl/v2" @@ -33,6 +34,7 @@ import ( "github.com/opencontainers/runtime-spec/specs-go/features" apitypes "github.com/containerd/containerd/api/types" + "github.com/containerd/containerd/v2/core/runtime" "github.com/containerd/containerd/v2/internal/cleanup" "github.com/containerd/containerd/v2/pkg/protobuf" @@ -41,6 +43,12 @@ import ( "github.com/containerd/containerd/v2/plugins" ) +// TaskConfig for the runtime task manager +type TaskConfig struct { + // Supported platforms + Platforms []string `toml:"platforms"` +} + func init() { registry.Register(&plugin.Registration{ Type: plugins.RuntimePluginV2, @@ -48,8 +56,19 @@ func init() { Requires: []plugin.Type{ plugins.ShimPlugin, }, + Config: &TaskConfig{ + Platforms: defaultPlatforms(), + }, 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 { return nil, err } diff --git a/pkg/shim/util.go b/pkg/shim/util.go index 46b1b1724..674c25715 100644 --- a/pkg/shim/util.go +++ b/pkg/shim/util.go @@ -44,9 +44,9 @@ type CommandConfig struct { Address string TTRPCAddress string Path string - SchedCore bool Args []string Opts *types.Any + Env []string } // 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", namespaceEnv, ns), ) - if config.SchedCore { - cmd.Env = append(cmd.Env, "SCHED_CORE=1") + if len(config.Env) > 0 { + cmd.Env = append(cmd.Env, config.Env...) } cmd.SysProcAttr = getSysProcAttr() if config.Opts != nil { diff --git a/plugins/sandbox/controller.go b/plugins/sandbox/controller.go index ed6436d79..ccf403638 100644 --- a/plugins/sandbox/controller.go +++ b/plugins/sandbox/controller.go @@ -49,7 +49,7 @@ func init() { plugins.EventPlugin, }, InitFn: func(ic *plugin.InitContext) (interface{}, error) { - shimPlugin, err := ic.GetByID(plugins.ShimPlugin, "shim") + shimPlugin, err := ic.GetSingle(plugins.ShimPlugin) if err != nil { return nil, err }