From e394a572dbff7757208b34be79e0d5ed816cf31a Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Tue, 28 May 2024 14:32:26 +0200 Subject: [PATCH] Move ToOCI functions out of specs-go package This change moves the ToOCI functions to the cdi package instead of having them implemented in the specs-go package. This means that there is no dependency on the OCI runtime spec for clients that only need a CDI spec definition. Signed-off-by: Evan Lezar --- pkg/cdi/container-edits.go | 34 ++++++++++++++++++++++++---------- {specs-go => pkg/cdi}/oci.go | 18 +++++++++++++++++- specs-go/go.mod | 2 -- specs-go/go.sum | 2 -- 4 files changed, 41 insertions(+), 15 deletions(-) rename {specs-go => pkg/cdi}/oci.go (66%) diff --git a/pkg/cdi/container-edits.go b/pkg/cdi/container-edits.go index e5b50db1..ea30fb01 100644 --- a/pkg/cdi/container-edits.go +++ b/pkg/cdi/container-edits.go @@ -89,7 +89,7 @@ func (e *ContainerEdits) Apply(spec *oci.Spec) error { if err != nil { return err } - dev := d.ToOCI() + dev := dn.ToOCI() if dev.UID == nil && spec.Process != nil { if uid := spec.Process.User.UID; uid > 0 { dev.UID = &uid @@ -116,29 +116,30 @@ func (e *ContainerEdits) Apply(spec *oci.Spec) error { if len(e.Mounts) > 0 { for _, m := range e.Mounts { specgen.RemoveMount(m.ContainerPath) - specgen.AddMount(m.ToOCI()) + specgen.AddMount((&Mount{m}).ToOCI()) } sortMounts(&specgen) } for _, h := range e.Hooks { + ociHook := (&Hook{h}).ToOCI() switch h.HookName { case PrestartHook: - specgen.AddPreStartHook(h.ToOCI()) + specgen.AddPreStartHook(ociHook) case PoststartHook: - specgen.AddPostStartHook(h.ToOCI()) + specgen.AddPostStartHook(ociHook) case PoststopHook: - specgen.AddPostStopHook(h.ToOCI()) + specgen.AddPostStopHook(ociHook) // TODO: Maybe runtime-tools/generate should be updated with these... case CreateRuntimeHook: ensureOCIHooks(spec) - spec.Hooks.CreateRuntime = append(spec.Hooks.CreateRuntime, h.ToOCI()) + spec.Hooks.CreateRuntime = append(spec.Hooks.CreateRuntime, ociHook) case CreateContainerHook: ensureOCIHooks(spec) - spec.Hooks.CreateContainer = append(spec.Hooks.CreateContainer, h.ToOCI()) + spec.Hooks.CreateContainer = append(spec.Hooks.CreateContainer, ociHook) case StartContainerHook: ensureOCIHooks(spec) - spec.Hooks.StartContainer = append(spec.Hooks.StartContainer, h.ToOCI()) + spec.Hooks.StartContainer = append(spec.Hooks.StartContainer, ociHook) default: return fmt.Errorf("unknown hook name %q", h.HookName) } @@ -148,7 +149,7 @@ func (e *ContainerEdits) Apply(spec *oci.Spec) error { // The specgen is missing functionality to set all parameters so we // just piggy-back on it to initialize all structs and the copy over. specgen.SetLinuxIntelRdtClosID(e.IntelRdt.ClosID) - spec.Linux.IntelRdt = e.IntelRdt.ToOCI() + spec.Linux.IntelRdt = (&IntelRdt{e.IntelRdt}).ToOCI() } for _, additionalGID := range e.AdditionalGIDs { @@ -186,7 +187,7 @@ func (e *ContainerEdits) Validate() error { } } if e.IntelRdt != nil { - if err := ValidateIntelRdt(e.IntelRdt); err != nil { + if err := (&IntelRdt{e.IntelRdt}).Validate(); err != nil { return err } } @@ -321,8 +322,21 @@ func (m *Mount) Validate() error { return nil } +// IntelRdt is a CDI IntelRdt wrapper. +// This is used for validation and conversion to OCI specifications. +type IntelRdt struct { + *specs.IntelRdt +} + // ValidateIntelRdt validates the IntelRdt configuration. +// +// Deprecated: ValidateIntelRdt is deprecated use IntelRdt.Validate() instead. func ValidateIntelRdt(i *specs.IntelRdt) error { + return (&IntelRdt{i}).Validate() +} + +// Validate validates the IntelRdt configuration. +func (i *IntelRdt) Validate() error { // ClosID must be a valid Linux filename if len(i.ClosID) >= 4096 || i.ClosID == "." || i.ClosID == ".." || strings.ContainsAny(i.ClosID, "/\n") { return errors.New("invalid ClosID") diff --git a/specs-go/oci.go b/pkg/cdi/oci.go similarity index 66% rename from specs-go/oci.go rename to pkg/cdi/oci.go index 1b4d576b..0e90cee9 100644 --- a/specs-go/oci.go +++ b/pkg/cdi/oci.go @@ -1,4 +1,20 @@ -package specs +/* +Copyright © 2021 The CDI Authors + +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 cdi import ( spec "github.com/opencontainers/runtime-spec/specs-go" diff --git a/specs-go/go.mod b/specs-go/go.mod index ad8f36b3..9b19b9ef 100644 --- a/specs-go/go.mod +++ b/specs-go/go.mod @@ -1,5 +1,3 @@ module tags.cncf.io/container-device-interface/specs-go go 1.19 - -require github.com/opencontainers/runtime-spec v1.1.0 diff --git a/specs-go/go.sum b/specs-go/go.sum index 7ccea55c..e69de29b 100644 --- a/specs-go/go.sum +++ b/specs-go/go.sum @@ -1,2 +0,0 @@ -github.com/opencontainers/runtime-spec v1.1.0 h1:HHUyrt9mwHUjtasSbXSMvs4cyFxh+Bll4AjJ9odEGpg= -github.com/opencontainers/runtime-spec v1.1.0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=