Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 0a4e04e

Browse files
committed
Add RuntimeHandler support
1 parent eb41de6 commit 0a4e04e

File tree

4 files changed

+84
-12
lines changed

4 files changed

+84
-12
lines changed

cri.go

+16
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func initCRIService(ic *plugin.InitContext) (interface{}, error) {
7171
}
7272
log.G(ctx).Infof("Start cri plugin with config %+v", c)
7373

74+
if err := validateConfig(&c); err != nil {
75+
return nil, errors.Wrap(err, "invalid config")
76+
}
77+
7478
if err := setGLogLevel(); err != nil {
7579
return nil, errors.Wrap(err, "failed to set glog level")
7680
}
@@ -104,6 +108,18 @@ func initCRIService(ic *plugin.InitContext) (interface{}, error) {
104108
return s, nil
105109
}
106110

111+
// validateConfig validates that the given configuration.
112+
func validateConfig(c *criconfig.Config) error {
113+
// It is an error to provide both an UntrustedWorkloadRuntime & define an 'untrusted' runtime.
114+
if _, ok := c.ContainerdConfig.Runtimes[criconfig.RuntimeUntrusted]; ok {
115+
if c.ContainerdConfig.UntrustedWorkloadRuntime.Type != "" {
116+
return errors.New("conflicting untrusted runtimes defined")
117+
}
118+
}
119+
120+
return nil
121+
}
122+
107123
// getServicesOpts get service options from plugin context.
108124
func getServicesOpts(ic *plugin.InitContext) ([]containerd.ServicesOpt, error) {
109125
plugins, err := ic.GetByType(plugin.ServicePlugin)

pkg/config/config.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ type Runtime struct {
3333
type ContainerdConfig struct {
3434
// Snapshotter is the snapshotter used by containerd.
3535
Snapshotter string `toml:"snapshotter" json:"snapshotter"`
36-
// DefaultRuntime is the runtime to use in containerd.
36+
// DefaultRuntime is the default runtime to use in containerd.
37+
// This runtime is used when no runtime handler (or the empty string) is provided.
3738
DefaultRuntime Runtime `toml:"default_runtime" json:"defaultRuntime"`
3839
// UntrustedWorkloadRuntime is a runtime to run untrusted workloads on it.
40+
// DEPRECATED: use Runtimes instead. If provided, this runtime is mapped to the runtime handler
41+
// named 'untrusted'. It is a configuration error to provide both.
3942
UntrustedWorkloadRuntime Runtime `toml:"untrusted_workload_runtime" json:"untrustedWorkloadRuntime"`
43+
// Runtimes maps a CRI RuntimeHandler string to a runtime configuration.
44+
Runtimes map[string]Runtime `toml:"runtimes" json:"runtimes"`
4045
// NoPivot disables pivot-root (linux only), required when running a container in a RamDisk with runc
4146
NoPivot bool `toml:"no_pivot" json:"noPivot"`
4247
}
@@ -183,3 +188,8 @@ func DefaultConfig() PluginConfig {
183188
},
184189
}
185190
}
191+
192+
const (
193+
// RuntimeUntrusted is the implicit runtime defined for ContainerdConfig.UntrustedWorkloadRuntime
194+
RuntimeUntrusted = "unstrusted"
195+
)

pkg/server/sandbox_run.go

+22-10
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
131131
}()
132132
}
133133

