Skip to content

Commit

Permalink
move version APIs to the specs-go
Browse files Browse the repository at this point in the history
Signed-off-by: Ed Bartosh <[email protected]>
  • Loading branch information
bart0sh committed Jul 29, 2024
1 parent 44a71fa commit 591c2a0
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 55 deletions.
15 changes: 3 additions & 12 deletions pkg/cdi/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,15 @@ func (s *Spec) edits() *ContainerEdits {

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

minVersion, err := MinimumRequiredVersion(s.Spec)
minVersion, err := cdi.MinimumRequiredVersion(s.Spec)
if err != nil {
return nil, fmt.Errorf("could not determine minimum required version: %v", err)
}
if newVersion(minVersion).IsGreaterThan(newVersion(s.Version)) {
if cdi.NewVersion(minVersion).IsGreaterThan(cdi.NewVersion(s.Version)) {
return nil, fmt.Errorf("the spec version must be at least v%v", minVersion)
}

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

// validateVersion checks whether the specified spec version is supported.
func validateVersion(version string) error {
if !validSpecVersions.isValidVersion(version) {
return fmt.Errorf("invalid version %q", version)
}

return nil
}

// ParseSpec parses CDI Spec data into a raw CDI Spec.
func ParseSpec(data []byte) (*cdi.Spec, error) {
var raw *cdi.Spec
Expand Down
4 changes: 2 additions & 2 deletions pkg/cdi/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ func specType(content []byte) string {
}

func TestCurrentVersionIsValid(t *testing.T) {
require.NoError(t, validateVersion(cdi.CurrentVersion))
require.NoError(t, cdi.ValidateVersion(cdi.CurrentVersion))
}

func TestRequiredVersion(t *testing.T) {
Expand Down Expand Up @@ -723,7 +723,7 @@ func TestRequiredVersion(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
v, err := MinimumRequiredVersion(tc.spec)
v, err := cdi.MinimumRequiredVersion(tc.spec)
require.NoError(t, err)

require.Equal(t, tc.expectedVersion, v)
Expand Down
3 changes: 0 additions & 3 deletions specs-go/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package specs

import "os"

// CurrentVersion is the current version of the Spec.
const CurrentVersion = "0.8.0"

// Spec is the base configuration for CDI
type Spec struct {
Version string `json:"cdiVersion"`
Expand Down
2 changes: 2 additions & 0 deletions specs-go/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module tags.cncf.io/container-device-interface/specs-go

go 1.19

require golang.org/x/mod v0.19.0
2 changes: 2 additions & 0 deletions specs-go/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
39 changes: 39 additions & 0 deletions specs-go/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright © The CDI Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package specs

import "strings"

// ParseQualifier splits a device qualifier into vendor and class.
// The syntax for a device qualifier is
//
// "<vendor>/<class>"
//
// If parsing fails, an empty vendor and the class set to the
// verbatim input is returned.
func ParseQualifier(kind string) (string, string) {
parts := strings.SplitN(kind, "/", 2)
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return "", kind
}
return parts[0], parts[1]
}

// IsLetter reports whether the rune is a letter.
func IsLetter(c rune) bool {
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')
}
82 changes: 44 additions & 38 deletions pkg/cdi/version.go → specs-go/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,34 @@
limitations under the License.
*/

package cdi
package specs

import (
"fmt"
"strings"

"golang.org/x/mod/semver"

"tags.cncf.io/container-device-interface/pkg/parser"
cdi "tags.cncf.io/container-device-interface/specs-go"
)

const (
// CurrentVersion is the current version of the CDI Spec.
CurrentVersion = cdi.CurrentVersion
// CurrentVersion is the current version of the Spec.
CurrentVersion = "0.8.0"

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

// These represent the released versions of the CDI specification
v010 version = "v0.1.0"
v020 version = "v0.2.0"
v030 version = "v0.3.0"
v040 version = "v0.4.0"
v050 version = "v0.5.0"
v060 version = "v0.6.0"
v070 version = "v0.7.0"
v080 version = "v0.8.0"
v010 Version = "v0.1.0"
v020 Version = "v0.2.0"
v030 Version = "v0.3.0"
v040 Version = "v0.4.0"
v050 Version = "v0.5.0"
v060 Version = "v0.6.0"
v070 Version = "v0.7.0"
v080 Version = "v0.8.0"

// vEarliest is the earliest supported version of the CDI specification
vEarliest version = v030
vEarliest Version = v030
)

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

// ValidateVersion checks whether the specified spec version is supported.
func ValidateVersion(version string) error {
if !validSpecVersions.isValidVersion(version) {
return fmt.Errorf("invalid version %q", version)
}
return nil
}

// MinimumRequiredVersion determines the minimum spec version for the input spec.
func MinimumRequiredVersion(spec *cdi.Spec) (string, error) {
func MinimumRequiredVersion(spec *Spec) (string, error) {
minVersion := validSpecVersions.requiredVersion(spec)
return minVersion.String(), nil
}

// version represents a semantic version string
type version string
// Version represents a semantic version string
type Version string

// newVersion creates a version that can be used for semantic version comparisons.
func newVersion(v string) version {
return version("v" + strings.TrimPrefix(v, "v"))
// NewVersion creates a version that can be used for semantic version comparisons.
func NewVersion(v string) Version {
return Version("v" + strings.TrimPrefix(v, "v"))
}

// String returns the string representation of the version.
// This trims a leading v if present.
func (v version) String() string {
func (v Version) String() string {
return strings.TrimPrefix(string(v), "v")
}

// IsGreaterThan checks with a version is greater than the specified version.
func (v version) IsGreaterThan(o version) bool {
func (v Version) IsGreaterThan(o Version) bool {
return semver.Compare(string(v), string(o)) > 0
}

// IsLatest checks whether the version is the latest supported version
func (v version) IsLatest() bool {
func (v Version) IsLatest() bool {
return v == vCurrent
}

type requiredFunc func(*cdi.Spec) bool
type requiredFunc func(*Spec) bool

type requiredVersionMap map[version]requiredFunc
type requiredVersionMap map[Version]requiredFunc

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

return ok
}

// requiredVersion returns the minimum version required for the given spec
func (r requiredVersionMap) requiredVersion(spec *cdi.Spec) version {
func (r requiredVersionMap) requiredVersion(spec *Spec) Version {
minVersion := vEarliest

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

// requiresV070 returns true if the spec uses v0.7.0 features
func requiresV070(spec *cdi.Spec) bool {
func requiresV070(spec *Spec) bool {
if spec.ContainerEdits.IntelRdt != nil {
return true
}
Expand All @@ -153,7 +159,7 @@ func requiresV070(spec *cdi.Spec) bool {
}

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

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

// requiresV050 returns true if the spec uses v0.5.0 features
func requiresV050(spec *cdi.Spec) bool {
var edits []*cdi.ContainerEdits
func requiresV050(spec *Spec) bool {
var edits []*ContainerEdits

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

// requiresV040 returns true if the spec uses v0.4.0 features
func requiresV040(spec *cdi.Spec) bool {
var edits []*cdi.ContainerEdits
func requiresV040(spec *Spec) bool {
var edits []*ContainerEdits

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

0 comments on commit 591c2a0

Please sign in to comment.