-
Notifications
You must be signed in to change notification settings - Fork 170
remove shell dependency from the driver image #972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
leiyiz
wants to merge
2
commits into
kubernetes-sigs:main
Choose a base branch
from
leiyiz:sh-to-go
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,226 @@ | ||
| /* | ||
| * Copyright (c) 2026 NVIDIA CORPORATION. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "time" | ||
|
|
||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| // Main intent: help users to self-troubleshoot when the GPU driver is not set up | ||
| // properly before installing this DRA driver. In that case, the log of the init | ||
| // container running the prestart code is meant to yield an actionable error message. | ||
| // For now, rely on k8s to implement a high-level retry with back-off. | ||
| func runPrestart(ctx context.Context) error { | ||
| waitS := 10 * time.Second | ||
| attempt := 0 | ||
|
|
||
| nvidiaDriverRoot := os.Getenv("NVIDIA_DRIVER_ROOT") | ||
| if nvidiaDriverRoot == "" { | ||
| // Not set, or set to empty string (not distinguishable). | ||
| // Normalize to "/" (treated as such elsewhere). | ||
| nvidiaDriverRoot = "/" | ||
| } | ||
|
|
||
| driverRootParent := "/driver-root-parent" | ||
| // filepath.Base removes trailing slash (if existing) and get last path element. | ||
| driverRootPath := filepath.Join(driverRootParent, filepath.Base(nvidiaDriverRoot)) | ||
|
|
||
| // Ensure the /driver-root-parent directory exists | ||
| if err := os.MkdirAll(driverRootParent, 0755); err != nil { | ||
| klog.Warningf("Failed to create %s: %v", driverRootParent, err) | ||
| } | ||
|
|
||
| // Create in-container path /driver-root as a symlink. Expectation: link may be | ||
| // broken initially (e.g., if the GPU operator isn't deployed yet. The link heals | ||
| // once the driver becomes mounted (e.g., once GPU operator provides the driver | ||
| // on the host at /run/nvidia/driver). | ||
| fmt.Printf("create symlink: /driver-root -> %s\n", driverRootPath) | ||
|
|
||
| if err := os.Symlink(driverRootPath, "/driver-root"); err != nil { | ||
| klog.Warningf("Failed to create symlink: %v", err) | ||
| } | ||
|
|
||
| for { | ||
| if validateAndExitOnSuccess(ctx, nvidiaDriverRoot, attempt) { | ||
| return nil | ||
| } | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| // DS pods may get deleted (terminated with SIGTERM) and re-created when the GPU | ||
| // Operator driver container creates a mount at /run/nvidia. Make that explicit. | ||
| fmt.Printf("%s: received SIGTERM\n", time.Now().UTC().Format("2006-01-02T15:04:05.000Z")) | ||
| return nil | ||
| case <-time.After(waitS): | ||
| attempt++ | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func emitCommonErr(nvidiaDriverRoot string) { | ||
| fmt.Printf("Check failed. Has the NVIDIA GPU driver been set up? "+ | ||
| "It is expected to be installed under "+ | ||
| "NVIDIA_DRIVER_ROOT (currently set to '%s') "+ | ||
| "in the host filesystem. If that path appears to be unexpected: "+ | ||
| "review the DRA driver's 'nvidiaDriverRoot' Helm chart variable. "+ | ||
| "Otherwise, review if the GPU driver has "+ | ||
| "actually been installed under that path.\n", nvidiaDriverRoot) | ||
| } | ||
|
|
||
| func validateAndExitOnSuccess(ctx context.Context, nvidiaDriverRoot string, attempt int) bool { | ||
| fmt.Printf("%s /driver-root (%s on host): ", time.Now().UTC().Format("2006-01-02T15:04:05Z"), nvidiaDriverRoot) | ||
|
|
||
| // Search specific set of directories (not recursively: not required, and | ||
| // /driver-root may be a big tree). Limit to first result (multiple results | ||
| // are a bit of a pathological state, but continue with validation logic). | ||
|
|
||
| // original script does not follow symlink for nvpath but since symlinkm | ||
| // can also execute so reuse findFirstFile to avoid new func that's largely | ||
| // duplicative. | ||
| nvPath := findFirstFile( | ||
| "nvidia-smi", | ||
| "/driver-root/opt/bin", | ||
| "/driver-root/usr/bin", | ||
| "/driver-root/usr/sbin", | ||
| "/driver-root/bin", | ||
| "/driver-root/sbin", | ||
| ) | ||
|
|
||
| nvLibPath := findFirstFile( | ||
| "libnvidia-ml.so.1", | ||
| "/driver-root/usr/lib64", | ||
| "/driver-root/usr/lib/x86_64-linux-gnu", | ||
| "/driver-root/usr/lib/aarch64-linux-gnu", | ||
| "/driver-root/lib64", | ||
| "/driver-root/lib/x86_64-linux-gnu", | ||
| "/driver-root/lib/aarch64-linux-gnu", | ||
| ) | ||
|
|
||
| if nvPath == "" { | ||
| fmt.Printf("nvidia-smi: not found, ") | ||
| } else { | ||
| fmt.Printf("nvidia-smi: '%s', ", nvPath) | ||
| } | ||
|
|
||
| if nvLibPath == "" { | ||
| fmt.Printf("libnvidia-ml.so.1: not found, ") | ||
| } else { | ||
| fmt.Printf("libnvidia-ml.so.1: '%s', ", nvLibPath) | ||
| } | ||
|
|
||
| // Log top-level entries in /driver-root (this may be valuable debug info). | ||
| entries, _ := os.ReadDir("/driver-root") | ||
| var entryNames string | ||
| for i, e := range entries { | ||
| if i > 0 { | ||
| entryNames += " " | ||
| } | ||
| entryNames += e.Name() | ||
| } | ||
| fmt.Printf("current contents: [%s].\n", entryNames) | ||
|
|
||
| if nvPath != "" && nvLibPath != "" { | ||
| // Run with clean environment (only LD_PRELOAD; nvidia-smi has only this | ||
| // dependency). Emit message before invocation (nvidia-smi may be slow or | ||
| // hang). | ||
| fmt.Printf("invoke: env -i LD_PRELOAD=%s %s\n", nvLibPath, nvPath) | ||
|
|
||
| // override default env to just LD_PRELOAD | ||
| cmd := exec.CommandContext(ctx, nvPath) | ||
| cmd.Env = []string{"LD_PRELOAD=" + nvLibPath} | ||
| cmd.Stdout = os.Stdout | ||
| cmd.Stderr = os.Stderr | ||
|
|
||
| err := cmd.Run() | ||
| // For checking GPU driver health: rely on nvidia-smi's exit code. Rely | ||
| // on code 0 signaling that the driver is properly set up. See section | ||
| // 'RETURN VALUE' in the nvidia-smi man page for meaning of error codes. | ||
| if err == nil { | ||
| fmt.Printf("nvidia-smi returned with code 0: success, leave\n") | ||
| return true | ||
| } | ||
|
|
||
| if exitErr, ok := err.(*exec.ExitError); ok { | ||
| fmt.Printf("exit code: %d\n", exitErr.ExitCode()) | ||
| } else { | ||
| // nvidia-smi fails to start. e.g. permission denied etc. | ||
| fmt.Printf("execution failed: %v, exit code: -1\n", err) | ||
| } | ||
| } | ||
|
|
||
| // Reduce log volume: log hints only every Nth attempt. | ||
| if attempt%6 != 0 { | ||
| return false | ||
| } | ||
|
|
||
| // nvidia-smi binaries not found, or execution failed. First, provide generic | ||
| // error message. Then, try to provide actionable hints for common problems. | ||
| fmt.Println() | ||
| emitCommonErr(nvidiaDriverRoot) | ||
|
|
||
| // For host-provided driver not at / provide feedback for two special cases. | ||
| if nvidiaDriverRoot != "/" { | ||
| if len(entries) == 0 { | ||
| fmt.Printf("Hint: Directory %s on the host is empty\n", nvidiaDriverRoot) | ||
| } else { | ||
| // Not empty, but at least one of the binaries not found: this is a | ||
| // rather pathological state. | ||
| if nvPath == "" || nvLibPath == "" { | ||
| fmt.Printf("Hint: Directory %s is not empty but at least one of the binaries wasn't found.\n", nvidiaDriverRoot) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Common mistake: driver container, but forgot `--set nvidiaDriverRoot` | ||
| if nvidiaDriverRoot == "/" { | ||
| if _, err := os.Stat("/driver-root/run/nvidia/driver/usr/bin/nvidia-smi"); err == nil { | ||
| fmt.Printf("Hint: '/run/nvidia/driver/usr/bin/nvidia-smi' exists on the host, you " + | ||
| "may want to re-install the DRA driver Helm chart with " + | ||
| "--set nvidiaDriverRoot=/run/nvidia/driver\n") | ||
| } | ||
| } | ||
|
|
||
| if nvidiaDriverRoot == "/run/nvidia/driver" { | ||
| fmt.Printf("Hint: NVIDIA_DRIVER_ROOT is set to '/run/nvidia/driver' " + | ||
| "which typically means that the NVIDIA GPU Operator " + | ||
| "manages the GPU driver. Make sure that the GPU Operator " + | ||
| "is deployed and healthy.\n") | ||
| } | ||
| fmt.Println() | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| // findFirstFile finds the first occurrence of filename in the provided | ||
| // directories not recursively. | ||
| // It follows symlinks (similar to find -L). | ||
| func findFirstFile(filename string, dirs ...string) string { | ||
| for _, dir := range dirs { | ||
| path := filepath.Join(dir, filename) | ||
| if info, err := os.Stat(path); err == nil && !info.IsDir() { | ||
| return path | ||
| } | ||
| } | ||
| return "" | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the comment might be out of date
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this bit does seem to be out of date with the forever loop. That's true for the shell script too, i can modify both to reflect that.
do you have other modifications in mind? other bits seems fine to me