clean up some defaults further

use the default_macro crate (so few downloads!) to remove all those
annoying default() calls at the end of every struct.
This commit is contained in:
James Andariese 2023-11-01 19:16:34 -05:00
parent 03a7cc5605
commit a8539a6d94
4 changed files with 38 additions and 32 deletions

7
Cargo.lock generated
View File

@ -378,6 +378,12 @@ version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308"
[[package]]
name = "default_macro"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75f72a760da9c45f6497260e61e104af5bfb9e9646efa19df2c1b60b8eff425e"
[[package]]
name = "digest"
version = "0.10.7"
@ -543,6 +549,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"clap",
"default_macro",
"futures",
"humantime",
"k8s-openapi",

View File

@ -13,6 +13,7 @@ categories = ["development-tools", "command-line-utilities"]
[dependencies]
anyhow = "1.0.75"
clap = { version = "4.4.7", features = ["derive", "env"] }
default_macro = "0.2.1"
futures = "0.3.29"
humantime = "2.1.0"
k8s-openapi = { version = "0.20.0", features = ["v1_27"] }

View File

@ -25,6 +25,7 @@
// * config mode (maybe)
// * set the pvc storageclass instead
use default_macro::default;
use futures::{StreamExt, TryStreamExt};
use k8s::apimachinery::pkg::api::resource::Quantity;
use kube_quantity::{ParseQuantityError, ParsedQuantity};
@ -257,12 +258,11 @@ async fn setup_pod(ctx: &mut AppContext) -> Result<(Api<Pod>, String)> {
if let None = existing_pvc {
info!("pvc doesn't exist yet. creating now.");
let repo_pvc = PersistentVolumeClaim {
metadata: ObjectMeta{
metadata: default!(ObjectMeta{
name: Some(kube_pvc.to_owned()),
namespace: Some(kube_ns.to_owned()),
..ObjectMeta::default()
},
spec: Some(PersistentVolumeClaimSpec {
}),
spec: Some(default!(PersistentVolumeClaimSpec {
access_modes: Some(vec!["ReadWriteOnce".to_owned()]),
resources: Some(ResourceRequirements {
claims: None,
@ -271,11 +271,7 @@ async fn setup_pod(ctx: &mut AppContext) -> Result<(Api<Pod>, String)> {
}),
storage_class_name: cfg.storageclass.clone(),
volume_mode: Some("Filesystem".to_owned()),
volume_name: None,
data_source: None,
data_source_ref: None,
selector: None,
}),
})),
status: None,
};
let pp = PostParams::default();
@ -286,8 +282,8 @@ async fn setup_pod(ctx: &mut AppContext) -> Result<(Api<Pod>, String)> {
// create the worker pod
let worker_pod = Pod{
metadata: ObjectMeta{
let worker_pod = default!(Pod{
metadata: default!(ObjectMeta{
name: Some(kube_worker_name.to_owned()),
namespace: Some(kube_ns.to_owned()),
labels: Some({
@ -299,11 +295,10 @@ async fn setup_pod(ctx: &mut AppContext) -> Result<(Api<Pod>, String)> {
}
labels
}),
..ObjectMeta::default()
},
spec: Some(PodSpec {
}),
spec: Some(default!(PodSpec {
restart_policy: Some("Never".to_owned()),
containers: vec![Container{
containers: vec![default!(Container{
name: KUBE_CONTAINER_NAME.to_owned(),
command: Some(vec![
kube_shell_executable.to_owned(),
@ -314,30 +309,24 @@ async fn setup_pod(ctx: &mut AppContext) -> Result<(Api<Pod>, String)> {
working_dir: Some(kube_repo_mount_path.to_owned()),
image_pull_policy: Some("IfNotPresent".to_owned()),
volume_mounts: Some(vec![
VolumeMount{
default!(VolumeMount{
mount_path: kube_repo_mount_path.to_owned(),
name: "repo".to_owned(),
..VolumeMount::default()
}
})
]),
..Container::default()
}],
})],
volumes: Some(vec![
Volume{
default!(Volume{
persistent_volume_claim: Some(
PersistentVolumeClaimVolumeSource {
claim_name: kube_pvc.to_owned(),
read_only: Some(false),
..PersistentVolumeClaimVolumeSource::default()
}
),
..Volume::default()
},
]),
..PodSpec::default()
}),
..Pod::default()
};
]),
})),
});
let mut lp = ListParams::default();
let mut ls: String = String::with_capacity(kube_pod_labels.len() * 100);

View File

@ -33,7 +33,9 @@ use crate::*;
pub trait PodExt {
fn label_selectors(&self) -> Vec<String>;
fn label_selector(&self) -> String;
fn field_selectors(&self) -> Result<Vec<String>>;
fn field_selector(&self) -> Result<String>;
}
impl PodExt for Pod {
@ -46,6 +48,10 @@ impl PodExt for Pod {
selectors
}
fn label_selector(&self) -> String {
self.label_selectors().join(",").to_owned()
}
fn field_selectors(&self) -> Result<Vec<String>> {
Ok(vec![
format!(
@ -64,13 +70,16 @@ impl PodExt for Pod {
),
])
}
fn field_selector(&self) -> Result<String> {
Ok(self.field_selectors()?.join(",").to_owned())
}
}
pub async fn wait_for_pod_running_watch(pods: &Api<Pod>, pod: Pod) -> Result<()> {
let mut wp = WatchParams::default();
for fs in pod.field_selectors()? {
wp = wp.fields(&fs);
}
let wp = default!(WatchParams{
field_selector: Some(pod.field_selector()?),
});
let mut stream = pods.watch(&wp, "0").await?.boxed();
while let Some(status) = stream.try_next().await? {
match status {