From 5d7b3a4a9667987a21671ead6a2ac6bd324175d3 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Mon, 2 May 2022 10:27:53 +0200 Subject: [PATCH] Return raw spec from Spec.Load Signed-off-by: Evan Lezar --- .../modifier/experimental.go | 3 ++- internal/oci/spec.go | 2 +- internal/oci/spec_file.go | 8 ++++---- internal/oci/spec_memory.go | 4 ++-- internal/oci/spec_mock.go | 12 +++++++----- internal/runtime/runtime_modifier.go | 2 +- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/cmd/nvidia-container-runtime/modifier/experimental.go b/cmd/nvidia-container-runtime/modifier/experimental.go index f8e927c5..2a048369 100644 --- a/cmd/nvidia-container-runtime/modifier/experimental.go +++ b/cmd/nvidia-container-runtime/modifier/experimental.go @@ -46,7 +46,8 @@ const ( // NewExperimentalModifier creates a modifier that applies the experimental // modications to an OCI spec if required by the runtime wrapper. func NewExperimentalModifier(logger *logrus.Logger, cfg *config.Config, ociSpec oci.Spec) (oci.SpecModifier, error) { - if err := ociSpec.Load(); err != nil { + _, err := ociSpec.Load() + if err != nil { return nil, fmt.Errorf("failed to load OCI spec: %v", err) } diff --git a/internal/oci/spec.go b/internal/oci/spec.go index ba7e7cee..407665c4 100644 --- a/internal/oci/spec.go +++ b/internal/oci/spec.go @@ -33,7 +33,7 @@ type SpecModifier interface { //go:generate moq -stub -out spec_mock.go . Spec // Spec defines the operations to be performed on an OCI specification type Spec interface { - Load() error + Load() (*specs.Spec, error) Flush() error Modify(SpecModifier) error LookupEnv(string) (string, bool) diff --git a/internal/oci/spec_file.go b/internal/oci/spec_file.go index 3465652d..7fba130d 100644 --- a/internal/oci/spec_file.go +++ b/internal/oci/spec_file.go @@ -45,19 +45,19 @@ func NewFileSpec(filepath string) Spec { // Load reads the contents of an OCI spec from file to be referenced internally. // The file is opened "read-only" -func (s *fileSpec) Load() error { +func (s *fileSpec) Load() (*specs.Spec, error) { specFile, err := os.Open(s.path) if err != nil { - return fmt.Errorf("error opening OCI specification file: %v", err) + return nil, fmt.Errorf("error opening OCI specification file: %v", err) } defer specFile.Close() spec, err := LoadFrom(specFile) if err != nil { - return fmt.Errorf("error loading OCI specification from file: %v", err) + return nil, fmt.Errorf("error loading OCI specification from file: %v", err) } s.Spec = spec - return nil + return s.Spec, nil } // LoadFrom reads the contents of the OCI spec from the specified io.Reader. diff --git a/internal/oci/spec_memory.go b/internal/oci/spec_memory.go index ce94447e..478db5a2 100644 --- a/internal/oci/spec_memory.go +++ b/internal/oci/spec_memory.go @@ -37,8 +37,8 @@ func NewMemorySpec(spec *specs.Spec) Spec { } // Load is a no-op for the memorySpec spec -func (s *memorySpec) Load() error { - return nil +func (s *memorySpec) Load() (*specs.Spec, error) { + return s.Spec, nil } // Flush is a no-op for the memorySpec spec diff --git a/internal/oci/spec_mock.go b/internal/oci/spec_mock.go index 1656552c..79676590 100644 --- a/internal/oci/spec_mock.go +++ b/internal/oci/spec_mock.go @@ -4,6 +4,7 @@ package oci import ( + "github.com/opencontainers/runtime-spec/specs-go" "sync" ) @@ -20,7 +21,7 @@ var _ Spec = &SpecMock{} // FlushFunc: func() error { // panic("mock out the Flush method") // }, -// LoadFunc: func() error { +// LoadFunc: func() (*specs.Spec, error) { // panic("mock out the Load method") // }, // LookupEnvFunc: func(s string) (string, bool) { @@ -40,7 +41,7 @@ type SpecMock struct { FlushFunc func() error // LoadFunc mocks the Load method. - LoadFunc func() error + LoadFunc func() (*specs.Spec, error) // LookupEnvFunc mocks the LookupEnv method. LookupEnvFunc func(s string) (string, bool) @@ -103,7 +104,7 @@ func (mock *SpecMock) FlushCalls() []struct { } // Load calls LoadFunc. -func (mock *SpecMock) Load() error { +func (mock *SpecMock) Load() (*specs.Spec, error) { callInfo := struct { }{} mock.lockLoad.Lock() @@ -111,9 +112,10 @@ func (mock *SpecMock) Load() error { mock.lockLoad.Unlock() if mock.LoadFunc == nil { var ( - errOut error + specOut *specs.Spec + errOut error ) - return errOut + return specOut, errOut } return mock.LoadFunc() } diff --git a/internal/runtime/runtime_modifier.go b/internal/runtime/runtime_modifier.go index f385d9c3..d11fda82 100644 --- a/internal/runtime/runtime_modifier.go +++ b/internal/runtime/runtime_modifier.go @@ -68,7 +68,7 @@ func (r *modifyingRuntimeWrapper) Exec(args []string) error { // modify loads, modifies, and flushes the OCI specification using the defined Modifier func (r *modifyingRuntimeWrapper) modify() error { - err := r.ociSpec.Load() + _, err := r.ociSpec.Load() if err != nil { return fmt.Errorf("error loading OCI specification for modification: %v", err) }