Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] parse OCI deployment status #5208

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion rust/rpmostree-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

use anyhow::Context;
use serde_derive::Deserialize;
use serde::{Deserialize, Deserializer};
use serde_json::Value;
use std::collections::HashMap;
use std::process::Command;

Expand Down Expand Up @@ -49,6 +50,7 @@ pub struct Deployment {
pub pinned: bool,
pub checksum: String,
pub base_checksum: Option<String>,
#[serde(deserialize_with = "deserialize_base_commit_meta")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be easier (and more reliable) to just add a method to this get_base_manifest(&self) -> Result<Option<oci_spec::image::ImageManifest>> that directly accesses the expected key, instead of trying to heuristically parse every single metadata key as json.

pub base_commit_meta: HashMap<String, serde_json::Value>,
pub staged: Option<bool>,
pub booted: bool,
Expand All @@ -58,6 +60,43 @@ pub struct Deployment {
pub version: Option<String>,
}

/// When the deployment is an OCI image, the base_commit_meta field contains serialized JSON.
/// So the values in this map can either be:
/// - Regular OSTree case: a JSON object
/// - OCI case : a string containing a serialized JSON object
///
/// In the OCI case, simply deserialize the strings before passing them to serde,
/// when necessary.
/// See https://github.com/coreos/rpm-ostree/issues/5196
fn deserialize_base_commit_meta<'de, D>(deserializer: D) -> Result<HashMap<String, Value>>
where
D: serde::Deserializer<'de>,
{
let buf: HashMap<String, Value> = HashMap::deserialize(deserializer)
.unwrap();
// .map_err(|_| Box::new(std::io::Error::new(std::io::ErrorKind::InvalidData, "Cannot deserialize base_commit_meta")) as Box<dyn std::error::Error + Send + Sync>)?;

//.map_err(|_| D::Error::custom("Cannot deserialize base_commit_meta"))?;

let mut result = HashMap::new();

for (key, value) in buf {
// If the value is a string, attempt to parse it as a nested JSON object
let deserialized_value = if let Value::String(s) = value {
serde_json::from_str(&s)//.map_err(|_| Err(format!("Cannot deserialize base_commit_meta").into()))??
.unwrap()
//.map_err(|_| Box::new(std::io::Error::new(std::io::ErrorKind::InvalidData, "Cannot deserialize base_commit_meta")) as Box<dyn std::error::Error + Send + Sync>)?
} else {
value
};

result.insert(key, deserialized_value);
}

Ok(result)
}


impl Status {
/// Find the booted deployment, if any.
pub fn find_booted(&self) -> Option<&Deployment> {
Expand Down
Loading
Loading