14
14
limitations under the License.
15
15
*/
16
16
17
- package cdi
17
+ package specs
18
18
19
19
import (
20
+ "fmt"
20
21
"strings"
21
22
22
23
"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"
26
24
)
27
25
28
26
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"
31
29
32
30
// vCurrent is the current version as a semver-comparable type
33
31
vCurrent version = "v" + CurrentVersion
@@ -60,17 +58,25 @@ var validSpecVersions = requiredVersionMap{
60
58
v080 : requiresV080 ,
61
59
}
62
60
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
+
63
69
// 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 ) {
65
71
minVersion := validSpecVersions .requiredVersion (spec )
66
72
return minVersion .String (), nil
67
73
}
68
74
69
75
// version represents a semantic version string
70
76
type version string
71
77
72
- // newVersion creates a version that can be used for semantic version comparisons.
73
- func newVersion (v string ) version {
78
+ // NewVersion creates a version that can be used for semantic version comparisons.
79
+ func NewVersion (v string ) version {
74
80
return version ("v" + strings .TrimPrefix (v , "v" ))
75
81
}
76
82
@@ -90,20 +96,20 @@ func (v version) IsLatest() bool {
90
96
return v == vCurrent
91
97
}
92
98
93
- type requiredFunc func (* cdi. Spec ) bool
99
+ type requiredFunc func (* Spec ) bool
94
100
95
101
type requiredVersionMap map [version ]requiredFunc
96
102
97
103
// isValidVersion checks whether the specified version is valid.
98
104
// A version is valid if it is contained in the required version map.
99
105
func (r requiredVersionMap ) isValidVersion (specVersion string ) bool {
100
- _ , ok := validSpecVersions [newVersion (specVersion )]
106
+ _ , ok := validSpecVersions [NewVersion (specVersion )]
101
107
102
108
return ok
103
109
}
104
110
105
111
// 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 {
107
113
minVersion := vEarliest
108
114
109
115
for v , isRequired := range validSpecVersions {
@@ -125,12 +131,12 @@ func (r requiredVersionMap) requiredVersion(spec *cdi.Spec) version {
125
131
// requiresV080 returns true if the spec uses v0.8.0 features.
126
132
// Since the v0.8.0 spec bump was due to the removed .ToOCI functions on the
127
133
// spec types, there are explicit spec changes.
128
- func requiresV080 (_ * cdi. Spec ) bool {
134
+ func requiresV080 (_ * Spec ) bool {
129
135
return false
130
136
}
131
137
132
138
// requiresV070 returns true if the spec uses v0.7.0 features
133
- func requiresV070 (spec * cdi. Spec ) bool {
139
+ func requiresV070 (spec * Spec ) bool {
134
140
if spec .ContainerEdits .IntelRdt != nil {
135
141
return true
136
142
}
@@ -153,7 +159,7 @@ func requiresV070(spec *cdi.Spec) bool {
153
159
}
154
160
155
161
// requiresV060 returns true if the spec uses v0.6.0 features
156
- func requiresV060 (spec * cdi. Spec ) bool {
162
+ func requiresV060 (spec * Spec ) bool {
157
163
// The v0.6.0 spec allows annotations to be specified at a spec level
158
164
for range spec .Annotations {
159
165
return true
@@ -167,7 +173,7 @@ func requiresV060(spec *cdi.Spec) bool {
167
173
}
168
174
169
175
// 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 )
171
177
if vendor != "" {
172
178
if strings .ContainsRune (class , '.' ) {
173
179
return true
@@ -178,12 +184,12 @@ func requiresV060(spec *cdi.Spec) bool {
178
184
}
179
185
180
186
// 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
183
189
184
190
for _ , d := range spec .Devices {
185
191
// 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 ])) {
187
193
return true
188
194
}
189
195
edits = append (edits , & d .ContainerEdits )
@@ -202,8 +208,8 @@ func requiresV050(spec *cdi.Spec) bool {
202
208
}
203
209
204
210
// 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
207
213
208
214
for _ , d := range spec .Devices {
209
215
edits = append (edits , & d .ContainerEdits )
0 commit comments