File: convert.go

package info (click to toggle)
docker.io 26.1.5%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,576 kB
  • sloc: sh: 5,748; makefile: 912; ansic: 664; asm: 228; python: 162
file content (35 lines) | stat: -rw-r--r-- 1,118 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package shimopts

import (
	runtimeoptions "github.com/containerd/containerd/pkg/runtimeoptions/v1"
	"github.com/containerd/containerd/plugin"
	runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
	"github.com/pelletier/go-toml"
)

// Generate converts opts into a runtime options value for the runtimeType which
// can be passed into containerd.
func Generate(runtimeType string, opts map[string]interface{}) (interface{}, error) {
	// This is horrible, but we have no other choice. The containerd client
	// can only handle options values which can be marshaled into a
	// typeurl.Any. And we're in good company: cri-containerd handles shim
	// options in the same way.
	var out interface{}
	switch runtimeType {
	case plugin.RuntimeRuncV1, plugin.RuntimeRuncV2:
		out = &runcoptions.Options{}
	default:
		out = &runtimeoptions.Options{}
	}

	// We can't use mergo.Map as it is too strict about type-assignability
	// with numeric types.
	tree, err := toml.TreeFromMap(opts)
	if err != nil {
		return nil, err
	}
	if err := tree.Unmarshal(out); err != nil {
		return nil, err
	}
	return out, nil
}