convert.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package shimopts
  2. import (
  3. runhcsoptions "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
  4. runtimeoptions "github.com/containerd/containerd/pkg/runtimeoptions/v1"
  5. "github.com/containerd/containerd/plugin"
  6. runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
  7. "github.com/pelletier/go-toml"
  8. )
  9. // Generate converts opts into a runtime options value for the runtimeType which
  10. // can be passed into containerd.
  11. func Generate(runtimeType string, opts map[string]interface{}) (interface{}, error) {
  12. // This is horrible, but we have no other choice. The containerd client
  13. // can only handle options values which can be marshaled into a
  14. // typeurl.Any. And we're in good company: cri-containerd handles shim
  15. // options in the same way.
  16. var out interface{}
  17. switch runtimeType {
  18. case plugin.RuntimeRuncV1, plugin.RuntimeRuncV2:
  19. out = &runcoptions.Options{}
  20. case "io.containerd.runhcs.v1":
  21. out = &runhcsoptions.Options{}
  22. default:
  23. out = &runtimeoptions.Options{}
  24. }
  25. // We can't use mergo.Map as it is too strict about type-assignability
  26. // with numeric types.
  27. tree, err := toml.TreeFromMap(opts)
  28. if err != nil {
  29. return nil, err
  30. }
  31. if err := tree.Unmarshal(out); err != nil {
  32. return nil, err
  33. }
  34. return out, nil
  35. }