-
Notifications
You must be signed in to change notification settings - Fork 299
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
Add nvidia-ctk runtime apply command to simulate OCI spec modifications #338
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
772cf77
Fix build and test on darwin
elezar 60b9e41
Move logic to extract devices from annotions to image
elezar ba87830
Use image.CUDA instead of raw spec for CDI modifier
elezar ae3258c
Make NewSpecModifier public
elezar 76fab0a
Add nvidia-ctk runtime apply command
elezar 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 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,126 @@ | ||
/** | ||
# Copyright (c) 2024, 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 list | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/opencontainers/runtime-spec/specs-go" | ||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/NVIDIA/nvidia-container-toolkit/internal/config" | ||
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/image" | ||
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger" | ||
"github.com/NVIDIA/nvidia-container-toolkit/internal/runtime" | ||
) | ||
|
||
type command struct { | ||
logger logger.Interface | ||
} | ||
|
||
// NewCommand constructs an list command with the specified logger | ||
func NewCommand(logger logger.Interface) *cli.Command { | ||
c := command{ | ||
logger: logger, | ||
} | ||
return c.build() | ||
} | ||
|
||
// options defines the options that can be set for the CLI through options files, | ||
type options struct { | ||
mode string | ||
envvars cli.StringSlice | ||
} | ||
|
||
func (m command) build() *cli.Command { | ||
// Create a options struct to hold the parsed environment variables or command line flags | ||
cfg := options{} | ||
|
||
// Create the 'configure' command | ||
configure := cli.Command{ | ||
Name: "list", | ||
Usage: "List the modifications made to the OCI runtime specification", | ||
Before: func(c *cli.Context) error { | ||
return m.validateFlags(c, &cfg) | ||
}, | ||
Action: func(c *cli.Context) error { | ||
return m.list(c, &cfg) | ||
}, | ||
} | ||
|
||
configure.Flags = []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "mode", | ||
Usage: "override the runtime mode a specified in the config file", | ||
Destination: &cfg.mode, | ||
}, | ||
&cli.StringSliceFlag{ | ||
Name: "envvar", | ||
Aliases: []string{"e"}, | ||
Usage: "add an envvar to the container definition", | ||
Destination: &cfg.envvars, | ||
}, | ||
} | ||
|
||
return &configure | ||
} | ||
|
||
func (m command) validateFlags(c *cli.Context, cfg *options) error { | ||
return nil | ||
} | ||
|
||
// list executes the list command. | ||
func (m command) list(c *cli.Context, cfg *options) error { | ||
toolkitConfig, err := config.GetDefault() | ||
if err != nil { | ||
return fmt.Errorf("failed to generate default config: %w", err) | ||
} | ||
|
||
if cfg.mode != "" { | ||
toolkitConfig.NVIDIAContainerRuntimeConfig.Mode = cfg.mode | ||
} | ||
|
||
container, err := image.New( | ||
image.WithEnv(cfg.envvars.Value()), | ||
) | ||
if err != nil { | ||
return fmt.Errorf("failed to construct container image: %w", err) | ||
} | ||
|
||
modifier, err := runtime.NewSpecModifier(m.logger, toolkitConfig, container) | ||
if err != nil { | ||
return fmt.Errorf("failed to contruct OCI runtime specification modifier: %w", err) | ||
} | ||
// TODO: We should handle this more cleanly. | ||
if modifier == nil { | ||
return fmt.Errorf("no modifications required") | ||
} | ||
|
||
spec := specs.Spec{} | ||
if err := modifier.Modify(&spec); err != nil { | ||
return fmt.Errorf("faile to apply modification to empty spec: %w", err) | ||
} | ||
|
||
specJSON, err := json.MarshalIndent(spec, "", " ") | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal OCI spec to JSON: %w", err) | ||
} | ||
fmt.Printf("%v", string(specJSON)) | ||
|
||
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
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
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
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
Oops, something went wrong.
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.
TODO: Update to
apply