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

View File

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

View File

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

View File

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