Skip to content

Commit 38a74d2

Browse files
committed
Add grammar for platform string
Platform string to be of the form <os>[(<osversion>)]|<arch>|<os>[(<OSVersion>)]/<arch>[/<variant>] OSVersion is optional only and currently used only by Windows OS. Signed-off-by: Kirtana Ashok <kiashok@microsoft.com>
1 parent db76a43 commit 38a74d2

File tree

7 files changed

+142
-70
lines changed

7 files changed

+142
-70
lines changed

defaults.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
package platforms
1818

19-
// DefaultString returns the default string specifier for the platform.
19+
// DefaultString returns the default string specifier for the platform,
20+
// with [PR#6](https://github.com/containerd/platforms/pull/6) the result
21+
// may now also include the OSVersion from the provided platform specification.
2022
func DefaultString() string {
21-
return Format(DefaultSpec())
23+
return FormatAll(DefaultSpec())
2224
}
2325

2426
// DefaultStrict returns strict form of Default.

defaults_unix_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestDefault(t *testing.T) {
3838
}
3939

4040
s := DefaultString()
41-
if s != Format(p) {
41+
if s != FormatAll(p) {
4242
t.Fatalf("default specifier should match formatted default spec: %v != %v", s, p)
4343
}
4444
}

defaults_windows_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestDefault(t *testing.T) {
4242
}
4343

4444
s := DefaultString()
45-
if s != Format(p) {
45+
if s != FormatAll(p) {
4646
t.Fatalf("default specifier should match formatted default spec: %v != %v", s, p)
4747
}
4848
}

platforms.go

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,12 @@ import (
121121
)
122122

123123
var (
124-
specifierRe = regexp.MustCompile(`^[A-Za-z0-9_-]+$`)
124+
specifierRe = regexp.MustCompile(`^[A-Za-z0-9_-]+$`)
125+
osAndVersionRe = regexp.MustCompile(`^([A-Za-z0-9_-]+)(?:\(([A-Za-z0-9_.-]*)\))?$`)
125126
)
126127

128+
const osAndVersionFormat = "%s(%s)"
129+
127130
// Platform is a type alias for convenience, so there is no need to import image-spec package everywhere.
128131
type Platform = specs.Platform
129132

@@ -156,7 +159,7 @@ func (m *matcher) Match(platform specs.Platform) bool {
156159
}
157160

158161
func (m *matcher) String() string {
159-
return Format(m.Platform)
162+
return FormatAll(m.Platform)
160163
}
161164

