Move modifying OCI runtime wrapper to oci package

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2023-02-07 21:24:44 +01:00
parent babb73295f
commit f71c419cfb
3 changed files with 15 additions and 18 deletions

View File

@ -23,7 +23,6 @@ import (
"github.com/NVIDIA/nvidia-container-toolkit/internal/info" "github.com/NVIDIA/nvidia-container-toolkit/internal/info"
"github.com/NVIDIA/nvidia-container-toolkit/internal/modifier" "github.com/NVIDIA/nvidia-container-toolkit/internal/modifier"
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci" "github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
"github.com/NVIDIA/nvidia-container-toolkit/internal/runtime"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -50,7 +49,7 @@ func newNVIDIAContainerRuntime(logger *logrus.Logger, cfg *config.Config, argv [
} }
// Create the wrapping runtime with the specified modifier // Create the wrapping runtime with the specified modifier
r := runtime.NewModifyingRuntimeWrapper( r := oci.NewModifyingRuntimeWrapper(
logger, logger,
lowLevelRuntime, lowLevelRuntime,
ociSpec, ociSpec,

View File

@ -14,27 +14,26 @@
# limitations under the License. # limitations under the License.
*/ */
package runtime package oci
import ( import (
"fmt" "fmt"
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
type modifyingRuntimeWrapper struct { type modifyingRuntimeWrapper struct {
logger *log.Logger logger *log.Logger
runtime oci.Runtime runtime Runtime
ociSpec oci.Spec ociSpec Spec
modifier oci.SpecModifier modifier SpecModifier
} }
var _ oci.Runtime = (*modifyingRuntimeWrapper)(nil) var _ Runtime = (*modifyingRuntimeWrapper)(nil)
// NewModifyingRuntimeWrapper creates a runtime wrapper that applies the specified modifier to the OCI specification // NewModifyingRuntimeWrapper creates a runtime wrapper that applies the specified modifier to the OCI specification
// before invoking the wrapped runtime. If the modifier is nil, the input runtime is returned. // before invoking the wrapped runtime. If the modifier is nil, the input runtime is returned.
func NewModifyingRuntimeWrapper(logger *log.Logger, runtime oci.Runtime, spec oci.Spec, modifier oci.SpecModifier) oci.Runtime { func NewModifyingRuntimeWrapper(logger *log.Logger, runtime Runtime, spec Spec, modifier SpecModifier) Runtime {
if modifier == nil { if modifier == nil {
logger.Infof("Using low-level runtime with no modification") logger.Infof("Using low-level runtime with no modification")
return runtime return runtime
@ -52,7 +51,7 @@ func NewModifyingRuntimeWrapper(logger *log.Logger, runtime oci.Runtime, spec oc
// Exec checks whether a modification of the OCI specification is required and modifies it accordingly before exec-ing // Exec checks whether a modification of the OCI specification is required and modifies it accordingly before exec-ing
// into the wrapped runtime. // into the wrapped runtime.
func (r *modifyingRuntimeWrapper) Exec(args []string) error { func (r *modifyingRuntimeWrapper) Exec(args []string) error {
if oci.HasCreateSubcommand(args) { if HasCreateSubcommand(args) {
err := r.modify() err := r.modify()
if err != nil { if err != nil {
return fmt.Errorf("could not apply required modification to OCI specification: %v", err) return fmt.Errorf("could not apply required modification to OCI specification: %v", err)

View File

@ -14,13 +14,12 @@
# limitations under the License. # limitations under the License.
*/ */
package runtime package oci
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
"github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/runtime-spec/specs-go"
testlog "github.com/sirupsen/logrus/hooks/test" testlog "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -38,7 +37,7 @@ func TestExec(t *testing.T) {
args []string args []string
modifyError error modifyError error
writeError error writeError error
modifer oci.SpecModifier modifer SpecModifier
}{ }{
{ {
description: "no args forwards", description: "no args forwards",
@ -92,9 +91,9 @@ func TestExec(t *testing.T) {
hook.Reset() hook.Reset()
t.Run(tc.description, func(t *testing.T) { t.Run(tc.description, func(t *testing.T) {
runtimeMock := &oci.RuntimeMock{} runtimeMock := &RuntimeMock{}
specMock := &oci.SpecMock{ specMock := &SpecMock{
ModifyFunc: func(specModifier oci.SpecModifier) error { ModifyFunc: func(specModifier SpecModifier) error {
return tc.modifyError return tc.modifyError
}, },
FlushFunc: func() error { FlushFunc: func() error {
@ -144,8 +143,8 @@ func TestExec(t *testing.T) {
func TestNilModiferReturnsRuntime(t *testing.T) { func TestNilModiferReturnsRuntime(t *testing.T) {
logger, _ := testlog.NewNullLogger() logger, _ := testlog.NewNullLogger()
runtimeMock := &oci.RuntimeMock{} runtimeMock := &RuntimeMock{}
specMock := &oci.SpecMock{} specMock := &SpecMock{}
shim := NewModifyingRuntimeWrapper( shim := NewModifyingRuntimeWrapper(
logger, logger,