Skip to content

Commit ca59fb0

Browse files
committed
Cleanup shim manager configuration
Keep platforms configuration on task manager and add environment config for shims. Signed-off-by: Derek McGowan <[email protected]>
1 parent 28b77e3 commit ca59fb0

File tree

6 files changed

+59
-37
lines changed

6 files changed

+59
-37
lines changed

core/runtime/v2/binary.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type shimBinaryConfig struct {
3939
runtime string
4040
address string
4141
ttrpcAddress string
42-
schedCore bool
42+
env []string
4343
}
4444

4545
func shimBinary(bundle *Bundle, config shimBinaryConfig) *binary {
@@ -48,16 +48,16 @@ func shimBinary(bundle *Bundle, config shimBinaryConfig) *binary {
4848
runtime: config.runtime,
4949
containerdAddress: config.address,
5050
containerdTTRPCAddress: config.ttrpcAddress,
51-
schedCore: config.schedCore,
51+
env: config.env,
5252
}
5353
}
5454

5555
type binary struct {
5656
runtime string
5757
containerdAddress string
5858
containerdTTRPCAddress string
59-
schedCore bool
6059
bundle *Bundle
60+
env []string
6161
}
6262

6363
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()) (_
7777
Path: b.bundle.Path,
7878
Opts: opts,
7979
Args: args,
80-
SchedCore: b.schedCore,
80+
Env: b.env,
8181
})
8282
if err != nil {
8383
return nil, err

core/runtime/v2/shim_load.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (m *ShimManager) loadShim(ctx context.Context, bundle *Bundle) error {
154154
runtime: runtime,
155155
address: m.containerdAddress,
156156
ttrpcAddress: m.containerdTTRPCAddress,
157-
schedCore: m.schedCore,
157+
env: m.env,
158158
})
159159
// TODO: It seems we can only call loadShim here if it is a sandbox shim?
160160
shim, err := loadShimTask(ctx, bundle, func() {

core/runtime/v2/shim_manager.go

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"sync"
2828

2929
"github.com/containerd/log"
30-
"github.com/containerd/platforms"
3130
"github.com/containerd/plugin"
3231
"github.com/containerd/plugin/registry"
3332

@@ -45,12 +44,10 @@ import (
4544
"github.com/containerd/containerd/v2/version"
4645
)
4746

48-
// Config for the shim
49-
type Config struct {
50-
// Supported platforms
51-
Platforms []string `toml:"platforms"`
52-
// SchedCore enabled linux core scheduling
53-
SchedCore bool `toml:"sched_core"`
47+
// ShimConfig for the shim
48+
type ShimConfig struct {
49+
// Env is environment variables added to shim processes
50+
Env []string `toml:"env"`
5451
}
5552

5653
func init() {
@@ -59,21 +56,14 @@ func init() {
5956
// so we make it an independent plugin
6057
registry.Register(&plugin.Registration{
6158
Type: plugins.ShimPlugin,
62-
ID: "shim",
59+
ID: "manager",
6360
Requires: []plugin.Type{
6461
plugins.EventPlugin,
6562
plugins.MetadataPlugin,
6663
},
67-
Config: &Config{
68-
Platforms: defaultPlatforms(),
69-
},
64+
Config: &ShimConfig{},
7065
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
71-
config := ic.Config.(*Config)
72-
supportedPlatforms, err := platforms.ParseAll(config.Platforms)
73-
if err != nil {
74-
return nil, err
75-
}
76-
ic.Meta.Platforms = supportedPlatforms
66+
config := ic.Config.(*ShimConfig)
7767

7868
m, err := ic.GetSingle(plugins.MetadataPlugin)
7969
if err != nil {
@@ -91,13 +81,13 @@ func init() {
9181
TTRPCAddress: ic.Properties[plugins.PropertyTTRPCAddress],
9282
Events: events,
9383
Store: cs,
94-
SchedCore: config.SchedCore,
84+
ShimEnv: config.Env,
9585
SandboxStore: ss,
9686
})
9787
},
9888
ConfigMigration: func(ctx context.Context, configVersion int, pluginConfigs map[string]interface{}) error {
9989
// Migrate configurations from io.containerd.runtime.v2.task
100-
// if the configVersion >= 3 please make sure the config is under io.containerd.shim.v1.shim.
90+
// if the configVersion >= 3 please make sure the config is under io.containerd.shim.v1.manager.
10191
if configVersion >= version.ConfigVersion {
10292
return nil
10393
}
@@ -106,9 +96,22 @@ func init() {
10696
if !ok {
10797
return nil
10898
}
109-
const newPluginName = string(plugins.ShimPlugin) + ".shim"
110-
pluginConfigs[originalPluginName] = nil
111-
pluginConfigs[newPluginName] = original
99+
src := original.(map[string]interface{})
100+
dest := map[string]interface{}{}
101+
102+
if v, ok := src["sched_core"]; ok {
103+
if schedCore, ok := v.(bool); schedCore {
104+
dest["env"] = []string{"SCHED_CORE=1"}
105+
} else if !ok {
106+
log.G(ctx).Warnf("skipping migration for non-boolean 'sched_core' value %v", v)
107+
}
108+
109+
delete(src, "sched_core")
110+
}
111+
112+
const newPluginName = string(plugins.ShimPlugin) + ".manager"
113+
pluginConfigs[originalPluginName] = src
114+
pluginConfigs[newPluginName] = dest
112115
return nil
113116
},
114117
})
@@ -119,8 +122,8 @@ type ManagerConfig struct {
119122
Events *exchange.Exchange
120123
Address string
121124
TTRPCAddress string
122-
SchedCore bool
123125
SandboxStore sandbox.Store
126+
ShimEnv []string
124127
}
125128

126129
// NewShimManager creates a manager for v2 shims
@@ -131,7 +134,7 @@ func NewShimManager(config *ManagerConfig) (*ShimManager, error) {
131134
shims: runtime.NewNSMap[ShimInstance](),
132135
events: config.Events,
133136
containers: config.Store,
134-
schedCore: config.SchedCore,
137+
env: config.ShimEnv,
135138
sandboxStore: config.SandboxStore,
136139
}
137140

@@ -145,7 +148,7 @@ func NewShimManager(config *ManagerConfig) (*ShimManager, error) {
145148
type ShimManager struct {
146149
containerdAddress string
147150
containerdTTRPCAddress string
148-
schedCore bool
151+
env []string
149152
shims *runtime.NSMap[ShimInstance]
150153
events *exchange.Exchange
151154
containers containers.Store
@@ -156,7 +159,7 @@ type ShimManager struct {
156159

157160
// ID of the shim manager
158161
func (m *ShimManager) ID() string {
159-
return plugins.ShimPlugin.String() + ".shim"
162+
return plugins.ShimPlugin.String() + ".manager"
160163
}
161164

162165
// Start launches a new shim instance
@@ -251,7 +254,7 @@ func (m *ShimManager) startShim(ctx context.Context, bundle *Bundle, id string,
251254
runtime: runtimePath,
252255
address: m.containerdAddress,
253256
ttrpcAddress: m.containerdTTRPCAddress,
254-
schedCore: m.schedCore,
257+
env: m.env,
255258
})
256259
shim, err := b.Start(ctx, protobuf.FromAny(topts), func() {
257260
log.G(ctx).WithField("id", id).Info("shim disconnected")

core/runtime/v2/task_manager.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,48 @@ import (
2626
"slices"
2727

2828
"github.com/containerd/errdefs"
29+
"github.com/containerd/platforms"
2930
"github.com/containerd/plugin"
3031
"github.com/containerd/plugin/registry"
3132
"github.com/containerd/typeurl/v2"
3233
"github.com/opencontainers/runtime-spec/specs-go"
3334
"github.com/opencontainers/runtime-spec/specs-go/features"
3435

3536
apitypes "github.com/containerd/containerd/api/types"
37+
3638
"github.com/containerd/containerd/v2/core/runtime"
3739
"github.com/containerd/containerd/v2/internal/cleanup"
3840
"github.com/containerd/containerd/v2/pkg/protobuf/proto"
3941
"github.com/containerd/containerd/v2/pkg/timeout"
4042
"github.com/containerd/containerd/v2/plugins"
4143
)
4244

45+
// TaskConfig for the runtime task manager
46+
type TaskConfig struct {
47+
// Supported platforms
48+
Platforms []string `toml:"platforms"`
49+
}
50+
4351
func init() {
4452
registry.Register(&plugin.Registration{
4553
Type: plugins.RuntimePluginV2,
4654
ID: "task",
4755
Requires: []plugin.Type{
4856
plugins.ShimPlugin,
4957
},
58+
Config: &TaskConfig{
59+
Platforms: defaultPlatforms(),
60+
},
5061
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
51-
shimManagerI, err := ic.GetByID(plugins.ShimPlugin, "shim")
62+
config := ic.Config.(*TaskConfig)
63+
64+
supportedPlatforms, err := platforms.ParseAll(config.Platforms)
65+
if err != nil {
66+
return nil, err
67+
}
68+
ic.Meta.Platforms = supportedPlatforms
69+
70+
shimManagerI, err := ic.GetSingle(plugins.ShimPlugin)
5271
if err != nil {
5372
return nil, err
5473
}

pkg/shim/util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ type CommandConfig struct {
4444
Address string
4545
TTRPCAddress string
4646
Path string
47-
SchedCore bool
4847
Args []string
4948
Opts *types.Any
49+
Env []string
5050
}
5151

5252
// 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) {
7575
fmt.Sprintf("%s=%s", grpcAddressEnv, config.Address),
7676
fmt.Sprintf("%s=%s", namespaceEnv, ns),
7777
)
78-
if config.SchedCore {
79-
cmd.Env = append(cmd.Env, "SCHED_CORE=1")
78+
if len(config.Env) > 0 {
79+
cmd.Env = append(cmd.Env, config.Env...)
8080
}
8181
cmd.SysProcAttr = getSysProcAttr()
8282
if config.Opts != nil {

plugins/sandbox/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func init() {
4949
plugins.EventPlugin,
5050
},
5151
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
52-
shimPlugin, err := ic.GetByID(plugins.ShimPlugin, "shim")
52+
shimPlugin, err := ic.GetSingle(plugins.ShimPlugin)
5353
if err != nil {
5454
return nil, err
5555
}

0 commit comments

Comments
 (0)