Skip to content

Commit

Permalink
fix. filter streamable descs by platform
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusl committed Jun 22, 2023
1 parent 6a59f9e commit 35dfeb6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
50 changes: 46 additions & 4 deletions src/content/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::BTreeMap;

use lifec::prelude::{AttributeIndex, Component, DefaultVecStorage, ThunkContext};
use serde::{Deserialize, Serialize};
use tracing::{trace, debug};
use tracing::{debug, trace};

use super::Platform;

Expand Down Expand Up @@ -88,10 +88,23 @@ impl Descriptor {
/// ```
///
pub fn try_parse_streamable_descriptor(&self) -> Option<Self> {
if !self.annotations.as_ref().map(|a| a.contains_key("streaming.mediaType")).unwrap_or_default() {
if !self
.annotations
.as_ref()
.map(|a| a.contains_key("streaming.mediaType"))
.unwrap_or_default()
{
let mut desc = self.clone();
debug!("Updated artifact manifest detected, skipping annotation parsing");
if let Some(platform) = self.try_parse_streamable_platform() {
desc.platform = Some(Platform {
architecture: platform.platform_arch.unwrap_or_default(),
os: platform.platform_os.unwrap_or_default(),
variant: None,
});
}

return Some(self.clone());
return Some(desc);
}

if let Some(annotations) = self
Expand All @@ -108,7 +121,11 @@ impl Descriptor {
annotations: None,
urls: None,
data: None,
platform: None,
platform: Some(Platform {
architecture: streaming_desc.platform_arch.unwrap_or_default(),
os: streaming_desc.platform_os.unwrap_or_default(),
variant: None,
}),
}),
Err(err) => {
trace!(
Expand All @@ -122,6 +139,18 @@ impl Descriptor {
None
}
}

fn try_parse_streamable_platform(&self) -> Option<StreamingPlatform> {
if let Some(annotations) = self
.annotations
.as_ref()
.and_then(|a| serde_json::to_string(a).ok())
{
serde_json::from_str::<StreamingPlatform>(annotations.as_str()).ok()
} else {
None
}
}
}

#[allow(dead_code)]
Expand All @@ -143,6 +172,19 @@ struct StreamingDescriptor {
platform_arch: Option<String>,
}

/// Struct to parse stremaing platform,
///
#[allow(dead_code)]
#[derive(Deserialize)]
struct StreamingPlatform {
#[serde(rename = "streaming.format")]
format: String,
#[serde(rename = "streaming.platform.os")]
platform_os: Option<String>,
#[serde(rename = "streaming.platform.arch")]
platform_arch: Option<String>,
}

#[allow(unused_imports)]
mod tests {
use serde_json::json;
Expand Down
2 changes: 1 addition & 1 deletion src/content/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ pub struct Platform {
pub os: String,
/// Operating system variant
#[serde(skip_serializing_if = "Option::is_none")]
variant: Option<String>,
pub variant: Option<String>,
}
17 changes: 16 additions & 1 deletion src/plugins/teleport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,22 @@ impl Plugin for Teleport {

let streamable = list.find_streamable_descriptors();

let digest = if let Some(streamable_desc) = streamable.first() {
let arch = if std::env::consts::ARCH == "x86_64" {
"amd64"
} else {
std::env::consts::ARCH
};
let os = std::env::consts::OS;

info!("Filtering streamable descriptors w/ os - {os}, arch - {arch}");

let digest = if let Some(streamable_desc) = streamable.iter().find(|s| {
if let Some(platform) = s.platform.as_ref() {
platform.architecture == arch && platform.os == os
} else {
false
}
}) {
info!("Streamable descriptor was found");
streamable_desc.digest.clone()
} else {
Expand Down

0 comments on commit 35dfeb6

Please sign in to comment.