Skip to content

Commit 093efc9

Browse files
committed
feat(config): switch to oci_spec's arch and os implementation
1 parent a1a9597 commit 093efc9

File tree

3 files changed

+17
-122
lines changed

3 files changed

+17
-122
lines changed

src/client.rs

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use futures_util::{future, Stream};
1010
use http::header::RANGE;
1111
use http::{HeaderValue, StatusCode};
1212
use http_auth::{parser::ChallengeParser, ChallengeRef};
13+
use oci_spec::image::{Arch, Os};
1314
use olpc_cjson::CanonicalFormatter;
1415
use reqwest::header::HeaderMap;
1516
use reqwest::{NoProxy, Proxy, RequestBuilder, Response, Url};
@@ -2047,10 +2048,9 @@ pub fn linux_amd64_resolver(manifests: &[ImageIndexEntry]) -> Option<String> {
20472048
manifests
20482049
.iter()
20492050
.find(|entry| {
2050-
entry
2051-
.platform
2052-
.as_ref()
2053-
.is_some_and(|platform| platform.os == "linux" && platform.architecture == "amd64")
2051+
entry.platform.as_ref().is_some_and(|platform| {
2052+
platform.os == Os::Linux && platform.architecture == Arch::Amd64
2053+
})
20542054
})
20552055
.map(|entry| entry.digest.clone())
20562056
}
@@ -2061,55 +2061,20 @@ pub fn windows_amd64_resolver(manifests: &[ImageIndexEntry]) -> Option<String> {
20612061
.iter()
20622062
.find(|entry| {
20632063
entry.platform.as_ref().is_some_and(|platform| {
2064-
platform.os == "windows" && platform.architecture == "amd64"
2064+
platform.os == Os::Windows && platform.architecture == Arch::Amd64
20652065
})
20662066
})
20672067
.map(|entry| entry.digest.clone())
20682068
}
20692069

2070-
const MACOS: &str = "macos";
2071-
const DARWIN: &str = "darwin";
2072-
2073-
fn go_os() -> &'static str {
2074-
// Massage Rust OS var to GO OS:
2075-
// - Rust: https://doc.rust-lang.org/std/env/consts/constant.OS.html
2076-
// - Go: https://golang.org/doc/install/source#environment
2077-
match std::env::consts::OS {
2078-
MACOS => DARWIN,
2079-
other => other,
2080-
}
2081-
}
2082-
2083-
const X86_64: &str = "x86_64";
2084-
const AMD64: &str = "amd64";
2085-
const X86: &str = "x86";
2086-
const AMD: &str = "amd";
2087-
const ARM64: &str = "arm64";
2088-
const AARCH64: &str = "aarch64";
2089-
const POWERPC64: &str = "powerpc64";
2090-
const PPC64LE: &str = "ppc64le";
2091-
2092-
fn go_arch() -> &'static str {
2093-
// Massage Rust Architecture vars to GO equivalent:
2094-
// - Rust: https://doc.rust-lang.org/std/env/consts/constant.ARCH.html
2095-
// - Go: https://golang.org/doc/install/source#environment
2096-
match std::env::consts::ARCH {
2097-
X86_64 => AMD64,
2098-
X86 => AMD,
2099-
AARCH64 => ARM64,
2100-
POWERPC64 => PPC64LE,
2101-
other => other,
2102-
}
2103-
}
2104-
21052070
/// A platform resolver that chooses the first variant matching the running OS/Arch, if present.
21062071
/// Doesn't currently handle platform.variants.
21072072
pub fn current_platform_resolver(manifests: &[ImageIndexEntry]) -> Option<String> {
21082073
manifests
21092074
.iter()
21102075
.find(|entry| {
21112076
entry.platform.as_ref().is_some_and(|platform| {
2112-
platform.os == go_os() && platform.architecture == go_arch()
2077+
platform.os == Os::default() && platform.architecture == Arch::default()
21132078
})
21142079
})
21152080
.map(|entry| entry.digest.clone())

src/config.rs

Lines changed: 7 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -10,85 +10,12 @@ use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer}
1010
/// The CPU architecture which the binaries in this image are
1111
/// built to run on.
1212
/// Validated values are listed in [Go Language document for GOARCH](https://golang.org/doc/install/source#environment)
13-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
14-
#[serde(rename_all = "lowercase")]
15-
pub enum Architecture {
16-
/// Arm
17-
Arm,
18-
/// Arm 64bit
19-
Arm64,
20-
/// Amd64/x86-64
21-
#[default]
22-
Amd64,
23-
/// Intel i386
24-
#[serde(rename = "386")]
25-
I386,
26-
/// Wasm
27-
Wasm,
28-
/// Loong64
29-
Loong64,
30-
/// MIPS
31-
Mips,
32-
/// MIPSle
33-
Mipsle,
34-
/// MIPS64
35-
Mips64,
36-
/// MIPS64le
37-
Mips64le,
38-
/// Power PC64
39-
PPC64,
40-
/// Power PC64le
41-
PPC64le,
42-
/// RiscV 64
43-
Riscv64,
44-
/// IBM s390x
45-
S390x,
46-
/// With this field empty
47-
#[serde(rename = "")]
48-
None,
49-
}
13+
pub type Architecture = oci_spec::image::Arch;
5014