134-
ociRuntime, err := c.getSandboxRuntime(config)
134+
ociRuntime, err := c.getSandboxRuntime(config, r.GetRuntimeHandler())
135135
if err != nil {
136136
return nil, errors.Wrap(err, "failed to get sandbox runtime")
137137
}
@@ -601,9 +601,13 @@ func hostAccessingSandbox(config *runtime.PodSandboxConfig) bool {
601601
// getSandboxRuntime returns the runtime configuration for sandbox.
602602
// If the sandbox contains untrusted workload, runtime for untrusted workload will be returned,
603603
// or else default runtime will be returned.
604-
func (c *criService) getSandboxRuntime(config *runtime.PodSandboxConfig) (criconfig.Runtime, error) {
605-
untrusted := false
604+
func (c *criService) getSandboxRuntime(config *runtime.PodSandboxConfig, runtimeHandler string) (criconfig.Runtime, error) {
606605
if untrustedWorkload(config) {
606+
// If the untrusted annotation is provided, runtimeHandler MUST be empty.
607+
if runtimeHandler != "" {
608+
return criconfig.Runtime{}, errors.New("untrusted workload with explicit runtime handler is not allowed")
609+
}
610+
607611
// If the untrusted workload is requesting access to the host/node, this request will fail.
608612
//
609613
// Note: If the workload is marked untrusted but requests privileged, this can be granted, as the
@@ -612,14 +616,22 @@ func (c *criService) getSandboxRuntime(config *runtime.PodSandboxConfig) (cricon
612616
if hostAccessingSandbox(config) {
613617
return criconfig.Runtime{}, errors.New("untrusted workload with host access is not allowed")
614618
}
615-
untrusted = true
616-
}
617619

618-
if untrusted {
619-
if c.config.ContainerdConfig.UntrustedWorkloadRuntime.Type == "" {
620-
return criconfig.Runtime{}, errors.New("no runtime for untrusted workload is configured")
620+
// Handle the deprecated UntrustedWorkloadRuntime.
621+
if c.config.ContainerdConfig.UntrustedWorkloadRuntime.Type != "" {
622+
return c.config.ContainerdConfig.UntrustedWorkloadRuntime, nil
621623
}
622-
return c.config.ContainerdConfig.UntrustedWorkloadRuntime, nil
624+
625+
runtimeHandler = criconfig.RuntimeUntrusted
626+
}
627+
628+
if runtimeHandler == "" {
629+
return c.config.ContainerdConfig.DefaultRuntime, nil
630+
}
631+
632+
handler, ok := c.config.ContainerdConfig.Runtimes[runtimeHandler]
633+
if !ok {
634+
return criconfig.Runtime{}, errors.Errorf("no runtime for %q is configured", runtimeHandler)
623635
}
624-
return c.config.ContainerdConfig.DefaultRuntime, nil
636+
return handler, nil
625637
}

pkg/server/sandbox_run_test.go

+35-1
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,18 @@ func TestGetSandboxRuntime(t *testing.T) {
533533
Root: "",
534534
}
535535

536+
fooRuntime := criconfig.Runtime{
537+
Type: "io.containerd.runtime.v1.linux",
538+
Engine: "foo-bar",
539+
Root: "",
540+
}
541+
536542
for desc, test := range map[string]struct {
537543
sandboxConfig *runtime.PodSandboxConfig
544+
runtimeHandler string
538545
defaultRuntime criconfig.Runtime
539546
untrustedWorkloadRuntime criconfig.Runtime
547+
runtimes map[string]criconfig.Runtime
540548
expectErr bool
541549
expectedRuntime criconfig.Runtime
542550
}{
@@ -595,6 +603,31 @@ func TestGetSandboxRuntime(t *testing.T) {
595603
defaultRuntime: defaultRuntime,
596604
expectErr: true,
597605
},
606+
"should use 'untrusted' runtime for untrusted workload": {
607+
sandboxConfig: &runtime.PodSandboxConfig{
608+
Annotations: map[string]string{
609+
annotations.UntrustedWorkload: "true",
610+
},
611+
},
612+
defaultRuntime: defaultRuntime,
613+
runtimes: map[string]criconfig.Runtime{criconfig.RuntimeUntrusted: untrustedWorkloadRuntime},
614+
expectedRuntime: untrustedWorkloadRuntime,
615+
},
616+
"should use correct runtime for a runtime handler": {
617+
sandboxConfig: &runtime.PodSandboxConfig{},
618+
runtimeHandler: "foo",
619+
defaultRuntime: defaultRuntime,
620+
untrustedWorkloadRuntime: untrustedWorkloadRuntime,
621+
runtimes: map[string]criconfig.Runtime{"foo": fooRuntime},
622+
expectedRuntime: fooRuntime,
623+
},
624+
"should return error if runtime handler is required but not configured": {
625+
sandboxConfig: &runtime.PodSandboxConfig{},
626+
runtimeHandler: "bar",
627+
defaultRuntime: defaultRuntime,
628+
runtimes: map[string]criconfig.Runtime{"foo": fooRuntime},
629+
expectErr: true,
630+
},
598631
} {
599632
t.Run(desc, func(t *testing.T) {
600633
cri := newTestCRIService()
@@ -603,7 +636,8 @@ func TestGetSandboxRuntime(t *testing.T) {
603636
}
604637
cri.config.ContainerdConfig.DefaultRuntime = test.defaultRuntime
605638
cri.config.ContainerdConfig.UntrustedWorkloadRuntime = test.untrustedWorkloadRuntime
606-
r, err := cri.getSandboxRuntime(test.sandboxConfig)
639+
cri.config.ContainerdConfig.Runtimes = test.runtimes
640+
r, err := cri.getSandboxRuntime(test.sandboxConfig, test.runtimeHandler)
607641
assert.Equal(t, test.expectErr, err != nil)
608642
assert.Equal(t, test.expectedRuntime, r)
609643
})

0 commit comments

Comments
 (0)