Skip to content

Commit d3ece78

Browse files
committed
[no-relnote] Add RuntimeMode type
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 980ca5d commit d3ece78

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

internal/info/auto.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,28 @@ import (
2323
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2424
)
2525

26+
// A RuntimeMode is used to select a specific mode of operation for the NVIDIA Container Runtime.
27+
type RuntimeMode string
28+
29+
const (
30+
// In LegacyRuntimeMode the nvidia-container-runtime injects the
31+
// nvidia-container-runtime-hook as a prestart hook into the incoming
32+
// container config. This hook invokes the nvidia-container-cli to perform
33+
// the required modifications to the container.
34+
LegacyRuntimeMode = RuntimeMode("legacy")
35+
// In CSVRuntimeMode the nvidia-container-runtime processes a set of CSV
36+
// files to determine which container modification are required. The
37+
// contents of these CSV files are used to generate an in-memory CDI
38+
// specification which is used to modify the container config.
39+
CSVRuntimeMode = RuntimeMode("csv")
40+
// In CDIRuntimeMode the nvidia-container-runtime applies the modifications
41+
// to the container config required for the requested CDI devices in the
42+
// same way that other CDI clients would.
43+
CDIRuntimeMode = RuntimeMode("cdi")
44+
)
45+
2646
type RuntimeModeResolver interface {
27-
ResolveRuntimeMode(string) string
47+
ResolveRuntimeMode(string) RuntimeMode
2848
}
2949

3050
type modeResolver struct {
@@ -67,7 +87,7 @@ func NewRuntimeModeResolver(opts ...Option) RuntimeModeResolver {
6787
}
6888

6989
// ResolveAutoMode determines the correct mode for the platform if set to "auto"
70-
func ResolveAutoMode(logger logger.Interface, mode string, image image.CUDA) (rmode string) {
90+
func ResolveAutoMode(logger logger.Interface, mode string, image image.CUDA) (rmode RuntimeMode) {
7191
r := modeResolver{
7292
logger: logger,
7393
image: &image,
@@ -76,17 +96,17 @@ func ResolveAutoMode(logger logger.Interface, mode string, image image.CUDA) (rm
7696
return r.ResolveRuntimeMode(mode)
7797
}
7898

79-
func (m *modeResolver) ResolveRuntimeMode(mode string) (rmode string) {
99+
func (m *modeResolver) ResolveRuntimeMode(mode string) (rmode RuntimeMode) {
80100
if mode != "auto" {
81101
m.logger.Infof("Using requested mode '%s'", mode)
82-
return mode
102+
return RuntimeMode(mode)
83103
}
84104
defer func() {
85105
m.logger.Infof("Auto-detected mode as '%v'", rmode)
86106
}()
87107

88108
if m.image.OnlyFullyQualifiedCDIDevices() {
89-
return "cdi"
109+
return CDIRuntimeMode
90110
}
91111

92112
nvinfo := info.New(
@@ -96,9 +116,9 @@ func (m *modeResolver) ResolveRuntimeMode(mode string) (rmode string) {
96116

97117
switch nvinfo.ResolvePlatform() {
98118
case info.PlatformNVML, info.PlatformWSL:
99-
return "legacy"
119+
return LegacyRuntimeMode
100120
case info.PlatformTegra:
101-
return "csv"
121+
return CSVRuntimeMode
102122
}
103-
return "legacy"
123+
return LegacyRuntimeMode
104124
}

internal/runtime/runtime_factory.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ func newSpecModifier(logger logger.Interface, cfg *config.Config, ociSpec oci.Sp
101101
return modifiers, nil
102102
}
103103

104-
func newModeModifier(logger logger.Interface, mode string, cfg *config.Config, image image.CUDA) (oci.SpecModifier, error) {
104+
func newModeModifier(logger logger.Interface, mode info.RuntimeMode, cfg *config.Config, image image.CUDA) (oci.SpecModifier, error) {
105105
switch mode {
106-
case "legacy":
106+
case info.LegacyRuntimeMode:
107107
return modifier.NewStableRuntimeModifier(logger, cfg.NVIDIAContainerRuntimeHookConfig.Path), nil
108-
case "csv":
108+
case info.CSVRuntimeMode:
109109
return modifier.NewCSVModifier(logger, cfg, image)
110-
case "cdi":
110+
case info.CDIRuntimeMode:
111111
return modifier.NewCDIModifier(logger, cfg, image)
112112
}
113113

@@ -119,7 +119,7 @@ func newModeModifier(logger logger.Interface, mode string, cfg *config.Config, i
119119
// The image is also used to determine the runtime mode to apply.
120120
// If a non-CDI mode is detected we ensure that the image does not process
121121
// annotation devices.
122-
func initRuntimeModeAndImage(logger logger.Interface, cfg *config.Config, ociSpec oci.Spec) (string, *image.CUDA, error) {
122+
func initRuntimeModeAndImage(logger logger.Interface, cfg *config.Config, ociSpec oci.Spec) (info.RuntimeMode, *image.CUDA, error) {
123123
rawSpec, err := ociSpec.Load()
124124
if err != nil {
125125
return "", nil, fmt.Errorf("failed to load OCI spec: %v", err)
@@ -142,7 +142,7 @@ func initRuntimeModeAndImage(logger logger.Interface, cfg *config.Config, ociSpe
142142
)
143143
mode := modeResolver.ResolveRuntimeMode(cfg.NVIDIAContainerRuntimeConfig.Mode)
144144
// We update the mode here so that we can continue passing just the config to other functions.
145-
cfg.NVIDIAContainerRuntimeConfig.Mode = mode
145+
cfg.NVIDIAContainerRuntimeConfig.Mode = string(mode)
146146

147147
if mode == "cdi" || len(cfg.NVIDIAContainerRuntimeConfig.Modes.CDI.AnnotationPrefixes) == 0 {
148148
return mode, &image, nil
@@ -158,12 +158,12 @@ func initRuntimeModeAndImage(logger logger.Interface, cfg *config.Config, ociSpe
158158
}
159159

160160
// supportedModifierTypes returns the modifiers supported for a specific runtime mode.
161-
func supportedModifierTypes(mode string) []string {
161+
func supportedModifierTypes(mode info.RuntimeMode) []string {
162162
switch mode {
163-
case "cdi":
163+
case info.CDIRuntimeMode:
164164
// For CDI mode we make no additional modifications.
165165
return []string{"nvidia-hook-remover", "mode"}
166-
case "csv":
166+
case info.CSVRuntimeMode:
167167
// For CSV mode we support mode and feature-gated modification.
168168
return []string{"nvidia-hook-remover", "feature-gated", "mode"}
169169
default:

0 commit comments

Comments
 (0)