summaryrefslogtreecommitdiff
path: root/vendor/github.com/container-orchestrated-devices/container-device-interface/specs-go
diff options
context:
space:
mode:
authorSebastian Jug <seb@stianj.ug>2021-03-29 20:21:00 -0400
committerSebastian Jug <seb@stianj.ug>2021-04-20 09:18:52 -0400
commitdb7cff8c86a35a4b1971c3fbb2365eff9cc205d4 (patch)
tree3dc20b52f6a8b2de4acf8cb99ebff81776680959 /vendor/github.com/container-orchestrated-devices/container-device-interface/specs-go
parentcf2c3a1f13710014892804aacf855cd6b001a5ea (diff)
downloadpodman-db7cff8c86a35a4b1971c3fbb2365eff9cc205d4.tar.gz
podman-db7cff8c86a35a4b1971c3fbb2365eff9cc205d4.tar.bz2
podman-db7cff8c86a35a4b1971c3fbb2365eff9cc205d4.zip
Add support for CDI device configuration
- Persist CDIDevices in container config - Add e2e test - Log HasDevice error and add additional condition for safety Signed-off-by: Sebastian Jug <seb@stianj.ug>
Diffstat (limited to 'vendor/github.com/container-orchestrated-devices/container-device-interface/specs-go')
-rw-r--r--vendor/github.com/container-orchestrated-devices/container-device-interface/specs-go/config.go50
-rw-r--r--vendor/github.com/container-orchestrated-devices/container-device-interface/specs-go/oci.go104
2 files changed, 154 insertions, 0 deletions
diff --git a/vendor/github.com/container-orchestrated-devices/container-device-interface/specs-go/config.go b/vendor/github.com/container-orchestrated-devices/container-device-interface/specs-go/config.go
new file mode 100644
index 000000000..0223bb703
--- /dev/null
+++ b/vendor/github.com/container-orchestrated-devices/container-device-interface/specs-go/config.go
@@ -0,0 +1,50 @@
+package specs
+
+// Spec is the base configuration for CDI
+type Spec struct {
+ Version string `json:"cdiVersion"`
+ Kind string `json:"kind"`
+ KindShort []string `json:"kindShort,omitempty"`
+ ContainerRuntime []string `json:"containerRuntime,omitempty"`
+
+ Devices []Devices `json:"devices"`
+ ContainerEdits ContainerEdits `json:"containerEdits,omitempty"`
+}
+
+// Devices is a "Device" a container runtime can add to a container
+type Devices struct {
+ Name string `json:"name"`
+ NameShort []string `json:"nameShort"`
+ ContainerEdits ContainerEdits `json:"containerEdits"`
+}
+
+// ContainerEdits are edits a container runtime must make to the OCI spec to expose the device.
+type ContainerEdits struct {
+ Env []string `json:"env,omitempty"`
+ DeviceNodes []*DeviceNode `json:"deviceNodes,omitempty"`
+ Hooks []*Hook `json:"hooks,omitempty"`
+ Mounts []*Mount `json:"mounts,omitempty"`
+}
+
+// DeviceNode represents a device node that needs to be added to the OCI spec.
+type DeviceNode struct {
+ HostPath string `json:"hostPath"`
+ ContainerPath string `json:"containerPath"`
+ Permissions []string `json:"permissions,omitempty"`
+}
+
+// Mount represents a mount that needs to be added to the OCI spec.
+type Mount struct {
+ HostPath string `json:"hostPath"`
+ ContainerPath string `json:"containerPath"`
+ Options []string `json:"options,omitempty"`
+}
+
+// Hook represents a hook that needs to be added to the OCI spec.
+type Hook struct {
+ HookName string `json:"hookName"`
+ Path string `json:"path"`
+ Args []string `json:"args,omitempty"`
+ Env []string `json:"env,omitempty"`
+ Timeout *int `json:"timeout,omitempty"`
+}
diff --git a/vendor/github.com/container-orchestrated-devices/container-device-interface/specs-go/oci.go b/vendor/github.com/container-orchestrated-devices/container-device-interface/specs-go/oci.go
new file mode 100644
index 000000000..c59cda55d
--- /dev/null
+++ b/vendor/github.com/container-orchestrated-devices/container-device-interface/specs-go/oci.go
@@ -0,0 +1,104 @@
+package specs
+
+import (
+ "errors"
+ "fmt"
+
+ spec "github.com/opencontainers/runtime-spec/specs-go"
+)
+
+// ApplyOCIEditsForDevice applies devices OCI edits, in other words
+// it finds the device in the CDI spec and applies the OCI patches that device
+// requires to the OCI specification.
+func ApplyOCIEditsForDevice(config *spec.Spec, cdi *Spec, dev string) error {
+ for _, d := range cdi.Devices {
+ if d.Name != dev {
+ continue
+ }
+
+ return ApplyEditsToOCISpec(config, &d.ContainerEdits)
+ }
+
+ return fmt.Errorf("CDI: device %q not found for spec %q", dev, cdi.Kind)
+}
+
+// ApplyOCIEdits applies the OCI edits the CDI spec declares globablly
+func ApplyOCIEdits(config *spec.Spec, cdi *Spec) error {
+ return ApplyEditsToOCISpec(config, &cdi.ContainerEdits)
+}
+
+// ApplyEditsToOCISpec applies the specified edits to the OCI spec.
+func ApplyEditsToOCISpec(config *spec.Spec, edits *ContainerEdits) error {
+ if config == nil {
+ return errors.New("spec is nil")
+ }
+ if edits == nil {
+ return nil
+ }
+
+ if len(edits.Env) > 0 {
+
+ if config.Process == nil {
+ config.Process = &spec.Process{}
+ }
+
+ config.Process.Env = append(config.Process.Env, edits.Env...)
+ }
+
+ for _, d := range edits.DeviceNodes {
+ config.Mounts = append(config.Mounts, toOCIDevice(d))
+ }
+
+ for _, m := range edits.Mounts {
+ config.Mounts = append(config.Mounts, toOCIMount(m))
+ }
+
+ for _, h := range edits.Hooks {
+ if config.Hooks == nil {
+ config.Hooks = &spec.Hooks{}
+ }
+ switch h.HookName {
+ case "prestart":
+ config.Hooks.Prestart = append(config.Hooks.Prestart, toOCIHook(h))
+ case "createRuntime":
+ config.Hooks.CreateRuntime = append(config.Hooks.CreateRuntime, toOCIHook(h))
+ case "createContainer":
+ config.Hooks.CreateContainer = append(config.Hooks.CreateContainer, toOCIHook(h))
+ case "startContainer":
+ config.Hooks.StartContainer = append(config.Hooks.StartContainer, toOCIHook(h))
+ case "poststart":
+ config.Hooks.Poststart = append(config.Hooks.Poststart, toOCIHook(h))
+ case "poststop":
+ config.Hooks.Poststop = append(config.Hooks.Poststop, toOCIHook(h))
+ default:
+ fmt.Printf("CDI: Unknown hook %q\n", h.HookName)
+ }
+ }
+
+ return nil
+}
+
+func toOCIHook(h *Hook) spec.Hook {
+ return spec.Hook{
+ Path: h.Path,
+ Args: h.Args,
+ Env: h.Env,
+ Timeout: h.Timeout,
+ }
+}
+
+func toOCIMount(m *Mount) spec.Mount {
+ return spec.Mount{
+ Source: m.HostPath,
+ Destination: m.ContainerPath,
+ Options: m.Options,
+ }
+}
+
+func toOCIDevice(d *DeviceNode) spec.Mount {
+ return spec.Mount{
+ Source: d.HostPath,
+ Destination: d.ContainerPath,
+ Options: d.Permissions,
+ }
+}