Skip to content

Commit 237cc1f

Browse files
committed
ref(oci): use AssemblyMode enum
Signed-off-by: Vaughn Dice <[email protected]>
1 parent 1dd115d commit 237cc1f

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

crates/oci/src/client.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ const MAX_LAYER_COUNT: usize = 500;
4848
/// rather than pushing as a separate layer
4949
const DEFAULT_CONTENT_REF_INLINE_MAX_SIZE: usize = 128;
5050

51+
/// Mode of assembly of a Spin application into an OCI image
52+
enum AssemblyMode {
53+
/// Assemble the application as one layer per component and one layer for
54+
/// every static asset included with a given component
55+
Simple,
56+
/// Assemble the application as one layer per component and one compressed
57+
/// archive layer containing all static assets included with a given component
58+
Archive,
59+
}
60+
5161
/// Client for interacting with an OCI registry for Spin applications.
5262
pub struct Client {
5363
/// Global cache for the metadata, Wasm modules, and static assets pulled from OCI registries.
@@ -134,7 +144,7 @@ impl Client {
134144
) -> Result<Option<String>> {
135145
let mut locked_app = locked.clone();
136146
let mut layers = self
137-
.assemble_layers(&mut locked_app, false)
147+
.assemble_layers(&mut locked_app, AssemblyMode::Simple)
138148
.await
139149
.context("could not assemble layers for locked application")?;
140150

@@ -143,7 +153,7 @@ impl Client {
143153
if layers.len() > MAX_LAYER_COUNT - 1 {
144154
locked_app = locked.clone();
145155
layers = self
146-
.assemble_layers(&mut locked_app, true)
156+
.assemble_layers(&mut locked_app, AssemblyMode::Archive)
147157
.await
148158
.context("could not assemble archive layers for locked application")?;
149159
}
@@ -198,16 +208,12 @@ impl Client {
198208
Ok(digest)
199209
}
200210

201-
/// Assemble ImageLayers for a locked application and return the
202-
/// resulting Vec<ImageLayer>.
203-
/// There will always be one layer per component wasm file and
204-
/// their may be one layer for each static asset included with each component
205-
/// or one archive layer per component consolidating all static assets together,
206-
/// to avoid exceeding certain registry layer count limits.
211+
/// Assemble ImageLayers for a locked application using the provided
212+
/// AssemblyMode and return the resulting Vec<ImageLayer>.
207213
async fn assemble_layers(
208214
&mut self,
209215
locked: &mut LockedApp,
210-
archive: bool,
216+
assembly_mode: AssemblyMode,
211217
) -> Result<Vec<ImageLayer>> {
212218
let mut layers = Vec::new();
213219
let mut components = Vec::new();
@@ -236,20 +242,21 @@ impl Client {
236242
.context("file mount loaded from disk should contain a file source")?;
237243
let source = parse_file_url(source.as_str())?;
238244

239-
if archive {
240-
self.push_archive_layer(&source, &mut files, &mut layers)
245+
match assembly_mode {
246+
AssemblyMode::Archive => self
247+
.push_archive_layer(&source, &mut files, &mut layers)
241248
.await
242249
.context(format!(
243250
"cannot push archive layer for source {}",
244251
quoted_path(&source)
245-
))?;
246-
} else {
247-
self.push_file_layers(&source, &mut files, &mut layers)
252+
))?,
253+
AssemblyMode::Simple => self
254+
.push_file_layers(&source, &mut files, &mut layers)
248255
.await
249256
.context(format!(
250257
"cannot push file layers for source {}",
251258
quoted_path(&source)
252-
))?;
259+
))?,
253260
}
254261
}
255262
c.files = files;
@@ -803,7 +810,7 @@ mod test {
803810
"digest": "digest",
804811
}
805812
}]),
806-
expected_layer_count: 2,
813+
expected_layer_count: 0,
807814
expected_error: Some("Invalid URL: \"\""),
808815
},
809816
TestCase {
@@ -892,7 +899,7 @@ mod test {
892899
assert_eq!(
893900
e,
894901
client
895-
.assemble_layers(&mut locked, false)
902+
.assemble_layers(&mut locked, AssemblyMode::Simple)
896903
.await
897904
.unwrap_err()
898905
.to_string(),
@@ -904,7 +911,7 @@ mod test {
904911
assert_eq!(
905912
tc.expected_layer_count,
906913
client
907-
.assemble_layers(&mut locked, false)
914+
.assemble_layers(&mut locked, AssemblyMode::Simple)
908915
.await
909916
.unwrap()
910917
.len(),

0 commit comments

Comments
 (0)