Skip to content

Commit

Permalink
fix!: tracer doesn't delete objects on restart
Browse files Browse the repository at this point in the history
  • Loading branch information
drmorr0 committed Nov 15, 2024
1 parent b3cde6f commit 7678afc
Show file tree
Hide file tree
Showing 43 changed files with 453 additions and 297 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion sk-cli/src/crd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use kube::CustomResourceExt;
use sk_core::prelude::*;

pub fn cmd() -> EmptyResult {
Expand Down
2 changes: 1 addition & 1 deletion sk-cli/src/validation/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod validation_store_test;
use std::collections::BTreeMap;

use rstest::*;
use sk_core::k8s::testutils::test_deployment;
use sk_core::prelude::*;
use sk_store::TraceEvent;

use super::annotated_trace::AnnotatedTraceEvent;
Expand Down
1 change: 0 additions & 1 deletion sk-cli/src/validation/tests/status_field_populated_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use assertables::*;
use kube::api::DynamicObject;
use serde_json::json;
use sk_core::k8s::testutils::test_deployment;
use sk_store::TraceEvent;

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion sk-cli/src/xray/view/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use chrono::TimeDelta;
use kube::api::DynamicObject;
use lazy_static::lazy_static;
use ratatui::prelude::*;
use sk_core::k8s::KubeResourceExt;
use sk_core::prelude::*;

use crate::validation::{
AnnotatedTraceEvent,
Expand Down
3 changes: 2 additions & 1 deletion sk-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ readme.workspace = true
edition.workspace = true

[features]
testutils = ["dep:http", "dep:httpmock", "dep:mockall", "dep:rstest"]
testutils = ["dep:http", "dep:httpmock", "dep:lazy_static", "dep:mockall", "dep:rstest"]

[dependencies]
anyhow = { workspace = true }
Expand Down Expand Up @@ -37,6 +37,7 @@ url = { workspace = true }
# testutils dependencies
http = { workspace = true, optional = true }
httpmock = { workspace = true, optional = true }
lazy_static = { workspace = true, optional = true }
mockall = { workspace = true, optional = true }
rstest = { workspace = true, optional = true }

Expand Down
10 changes: 9 additions & 1 deletion sk-core/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ pub const ERROR_RETRY_DELAY_SECONDS: u64 = 30;

#[cfg(feature = "testutils")]
mod test_constants {
pub const EMPTY_OBJ_HASH: u64 = 15130871412783076140;
use lazy_static::lazy_static;

use crate::k8s::GVK;

pub const EMPTY_POD_SPEC_HASH: u64 = 17506812802394981455;
pub const TEST_DEPLOYMENT: &str = "the-deployment";
pub const TEST_NAMESPACE: &str = "test-namespace";
Expand All @@ -40,6 +43,11 @@ mod test_constants {
pub const TEST_DRIVER_ROOT_NAME: &str = "sk-test-driver-12345-root";
pub const TEST_VIRT_NS_PREFIX: &str = "virt-test";
pub const TEST_CTRL_NAMESPACE: &str = "ctrl-ns";

lazy_static! {
pub static ref DEPL_GVK: GVK = GVK::new("apps", "v1", "Deployment");
pub static ref DS_GVK: GVK = GVK::new("apps", "v1", "DaemonSet");
}
}

#[cfg(feature = "testutils")]
Expand Down
2 changes: 1 addition & 1 deletion sk-core/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use tokio::io::{
BufWriter,
};
use tokio::process::Command;
use tracing::*;

use crate::prelude::*;

Expand Down Expand Up @@ -70,7 +71,6 @@ mod test {
use tracing_test::*;

use super::*;
use crate::k8s::testutils::test_sim;

#[rstest]
#[traced_test]
Expand Down
2 changes: 2 additions & 0 deletions sk-core/src/k8s/container_state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use tracing::*;

use super::*;
use crate::prelude::*;

Expand Down
28 changes: 21 additions & 7 deletions sk-core/src/k8s/gvk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::ops::Deref;
use kube::api::{
DynamicObject,
GroupVersionKind,
TypeMeta,
};
use serde::{
de,
Expand Down Expand Up @@ -49,6 +50,13 @@ impl GVK {
bail!("invalid format for api_version: {}", rf.api_version);
}
}

pub fn into_type_meta(&self) -> TypeMeta {
TypeMeta {
api_version: self.0.api_version(),
kind: self.0.kind.clone(),
}
}
}

// Impl Deref lets a GVK act like a GroupVersionKind anywhere one of those is expected
Expand All @@ -60,18 +68,24 @@ impl Deref for GVK {
}
}

impl Serialize for GVK {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
impl fmt::Display for GVK {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut group = Cow::from(&self.0.group);
if !group.is_empty() {
group.to_mut().push('/');
}

let skey = format!("{group}{}.{}", self.0.version, self.0.kind);
serializer.serialize_str(&skey)
write!(f, "{group}{}.{}", self.0.version, self.0.kind)
}
}

impl Serialize for GVK {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
// reuse the display impl for serializing
serializer.serialize_str(&format!("{self}"))
}
}

Expand Down
2 changes: 1 addition & 1 deletion sk-core/src/k8s/lease.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use clockabilly::{
};
use k8s_openapi::api::coordination::v1 as coordinationv1;
use kube::api::Patch;
use kube::ResourceExt;
use serde_json::json;
use tracing::*;

use crate::k8s::{
build_object_meta,
Expand Down
5 changes: 1 addition & 4 deletions sk-core/src/k8s/owners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use kube::discovery::{
ApiCapabilities,
Scope,
};
use kube::{
Resource,
ResourceExt,
};
use kube::Resource;
use tracing::*;

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion sk-core/src/k8s/pod_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::cmp::{
};

use clockabilly::Clockable;
use kube::ResourceExt;
use tracing::*;

use super::*;
use crate::prelude::*;
Expand Down
1 change: 0 additions & 1 deletion sk-core/src/k8s/sim.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use kube::ResourceExt;
use sk_api::v1::{
Simulation,
SimulationMetricsConfig,
Expand Down
1 change: 0 additions & 1 deletion sk-core/src/k8s/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ use rstest::*;
use tracing_test::traced_test;

use super::*;
use crate::k8s::testutils::*;
use crate::macros::*;
1 change: 0 additions & 1 deletion sk-core/src/k8s/tests/owners_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;

use kube::ResourceExt;
use serde_json::json;

use super::*;
Expand Down
25 changes: 16 additions & 9 deletions sk-core/src/k8s/testutils/objs.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
use kube::api::{
DynamicObject,
GroupVersionKind,
};
use kube::api::DynamicObject;
use kube::discovery::ApiResource;
use rstest::*;
use serde_json::json;

use crate::prelude::*;

// If the fixture objects below change, these hash values will need to be updated
pub const TEST_DEPL_HASH: u64 = 3664028200602729212;
pub const TEST_DS_HASH: u64 = 16161139027557399432;

#[fixture]
pub fn test_deployment(#[default(TEST_DEPLOYMENT)] name: &str) -> DynamicObject {
DynamicObject::new(
&name,
&ApiResource::from_gvk(&GroupVersionKind::gvk("core".into(), "v1".into(), "deployment".into())),
)
.within(TEST_NAMESPACE)
DynamicObject::new(&name, &ApiResource::from_gvk(&DEPL_GVK))
.within(TEST_NAMESPACE)
.data(json!({"spec": {"replicas": 42}}))
}

#[fixture]
pub fn test_daemonset(#[default(TEST_DEPLOYMENT)] name: &str) -> DynamicObject {
DynamicObject::new(&name, &ApiResource::from_gvk(&DS_GVK))
.within(TEST_NAMESPACE)
.data(json!({"spec": {"updateStrategy": {"type": "onDelete"}}}))
}
5 changes: 2 additions & 3 deletions sk-core/src/k8s/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::collections::BTreeMap;
use kube::api::{
DynamicObject,
Resource,
ResourceExt,
TypeMeta,
};
use serde_json as json;
Expand Down Expand Up @@ -39,15 +38,15 @@ where
});
}

