Skip to content

Commit 591c2a0

Browse files
committed
move version APIs to the specs-go
Signed-off-by: Ed Bartosh <[email protected]>
1 parent 44a71fa commit 591c2a0

File tree

7 files changed

+92
-55
lines changed

7 files changed

+92
-55
lines changed

pkg/cdi/spec.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,15 @@ func (s *Spec) edits() *ContainerEdits {
202202

203203
// Validate the Spec.
204204
func (s *Spec) validate() (map[string]*Device, error) {
205-
if err := validateVersion(s.Version); err != nil {
205+
if err := cdi.ValidateVersion(s.Version); err != nil {
206206
return nil, err
207207
}
208208

209-
minVersion, err := MinimumRequiredVersion(s.Spec)
209+
minVersion, err := cdi.MinimumRequiredVersion(s.Spec)
210210
if err != nil {
211211
return nil, fmt.Errorf("could not determine minimum required version: %v", err)
212212
}
213-
if newVersion(minVersion).IsGreaterThan(newVersion(s.Version)) {
213+
if cdi.NewVersion(minVersion).IsGreaterThan(cdi.NewVersion(s.Version)) {
214214
return nil, fmt.Errorf("the spec version must be at least v%v", minVersion)
215215
}
216216

@@ -242,15 +242,6 @@ func (s *Spec) validate() (map[string]*Device, error) {
242242
return devices, nil
243243
}
244244

245-
// validateVersion checks whether the specified spec version is supported.
246-
func validateVersion(version string) error {
247-
if !validSpecVersions.isValidVersion(version) {
248-
return fmt.Errorf("invalid version %q", version)
249-
}
250-
251-
return nil
252-
}
253-
254245
// ParseSpec parses CDI Spec data into a raw CDI Spec.
255246
func ParseSpec(data []byte) (*cdi.Spec, error) {
256247
var raw *cdi.Spec

pkg/cdi/spec_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ func specType(content []byte) string {
525525
}
526526

527527
func TestCurrentVersionIsValid(t *testing.T) {
528-
require.NoError(t, validateVersion(cdi.CurrentVersion))
528+
require.NoError(t, cdi.ValidateVersion(cdi.CurrentVersion))
529529
}
530530

531531
func TestRequiredVersion(t *testing.T) {
@@ -723,7 +723,7 @@ func TestRequiredVersion(t *testing.T) {
723723

724724
for _, tc := range testCases {
725725
t.Run(tc.description, func(t *testing.T) {
726-
v, err := MinimumRequiredVersion(tc.spec)
726+
v, err := cdi.MinimumRequiredVersion(tc.spec)
727727
require.NoError(t, err)
728728

729729
require.Equal(t, tc.expectedVersion, v)

specs-go/config.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package specs
22

33
import "os"
44

5-
// CurrentVersion is the current version of the Spec.
6-
const CurrentVersion = "0.8.0"
7-
85
// Spec is the base configuration for CDI
96
type Spec struct {
107
Version string `json:"cdiVersion"`

specs-go/go.mod

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

33
go 1.19
4+
5+
require golang.org/x/mod v0.19.0

specs-go/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
2+
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=

specs-go/parser.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright © 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 specs
18+
19+
import "strings"
20+
21+
// ParseQualifier splits a device qualifier into vendor and class.
22+
// The syntax for a device qualifier is
23+
//
24+
// "<vendor>/<class>"
25+
//
26+
// If parsing fails, an empty vendor and the class set to the
27+
// verbatim input is returned.
28+
func ParseQualifier(kind string) (string, string) {
29+
parts := strings.SplitN(kind, "/", 2)
30+
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
31+
return "", kind
32+
}
33+
return parts[0], parts[1]
34+
}
35+
36+
// IsLetter reports whether the rune is a letter.
37+
func IsLetter(c rune) bool {
38+
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')
39+
}

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

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,34 @@
1414
limitations under the License.
1515
*/
1616

17-
package cdi
17+
package specs
1818

1919
import (
20+
"fmt"
2021
"strings"
2122

2223
"golang.org/x/mod/semver"
23-
24-
"tags.cncf.io/container-device-interface/pkg/parser"
25-
cdi "tags.cncf.io/container-device-interface/specs-go"
2624
)
2725

2826
const (
29-
// CurrentVersion is the current version of the CDI Spec.
30-
CurrentVersion = cdi.CurrentVersion
27+
// CurrentVersion is the current version of the Spec.
28+
CurrentVersion = "0.8.0"
3129

3230
// vCurrent is the current version as a semver-comparable type
33-
vCurrent version = "v" + CurrentVersion
31+
vCurrent Version = "v" + CurrentVersion
3432

3533
// These represent the released versions of the CDI specification
36-
v010 version = "v0.1.0"
37-
v020 version = "v0.2.0"
38-
v030 version = "v0.3.0"
39-
v040 version = "v0.4.0"
40-
v050 version = "v0.5.0"
41-
v060 version = "v0.6.0"
42-
v070 version = "v0.7.0"
43-
v080 version = "v0.8.0"
34+
v010 Version = "v0.1.0"
35+
v020 Version = "v0.2.0"
36+
v030 Version = "v0.3.0"
37+
v040 Version = "v0.4.0"
38+
v050 Version = "v0.5.0"
39+
v060 Version = "v0.6.0"
40+
v070 Version = "v0.7.0"
41+
v080 Version = "v0.8.0"
4442

4543
// vEarliest is the earliest supported version of the CDI specification
46-
vEarliest version = v030
44+
vEarliest Version = v030
4745
)
4846

4947
// validSpecVersions stores a map of spec versions to functions to check the required versions.
@@ -60,50 +58,58 @@ var validSpecVersions = requiredVersionMap{
6058
v080: requiresV080,
6159
}
6260

61+
// ValidateVersion checks whether the specified spec version is supported.
62+
func ValidateVersion(version string) error {
63+
if !validSpecVersions.isValidVersion(version) {
64+
return fmt.Errorf("invalid version %q", version)
65+
}
66+
return nil
67+
}
68+
6369
// MinimumRequiredVersion determines the minimum spec version for the input spec.
64-
func MinimumRequiredVersion(spec *cdi.Spec) (string, error) {
70+
func MinimumRequiredVersion(spec *Spec) (string, error) {
6571
minVersion := validSpecVersions.requiredVersion(spec)
6672
return minVersion.String(), nil
6773
}
6874

69-
// version represents a semantic version string
70-
type version string
75+
// Version represents a semantic version string
76+
type Version string
7177

72-
// newVersion creates a version that can be used for semantic version comparisons.
73-
func newVersion(v string) version {
74-
return version("v" + strings.TrimPrefix(v, "v"))
78+
// NewVersion creates a version that can be used for semantic version comparisons.
79+
func NewVersion(v string) Version {
80+
return Version("v" + strings.TrimPrefix(v, "v"))
7581
}
7682

7783
// String returns the string representation of the version.
7884
// This trims a leading v if present.
79-
func (v version) String() string {
85+
func (v Version) String() string {
8086
return strings.TrimPrefix(string(v), "v")
8187
}
8288

8389
// IsGreaterThan checks with a version is greater than the specified version.
84-
func (v version) IsGreaterThan(o version) bool {
90+
func (v Version) IsGreaterThan(o Version) bool {
8591
return semver.Compare(string(v), string(o)) > 0
8692
}
8793

8894
// IsLatest checks whether the version is the latest supported version
89-
func (v version) IsLatest() bool {
95+
func (v Version) IsLatest() bool {
9096
return v == vCurrent
9197
}
9298

93-
type requiredFunc func(*cdi.Spec) bool
99+
type requiredFunc func(*Spec) bool
94100

95-
type requiredVersionMap map[version]requiredFunc
101+
type requiredVersionMap map[Version]requiredFunc
96102

97103
// isValidVersion checks whether the specified version is valid.
98104
// A version is valid if it is contained in the required version map.
99105
func (r requiredVersionMap) isValidVersion(specVersion string) bool {
100-
_, ok := validSpecVersions[newVersion(specVersion)]
106+
_, ok := validSpecVersions[NewVersion(specVersion)]
101107

102108
return ok
103109
}
104110

105111
// requiredVersion returns the minimum version required for the given spec
106-
func (r requiredVersionMap) requiredVersion(spec *cdi.Spec) version {
112+
func (r requiredVersionMap) requiredVersion(spec *Spec) Version {
107113
minVersion := vEarliest
108114

109115
for v, isRequired := range validSpecVersions {
@@ -125,12 +131,12 @@ func (r requiredVersionMap) requiredVersion(spec *cdi.Spec) version {
125131
// requiresV080 returns true if the spec uses v0.8.0 features.
126132
// Since the v0.8.0 spec bump was due to the removed .ToOCI functions on the
127133
// spec types, there are explicit spec changes.
128-
func requiresV080(_ *cdi.Spec) bool {
134+
func requiresV080(_ *Spec) bool {
129135
return false
130136
}
131137

132138
// requiresV070 returns true if the spec uses v0.7.0 features
133-
func requiresV070(spec *cdi.Spec) bool {
139+
func requiresV070(spec *Spec) bool {
134140
if spec.ContainerEdits.IntelRdt != nil {
135141
return true
136142
}
@@ -153,7 +159,7 @@ func requiresV070(spec *cdi.Spec) bool {
153159
}
154160

155161
// requiresV060 returns true if the spec uses v0.6.0 features
156-
func requiresV060(spec *cdi.Spec) bool {
162+
func requiresV060(spec *Spec) bool {
157163
// The v0.6.0 spec allows annotations to be specified at a spec level
158164
for range spec.Annotations {
159165
return true
@@ -167,7 +173,7 @@ func requiresV060(spec *cdi.Spec) bool {
167173
}
168174

169175
// The v0.6.0 spec allows dots "." in Kind name label (class)
170-
vendor, class := parser.ParseQualifier(spec.Kind)
176+
vendor, class := ParseQualifier(spec.Kind)
171177
if vendor != "" {
172178
if strings.ContainsRune(class, '.') {
173179
return true
@@ -178,12 +184,12 @@ func requiresV060(spec *cdi.Spec) bool {
178184
}
179185

180186
// requiresV050 returns true if the spec uses v0.5.0 features
181-
func requiresV050(spec *cdi.Spec) bool {
182-
var edits []*cdi.ContainerEdits
187+
func requiresV050(spec *Spec) bool {
188+
var edits []*ContainerEdits
183189

184190
for _, d := range spec.Devices {
185191
// The v0.5.0 spec allowed device names to start with a digit instead of requiring a letter
186-
if len(d.Name) > 0 && !parser.IsLetter(rune(d.Name[0])) {
192+
if len(d.Name) > 0 && !IsLetter(rune(d.Name[0])) {
187193
return true
188194
}
189195
edits = append(edits, &d.ContainerEdits)
@@ -202,8 +208,8 @@ func requiresV050(spec *cdi.Spec) bool {
202208
}
203209

204210
// requiresV040 returns true if the spec uses v0.4.0 features
205-
func requiresV040(spec *cdi.Spec) bool {
206-
var edits []*cdi.ContainerEdits
211+
func requiresV040(spec *Spec) bool {
212+
var edits []*ContainerEdits
207213

208214
for _, d := range spec.Devices {
209215
edits = append(edits, &d.ContainerEdits)

0 commit comments

Comments
 (0)