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
- vCurrent version = "v" + CurrentVersion
31
+ vCurrent Version = "v" + CurrentVersion
34
32
35
33
// 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"
44
42
45
43
// vEarliest is the earliest supported version of the CDI specification
46
- vEarliest version = v030
44
+ vEarliest Version = v030
47
45
)
48
46
49
47
// validSpecVersions stores a map of spec versions to functions to check the required versions.
@@ -60,50 +58,58 @@ 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
- // version represents a semantic version string
70
- type version string
75
+ // Version represents a semantic version string
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 {
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" ))
75
81
}
76
82
77
83
// String returns the string representation of the version.
78
84
// This trims a leading v if present.
79
- func (v version ) String () string {
85
+ func (v Version ) String () string {
80
86
return strings .TrimPrefix (string (v ), "v" )
81
87
}
82
88
83
89
// 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 {
85
91
return semver .Compare (string (v ), string (o )) > 0
86
92
}
87
93
88
94
// IsLatest checks whether the version is the latest supported version
89
- func (v version ) IsLatest () bool {
95
+ 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
- type requiredVersionMap map [version ]requiredFunc
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