Skip to content

Commit 329d384

Browse files
authored
Merge pull request #1911 from lann/oci-client-improvements
oci: Client improvements
2 parents 9f772ec + ce1827a commit 329d384

File tree

1 file changed

+49
-57
lines changed

1 file changed

+49
-57
lines changed

crates/oci/src/client.rs

Lines changed: 49 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use anyhow::{bail, Context, Result};
66
use docker_credential::DockerCredential;
77
use futures_util::future;
88
use futures_util::stream::{self, StreamExt, TryStreamExt};
9-
use oci_distribution::errors::OciDistributionError;
109
use oci_distribution::token_cache::RegistryTokenType;
1110
use oci_distribution::RegistryOperation;
1211
use oci_distribution::{
@@ -195,18 +194,19 @@ impl Client {
195194
// Add all archived file entries to the locked app manifest
196195
for entry in WalkDir::new(source) {
197196
let entry = entry?;
198-
if entry.file_type().is_file() {
199-
// Can unwrap because we got to 'entry' from walking 'source'
200-
let rel_path = entry.path().strip_prefix(source).unwrap();
201-
tracing::trace!("Adding asset {rel_path:?} to component files list");
202-
// Add content/path to the locked component files list
203-
let layer = Self::data_layer(entry.path(), DATA_MEDIATYPE.to_string()).await?;
204-
let content = Self::content_ref_for_layer(&layer);
205-
files.push(ContentPath {
206-
content,
207-
path: rel_path.into(),
208-
});
197+
if !entry.file_type().is_file() {
198+
continue;
209199
}
200+
// Can unwrap because we got to 'entry' from walking 'source'
201+
let rel_path = entry.path().strip_prefix(source).unwrap();
202+
tracing::trace!("Adding asset {rel_path:?} to component files list");
203+
// Add content/path to the locked component files list
204+
let layer = Self::data_layer(entry.path(), DATA_MEDIATYPE.to_string()).await?;
205+
let content = Self::content_ref_for_layer(&layer);
206+
files.push(ContentPath {
207+
content,
208+
path: rel_path.into(),
209+
});
210210
}
211211

212212
// Only add the archive layer to the OCI manifest
@@ -235,25 +235,26 @@ impl Client {
235235
tracing::trace!("Adding new layer per file under source {:?}", source);
236236
for entry in WalkDir::new(source) {
237237
let entry = entry?;
238-
if entry.file_type().is_file() {
239-
// Can unwrap because we got to 'entry' from walking 'source'
240-
let rel_path = entry.path().strip_prefix(source).unwrap();
241-
tracing::trace!("Adding new layer for asset {rel_path:?}");
242-
// Construct and push layer, adding its digest to the locked component files Vec
243-
let layer = Self::data_layer(entry.path(), DATA_MEDIATYPE.to_string()).await?;
244-
let content = Self::content_ref_for_layer(&layer);
245-
let content_inline = content.inline.is_some();
246-
files.push(ContentPath {
247-
content,
248-
path: rel_path.into(),
249-
});
250-
// As a workaround for OCI implementations that don't support very small blobs,
251-
// don't push very small content that has been inlined into the manifest:
252-
// https://github.com/distribution/distribution/discussions/4029
253-
let skip_layer = content_inline;
254-
if !skip_layer {
255-
layers.push(layer);
256-
}
238+
if !entry.file_type().is_file() {
239+
continue;
240+
}
241+
// Can unwrap because we got to 'entry' from walking 'source'
242+
let rel_path = entry.path().strip_prefix(source).unwrap();
243+
tracing::trace!("Adding new layer for asset {rel_path:?}");
244+
// Construct and push layer, adding its digest to the locked component files Vec
245+
let layer = Self::data_layer(entry.path(), DATA_MEDIATYPE.to_string()).await?;
246+
let content = Self::content_ref_for_layer(&layer);
247+
let content_inline = content.inline.is_some();
248+
files.push(ContentPath {
249+
content,
250+
path: rel_path.into(),
251+
});
252+
// As a workaround for OCI implementations that don't support very small blobs,
253+
// don't push very small content that has been inlined into the manifest:
254+
// https://github.com/distribution/distribution/discussions/4029
255+
let skip_layer = content_inline;
256+
if !skip_layer {
257+
layers.push(layer);
257258
}
258259
}
259260
Ok(())
@@ -297,32 +298,23 @@ impl Client {
297298
|| this.cache.data_file(&layer.digest).is_ok()
298299
{
299300
tracing::debug!("Layer {} already exists in cache", &layer.digest);
300-
} else {
301-
tracing::debug!("Pulling layer {}", &layer.digest);
302-
let mut bytes = Vec::new();
303-
match this
304-
.oci
305-
.pull_blob(&reference, &layer.digest, &mut bytes)
306-
.await
307-
{
308-
Err(e) => return Err(e),
309-
_ => match layer.media_type.as_str() {
310-
WASM_LAYER_MEDIA_TYPE => {
311-
let _ = this.cache.write_wasm(&bytes, &layer.digest).await;
312-
}
313-
ARCHIVE_MEDIATYPE => {
314-
if let Err(e) =
315-
this.unpack_archive_layer(&bytes, &layer.digest).await
316-
{
317-
return Err(OciDistributionError::GenericError(Some(
318-
e.to_string(),
319-
)));
320-
}
321-
}
322-
_ => {
323-
let _ = this.cache.write_data(&bytes, &layer.digest).await;
324-
}
325-
},
301+
return anyhow::Ok(());
302+
}
303+
304+
tracing::debug!("Pulling layer {}", &layer.digest);
305+
let mut bytes = Vec::with_capacity(layer.size.try_into()?);
306+
this.oci
307+
.pull_blob(&reference, &layer.digest, &mut bytes)
308+
.await?;
309+
match layer.media_type.as_str() {
310+
WASM_LAYER_MEDIA_TYPE => {
311+
this.cache.write_wasm(&bytes, &layer.digest).await?;
312+
}
313+
ARCHIVE_MEDIATYPE => {
314+
this.unpack_archive_layer(&bytes, &layer.digest).await?;
315+
}
316+
_ => {
317+
this.cache.write_data(&bytes, &layer.digest).await?;
326318
}
327319
}
328320
Ok(())

0 commit comments

Comments
 (0)