Skip to content

Commit 41ac3aa

Browse files
refactor: update import paths to use Project-HAMi/HAMi package structure and remove unused files
Signed-off-by: haitwang-cloud <[email protected]>
1 parent 0a38996 commit 41ac3aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+424
-6084
lines changed

cmd/device-plugin/nvidia/main.go

Lines changed: 164 additions & 151 deletions
Large diffs are not rendered by default.
Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,80 @@
11
/*
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+
*/
1616

1717
package main
1818

1919
import (
2020
"fmt"
2121

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"
2525

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"
2830
)
2931

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)
4236

4337
deviceListStrategies, err := spec.NewDeviceListStrategies(*config.Flags.Plugin.DeviceListStrategy)
4438
if err != nil {
4539
return nil, fmt.Errorf("invalid device list strategy: %v", err)
4640
}
4741

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+
}
4946

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()),
5351
cdi.WithTargetDriverRoot(*config.Flags.NvidiaDriverRoot),
52+
cdi.WithTargetDevRoot(*config.Flags.NvidiaDevRoot),
5453
cdi.WithNvidiaCTKPath(*config.Flags.Plugin.NvidiaCTKPath),
55-
cdi.WithNvml(nvmllib),
5654
cdi.WithDeviceIDStrategy(*config.Flags.Plugin.DeviceIDStrategy),
5755
cdi.WithVendor("k8s.device-plugin.nvidia.com"),
5856
cdi.WithGdsEnabled(*config.Flags.GDSEnabled),
5957
cdi.WithMofedEnabled(*config.Flags.MOFEDEnabled),
58+
cdi.WithImexChannels(imexChannels),
6059
)
6160
if err != nil {
6261
return nil, fmt.Errorf("unable to create cdi handler: %v", err)
6362
}
6463

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),
7270
)
7371
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)
7573
}
7674

77-
if err := m.CreateCDISpecFile(); err != nil {
75+
if err := cdiHandler.CreateSpecFile(); err != nil {
7876
return nil, fmt.Errorf("unable to create cdi spec file: %v", err)
7977
}
8078

81-
return m, nil
79+
return plugins, nil
8280
}

cmd/device-plugin/nvidia/root.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 main
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"path/filepath"
23+
)
24+
25+
type root string
26+
27+
func (r root) join(parts ...string) string {
28+
return filepath.Join(append([]string{string(r)}, parts...)...)
29+
}
30+
31+
// getDevRoot returns the dev root associated with the root.
32+
// If the root is not a dev root, this defaults to "/".
33+
func (r root) getDevRoot() string {
34+
if r.isDevRoot() {
35+
return string(r)
36+
}
37+
return "/"
38+
}
39+
40+
// isDevRoot checks whether the specified root is a dev root.
41+
// A dev root is defined as a root containing a /dev folder.
42+
func (r root) isDevRoot() bool {
43+
stat, err := os.Stat(filepath.Join(string(r), "dev"))
44+
if err != nil {
45+
return false
46+
}
47+
return stat.IsDir()
48+
}
49+
50+
func (r root) tryResolveLibrary(libraryName string) string {
51+
if r == "" || r == "/" {
52+
return libraryName
53+
}
54+
55+
librarySearchPaths := []string{
56+
"/usr/lib64",
57+
"/usr/lib/x86_64-linux-gnu",
58+
"/usr/lib/aarch64-linux-gnu",
59+
"/lib64",
60+
"/lib/x86_64-linux-gnu",
61+
"/lib/aarch64-linux-gnu",
62+
}
63+
64+
for _, d := range librarySearchPaths {
65+
l := r.join(d, libraryName)
66+
resolved, err := resolveLink(l)
67+
if err != nil {
68+
continue
69+
}
70+
return resolved
71+
}
72+
73+
return libraryName
74+
}
75+
76+
// resolveLink finds the target of a symlink or the file itself in the
77+
// case of a regular file.
78+
// This is equivalent to running `readlink -f ${l}`.
79+
func resolveLink(l string) (string, error) {
80+
resolved, err := filepath.EvalSymlinks(l)
81+
if err != nil {
82+
return "", fmt.Errorf("error resolving link '%v': %w", l, err)
83+
}
84+
return resolved, nil
85+
}

cmd/device-plugin/nvidia/vgpucfg.go

Lines changed: 0 additions & 121 deletions
This file was deleted.

0 commit comments

Comments
 (0)