162165
// ParseAll parses a list of platform specifiers into a list of platform.
@@ -174,9 +177,12 @@ func ParseAll(specifiers []string) ([]specs.Platform, error) {
174177

175178
// Parse parses the platform specifier syntax into a platform declaration.
176179
//
177-
// Platform specifiers are in the format `<os>|<arch>|<os>/<arch>[/<variant>]`.
180+
// Platform specifiers are in the format `<os>[(<OSVersion>)]|<arch>|<os>[(<OSVersion>)]/<arch>[/<variant>]`.
178181
// The minimum required information for a platform specifier is the operating
179-
// system or architecture. If there is only a single string (no slashes), the
182+
// system or architecture. The OSVersion can be part of the OS like `windows(10.0.17763)`
183+
// When an OSVersion is specified, then specs.Platform.OSVersion is populated with that value,
184+
// and an empty string otherwise.
185+
// If there is only a single string (no slashes), the
180186
// value will be matched against the known set of operating systems, then fall
181187
// back to the known set of architectures. The missing component will be
182188
// inferred based on the local environment.
@@ -186,34 +192,42 @@ func Parse(specifier string) (specs.Platform, error) {
186192
return specs.Platform{}, fmt.Errorf("%q: wildcards not yet supported: %w", specifier, errInvalidArgument)
187193
}
188194

189-
parts := strings.Split(specifier, "/")
195+
// Limit to 4 elements to prevent unbounded split
196+
parts := strings.SplitN(specifier, "/", 4)
190197

191-
for _, part := range parts {
192-
if !specifierRe.MatchString(part) {
193-
return specs.Platform{}, fmt.Errorf("%q is an invalid component of %q: platform specifier component must match %q: %w", part, specifier, specifierRe.String(), errInvalidArgument)
198+
var p specs.Platform
199+
for i, part := range parts {
200+
if i == 0 {
201+
// First element is <os>[(<OSVersion>)]
202+
osVer := osAndVersionRe.FindStringSubmatch(part)
203+
if osVer == nil {
204+
return specs.Platform{}, fmt.Errorf("%q is an invalid OS component of %q: OSAndVersion specifier component must match %q: %w", part, specifier, osAndVersionRe.String(), errInvalidArgument)
205+
}
206+
207+
p.OS = normalizeOS(osVer[1])
208+
p.OSVersion = osVer[2]
209+
} else {
210+
if !specifierRe.MatchString(part) {
211+
return specs.Platform{}, fmt.Errorf("%q is an invalid component of %q: platform specifier component must match %q: %w", part, specifier, specifierRe.String(), errInvalidArgument)
212+
}
194213
}
195214
}
196215

197-
var p specs.Platform
198216
switch len(parts) {
199217
case 1:
200-
// in this case, we will test that the value might be an OS, then look
201-
// it up. If it is not known, we'll treat it as an architecture. Since
218+
// in this case, we will test that the value might be an OS (with or
219+
// without the optional OSVersion specified) and look it up.
220+
// If it is not known, we'll treat it as an architecture. Since
202221
// we have very little information about the platform here, we are
203222
// going to be a little more strict if we don't know about the argument
204223
// value.
205-
p.OS = normalizeOS(parts[0])
206224
if isKnownOS(p.OS) {
207225
// picks a default architecture
208226
p.Architecture = runtime.GOARCH
209227
if p.Architecture == "arm" && cpuVariant() != "v7" {
210228
p.Variant = cpuVariant()
211229
}
212230

213-
if p.OS == "windows" {
214-
p.OSVersion = GetWindowsOsVersion()
215-
}
216-
217231
return p, nil
218232
}
219233

@@ -228,31 +242,21 @@ func Parse(specifier string) (specs.Platform, error) {
228242

229243
return specs.Platform{}, fmt.Errorf("%q: unknown operating system or architecture: %w", specifier, errInvalidArgument)
230244
case 2:
231-
// In this case, we treat as a regular os/arch pair. We don't care
245+
// In this case, we treat as a regular OS[(OSVersion)]/arch pair. We don't care
232246
// about whether or not we know of the platform.
233-
p.OS = normalizeOS(parts[0])
234247
p.Architecture, p.Variant = normalizeArch(parts[1], "")
235248
if p.Architecture == "arm" && p.Variant == "v7" {
236249
p.Variant = ""
237250
}
238251

239-
if p.OS == "windows" {
240-
p.OSVersion = GetWindowsOsVersion()
241-
}
242-
243252
return p, nil
244253
case 3:
245254
// we have a fully specified variant, this is rare
246-
p.OS = normalizeOS(parts[0])
247255
p.Architecture, p.Variant = normalizeArch(parts[1], parts[2])
248256
if p.Architecture == "arm64" && p.Variant == "" {
249257
p.Variant = "v8"
250258
}
251259

252-
if p.OS == "windows" {
253-
p.OSVersion = GetWindowsOsVersion()
254-
}
255-
256260
return p, nil
257261
}
258262

@@ -278,6 +282,20 @@ func Format(platform specs.Platform) string {
278282
return path.Join(platform.OS, platform.Architecture, platform.Variant)
279283
}
280284

285+
// FormatAll returns a string specifier that also includes the OSVersion from the
286+
// provided platform specification.
287+
func FormatAll(platform specs.Platform) string {
288+
if platform.OS == "" {
289+
return "unknown"
290+
}
291+
292+
if platform.OSVersion != "" {
293+
OSAndVersion := fmt.Sprintf(osAndVersionFormat, platform.OS, platform.OSVersion)
294+
return path.Join(OSAndVersion, platform.Architecture, platform.Variant)
295+
}
296+
return path.Join(platform.OS, platform.Architecture, platform.Variant)
297+
}
298+
281299
// Normalize validates and translate the platform to the canonical value.
282300
//
283301
// For example, if "Aarch64" is encountered, we change it to "arm64" or if

platforms_other.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,3 @@ func newDefaultMatcher(platform specs.Platform) Matcher {
2828
Platform: Normalize(platform),
2929
}
3030
}
31-
32-
func GetWindowsOsVersion() string {
33-
return ""
34-
}

0 commit comments

Comments
 (0)