Skip to content

Commit

Permalink
Add version and timestamp to image status fields
Browse files Browse the repository at this point in the history
These are very practical things to want; the switch to a k8s-native
API lost these user-facing ergonomics.
  • Loading branch information
cgwalters committed Sep 16, 2023
1 parent 6c0b7f3 commit 9bc0cb7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ rust-version = "1.64.0"
anyhow = "1.0"
camino = { version = "1.0.4", features = ["serde1"] }
ostree-ext = "0.12"
chrono = { version = "0.4.23", features = ["serde"] }
clap = { version= "4.2", features = ["derive"] }
clap_mangen = { version = "0.2", optional = true }
cap-std-ext = "3"
hex = "^0.4"
fn-error-context = "0.2.0"
gvariant = "0.4.0"
indicatif = "0.17.0"
k8s-openapi = { version = "0.18.0", features = ["v1_25"] }
k8s-openapi = { version = "0.18.0", features = ["v1_25", "schemars"] }
kube = { version = "0.83.0", features = ["runtime", "derive"] }
libc = "^0.2"
liboverdrop = "0.1.0"
Expand All @@ -29,7 +30,7 @@ openssl = "^0.10"
nix = { version = "0.27", features = ["ioctl"] }
regex = "1.7.1"
rustix = { "version" = "0.38", features = ["thread", "fs", "system", "process"] }
schemars = "0.8.6"
schemars = { version = "0.8.6", features = ["chrono"] }
serde = { features = ["derive"], version = "1.0.125" }
serde_json = "1.0.64"
serde_yaml = "0.9.17"
Expand Down
5 changes: 5 additions & 0 deletions lib/src/spec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! The definition for host system state.

use k8s_openapi::apimachinery::pkg::apis::meta::v1 as k8smeta;
use kube::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -54,6 +55,10 @@ pub struct ImageReference {
pub struct ImageStatus {
/// The currently booted image
pub image: ImageReference,
/// The version string, if any
pub version: Option<String>,
/// The build timestamp, if any
pub timestamp: Option<k8smeta::Time>,
/// The digest of the fetched image (e.g. sha256:a0...);
pub image_digest: String,
}
Expand Down
32 changes: 32 additions & 0 deletions lib/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use std::collections::VecDeque;
use crate::spec::{BootEntry, Host, HostSpec, HostStatus, ImageStatus};
use crate::spec::{ImageReference, ImageSignature};
use anyhow::{Context, Result};
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as k8smeta;
use ostree::glib;
use ostree_container::OstreeImageReference;
use ostree_ext::container as ostree_container;
use ostree_ext::keyfileext::KeyFileExt;
use ostree_ext::oci_spec;
use ostree_ext::ostree;
use ostree_ext::sysroot::SysrootLock;

Expand Down Expand Up @@ -87,6 +89,22 @@ pub(crate) struct Deployments {
pub(crate) other: VecDeque<ostree::Deployment>,
}

fn try_deserialize_timestamp(t: &str) -> Option<k8smeta::Time> {
match chrono::DateTime::parse_from_rfc3339(t).context("Parsing timestamp") {
Ok(t) => Some(k8smeta::Time(t.with_timezone(&chrono::Utc))),
Err(e) => {
tracing::warn!("Invalid timestamp in image: {:#}", e);
None
}
}
}

pub(crate) fn labels_of_config(
config: &oci_spec::image::ImageConfiguration,
) -> Option<&std::collections::HashMap<String, String>> {
config.config().as_ref().and_then(|c| c.labels().as_ref())
}

fn boot_entry_from_deployment(
sysroot: &SysrootLock,
deployment: &ostree::Deployment,
Expand All @@ -98,9 +116,23 @@ fn boot_entry_from_deployment(
let csum = deployment.csum();
let incompatible = crate::utils::origin_has_rpmostree_stuff(origin);
let imgstate = ostree_container::store::query_image_commit(repo, &csum)?;
let config = imgstate.configuration.as_ref();
let labels = config.and_then(labels_of_config);
let timestamp = labels
.and_then(|l| {
l.get(oci_spec::image::ANNOTATION_CREATED)
.map(|s| s.as_str())
})
.and_then(try_deserialize_timestamp);

let version = config
.and_then(ostree_container::version_for_config)
.map(ToOwned::to_owned);
(
Some(ImageStatus {
image,
version,
timestamp,
image_digest: imgstate.manifest_digest,
}),
incompatible,
Expand Down

0 comments on commit 9bc0cb7

Please sign in to comment.