5115
/// The name of the operating system which the image is
5216
/// built to run on.
5317
/// Validated values are listed in [Go Language document for GOARCH](https://golang.org/doc/install/source#environment)
54-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
55-
#[serde(rename_all = "lowercase")]
56-
pub enum Os {
57-
/// IBM AIX
58-
Aix,
59-
/// Android
60-
Android,
61-
/// Apple Darwin
62-
Darwin,
63-
/// FreeBSD Dragonfly
64-
Dragonfly,
65-
/// FreeBSD
66-
Freebsd,
67-
/// Illumos
68-
Illumos,
69-
/// iOS
70-
Ios,
71-
/// Js
72-
Js,
73-
/// Linux
74-
#[default]
75-
Linux,
76-
/// NetBSD
77-
Netbsd,
78-
/// OpenBSD
79-
Openbsd,
80-
/// Plan9 from Bell Labs
81-
Plan9,
82-
/// Solaris
83-
Solaris,
84-
/// WASI Preview 1
85-
Wasip1,
86-
/// Microsoft Windows
87-
Windows,
88-
/// With this field empty
89-
#[serde(rename = "")]
90-
None,
91-
}
18+
pub type Os = oci_spec::image::Os;
9219

9320
/// An OCI Image is an ordered collection of root filesystem changes
9421
/// and the corresponding execution parameters for use within a
@@ -310,11 +237,12 @@ pub struct History {
310237
mod tests {
311238
use assert_json_diff::assert_json_eq;
312239
use chrono::DateTime;
240+
use oci_spec::image::Arch;
313241
use rstest::*;
314242
use serde_json::Value;
315243
use std::collections::{HashMap, HashSet};
316244

317-
use super::{Architecture, Config, ConfigFile, History, Os, Rootfs};
245+
use super::{Config, ConfigFile, History, Os, Rootfs};
318246

319247
const EXAMPLE_CONFIG: &str = r#"
320248
{
@@ -431,7 +359,7 @@ mod tests {
431359
.into(),
432360
),
433361
author: Some("Alyssa P. Hacker <[email protected]>".into()),
434-
architecture: Architecture::Amd64,
362+
architecture: Arch::Amd64,
435363
os: Os::Linux,
436364
config: Some(config),
437365
rootfs,
@@ -462,7 +390,7 @@ mod tests {
462390
};
463391

464392
ConfigFile {
465-
architecture: Architecture::Amd64,
393+
architecture: Arch::Amd64,
466394
os: Os::Linux,
467395
config: None,
468396
rootfs,
@@ -522,7 +450,7 @@ mod tests {
522450
};
523451

524452
ConfigFile {
525-
architecture: Architecture::Arm64,
453+
architecture: Arch::ARM64,
526454
os: Os::Linux,
527455
config,
528456
rootfs,

src/manifest.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! OCI Manifest
22
use std::collections::BTreeMap;
33

4+
use oci_spec::image::{Arch, Os};
5+
46
use crate::{
57
client::{Config, ImageLayer},
68
sha256_digest,
@@ -420,11 +422,11 @@ pub struct Platform {
420422
/// This REQUIRED property specifies the CPU architecture.
421423
/// Image indexes SHOULD use, and implementations SHOULD understand, values
422424
/// listed in the Go Language document for [`GOARCH`](https://golang.org/doc/install/source#environment).
423-
pub architecture: String,
425+
pub architecture: Arch,
424426
/// This REQUIRED property specifies the operating system.
425427
/// Image indexes SHOULD use, and implementations SHOULD understand, values
426428
/// listed in the Go Language document for [`GOOS`](https://golang.org/doc/install/source#environment).
427-
pub os: String,
429+
pub os: Os,
428430
/// This OPTIONAL property specifies the version of the operating system
429431
/// targeted by the referenced blob.
430432
/// Implementations MAY refuse to use manifests where `os.version` is not known

0 commit comments

Comments
 (0)