|
1 | 1 | package specs |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | | - "fmt" |
6 | | - |
7 | 4 | spec "github.com/opencontainers/runtime-spec/specs-go" |
8 | 5 | ) |
9 | 6 |
|
10 | | -// ApplyOCIEditsForDevice applies devices OCI edits, in other words |
11 | | -// it finds the device in the CDI spec and applies the OCI patches that device |
12 | | -// requires to the OCI specification. |
13 | | -func ApplyOCIEditsForDevice(config *spec.Spec, cdi *Spec, dev string) error { |
14 | | - for _, d := range cdi.Devices { |
15 | | - if d.Name != dev { |
16 | | - continue |
17 | | - } |
18 | | - |
19 | | - return ApplyEditsToOCISpec(config, &d.ContainerEdits) |
20 | | - } |
21 | | - |
22 | | - return fmt.Errorf("CDI: device %q not found for spec %q", dev, cdi.Kind) |
23 | | -} |
24 | | - |
25 | | -// ApplyOCIEdits applies the OCI edits the CDI spec declares globally |
26 | | -func ApplyOCIEdits(config *spec.Spec, cdi *Spec) error { |
27 | | - return ApplyEditsToOCISpec(config, &cdi.ContainerEdits) |
28 | | -} |
29 | | - |
30 | | -// ApplyEditsToOCISpec applies the specified edits to the OCI spec. |
31 | | -func ApplyEditsToOCISpec(config *spec.Spec, edits *ContainerEdits) error { |
32 | | - if config == nil { |
33 | | - return errors.New("spec is nil") |
34 | | - } |
35 | | - if edits == nil { |
36 | | - return nil |
37 | | - } |
38 | | - |
39 | | - if len(edits.Env) > 0 { |
40 | | - if config.Process == nil { |
41 | | - config.Process = &spec.Process{} |
42 | | - } |
43 | | - config.Process.Env = append(config.Process.Env, edits.Env...) |
44 | | - } |
45 | | - |
46 | | - for _, d := range edits.DeviceNodes { |
47 | | - if config.Linux == nil { |
48 | | - config.Linux = &spec.Linux{} |
49 | | - } |
50 | | - config.Linux.Devices = append(config.Linux.Devices, d.ToOCI()) |
51 | | - } |
52 | | - |
53 | | - for _, m := range edits.Mounts { |
54 | | - config.Mounts = append(config.Mounts, m.ToOCI()) |
55 | | - } |
56 | | - |
57 | | - for _, h := range edits.Hooks { |
58 | | - if config.Hooks == nil { |
59 | | - config.Hooks = &spec.Hooks{} |
60 | | - } |
61 | | - switch h.HookName { |
62 | | - case "prestart": |
63 | | - config.Hooks.Prestart = append(config.Hooks.Prestart, h.ToOCI()) |
64 | | - case "createRuntime": |
65 | | - config.Hooks.CreateRuntime = append(config.Hooks.CreateRuntime, h.ToOCI()) |
66 | | - case "createContainer": |
67 | | - config.Hooks.CreateContainer = append(config.Hooks.CreateContainer, h.ToOCI()) |
68 | | - case "startContainer": |
69 | | - config.Hooks.StartContainer = append(config.Hooks.StartContainer, h.ToOCI()) |
70 | | - case "poststart": |
71 | | - config.Hooks.Poststart = append(config.Hooks.Poststart, h.ToOCI()) |
72 | | - case "poststop": |
73 | | - config.Hooks.Poststop = append(config.Hooks.Poststop, h.ToOCI()) |
74 | | - default: |
75 | | - fmt.Printf("CDI: Unknown hook %q\n", h.HookName) |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - return nil |
80 | | -} |
81 | | - |
82 | 7 | // ToOCI returns the opencontainers runtime Spec Hook for this Hook. |
83 | 8 | func (h *Hook) ToOCI() spec.Hook { |
84 | 9 | return spec.Hook{ |
|
0 commit comments