|
| 1 | +/** |
| 2 | +# Copyright 2024 NVIDIA CORPORATION |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +**/ |
| 16 | + |
| 17 | +package edits |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "tags.cncf.io/container-device-interface/pkg/cdi" |
| 23 | + |
| 24 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/discover" |
| 25 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" |
| 26 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/oci" |
| 27 | +) |
| 28 | + |
| 29 | +// New creates an Interface from the supplied options. |
| 30 | +func New(opts ...Option) Interface { |
| 31 | + o := &options{} |
| 32 | + for _, opt := range opts { |
| 33 | + opt(o) |
| 34 | + } |
| 35 | + if o.logger == nil { |
| 36 | + o.logger = logger.New() |
| 37 | + } |
| 38 | + |
| 39 | + return o |
| 40 | +} |
| 41 | + |
| 42 | +// EditsFromDiscoverer creates CDI container edits for the specified discoverer. |
| 43 | +func (o *options) EditsFromDiscoverer(d discover.Discover) (*cdi.ContainerEdits, error) { |
| 44 | + devices, err := d.Devices() |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("failed to discover devices: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + mounts, err := d.Mounts() |
| 50 | + if err != nil { |
| 51 | + return nil, fmt.Errorf("failed to discover mounts: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + hooks, err := d.Hooks() |
| 55 | + if err != nil { |
| 56 | + return nil, fmt.Errorf("failed to discover hooks: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + c := NewContainerEdits() |
| 60 | + for _, d := range devices { |
| 61 | + edits, err := device(d).toEdits() |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("failed to create container edits for device: %w", err) |
| 64 | + } |
| 65 | + c.Append(edits) |
| 66 | + } |
| 67 | + |
| 68 | + for _, m := range mounts { |
| 69 | + c.Append(mount(m).toEdits()) |
| 70 | + } |
| 71 | + |
| 72 | + for _, h := range hooks { |
| 73 | + c.Append(hook(h).toEdits()) |
| 74 | + } |
| 75 | + |
| 76 | + return c, nil |
| 77 | +} |
| 78 | + |
| 79 | +// SpecModifierFromDiscoverer creates a SpecModifier that defines the required OCI spec edits (as CDI ContainerEdits) from the specified |
| 80 | +// discoverer. |
| 81 | +func (o *options) SpecModifierFromDiscoverer(d discover.Discover) (oci.SpecModifier, error) { |
| 82 | + c, err := o.EditsFromDiscoverer(d) |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("error constructing container edits: %w", err) |
| 85 | + } |
| 86 | + e := edits{ |
| 87 | + ContainerEdits: *c, |
| 88 | + logger: o.logger, |
| 89 | + } |
| 90 | + |
| 91 | + return &e, nil |
| 92 | +} |
0 commit comments