Skip to content

Commit 1ebf298

Browse files
committed
Remove the ToOCI functions from the specs-go package
This change removes the ToOCI functions from the specs-go package. They are implemented as private toOCI functions in the cdi package instead. 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 1ebf298

File tree

5 files changed

+131
-49
lines changed

5 files changed

+131
-49
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")

pkg/cdi/oci.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
17+
package cdi
18+
19+
import (
20+
spec "github.com/opencontainers/runtime-spec/specs-go"
21+
)
22+
23+
// toOCI returns the opencontainers runtime Spec Hook for this Hook.
24+
func (h *Hook) toOCI() spec.Hook {
25+
return spec.Hook{
26+
Path: h.Path,
27+
Args: h.Args,
28+
Env: h.Env,
29+
Timeout: h.Timeout,
30+
}
31+
}
32+
33+
// toOCI returns the opencontainers runtime Spec Mount for this Mount.
34+
func (m *Mount) toOCI() spec.Mount {
35+
return spec.Mount{
36+
Source: m.HostPath,
37+
Destination: m.ContainerPath,
38+
Options: m.Options,
39+
Type: m.Type,
40+
}
41+
}
42+
43+
// toOCI returns the opencontainers runtime Spec LinuxDevice for this DeviceNode.
44+
func (d *DeviceNode) toOCI() spec.LinuxDevice {
45+
return spec.LinuxDevice{
46+
Path: d.Path,
47+
Type: d.Type,
48+
Major: d.Major,
49+
Minor: d.Minor,
50+
FileMode: d.FileMode,
51+
UID: d.UID,
52+
GID: d.GID,
53+
}
54+
}
55+
56+
// toOCI returns the opencontainers runtime Spec LinuxIntelRdt for this IntelRdt config.
57+
func (i *IntelRdt) toOCI() *spec.LinuxIntelRdt {
58+
return &spec.LinuxIntelRdt{
59+
ClosID: i.ClosID,
60+
L3CacheSchema: i.L3CacheSchema,
61+
MemBwSchema: i.MemBwSchema,
62+
EnableCMT: i.EnableCMT,
63+
EnableMBM: i.EnableMBM,
64+
}
65+
}

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=

specs-go/oci.go

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,56 @@
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+
117
package specs
218

3-
import (
4-
spec "github.com/opencontainers/runtime-spec/specs-go"
5-
)
19+
import "errors"
20+
21+
// errDeprecated is returned for the ToOCI functions below.
22+
// This should provide better guidance for user when migrating from the API
23+
// below to the APIs provided in the cdi package.
24+
var errDeprecated = errors.New("deprecated; Use cdi package functions instead")
625

726
// ToOCI returns the opencontainers runtime Spec Hook for this Hook.
8-
func (h *Hook) ToOCI() spec.Hook {
9-
return spec.Hook{
10-
Path: h.Path,
11-
Args: h.Args,
12-
Env: h.Env,
13-
Timeout: h.Timeout,
14-
}
27+
//
28+
// Deprecated: This function has been moved to tags.cncf.io/container-device-interface/pkg/cdi.Hook.toOCI
29+
// and made private.
30+
func (h *Hook) ToOCI() error {
31+
return errDeprecated
1532
}
1633

1734
// ToOCI returns the opencontainers runtime Spec Mount for this Mount.
18-
func (m *Mount) ToOCI() spec.Mount {
19-
return spec.Mount{
20-
Source: m.HostPath,
21-
Destination: m.ContainerPath,
22-
Options: m.Options,
23-
Type: m.Type,
24-
}
35+
//
36+
// Deprecated: This function has been moved to tags.cncf.io/container-device-interface/pkg/cdi.Mount.toOCI
37+
// and made private.
38+
func (m *Mount) ToOCI() error {
39+
return errDeprecated
2540
}
2641

2742
// ToOCI returns the opencontainers runtime Spec LinuxDevice for this DeviceNode.
28-
func (d *DeviceNode) ToOCI() spec.LinuxDevice {
29-
return spec.LinuxDevice{
30-
Path: d.Path,
31-
Type: d.Type,
32-
Major: d.Major,
33-
Minor: d.Minor,
34-
FileMode: d.FileMode,
35-
UID: d.UID,
36-
GID: d.GID,
37-
}
43+
//
44+
// Deprecated: This function has been moved to tags.cncf.io/container-device-interface/pkg/cdi.DeviceNode.toOCI
45+
// and made private.
46+
func (d *DeviceNode) ToOCI() error {
47+
return errDeprecated
3848
}
3949

4050
// ToOCI returns the opencontainers runtime Spec LinuxIntelRdt for this IntelRdt config.
41-
func (i *IntelRdt) ToOCI() *spec.LinuxIntelRdt {
42-
return &spec.LinuxIntelRdt{
43-
ClosID: i.ClosID,
44-
L3CacheSchema: i.L3CacheSchema,
45-
MemBwSchema: i.MemBwSchema,
46-
EnableCMT: i.EnableCMT,
47-
EnableMBM: i.EnableMBM,
48-
}
51+
//
52+
// Deprecated: This function has been moved to tags.cncf.io/container-device-interface/pkg/cdi.IntelRdt.toOCI
53+
// and made private.
54+
func (i *IntelRdt) ToOCI() error {
55+
return errDeprecated
4956
}

0 commit comments

Comments
 (0)