Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move common k8s api stuff into prelude #31

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions ctrl/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ use std::sync::Arc;
use anyhow::bail;
use k8s_openapi::api::admissionregistration::v1 as admissionv1;
use k8s_openapi::api::batch::v1 as batchv1;
use k8s_openapi::api::core::v1 as corev1;
use kube::runtime::controller::Action;
use kube::ResourceExt;
use simkube::errors::*;
use simkube::k8s::label_selector;
use simkube::prelude::*;
use tokio::time::Duration;

use super::objects::*;
use super::*;

const REQUEUE_DURATION: Duration = Duration::from_secs(5);
Expand Down
1 change: 1 addition & 0 deletions ctrl/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::controller::{
error_policy,
reconcile,
};
use crate::objects::*;

#[derive(Clone, Debug, Parser)]
struct Options {
Expand Down
1 change: 0 additions & 1 deletion ctrl/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::env;

use k8s_openapi::api::admissionregistration::v1 as admissionv1;
use k8s_openapi::api::batch::v1 as batchv1;
use k8s_openapi::api::core::v1 as corev1;
use k8s_openapi::apimachinery::pkg::util::intstr::IntOrString;
use reqwest::Url;
use simkube::k8s::{
Expand Down
2 changes: 1 addition & 1 deletion ctrl/trace.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::path::PathBuf;

use anyhow::anyhow;
use k8s_openapi::api::core::v1 as corev1;
use reqwest::Url;
use simkube::prelude::*;

const TRACE_VOLUME_NAME: &str = "trace-data";
const TRACE_PATH: &str = "/trace-data";
Expand Down
2 changes: 0 additions & 2 deletions driver/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use json_patch::{
Patch,
PatchOperation,
};
use k8s_openapi::api::core::v1 as corev1;
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as metav1;
use kube::core::admission::{
AdmissionRequest,
AdmissionResponse,
Expand Down
5 changes: 2 additions & 3 deletions driver/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::cmp::max;
use std::time::Duration;

use anyhow::anyhow;
use k8s_openapi::api::core::v1 as corev1;
use kube::api::{
DynamicObject,
Patch,
Expand Down Expand Up @@ -31,7 +30,7 @@ fn build_virtual_ns(ctx: &DriverContext, owner: &SimulationRoot, namespace: &str
metadata: build_global_object_meta(namespace, &ctx.name, owner)?,
..Default::default()
};
klabel_insert!(ns, VIRTUAL_LABEL_KEY = "true");
klabel_insert!(ns, VIRTUAL_LABEL_KEY => "true");

Ok(ns)
}
Expand All @@ -47,7 +46,7 @@ fn build_virtual_obj(
let mut vobj = obj.clone();
add_common_metadata(&ctx.name, owner, &mut vobj.metadata)?;
vobj.metadata.namespace = Some(virtual_ns.into());
klabel_insert!(vobj, VIRTUAL_LABEL_KEY = "true");
klabel_insert!(vobj, VIRTUAL_LABEL_KEY => "true");

jsonutils::patch_ext::add(
&format!("{}/metadata", pod_spec_template_path),
Expand Down
2 changes: 0 additions & 2 deletions driver/tests/mutation_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use json_patch::{
patch,
Patch,
};
use k8s_openapi::api::core::v1 as corev1;
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as metav1;
use kube::api::TypeMeta;
use kube::core::admission::{
AdmissionRequest,
Expand Down
1 change: 1 addition & 0 deletions lib/rust/k8s/container.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::prelude::*;

// StartEndTimeable provides helper functions for computing the start and end times of a container
// from its corresponding ContainerState object. Note that as per the Kubernetes spec, it is an
Expand Down
2 changes: 1 addition & 1 deletion lib/rust/k8s/gvk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use serde::{
Serializer,
};

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

// GVK is a "newtype" wrapper around the metav1::GroupVersionKind object that lets me provide
// custom serialization methods. We also add some handy helper/conversion functions.
Expand Down
3 changes: 1 addition & 2 deletions lib/rust/k8s/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ mod util;

pub use apiset::*;
pub use gvk::*;
use k8s_openapi::api::core::v1 as corev1;
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as metav1;
pub use owners::OwnersCache;
use serde::{
Deserialize,
Expand All @@ -19,6 +17,7 @@ pub use util::*;

use crate::errors::*;
use crate::macros::partial_ord_eq_ref;
use crate::prelude::*;

const LAST_APPLIED_CONFIG_LABEL_KEY: &str = "kubectl.kubernetes.io/last-applied-configuration";
const DEPL_REVISION_LABEL_KEY: &str = "deployment.kubernetes.io/revision";
Expand Down
1 change: 1 addition & 0 deletions lib/rust/k8s/owners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use tracing::*;

use super::*;
use crate::k8s::ApiSet;
use crate::prelude::*;

pub struct OwnersCache {
apiset: ApiSet,
Expand Down
1 change: 1 addition & 0 deletions lib/rust/k8s/pod_ext.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::prelude::*;

const KUBE_SVC_ACCOUNT_VOLUME_NAME_PREFIX: &str = "kube-api-access";

Expand Down
70 changes: 1 addition & 69 deletions lib/rust/k8s/pod_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use kube::ResourceExt;
use serde_json::Value;

use super::*;
use crate::constants::*;
use crate::jsonutils;
use crate::prelude::*;
use crate::time::Clockable;
use crate::util::min_some;

Expand Down Expand Up @@ -231,71 +231,3 @@ impl PartialOrd<Option<&PodLifecycleData>> for PodLifecycleData {
}
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_partial_eq() {
assert_eq!(PodLifecycleData::Empty, None);
assert_eq!(PodLifecycleData::Empty, Some(&PodLifecycleData::Empty));
assert_eq!(PodLifecycleData::Running(1), Some(&PodLifecycleData::Running(1)));
assert_eq!(PodLifecycleData::Finished(1, 2), Some(&PodLifecycleData::Finished(1, 2)));

assert_ne!(PodLifecycleData::Empty, Some(&PodLifecycleData::Running(1)));
assert_ne!(PodLifecycleData::Empty, Some(&PodLifecycleData::Finished(1, 2)));
assert_ne!(PodLifecycleData::Running(1), None);
assert_ne!(PodLifecycleData::Running(1), Some(&PodLifecycleData::Empty));
assert_ne!(PodLifecycleData::Running(1), Some(&PodLifecycleData::Running(2)));
assert_ne!(PodLifecycleData::Running(1), Some(&PodLifecycleData::Finished(1, 2)));
assert_ne!(PodLifecycleData::Finished(1, 2), None);
assert_ne!(PodLifecycleData::Finished(1, 2), Some(&PodLifecycleData::Empty));
assert_ne!(PodLifecycleData::Finished(1, 2), Some(&PodLifecycleData::Running(2)));
assert_ne!(PodLifecycleData::Finished(1, 2), Some(&PodLifecycleData::Finished(1, 3)));
}

#[test]
fn test_partial_ord() {
for cmp in [
PodLifecycleData::Empty.partial_cmp(&None),
PodLifecycleData::Empty.partial_cmp(&Some(&PodLifecycleData::Empty)),
PodLifecycleData::Running(1).partial_cmp(&Some(&PodLifecycleData::Running(1))),
PodLifecycleData::Finished(1, 2).partial_cmp(&Some(&PodLifecycleData::Finished(1, 2))),
] {
assert_eq!(cmp, Some(Ordering::Equal));
}

for cmp in [
PodLifecycleData::Empty.partial_cmp(&Some(&PodLifecycleData::Running(1))),
PodLifecycleData::Empty.partial_cmp(&Some(&PodLifecycleData::Finished(1, 2))),
PodLifecycleData::Running(1).partial_cmp(&None),
PodLifecycleData::Running(1).partial_cmp(&Some(&PodLifecycleData::Empty)),
PodLifecycleData::Running(1).partial_cmp(&Some(&PodLifecycleData::Running(2))),
PodLifecycleData::Running(1).partial_cmp(&Some(&PodLifecycleData::Finished(1, 2))),
PodLifecycleData::Finished(1, 2).partial_cmp(&None),
PodLifecycleData::Finished(1, 2).partial_cmp(&Some(&PodLifecycleData::Empty)),
PodLifecycleData::Finished(1, 2).partial_cmp(&Some(&PodLifecycleData::Running(2))),
PodLifecycleData::Finished(1, 2).partial_cmp(&Some(&PodLifecycleData::Finished(1, 3))),
] {
assert_ne!(cmp, Some(Ordering::Equal));
}

assert!(PodLifecycleData::Empty < Some(&PodLifecycleData::Running(1)));
assert!(PodLifecycleData::Empty < Some(&PodLifecycleData::Finished(1, 2)));
assert!(PodLifecycleData::Running(1) < Some(&PodLifecycleData::Finished(1, 2)));

assert!(PodLifecycleData::Running(1) > None);
assert!(PodLifecycleData::Running(1) > Some(&PodLifecycleData::Empty));
assert!(PodLifecycleData::Finished(1, 2) > None);
assert!(PodLifecycleData::Finished(1, 2) > Some(&PodLifecycleData::Empty));
assert!(PodLifecycleData::Finished(1, 2) > Some(&PodLifecycleData::Running(1)));

assert!(!(PodLifecycleData::Finished(1, 2) > Some(&PodLifecycleData::Running(0))));
assert!(!(PodLifecycleData::Finished(1, 2) < Some(&PodLifecycleData::Running(0))));
assert!(!(PodLifecycleData::Finished(1, 2) > Some(&PodLifecycleData::Finished(1, 3))));
assert!(!(PodLifecycleData::Finished(1, 2) < Some(&PodLifecycleData::Finished(1, 3))));
assert!(!(PodLifecycleData::Running(1) < Some(&PodLifecycleData::Running(2))));
assert!(!(PodLifecycleData::Running(1) > Some(&PodLifecycleData::Running(2))));
}
}
65 changes: 65 additions & 0 deletions lib/rust/k8s/tests/pod_lifecycle_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cmp::Ordering;

use super::*;
use crate::testutils::{
pods,
Expand Down Expand Up @@ -52,3 +54,66 @@ fn test_pod_lifecycle_data_for_with_end_times(mut test_pod: corev1::Pod) {
let res = PodLifecycleData::new_for(&test_pod).unwrap();
assert_eq!(res, PodLifecycleData::Finished(START_TS, end_ts));
}

#[test]
fn test_partial_eq() {
assert_eq!(PodLifecycleData::Empty, None);
assert_eq!(PodLifecycleData::Empty, Some(&PodLifecycleData::Empty));
assert_eq!(PodLifecycleData::Running(1), Some(&PodLifecycleData::Running(1)));
assert_eq!(PodLifecycleData::Finished(1, 2), Some(&PodLifecycleData::Finished(1, 2)));

assert_ne!(PodLifecycleData::Empty, Some(&PodLifecycleData::Running(1)));
assert_ne!(PodLifecycleData::Empty, Some(&PodLifecycleData::Finished(1, 2)));
assert_ne!(PodLifecycleData::Running(1), None);
assert_ne!(PodLifecycleData::Running(1), Some(&PodLifecycleData::Empty));
assert_ne!(PodLifecycleData::Running(1), Some(&PodLifecycleData::Running(2)));
assert_ne!(PodLifecycleData::Running(1), Some(&PodLifecycleData::Finished(1, 2)));
assert_ne!(PodLifecycleData::Finished(1, 2), None);
assert_ne!(PodLifecycleData::Finished(1, 2), Some(&PodLifecycleData::Empty));
assert_ne!(PodLifecycleData::Finished(1, 2), Some(&PodLifecycleData::Running(2)));
assert_ne!(PodLifecycleData::Finished(1, 2), Some(&PodLifecycleData::Finished(1, 3)));
}

#[test]
fn test_partial_ord() {
for cmp in [
PodLifecycleData::Empty.partial_cmp(&None),
PodLifecycleData::Empty.partial_cmp(&Some(&PodLifecycleData::Empty)),
PodLifecycleData::Running(1).partial_cmp(&Some(&PodLifecycleData::Running(1))),
PodLifecycleData::Finished(1, 2).partial_cmp(&Some(&PodLifecycleData::Finished(1, 2))),
] {
assert_eq!(cmp, Some(Ordering::Equal));
}

for cmp in [
PodLifecycleData::Empty.partial_cmp(&Some(&PodLifecycleData::Running(1))),
PodLifecycleData::Empty.partial_cmp(&Some(&PodLifecycleData::Finished(1, 2))),
PodLifecycleData::Running(1).partial_cmp(&None),
PodLifecycleData::Running(1).partial_cmp(&Some(&PodLifecycleData::Empty)),
PodLifecycleData::Running(1).partial_cmp(&Some(&PodLifecycleData::Running(2))),
PodLifecycleData::Running(1).partial_cmp(&Some(&PodLifecycleData::Finished(1, 2))),
PodLifecycleData::Finished(1, 2).partial_cmp(&None),
PodLifecycleData::Finished(1, 2).partial_cmp(&Some(&PodLifecycleData::Empty)),
PodLifecycleData::Finished(1, 2).partial_cmp(&Some(&PodLifecycleData::Running(2))),
PodLifecycleData::Finished(1, 2).partial_cmp(&Some(&PodLifecycleData::Finished(1, 3))),
] {
assert_ne!(cmp, Some(Ordering::Equal));
}

assert!(PodLifecycleData::Empty < Some(&PodLifecycleData::Running(1)));
assert!(PodLifecycleData::Empty < Some(&PodLifecycleData::Finished(1, 2)));
assert!(PodLifecycleData::Running(1) < Some(&PodLifecycleData::Finished(1, 2)));

assert!(PodLifecycleData::Running(1) > None);
assert!(PodLifecycleData::Running(1) > Some(&PodLifecycleData::Empty));
assert!(PodLifecycleData::Finished(1, 2) > None);
assert!(PodLifecycleData::Finished(1, 2) > Some(&PodLifecycleData::Empty));
assert!(PodLifecycleData::Finished(1, 2) > Some(&PodLifecycleData::Running(1)));

assert!(!(PodLifecycleData::Finished(1, 2) > Some(&PodLifecycleData::Running(0))));
assert!(!(PodLifecycleData::Finished(1, 2) < Some(&PodLifecycleData::Running(0))));
assert!(!(PodLifecycleData::Finished(1, 2) > Some(&PodLifecycleData::Finished(1, 3))));
assert!(!(PodLifecycleData::Finished(1, 2) < Some(&PodLifecycleData::Finished(1, 3))));
assert!(!(PodLifecycleData::Running(1) < Some(&PodLifecycleData::Running(2))));
assert!(!(PodLifecycleData::Running(1) > Some(&PodLifecycleData::Running(2))));
}
2 changes: 1 addition & 1 deletion lib/rust/k8s/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use kube::api::{
use serde_json as json;

use super::*;
use crate::constants::SIMULATION_LABEL_KEY;
use crate::errors::*;
use crate::prelude::*;

pub fn add_common_metadata<K>(sim_name: &str, owner: &K, meta: &mut metav1::ObjectMeta) -> EmptyResult
where
Expand Down
3 changes: 3 additions & 0 deletions lib/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub struct SimulationSpec {
pub struct SimulationRootSpec {}

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 super::{
Simulation,
SimulationRoot,
Expand Down
2 changes: 1 addition & 1 deletion lib/rust/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ macro_rules! klabel {

#[macro_export]
macro_rules! klabel_insert {
($obj:ident, $($key:tt=$val:tt),+$(,)?) => {
($obj:ident, $($key:tt=>$val:tt),+$(,)?) => {
$($obj.labels_mut().insert($key.to_string(), $val.to_string()));+
};
}
Expand Down
5 changes: 1 addition & 4 deletions lib/rust/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use std::collections::{
VecDeque,
};

use k8s_openapi::api::core::v1 as corev1;
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as metav1;
use kube::api::DynamicObject;
use serde::{
Deserialize,
Expand All @@ -22,9 +20,8 @@ use self::pod_owners_map::{
};
use self::trace_filter::filter_event;
pub use self::trace_filter::TraceFilter;
use crate::config::TracerConfig;
use crate::errors::*;
use crate::k8s::PodLifecycleData;
use crate::prelude::*;

#[derive(Debug)]
enum TraceAction {
Expand Down
3 changes: 1 addition & 2 deletions lib/rust/store/trace_filter.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as metav1;
use kube::api::DynamicObject;
use serde::Deserialize;

use super::TraceEvent;
use crate::k8s::KubeResourceExt;
use crate::prelude::*;

// TODO: we also want to add "positive" selectors, e.g., only take objects in a particular
// namespace or matching a particular label-selector.
Expand Down
8 changes: 2 additions & 6 deletions lib/rust/store/trace_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ use tracing::*;

use super::*;
use crate::jsonutils;
use crate::k8s::{
build_deletable,
KubeResourceExt,
PodExt,
PodLifecycleData,
};
use crate::k8s::build_deletable;
use crate::prelude::*;

// The TraceStore object is an in-memory store of a cluster trace. It keeps track of all the
// configured Kubernetes objects, as well as lifecycle data for any pods that are owned by the
Expand Down
3 changes: 1 addition & 2 deletions lib/rust/testutils/pods.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use chrono::DateTime;
use k8s_openapi::api::core::v1 as corev1;
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as metav1;

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

const CONTAINER_PREFIX: &str = "container";
const INIT_CONTAINER_PREFIX: &str = "init-container";
Expand Down
5 changes: 1 addition & 4 deletions lib/rust/testutils/store.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use k8s_openapi::api::core::v1 as corev1;
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as metav1;
use kube::api::DynamicObject;
use mockall::mock;

use crate::config::TracerConfig;
use crate::errors::*;
use crate::k8s::PodLifecycleData;
use crate::prelude::*;
use crate::store::{
TraceIterator,
TraceStorable,
Expand Down
3 changes: 2 additions & 1 deletion lib/rust/watch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ mod pod_watcher;
use std::pin::Pin;

use futures::Stream;
use k8s_openapi::api::core::v1 as corev1;
use kube::api::DynamicObject;
use kube::runtime::watcher::Event;

use crate::prelude::*;

pub type KubeObjectStream = Pin<Box<dyn Stream<Item = anyhow::Result<Event<DynamicObject>>> + Send>>;
pub type PodStream = Pin<Box<dyn Stream<Item = anyhow::Result<Event<corev1::Pod>>> + Send>>;

Expand Down
Loading