@@ -22,6 +22,17 @@ const (
2222 ldPreloadEnvVar = "LD_PRELOAD"
2323)
2424
25+ type reconfigurer struct {
26+ * reconfigureMIGOptions
27+ commandRunner
28+ }
29+
30+ // A commandWithOutput runs a command and ensures that STDERR and STDOUT are
31+ // set.
32+ type commandWithOutput struct {}
33+
34+ var _ commandRunner = (* commandWithOutput )(nil )
35+
2536// New creates a MIG Reconfigurer with the supplied options.
2637func New (opts ... Option ) (Reconfigurer , error ) {
2738 o := & reconfigureMIGOptions {}
@@ -34,15 +45,20 @@ func New(opts ...Option) (Reconfigurer, error) {
3445 return nil , err
3546 }
3647
37- return o , nil
48+ r := & reconfigurer {
49+ reconfigureMIGOptions : o ,
50+ commandRunner : & commandWithOutput {},
51+ }
52+
53+ return r , nil
3854}
3955
4056// Reconfigure configures MIG (Multi-Instance GPU) settings on a Kubernetes
4157// node. It validates the requested configuration, checks the current state,
4258// applies MIG mode changes, manages host GPU client services, and handles
4359// reboots when necessary. The function ensures that MIG configurations are
4460// applied safely with proper service lifecycle management.
45- func (opts * reconfigureMIGOptions ) Reconfigure () error {
61+ func (opts * reconfigurer ) Reconfigure () error {
4662 log .Info ("Asserting that the requested configuration is present in the configuration file" )
4763 if err := opts .assertValidMIGConfig (); err != nil {
4864 return fmt .Errorf ("error validating the selected MIG configuration: %w" , err )
@@ -124,7 +140,7 @@ func (opts *reconfigureMIGOptions) Reconfigure() error {
124140 return nil
125141}
126142
127- func (opts * reconfigureMIGOptions ) assertValidMIGConfig () error {
143+ func (opts * reconfigurer ) assertValidMIGConfig () error {
128144 args := []string {
129145 "--debug" ,
130146 "assert" ,
@@ -135,7 +151,7 @@ func (opts *reconfigureMIGOptions) assertValidMIGConfig() error {
135151 return opts .runMigParted (args ... )
136152}
137153
138- func (opts * reconfigureMIGOptions ) assertMIGConfig () error {
154+ func (opts * reconfigurer ) assertMIGConfig () error {
139155 args := []string {
140156 "--debug" ,
141157 "assert" ,
@@ -145,7 +161,7 @@ func (opts *reconfigureMIGOptions) assertMIGConfig() error {
145161 return opts .runMigParted (args ... )
146162}
147163
148- func (opts * reconfigureMIGOptions ) assertMIGModeOnly () error {
164+ func (opts * reconfigurer ) assertMIGModeOnly () error {
149165 args := []string {
150166 "--debug" ,
151167 "assert" ,
@@ -156,7 +172,7 @@ func (opts *reconfigureMIGOptions) assertMIGModeOnly() error {
156172 return opts .runMigParted (args ... )
157173}
158174
159- func (opts * reconfigureMIGOptions ) applyMIGModeOnly () error {
175+ func (opts * reconfigurer ) applyMIGModeOnly () error {
160176 args := []string {
161177 "--debug" ,
162178 "apply" ,
@@ -167,7 +183,7 @@ func (opts *reconfigureMIGOptions) applyMIGModeOnly() error {
167183 return opts .runMigParted (args ... )
168184}
169185
170- func (opts * reconfigureMIGOptions ) applyMIGConfig () error {
186+ func (opts * reconfigurer ) applyMIGConfig () error {
171187 args := []string {
172188 "--debug" ,
173189 "apply" ,
@@ -177,7 +193,7 @@ func (opts *reconfigureMIGOptions) applyMIGConfig() error {
177193 return opts .runMigParted (args ... )
178194}
179195
180- func (opts * reconfigureMIGOptions ) hostPersistConfig () error {
196+ func (opts * reconfigurer ) hostPersistConfig () error {
181197 config := fmt .Sprintf (`[Service]
182198Environment="MIG_PARTED_SELECTED_CONFIG=%s"
183199` , opts .SelectedMIGConfig )
@@ -189,7 +205,7 @@ Environment="MIG_PARTED_SELECTED_CONFIG=%s"
189205 }
190206
191207 cmd := exec .Command ("chroot" , opts .HostRootMount , "systemctl" , "daemon-reload" ) // #nosec G204 -- HostRootMount is validated via dirpath validator.
192- return runCommandWithOutput (cmd )
208+ return opts . Run (cmd )
193209}
194210
195211func (opts * reconfigureMIGOptions ) hostStopSystemdServices () error {
@@ -217,7 +233,7 @@ func (opts *reconfigureMIGOptions) hostStartSystemdServices() error {
217233 log .Infof ("Starting %s" , service )
218234 cmd := exec .Command ("chroot" , opts .HostRootMount , "systemctl" , "start" , service ) // #nosec G204 -- HostRootMount validated via dirpath, service validated via systemd_service_name.
219235 if err := cmd .Run (); err != nil {
220- serviceError := fmt .Errorf ("error starting %q: %w" , err )
236+ serviceError := fmt .Errorf ("error starting %q: %w" , service , err )
221237 log .Errorf ("%v; skipping, but continuing..." , serviceError )
222238
223239 errs = errors .Join (errs , serviceError )
@@ -315,15 +331,15 @@ func shouldRestartService(opts *reconfigureMIGOptions, service string) bool {
315331 return true
316332}
317333
318- func runCommandWithOutput (cmd * exec.Cmd ) error {
334+ func ( c * commandWithOutput ) Run (cmd * exec.Cmd ) error {
319335 cmd .Stdout = os .Stdout
320336 cmd .Stderr = os .Stderr
321337 return cmd .Run ()
322338}
323339
324- func (opts * reconfigureMIGOptions ) runMigParted (args ... string ) error {
340+ func (opts * reconfigurer ) runMigParted (args ... string ) error {
325341 cmd := opts .migPartedCmd (args ... )
326- return runCommandWithOutput (cmd )
342+ return opts . commandRunner . Run (cmd )
327343}
328344
329345func (opts * reconfigureMIGOptions ) migPartedCmd (args ... string ) * exec.Cmd {
0 commit comments