|
1 | 1 | /* |
2 | | -Copyright 2024 The HAMi Authors. |
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 | | -*/ |
| 2 | + * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. |
| 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 | 16 |
|
17 | 17 | package main |
18 | 18 |
|
19 | 19 | import ( |
20 | 20 | "fmt" |
21 | 21 |
|
22 | | - "github.com/Project-HAMi/HAMi/pkg/device-plugin/nvidiadevice/nvinternal/cdi" |
23 | | - "github.com/Project-HAMi/HAMi/pkg/device-plugin/nvidiadevice/nvinternal/plugin/manager" |
24 | | - "github.com/Project-HAMi/HAMi/pkg/device/nvidia" |
| 22 | + "github.com/NVIDIA/go-nvlib/pkg/nvlib/device" |
| 23 | + "github.com/NVIDIA/go-nvlib/pkg/nvlib/info" |
| 24 | + "github.com/NVIDIA/go-nvml/pkg/nvml" |
25 | 25 |
|
26 | | - "github.com/NVIDIA/go-nvlib/pkg/nvml" |
27 | | - spec "github.com/NVIDIA/k8s-device-plugin/api/config/v1" |
| 26 | + spec "github.com/Project-HAMi/HAMi/pkg/nvidia-plugin/api/config/v1" |
| 27 | + "github.com/Project-HAMi/HAMi/pkg/nvidia-plugin/pkg/cdi" |
| 28 | + "github.com/Project-HAMi/HAMi/pkg/nvidia-plugin/pkg/imex" |
| 29 | + "github.com/Project-HAMi/HAMi/pkg/nvidia-plugin/pkg/plugin" |
28 | 30 | ) |
29 | 31 |
|
30 | | -// NewPluginManager creates an NVML-based plugin manager. |
31 | | -func NewPluginManager(config *nvidia.DeviceConfig) (manager.Interface, error) { |
32 | | - var err error |
33 | | - switch *config.Flags.MigStrategy { |
34 | | - case spec.MigStrategyNone: |
35 | | - case spec.MigStrategySingle: |
36 | | - case spec.MigStrategyMixed: |
37 | | - default: |
38 | | - return nil, fmt.Errorf("unknown strategy: %v", *config.Flags.MigStrategy) |
39 | | - } |
40 | | - |
41 | | - nvmllib := nvml.New() |
| 32 | +// GetPlugins returns a set of plugins for the specified configuration. |
| 33 | +func GetPlugins(infolib info.Interface, nvmllib nvml.Interface, devicelib device.Interface, config *spec.Config) ([]plugin.Interface, error) { |
| 34 | + // TODO: We could consider passing this as an argument since it should already be used to construct nvmllib. |
| 35 | + driverRoot := root(*config.Flags.Plugin.ContainerDriverRoot) |
42 | 36 |
|
43 | 37 | deviceListStrategies, err := spec.NewDeviceListStrategies(*config.Flags.Plugin.DeviceListStrategy) |
44 | 38 | if err != nil { |
45 | 39 | return nil, fmt.Errorf("invalid device list strategy: %v", err) |
46 | 40 | } |
47 | 41 |
|
48 | | - cdiEnabled := deviceListStrategies.IsCDIEnabled() |
| 42 | + imexChannels, err := imex.GetChannels(config, driverRoot.getDevRoot()) |
| 43 | + if err != nil { |
| 44 | + return nil, fmt.Errorf("error querying IMEX channels: %w", err) |
| 45 | + } |
49 | 46 |
|
50 | | - cdiHandler, err := cdi.New( |
51 | | - cdi.WithEnabled(cdiEnabled), |
52 | | - cdi.WithDriverRoot(*config.Flags.Plugin.ContainerDriverRoot), |
| 47 | + cdiHandler, err := cdi.New(infolib, nvmllib, devicelib, |
| 48 | + cdi.WithDeviceListStrategies(deviceListStrategies), |
| 49 | + cdi.WithDriverRoot(string(driverRoot)), |
| 50 | + cdi.WithDevRoot(driverRoot.getDevRoot()), |
53 | 51 | cdi.WithTargetDriverRoot(*config.Flags.NvidiaDriverRoot), |
| 52 | + cdi.WithTargetDevRoot(*config.Flags.NvidiaDevRoot), |
54 | 53 | cdi.WithNvidiaCTKPath(*config.Flags.Plugin.NvidiaCTKPath), |
55 | | - cdi.WithNvml(nvmllib), |
56 | 54 | cdi.WithDeviceIDStrategy(*config.Flags.Plugin.DeviceIDStrategy), |
57 | 55 | cdi.WithVendor("k8s.device-plugin.nvidia.com"), |
58 | 56 | cdi.WithGdsEnabled(*config.Flags.GDSEnabled), |
59 | 57 | cdi.WithMofedEnabled(*config.Flags.MOFEDEnabled), |
| 58 | + cdi.WithImexChannels(imexChannels), |
60 | 59 | ) |
61 | 60 | if err != nil { |
62 | 61 | return nil, fmt.Errorf("unable to create cdi handler: %v", err) |
63 | 62 | } |
64 | 63 |
|
65 | | - m, err := manager.New( |
66 | | - manager.WithNVML(nvmllib), |
67 | | - manager.WithCDIEnabled(cdiEnabled), |
68 | | - manager.WithCDIHandler(cdiHandler), |
69 | | - manager.WithConfig(config), |
70 | | - manager.WithFailOnInitError(*config.Flags.FailOnInitError), |
71 | | - manager.WithMigStrategy(*config.Flags.MigStrategy), |
| 64 | + plugins, err := plugin.New(infolib, nvmllib, devicelib, |
| 65 | + plugin.WithCDIHandler(cdiHandler), |
| 66 | + plugin.WithConfig(config), |
| 67 | + plugin.WithDeviceListStrategies(deviceListStrategies), |
| 68 | + plugin.WithFailOnInitError(*config.Flags.FailOnInitError), |
| 69 | + plugin.WithImexChannels(imexChannels), |
72 | 70 | ) |
73 | 71 | if err != nil { |
74 | | - return nil, fmt.Errorf("unable to create plugin manager: %v", err) |
| 72 | + return nil, fmt.Errorf("unable to create plugins: %w", err) |
75 | 73 | } |
76 | 74 |
|
77 | | - if err := m.CreateCDISpecFile(); err != nil { |
| 75 | + if err := cdiHandler.CreateSpecFile(); err != nil { |
78 | 76 | return nil, fmt.Errorf("unable to create cdi spec file: %v", err) |
79 | 77 | } |
80 | 78 |
|
81 | | - return m, nil |
| 79 | + return plugins, nil |
82 | 80 | } |
0 commit comments