Skip to content

Commit

Permalink
Drop k8s-openapi and kube crates
Browse files Browse the repository at this point in the history
These are massive overkill for what we need now, and in particular
k8s-openapi contains huge amounts of generated code.

Reimplement the tiny subset of k8s metadata we need.

Signed-off-by: Colin Walters <[email protected]>
  • Loading branch information
cgwalters committed Oct 18, 2023
1 parent 24919a9 commit 4b78911
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 22 deletions.
2 changes: 0 additions & 2 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ hex = "^0.4"
fn-error-context = "0.2.0"
gvariant = "0.4.0"
indicatif = "0.17.0"
k8s-openapi = { version = "0.20.0", features = ["v1_28", "schemars"] }
kube = { version = "0.86.0", default-features = false, features = ["client", "openssl-tls", "runtime", "derive", "openssl-tls"] }
libc = "^0.2"
liboverdrop = "0.1.0"
once_cell = "1.9"
Expand Down
28 changes: 28 additions & 0 deletions lib/src/k8sapitypes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Subset of API definitions for selected Kubernetes API types.
//! We avoid dragging in all of k8s-openapi because it's *huge*.
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Resource {
pub api_version: String,
pub kind: String,
#[serde(default)]
pub metadata: ObjectMeta,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ObjectMeta {
#[serde(skip_serializing_if = "Option::is_none")]
pub annotations: Option<BTreeMap<String, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub labels: Option<BTreeMap<String, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub namespace: Option<String>,
}
1 change: 1 addition & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod containerenv;
pub(crate) mod ignition;
#[cfg(feature = "install")]
mod install;
mod k8sapitypes;
#[cfg(feature = "install")]
pub(crate) mod mount;
#[cfg(feature = "install")]
Expand Down
58 changes: 41 additions & 17 deletions lib/src/spec.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
//! 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};

/// Representation of a bootc host system
#[derive(
CustomResource, Serialize, Deserialize, Default, Debug, PartialEq, Eq, Clone, JsonSchema,
)]
#[kube(
group = "org.containers.bootc",
version = "v1alpha1",
kind = "BootcHost",
struct = "Host",
namespaced,
status = "HostStatus",
derive = "PartialEq",
derive = "Default"
)]
use crate::k8sapitypes;

const API_VERSION: &str = "org.containers.bootc/v1alpha1";
const KIND: &str = "BootcHost";

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
/// The core host definition
pub struct Host {
/// Metadata
#[serde(flatten)]
pub resource: k8sapitypes::Resource,
/// The spec
#[serde(default)]
pub spec: HostSpec,
/// The status
pub status: Option<HostStatus>,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
/// The host specification
pub struct HostSpec {
/// The host image
pub image: Option<ImageReference>,
Expand Down Expand Up @@ -58,7 +63,7 @@ pub struct ImageStatus {
/// The version string, if any
pub version: Option<String>,
/// The build timestamp, if any
pub timestamp: Option<k8smeta::Time>,
pub timestamp: Option<chrono::DateTime<chrono::Utc>>,
/// The digest of the fetched image (e.g. sha256:a0...);
pub image_digest: String,
}
Expand Down Expand Up @@ -102,6 +107,25 @@ pub struct HostStatus {
pub is_container: bool,
}

impl Host {
/// Create a new host
pub fn new(name: &str, spec: HostSpec) -> Self {
let metadata = k8sapitypes::ObjectMeta {
name: Some(name.to_owned()),
..Default::default()
};
Self {
resource: k8sapitypes::Resource {
api_version: API_VERSION.to_owned(),
kind: KIND.to_owned(),
metadata,
},
spec,
status: None,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
5 changes: 2 additions & 3 deletions lib/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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;
Expand Down Expand Up @@ -89,9 +88,9 @@ pub(crate) struct Deployments {
pub(crate) other: VecDeque<ostree::Deployment>,
}

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

0 comments on commit 4b78911

Please sign in to comment.