diff --git a/metrics-cache/src/section/pvc.rs b/metrics-cache/src/section/pvc.rs index 272e7b9..4722087 100644 --- a/metrics-cache/src/section/pvc.rs +++ b/metrics-cache/src/section/pvc.rs @@ -337,6 +337,10 @@ mod tests { .or_default() .insert(pvc.metadata.name.clone().unwrap(), pvc.into()); } + let pv = pv("test-local-pv"); + indexes + .pvs + .insert(pv.metadata.name.clone().unwrap(), pv.into()); indexes } @@ -382,6 +386,37 @@ mod tests { ); } + #[test] + fn kube_pvc_pvs_v1() { + let mut indexes = pvc_indexes(); + + // Both claims resolve to their backing PV, section renders + insta::assert_json_snapshot!(KubePvcPvsV1::from_claim_names( + &indexes, + "really-cool-namespace", + ["pvc-1", "pvc-2"] + )); + + // No claim resolves to a PVC at all -> no section + assert_matches!( + KubePvcPvsV1::from_claim_names(&indexes, "really-cool-namespace", ["bad", "worse"]), + None + ); + + // A claim whose PVC exists but isn't bound to a PV is skipped + let mut unbound = pvc("pvc-unbound"); + unbound.spec.as_mut().unwrap().volume_name = None; + indexes + .pvcs + .entry(s("really-cool-namespace")) + .or_default() + .insert(s("pvc-unbound"), unbound.into()); + assert_matches!( + KubePvcPvsV1::from_claim_names(&indexes, "really-cool-namespace", ["pvc-unbound"]), + None + ); + } + #[test] fn workload_pvc_claim_names_are_deduplicated() { let mut first = pod("first", None); diff --git a/metrics-cache/src/section/snapshots/metrics_cache__section__pvc__tests__kube_pvc_pvs_v1.snap b/metrics-cache/src/section/snapshots/metrics_cache__section__pvc__tests__kube_pvc_pvs_v1.snap new file mode 100644 index 0000000..5ba7517 --- /dev/null +++ b/metrics-cache/src/section/snapshots/metrics_cache__section__pvc__tests__kube_pvc_pvs_v1.snap @@ -0,0 +1,18 @@ +--- +source: metrics-cache/src/section/pvc.rs +expression: "KubePvcPvsV1::from_claim_names(&indexes, \"really-cool-namespace\",\n[\"pvc-1\", \"pvc-2\"])" +--- +{ + "volumes": { + "test-local-pv": { + "name": "test-local-pv", + "spec": { + "access_modes": [ + "ReadWriteOnce" + ], + "storage_class_name": "manual", + "volume_mode": "Filesystem" + } + } + } +} diff --git a/metrics-cache/src/test_support.rs b/metrics-cache/src/test_support.rs index 723373c..5fa761f 100644 --- a/metrics-cache/src/test_support.rs +++ b/metrics-cache/src/test_support.rs @@ -6,8 +6,8 @@ use k8s_openapi::api::apps::v1::{ use k8s_openapi::api::batch::v1::{CronJob, CronJobSpec, Job}; use k8s_openapi::api::core::v1::{ Container, ContainerStatus, Namespace, Node, NodeAddress, NodeStatus, NodeSystemInfo, - PersistentVolumeClaim, PersistentVolumeClaimSpec, PersistentVolumeClaimStatus, Pod, PodSpec, - VolumeResourceRequirements, + PersistentVolume, PersistentVolumeClaim, PersistentVolumeClaimSpec, + PersistentVolumeClaimStatus, PersistentVolumeSpec, Pod, PodSpec, VolumeResourceRequirements, }; use k8s_openapi::apimachinery::pkg::api::resource::Quantity; use k8s_openapi::apimachinery::pkg::apis::meta::v1::{ @@ -382,3 +382,19 @@ pub fn pvc(name: &str) -> PersistentVolumeClaim { }), } } + +pub fn pv(name: &str) -> PersistentVolume { + PersistentVolume { + metadata: ObjectMeta { + name: Some(name.to_string()), + ..Default::default() + }, + spec: Some(PersistentVolumeSpec { + access_modes: Some(vec![s("ReadWriteOnce")]), + storage_class_name: Some(s("manual")), + volume_mode: Some(s("Filesystem")), + ..Default::default() + }), + ..Default::default() + } +}