-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: nvidia-gpu 2.0 make compatible for k8s
Signed-off-by: codejuan <xh@decbug.com>
- Loading branch information
Showing
3 changed files
with
129 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package mgr | ||
|
||
import ( | ||
"os/exec" | ||
|
||
"github.com/alibaba/pouch/pkg/utils" | ||
|
||
"github.com/opencontainers/runtime-spec/specs-go" | ||
) | ||
|
||
var ( | ||
nvidiaHookName = "nvidia-container-runtime-hook" | ||
) | ||
|
||
func setNvidiaHook(c *Container, spec *SpecWrapper) error { | ||
n := c.HostConfig.NvidiaConfig | ||
|
||
// to make compatible for k8s. | ||
// if user set environments of NVIDIA, then set prestart hook | ||
kv := utils.ConvertKVStrToMapWithNoErr(c.Config.Env) | ||
_, hasEnvCapabilities := kv["NVIDIA_DRIVER_CAPABILITIES"] | ||
_, hasEnvDevices := kv["NVIDIA_VISIBLE_DEVICES"] | ||
|
||
if n == nil && !hasEnvCapabilities && !hasEnvDevices { | ||
return nil | ||
} | ||
|
||
path, err := exec.LookPath(nvidiaHookName) | ||
if err != nil { | ||
return err | ||
} | ||
args := []string{path} | ||
nvidiaPrestart := specs.Hook{ | ||
Path: path, | ||
Args: append(args, "prestart"), | ||
} | ||
spec.s.Hooks.Prestart = append(spec.s.Hooks.Prestart, nvidiaPrestart) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package mgr | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"path" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
"github.com/opencontainers/runtime-spec/specs-go" | ||
) | ||
|
||
func Test_setNvidiaHook(t *testing.T) { | ||
nvidiaHookName = "test-nvidia-container-runtime-hook" | ||
installDir := "/usr/local/bin/" | ||
fullname := path.Join(installDir, nvidiaHookName) | ||
os.Remove(fullname) | ||
os.Create(fullname) | ||
os.Chmod(fullname, 0755) | ||
path, _ := exec.LookPath(nvidiaHookName) | ||
defer func() { | ||
os.Remove(fullname) | ||
}() | ||
|
||
tests := []struct { | ||
name string | ||
c *Container | ||
specWrapper *SpecWrapper | ||
expectedPrestart []specs.Hook | ||
}{ | ||
{ | ||
"NvidiaConfig is nil, NvidiaEnv is null", | ||
&Container{ | ||
HostConfig: &types.HostConfig{ | ||
Resources: types.Resources{ | ||
NvidiaConfig: nil, | ||
}, | ||
}, | ||
Config: &types.ContainerConfig{ | ||
Env: []string{}, | ||
}, | ||
}, | ||
&SpecWrapper{ | ||
s: &specs.Spec{ | ||
Hooks: &specs.Hooks{ | ||
Prestart: []specs.Hook{}, | ||
}, | ||
}, | ||
}, | ||
[]specs.Hook{}, | ||
}, | ||
{ | ||
"NvidiaConfig is nil, NvidiaEnv not null", | ||
&Container{ | ||
HostConfig: &types.HostConfig{ | ||
Resources: types.Resources{ | ||
NvidiaConfig: nil, | ||
}, | ||
}, | ||
Config: &types.ContainerConfig{ | ||
Env: []string{"NVIDIA_DRIVER_CAPABILITIES=all", "NVIDIA_VISIBLE_DEVICES=all"}, | ||
}, | ||
}, | ||
&SpecWrapper{ | ||
s: &specs.Spec{ | ||
Hooks: &specs.Hooks{ | ||
Prestart: []specs.Hook{}, | ||
}, | ||
}, | ||
}, | ||
// exec.LookPath("nvidia-container-runtime-hook") return error, | ||
[]specs.Hook{specs.Hook{ | ||
Path: path, | ||
Args: append([]string{path}, "prestart"), | ||
}}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
err := setNvidiaHook(tt.c, tt.specWrapper) | ||
if err != nil { | ||
t.Errorf("setNvidiaHook = %v, want %v", err, nil) | ||
} | ||
if !reflect.DeepEqual(tt.specWrapper.s.Hooks.Prestart, tt.expectedPrestart) { | ||
t.Errorf("setNvidiaHook = %v, want %v", tt.specWrapper.s.Hooks.Poststart, tt.expectedPrestart) | ||
} | ||
} | ||
} |