|
| 1 | +use k8s_openapi::api::batch::v1::CronJob; |
| 2 | +use serde::Serialize; |
| 3 | +use std::collections::BTreeMap; |
| 4 | + |
| 5 | +use crate::host_settings::HostSettings; |
| 6 | +use crate::section::Section; |
| 7 | +use crate::section::common::LabelRef; |
| 8 | + |
| 9 | +/// CronJob info. (`kube_cron_job_info_v1`) |
| 10 | +#[derive(Serialize)] |
| 11 | +pub(crate) struct KubeCronJobInfoV1<'a> { |
| 12 | + pub name: &'a str, |
| 13 | + pub namespace: &'a str, |
| 14 | + pub creation_timestamp: Option<f64>, |
| 15 | + pub labels: BTreeMap<&'a str, LabelRef<'a>>, |
| 16 | + /// Annotations filtered with user input. |
| 17 | + /// |
| 18 | + /// After receiving the annotations from the Kubernetes API, we cannot |
| 19 | + /// process all of them as HostLabels. FilteredAnnotations are those |
| 20 | + /// annotations, which can be processed. This means that the annotations can |
| 21 | + /// no longer be arbitrary json objects and that options from the |
| 22 | + /// `Kubernetes` rule have been taken into account. |
| 23 | + pub annotations: BTreeMap<&'a str, &'a str>, |
| 24 | + pub schedule: &'a str, |
| 25 | + pub concurrency_policy: &'a str, |
| 26 | + pub failed_jobs_history_limit: i32, |
| 27 | + pub successful_jobs_history_limit: i32, |
| 28 | + pub suspend: bool, |
| 29 | + pub cluster: &'a str, |
| 30 | + pub kubernetes_cluster_hostname: &'a str, |
| 31 | +} |
| 32 | + |
| 33 | +impl<'a> KubeCronJobInfoV1<'a> { |
| 34 | + pub fn from_cron_job( |
| 35 | + cron_job: &'a CronJob, |
| 36 | + settings: &'a HostSettings, |
| 37 | + ) -> Option<KubeCronJobInfoV1<'a>> { |
| 38 | + let section = KubeCronJobInfoV1 { |
| 39 | + name: cron_job.metadata.name.as_deref()?, |
| 40 | + namespace: cron_job.metadata.namespace.as_deref()?, |
| 41 | + creation_timestamp: cron_job |
| 42 | + .metadata |
| 43 | + .creation_timestamp |
| 44 | + .as_ref() |
| 45 | + .map(|t| t.0.as_millisecond() as f64 / 1000.0), |
| 46 | + labels: cron_job |
| 47 | + .metadata |
| 48 | + .labels |
| 49 | + .as_ref() |
| 50 | + .map(LabelRef::from_map) |
| 51 | + .unwrap_or_default(), |
| 52 | + annotations: cron_job |
| 53 | + .metadata |
| 54 | + .annotations |
| 55 | + .as_ref() |
| 56 | + .map(|m| settings.annotation_key_pattern.filter(m)) |
| 57 | + .unwrap_or_default(), |
| 58 | + schedule: &cron_job.spec.schedule, |
| 59 | + concurrency_policy: cron_job.spec.concurrency_policy.as_deref()?, |
| 60 | + failed_jobs_history_limit: cron_job.spec.failed_jobs_history_limit?, |
| 61 | + successful_jobs_history_limit: cron_job.spec.successful_jobs_history_limit?, |
| 62 | + suspend: cron_job.spec.suspend?, |
| 63 | + cluster: &settings.cluster_name, |
| 64 | + kubernetes_cluster_hostname: &settings.cluster_host_name, |
| 65 | + }; |
| 66 | + Some(section) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl Section for KubeCronJobInfoV1<'_> { |
| 71 | + const NAME: &'static str = "kube_cron_job_info_v1"; |
| 72 | +} |
| 73 | + |
| 74 | +#[cfg(test)] |
| 75 | +mod tests { |
| 76 | + use super::*; |
| 77 | + use regex::Regex; |
| 78 | + |
| 79 | + use crate::host_settings::AnnotationKeyPattern; |
| 80 | + use crate::test_support::{cron_job, host_settings}; |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn kube_cron_job_info_v1() { |
| 84 | + let cron_job = cron_job("important-job"); |
| 85 | + let mut settings = host_settings(); |
| 86 | + insta::assert_json_snapshot!(KubeCronJobInfoV1::from_cron_job(&cron_job, &settings)); |
| 87 | + |
| 88 | + // This pattern should only match one annotation |
| 89 | + settings.annotation_key_pattern = |
| 90 | + AnnotationKeyPattern::Pattern(Regex::new("^example").unwrap()); |
| 91 | + assert_eq!( |
| 92 | + KubeCronJobInfoV1::from_cron_job(&cron_job, &settings) |
| 93 | + .unwrap() |
| 94 | + .annotations |
| 95 | + .len(), |
| 96 | + 1 |
| 97 | + ); |
| 98 | + |
| 99 | + // Ignore all annotations, emit 0 of them |
| 100 | + settings.annotation_key_pattern = AnnotationKeyPattern::IgnoreAll; |
| 101 | + assert_eq!( |
| 102 | + KubeCronJobInfoV1::from_cron_job(&cron_job, &settings) |
| 103 | + .unwrap() |
| 104 | + .annotations |
| 105 | + .len(), |
| 106 | + 0 |
| 107 | + ); |
| 108 | + |
| 109 | + // Import all annotations (fixture default, captured by insta above, but let's be explicit) |
| 110 | + settings.annotation_key_pattern = AnnotationKeyPattern::ImportAll; |
| 111 | + assert_eq!( |
| 112 | + KubeCronJobInfoV1::from_cron_job(&cron_job, &settings) |
| 113 | + .unwrap() |
| 114 | + .annotations |
| 115 | + .len(), |
| 116 | + 2 |
| 117 | + ); |
| 118 | + } |
| 119 | +} |
0 commit comments