pub fn build_deletable(ns_name: &str) -> DynamicObject {
pub fn build_deletable(gvk: &GVK, ns_name: &str) -> DynamicObject {
let (ns, name) = split_namespaced_name(ns_name);
DynamicObject {
metadata: metav1::ObjectMeta {
namespace: Some(ns),
name: Some(name),
..Default::default()
},
types: None,
types: Some(gvk.into_type_meta()),
data: json::Value::Null,
}
}
Expand Down
8 changes: 7 additions & 1 deletion sk-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ pub mod time;
pub mod prelude {
pub use k8s_openapi::api::core::v1 as corev1;
pub use k8s_openapi::apimachinery::pkg::apis::meta::v1 as metav1;
pub use kube::{
CustomResourceExt,
ResourceExt,
};
pub use sk_api::v1::{
Simulation,
SimulationRoot,
};
pub use tracing::*;

pub use crate::constants::*;
pub use crate::errors::EmptyResult;
#[cfg(feature = "testutils")]
pub use crate::k8s::testutils::*;
pub use crate::k8s::KubeResourceExt;
}
1 change: 1 addition & 0 deletions sk-ctrl/src/cert_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use serde::{
use sk_core::k8s::build_object_meta;
use sk_core::macros::*;
use sk_core::prelude::*;
use tracing::*;

use crate::context::SimulationContext;

Expand Down
2 changes: 1 addition & 1 deletion sk-ctrl/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use kube::ResourceExt;
use sk_api::v1::Simulation;
use sk_core::prelude::*;

use crate::Options;

Expand Down
3 changes: 1 addition & 2 deletions sk-ctrl/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use kube::api::{
Patch,
};
use kube::runtime::controller::Action;
use kube::ResourceExt;
use serde_json::json;
use sk_api::prometheus::*;
use sk_api::v1::{
Expand All @@ -34,13 +33,13 @@ use sk_core::k8s::{
is_terminal,
metrics_ns,
try_claim_lease,
KubeResourceExt,
LeaseState,
};
use sk_core::prelude::*;
use tokio::runtime::Handle;
use tokio::task::block_in_place;
use tokio::time::Duration;
use tracing::*;

use crate::cert_manager;
use crate::context::SimulationContext;
Expand Down
1 change: 1 addition & 0 deletions sk-ctrl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use kube::runtime::{
};
use sk_core::logging;
use sk_core::prelude::*;
use tracing::*;

use crate::context::SimulationContext;
use crate::controller::{
Expand Down
1 change: 0 additions & 1 deletion sk-ctrl/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use anyhow::anyhow;
use k8s_openapi::api::admissionregistration::v1 as admissionv1;
use k8s_openapi::api::batch::v1 as batchv1;
use k8s_openapi::apimachinery::pkg::util::intstr::IntOrString;
use kube::ResourceExt;
use object_store::ObjectStoreScheme;
use reqwest::Url;
use sk_api::prometheus::{
Expand Down
2 changes: 1 addition & 1 deletion sk-driver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use sk_core::external_storage::{
};
use sk_core::k8s::{
ApiSet,
KubeResourceExt,
OwnersCache,
};
use sk_core::prelude::*;
Expand All @@ -32,6 +31,7 @@ use sk_store::{
};
use tokio::sync::Mutex;
use tokio::time::sleep;
use tracing::*;

use crate::mutation::MutationData;
use crate::runner::run_trace;
Expand Down
7 changes: 4 additions & 3 deletions sk-driver/src/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ use kube::core::admission::{
AdmissionResponse,
AdmissionReview,
};
use kube::ResourceExt;
use rocket::serde::json::Json;
use serde_json::{
json,
Value,
};
use sk_core::jsonutils;
use sk_core::k8s::{
KubeResourceExt,
PodExt,
PodLifecycleData,
GVK,
};
use sk_core::prelude::*;
use tracing::*;

use crate::DriverContext;

Expand Down Expand Up @@ -119,8 +119,9 @@ fn add_lifecycle_annotation(
) -> EmptyResult {
if let Some(orig_ns) = pod.annotations().get(ORIG_NAMESPACE_ANNOTATION_KEY) {
for owner in owners {
let gvk = GVK::from_owner_ref(owner)?;
let owner_ns_name = format!("{}/{}", orig_ns, owner.name);
if !ctx.store.has_obj(&owner_ns_name) {
if !ctx.store.has_obj(&gvk, &owner_ns_name) {
continue;
}

Expand Down
Loading

0 comments on commit 7678afc

Please sign in to comment.