-
Notifications
You must be signed in to change notification settings - Fork 415
Fix handling of unrecognised CDI hooks #1308
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
Conversation
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.
Pull Request Overview
This PR adds warning functionality for unrecognized CDI hooks that include flags, addressing issue #1225. The change enhances error handling to catch cases where unknown hooks are invoked with command-line flags.
- Adds a
OnUsageError
handler to detect unrecognized hooks with flags - Imports the
strings
package for string manipulation in error checking
Pull Request Test Coverage Report for Build 18008895528Details
💛 - Coveralls |
Tested ./nvidia-cdi-hook unknown-hook --some-flag value
time="2025-09-22T11:25:26+02:00" level=warning msg="Unsupported CDI hook: unknown-hook" |
cmd/nvidia-cdi-hook/main.go
Outdated
OnUsageError: func(ctx context.Context, cmd *cli.Command, err error, isSubcommand bool) error { | ||
// Check if this is a "flag provided but not defined" error | ||
errMsg := err.Error() | ||
if strings.HasPrefix(errMsg, "flag provided but not defined: ") { |
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.
One nit: The err in urfave
is defined as:
providedButNotDefinedErrMsg = "flag provided but not defined: -"
and we don't include the -
here.
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.
ah yeah, let me edit!
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.
edited
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.
What about the case of a subcommand that does not accept arguments:
$ ./nvidia-cdi-hook foo
No help topic for 'foo'
$ echo $?
3
9bc97f8
to
28c5e29
Compare
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.
Pull Request Overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.
cmd/nvidia-cdi-hook/main.go
Outdated
OnUsageError: func(ctx context.Context, cmd *cli.Command, err error, isSubcommand bool) error { | ||
errMsg := err.Error() | ||
|
||
// Check if this is a "No help topic for" error (unrecognized |
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.
Since in Urfave we have a CommandNotFound
handler does it not make sense to handle this separately?
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.
One thing that I forgot to mention is that we would also have to update the nvidia-ctk hook
command.
28c5e29
to
13bbb7d
Compare
cmd/nvidia-ctk/hook/hook.go
Outdated
}, | ||
// Handle unrecognized commands when help is requested (e.g., help | ||
// unknowncommand) | ||
CommandNotFound: func(ctx context.Context, cmd *cli.Command, commandName string) { |
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.
These and the Action
are now strictly duplicated from nvidia-cdi-hook
. I don't think this is required with urfave/v3
. Do we want to refactor this?
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.
I did a small refactor, creating a shared function so we don't have duplicated code
13bbb7d
to
4616c6f
Compare
} | ||
} | ||
|
||
// NewHookCommand creates a new hook command with common behavior for handling |
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.
Nit: This is specifically CDI hooks, so maybe rename NewCDIHookCommand
?
// Handle unrecognized commands when help is requested (e.g., help | ||
// unknowncommand) | ||
CommandNotFound: func(ctx context.Context, cmd *cli.Command, commandName string) { | ||
logger.Warningf("Unsupported CDI hook: %v", commandName) |
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.
Question: Is there a reason that we don't call IssueUnsupportedHookWarning
(or at least reuse some of the logic there)?
if len(args) > 0 && cmd.Command(args[0]) == nil { | ||
// This is an unrecognized hook with flags - the default | ||
// Action will handle it | ||
return nil |
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.
Should we not issue a warning here?
opts := options{} | ||
|
||
// Create the top-level CLI | ||
c := cli.Command{ |
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.
Does it make sense to still construct the Command
s and then call a fuction to modify the members that we want instead?
4616c6f
to
57efba1
Compare
// ConfigureCDIHookCommand takes a base command and adds the common CDI hook | ||
// behavior for handling unsupported hooks. | ||
// The base command should have Name, Usage, and Version set as desired. | ||
func ConfigureCDIHookCommand(base *cli.Command, logger logger.Interface) *cli.Command { |
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 seems to indicate that we should call the functions something like AddUnsupportedHooksChecks
but we do add the subcommands at the end, so it's probably just the comment that needs to be udpated for accuracy.
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.
Func doc comment has been updated
cmd/nvidia-cdi-hook/main.go
Outdated
return nil | ||
} | ||
// Set log-level for all subcommands | ||
c.Before = func(ctx context.Context, cmd *cli.Command) (context.Context, error) { |
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.
Givent that we're only ADDing to the hook in the new function, I would not expect this to be part of the diff.
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 diff has been updated
Let's update the description to show the output for the various test cases. |
57efba1
to
d7c7174
Compare
// ConfigureCDIHookCommand takes a base command and configures it to handle | ||
// unsupported CDI hooks gracefully. |
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 is still not accurate.
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.
How about now?
d7c7174
to
5d24958
Compare
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.
There are still unaddressed comments.
5d24958
to
0a64ad5
Compare
This change moves the common logic for the nvidia-cdi-hook and nvidia-ctk hook commands to the commands package. Signed-off-by: Evan Lezar <[email protected]>
The initial implementation for skipping unknown hooks did not properly handle hooks with flags. This change ensures that these are properly handled. Signed-off-by: Carlos Eduardo Arango Gutierrez <[email protected]>
Fixes #1225
Previously, when an unrecognized CDI hook was invoked with command-line flags, the nvidia-cdi-hook command would fail with a flag parsing error instead of gracefully handling the unsupported hook. This could cause container launch failures when CDI specifications reference hooks that are not yet supported by the current NVIDIA Container Toolkit version.
This PR enhances the error handling in nvidia-cdi-hook to gracefully handle unrecognized CDI hooks that include command-line flags. The changes ensure that:
Unrecognized hooks with flags are handled gracefully - Instead of failing with flag parsing errors, the command now issues a warning and continues
Backwards compatibility is maintained - Existing behavior for supported hooks remains unchanged
Container launches don't fail - Unsupported hooks result in warnings rather than errors
Changes
Added OnUsageError handler in ConfigureCDIHookCommand to detect flag parsing errors for unrecognized commands
Added strings import for error message parsing to identify flag-related errors
Enhanced error detection to specifically catch "flag provided but not defined" errors for unrecognized hooks
Improved warning consistency by reusing the existing IssueUnsupportedHookWarning logic
Test Cases
The following test cases demonstrate the improved behavior:
Before (failing):
After (graceful handling):
Additional test cases: