Skip to content

Commit e6d1ca6

Browse files
committed
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 <[email protected]>
1 parent d7f8736 commit e6d1ca6

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

pkg/cdi/container-edits.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (e *ContainerEdits) Apply(spec *oci.Spec) error {
8989
if err != nil {
9090
return err
9191
}
92-
dev := d.ToOCI()
92+
dev := dn.ToOCI()
9393
if dev.UID == nil && spec.Process != nil {
9494
if uid := spec.Process.User.UID; uid > 0 {
9595
dev.UID = &uid
@@ -116,29 +116,30 @@ func (e *ContainerEdits) Apply(spec *oci.Spec) error {
116116
if len(e.Mounts) > 0 {
117117
for _, m := range e.Mounts {
118118
specgen.RemoveMount(m.ContainerPath)
119-
specgen.AddMount(m.ToOCI())
119+
specgen.AddMount((&Mount{m}).ToOCI())
120120
}
121121
sortMounts(&specgen)
122122
}
123123

124124
for _, h := range e.Hooks {
125+
ociHook := (&Hook{h}).ToOCI()
125126
switch h.HookName {
126127
case PrestartHook:
127-
specgen.AddPreStartHook(h.ToOCI())
128+
specgen.AddPreStartHook(ociHook)
128129
case PoststartHook:
129-
specgen.AddPostStartHook(h.ToOCI())
130+
specgen.AddPostStartHook(ociHook)
130131
case PoststopHook:
131-
specgen.AddPostStopHook(h.ToOCI())
132+
specgen.AddPostStopHook(ociHook)
132133
// TODO: Maybe runtime-tools/generate should be updated with these...
133134
case CreateRuntimeHook:
134135
ensureOCIHooks(spec)
135-
spec.Hooks.CreateRuntime = append(spec.Hooks.CreateRuntime, h.ToOCI())
136+
spec.Hooks.CreateRuntime = append(spec.Hooks.CreateRuntime, ociHook)
136137
case CreateContainerHook:
137138
ensureOCIHooks(spec)
138-
spec.Hooks.CreateContainer = append(spec.Hooks.CreateContainer, h.ToOCI())
139+
spec.Hooks.CreateContainer = append(spec.Hooks.CreateContainer, ociHook)
139140
case StartContainerHook:
140141
ensureOCIHooks(spec)
141-
spec.Hooks.StartContainer = append(spec.Hooks.StartContainer, h.ToOCI())
142+
spec.Hooks.StartContainer = append(spec.Hooks.StartContainer, ociHook)
142143
default:
143144
return fmt.Errorf("unknown hook name %q", h.HookName)
144145
}
@@ -148,7 +149,7 @@ func (e *ContainerEdits) Apply(spec *oci.Spec) error {
148149
// The specgen is missing functionality to set all parameters so we
149150
// just piggy-back on it to initialize all structs and the copy over.
150151
specgen.SetLinuxIntelRdtClosID(e.IntelRdt.ClosID)
151-
spec.Linux.IntelRdt = e.IntelRdt.ToOCI()
152+
spec.Linux.IntelRdt = (&IntelRdt{e.IntelRdt}).ToOCI()
152153
}
153154

154155
for _, additionalGID := range e.AdditionalGIDs {
@@ -186,7 +187,7 @@ func (e *ContainerEdits) Validate() error {
186187
}
187188
}
188189
if e.IntelRdt != nil {
189-
if err := ValidateIntelRdt(e.IntelRdt); err != nil {
190+
if err := (&IntelRdt{e.IntelRdt}).Validate(); err != nil {
190191
return err
191192
}
192193
}
@@ -321,8 +322,21 @@ func (m *Mount) Validate() error {
321322
return nil
322323
}
323324

325+
// IntelRdt is a CDI IntelRdt wrapper.
326+
// This is used for validation and conversion to OCI specifications.
327+
type IntelRdt struct {
328+
*specs.IntelRdt
329+
}
330+
324331
// ValidateIntelRdt validates the IntelRdt configuration.
332+
//
333+
// Deprecated: ValidateIntelRdt is deprecated use IntelRdt.Validate() instead.
325334
func ValidateIntelRdt(i *specs.IntelRdt) error {
335+
return (&IntelRdt{i}).Validate()
336+
}
337+
338+
// Validate validates the IntelRdt configuration.
339+
func (i *IntelRdt) Validate() error {
326340
// ClosID must be a valid Linux filename
327341
if len(i.ClosID) >= 4096 || i.ClosID == "." || i.ClosID == ".." || strings.ContainsAny(i.ClosID, "/\n") {
328342
return errors.New("invalid ClosID")

specs-go/oci.go renamed to pkg/cdi/oci.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
package specs
1+
/*
2+
Copyright © 2021 The CDI Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cdi
217

318
import (
419
spec "github.com/opencontainers/runtime-spec/specs-go"

specs-go/go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
module tags.cncf.io/container-device-interface/specs-go
22

33
go 1.19
4-
5-
require github.com/opencontainers/runtime-spec v1.1.0

specs-go/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
github.com/opencontainers/runtime-spec v1.1.0 h1:HHUyrt9mwHUjtasSbXSMvs4cyFxh+Bll4AjJ9odEGpg=
2-
github.com/opencontainers/runtime-spec v1.1.0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=

0 commit comments

Comments
 (0)