|
| 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 cdi |
| 18 | + |
| 19 | +import ( |
| 20 | + "strings" |
| 21 | + |
| 22 | + "golang.org/x/mod/semver" |
| 23 | + |
| 24 | + cdi "github.com/container-orchestrated-devices/container-device-interface/specs-go" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + // CurrentVersion is the current version of the CDI Spec. |
| 29 | + CurrentVersion = cdi.CurrentVersion |
| 30 | + |
| 31 | + // vCurrent is the current version as a semver-comparable type |
| 32 | + vCurrent version = "v" + CurrentVersion |
| 33 | + |
| 34 | + // These represent the released versions of the CDI specification |
| 35 | + v010 version = "v0.1.0" |
| 36 | + v020 version = "v0.2.0" |
| 37 | + v030 version = "v0.3.0" |
| 38 | + v040 version = "v0.4.0" |
| 39 | + v050 version = "v0.5.0" |
| 40 | + |
| 41 | + // vEarliest is the earliest supported version of the CDI specification |
| 42 | + vEarliest version = v030 |
| 43 | +) |
| 44 | + |
| 45 | +// validSpecVersions stores a map of spec versions to functions to check the required versions. |
| 46 | +// Adding new fields / spec versions requires that a `requiredFunc` be implemented and |
| 47 | +// this map be updated. |
| 48 | +var validSpecVersions = requiredVersionMap{ |
| 49 | + v010: nil, |
| 50 | + v020: nil, |
| 51 | + v030: nil, |
| 52 | + v040: requiresV040, |
| 53 | + v050: requiresV050, |
| 54 | +} |
| 55 | + |
| 56 | +// MinimumRequiredVersion determines the minumum spec version for the input spec. |
| 57 | +func MinimumRequiredVersion(spec *cdi.Spec) (string, error) { |
| 58 | + minVersion := validSpecVersions.requiredVersion(spec) |
| 59 | + return minVersion.String(), nil |
| 60 | +} |
| 61 | + |
| 62 | +// version represents a semantic version string |
| 63 | +type version string |
| 64 | + |
| 65 | +// newVersion creates a version that can be used for semantic version comparisons. |
| 66 | +func newVersion(v string) version { |
| 67 | + return version("v" + strings.TrimPrefix(v, "v")) |
| 68 | +} |
| 69 | + |
| 70 | +// String returns the string representation of the version. |
| 71 | +// This trims a leading v if present. |
| 72 | +func (v version) String() string { |
| 73 | + return strings.TrimPrefix(string(v), "v") |
| 74 | +} |
| 75 | + |
| 76 | +// IsGreaterThan checks with a version is greater than the specified version. |
| 77 | +func (v version) IsGreaterThan(o version) bool { |
| 78 | + return semver.Compare(string(v), string(o)) > 0 |
| 79 | +} |
| 80 | + |
| 81 | +// IsLatest checks whether the version is the latest supported version |
| 82 | +func (v version) IsLatest() bool { |
| 83 | + return v == vCurrent |
| 84 | +} |
| 85 | + |
| 86 | +type requiredFunc func(*cdi.Spec) bool |
| 87 | + |
| 88 | +type requiredVersionMap map[version]requiredFunc |
| 89 | + |
| 90 | +// isValidVersion checks whether the specified version is valid. |
| 91 | +// A version is valid if it is contained in the required version map. |
| 92 | +func (r requiredVersionMap) isValidVersion(specVersion string) bool { |
| 93 | + _, ok := validSpecVersions[newVersion(specVersion)] |
| 94 | + |
| 95 | + return ok |
| 96 | +} |
| 97 | + |
| 98 | +// requiredVersion returns the minimum version required for the given spec |
| 99 | +func (r requiredVersionMap) requiredVersion(spec *cdi.Spec) version { |
| 100 | + minVersion := vEarliest |
| 101 | + |
| 102 | + for v, isRequired := range validSpecVersions { |
| 103 | + if isRequired == nil { |
| 104 | + continue |
| 105 | + } |
| 106 | + if isRequired(spec) && v.IsGreaterThan(minVersion) { |
| 107 | + minVersion = v |
| 108 | + } |
| 109 | + // If we have already detected the latest version then no later version could be detected |
| 110 | + if minVersion.IsLatest() { |
| 111 | + break |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return minVersion |
| 116 | +} |
| 117 | + |
| 118 | +// requiresV050 returns true if the spec uses v0.5.0 features |
| 119 | +func requiresV050(spec *cdi.Spec) bool { |
| 120 | + var edits []*cdi.ContainerEdits |
| 121 | + |
| 122 | + for _, d := range spec.Devices { |
| 123 | + // The v0.5.0 spec allowed device names to start with a digit instead of requiring a letter |
| 124 | + if len(d.Name) > 0 && !isLetter(rune(d.Name[0])) { |
| 125 | + return true |
| 126 | + } |
| 127 | + edits = append(edits, &d.ContainerEdits) |
| 128 | + } |
| 129 | + |
| 130 | + edits = append(edits, &spec.ContainerEdits) |
| 131 | + for _, e := range edits { |
| 132 | + for _, dn := range e.DeviceNodes { |
| 133 | + // The HostPath field was added in v0.5.0 |
| 134 | + if dn.HostPath != "" { |
| 135 | + return true |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + return false |
| 140 | +} |
| 141 | + |
| 142 | +// requiresV040 returns true if the spec uses v0.4.0 features |
| 143 | +func requiresV040(spec *cdi.Spec) bool { |
| 144 | + var edits []*cdi.ContainerEdits |
| 145 | + |
| 146 | + for _, d := range spec.Devices { |
| 147 | + edits = append(edits, &d.ContainerEdits) |
| 148 | + } |
| 149 | + |
| 150 | + edits = append(edits, &spec.ContainerEdits) |
| 151 | + for _, e := range edits { |
| 152 | + for _, m := range e.Mounts { |
| 153 | + // The Type field was added in v0.4.0 |
| 154 | + if m.Type != "" { |
| 155 | + return true |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + return false |
| 160 | +} |
0 commit comments