1414 limitations under the License.
1515*/
1616
17- package cdi
17+ package specs
1818
1919import (
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
2826const (
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.
99105func (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