-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: service_account_missing validation check
- Loading branch information
Showing
11 changed files
with
139 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
mod annotated_trace; | ||
mod status_field_populated; | ||
mod rules; | ||
mod summary; | ||
mod validation_store; | ||
mod validator; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod service_account_missing; | ||
pub mod status_field_populated; | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use std::collections::HashSet; | ||
use std::sync::{ | ||
Arc, | ||
RwLock, | ||
}; | ||
|
||
use json_patch_ext::prelude::*; | ||
use sk_core::k8s::GVK; | ||
use sk_core::prelude::*; | ||
use sk_store::TracerConfig; | ||
|
||
use crate::validation::annotated_trace::AnnotatedTraceEvent; | ||
use crate::validation::validator::{ | ||
Diagnostic, | ||
Validator, | ||
ValidatorType, | ||
}; | ||
|
||
const HELP: &str = r#"A pod needs a service account that is not present in | ||
the trace file. The simulation will fail because pods cannot be created | ||
if their service account does not exist."#; | ||
|
||
#[derive(Default)] | ||
pub struct ServiceAccountMissing { | ||
seen_service_accounts: HashSet<String>, | ||
} | ||
|
||
fn get_service_account(obj: &DynamicObject, config: &TracerConfig) -> anyhow::Result<Option<String>> { | ||
let gvk = GVK::from_dynamic_obj(obj)?; | ||
if let Some(pod_spec_template_path) = config.pod_spec_template_path(&gvk) { | ||
let sa_ptr = format_ptr!("{pod_spec_template_path}/spec/serviceAccount"); | ||
let sa_name_ptr = format_ptr!("{pod_spec_template_path}/spec/serviceAccountName"); | ||
if let Ok(sa) = sa_ptr.resolve(&obj.data) { | ||
return Ok(Some(sa.to_string())); | ||
} else if let Ok(sa) = sa_name_ptr.resolve(&obj.data) { | ||
return Ok(Some(sa.to_string())); | ||
} | ||
} | ||
Ok(None) | ||
} | ||
|
||
impl Diagnostic for ServiceAccountMissing { | ||
fn check_next_event( | ||
&mut self, | ||
event: &mut AnnotatedTraceEvent, | ||
config: &TracerConfig, | ||
) -> anyhow::Result<Vec<usize>> { | ||
for obj in &event.data.applied_objs { | ||
if let Some(ref type_meta) = obj.types { | ||
if &type_meta.kind == "ServiceAccount" { | ||
self.seen_service_accounts.insert(obj.namespaced_name()); | ||
} | ||
} | ||
} | ||
for obj in &event.data.deleted_objs { | ||
if let Some(ref type_meta) = obj.types { | ||
if &type_meta.kind == "ServiceAccount" { | ||
self.seen_service_accounts.remove(&obj.namespaced_name()); | ||
} | ||
} | ||
} | ||
|
||
let mut indices = vec![]; | ||
for (i, obj) in event.data.applied_objs.iter().enumerate() { | ||
let maybe_sa = get_service_account(obj, config)?; | ||
if let Some(sa) = maybe_sa { | ||
if !self.seen_service_accounts.contains(&sa) { | ||
indices.push(i); | ||
} | ||
} | ||
} | ||
|
||
Ok(indices) | ||
} | ||
|
||
fn fixes(&self) -> Vec<PatchOperation> { | ||
vec![remove_operation(format_ptr!("/spec/serviceAccount"))] | ||
} | ||
|
||
fn reset(&mut self) {} | ||
} | ||
|
||
pub fn validator() -> Validator { | ||
Validator { | ||
type_: ValidatorType::Error, | ||
name: "service_account_missing", | ||
help: HELP, | ||
diagnostic: Arc::new(RwLock::new(ServiceAccountMissing::default())), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mod status_field_populated_test; | ||
|
||
use rstest::*; | ||
use sk_core::prelude::*; | ||
|
||
use super::*; | ||
use crate::validation::AnnotatedTraceEvent; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters