Skip to content

Commit

Permalink
chore: pull json_patch_ext into its own crate
Browse files Browse the repository at this point in the history
  • Loading branch information
drmorr0 committed Nov 8, 2024
1 parent 951eb7e commit c196c0a
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 216 deletions.
49 changes: 42 additions & 7 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ derive_setters = "0.1.6"
dirs = "5.0.1"
either = "1.12.0"
futures = "0.3.28"
json-patch = "1.2.0"
# remove this when we bump kube-rs
json-patch = { version = "1.0.0" }
json-patch-ext = "0.1.0"
k8s-openapi = { version = "0.19.0", features = ["v1_27"] }
lazy_static = "1.5.0"
object_store = { version = "0.11.0", features = ["aws", "gcp", "azure", "http"] }
Expand Down
2 changes: 1 addition & 1 deletion sk-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ clap = { workspace = true }
clap_complete = { workspace = true }
derive_setters = { workspace = true }
dirs = { workspace = true }
json-patch = { workspace = true }
json-patch-ext = { workspace = true }
kube = { workspace = true }
k8s-openapi = { workspace = true }
lazy_static = { workspace = true }
Expand Down
1 change: 0 additions & 1 deletion sk-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ async-recursion = { workspace = true }
async-trait = { workspace = true }
bytes = { workspace = true }
clockabilly = { workspace = true }
json-patch = { workspace = true }
kube = { workspace = true }
k8s-openapi = { workspace = true }
object_store = { workspace = true }
Expand Down
File renamed without changes.
11 changes: 0 additions & 11 deletions sk-core/src/jsonutils/mod.rs

This file was deleted.

98 changes: 0 additions & 98 deletions sk-core/src/jsonutils/patch_ext.rs

This file was deleted.

5 changes: 0 additions & 5 deletions sk-core/src/jsonutils/tests/mod.rs

This file was deleted.

54 changes: 0 additions & 54 deletions sk-core/src/jsonutils/tests/patch_ext_test.rs

This file was deleted.

16 changes: 0 additions & 16 deletions sk-core/src/k8s/pod_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ use std::cmp::{
};

use clockabilly::Clockable;
use json_patch::{
AddOperation,
PatchOperation,
};
use kube::ResourceExt;
use serde_json::Value;

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

// A PodLifecycleData object is how we track the length of time a pod was running in a cluster. It
Expand Down Expand Up @@ -147,16 +141,6 @@ impl PodLifecycleData {
pub fn finished(&self) -> bool {
matches!(self, PodLifecycleData::Finished(..))
}

pub fn to_annotation_patch(&self) -> Option<PatchOperation> {
match self {
PodLifecycleData::Empty | PodLifecycleData::Running(_) => None,
PodLifecycleData::Finished(start_ts, end_ts) => Some(PatchOperation::Add(AddOperation {
path: format!("/metadata/annotations/{}", jsonutils::escape(LIFETIME_ANNOTATION_KEY)),
value: Value::String(format!("{}", end_ts - start_ts)),
})),
}
}
}

// We implement PartialOrd and PartialEq for PodLifecycleData; this is maybe a little bit magic,
Expand Down
1 change: 1 addition & 0 deletions sk-driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ either = { workspace = true }
kube = { workspace = true }
k8s-openapi = { workspace = true }
json-patch = { workspace = true }
json-patch-ext = { workspace = true }
rocket = { workspace = true }
serde_json = { workspace = true }
sk-api = { workspace = true }
Expand Down
16 changes: 14 additions & 2 deletions sk-driver/src/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use json_patch::{
Patch,
PatchOperation,
};
use json_patch_ext::escape;
use kube::core::admission::{
AdmissionRequest,
AdmissionResponse,
Expand All @@ -21,6 +22,7 @@ use sk_core::jsonutils;
use sk_core::k8s::{
KubeResourceExt,
PodExt,
PodLifecycleData,
};
use sk_core::prelude::*;

Expand Down Expand Up @@ -101,7 +103,7 @@ fn add_simulation_labels(ctx: &DriverContext, pod: &corev1::Pod, patches: &mut V
patches.push(PatchOperation::Add(AddOperation { path: "/metadata/labels".into(), value: json!({}) }));
}
patches.push(PatchOperation::Add(AddOperation {
path: format!("/metadata/labels/{}", jsonutils::escape(SIMULATION_LABEL_KEY)),
path: format!("/metadata/labels/{}", escape(SIMULATION_LABEL_KEY)),
value: Value::String(ctx.name.clone()),
}));

Expand All @@ -126,7 +128,7 @@ fn add_lifecycle_annotation(
let seq = mut_data.count(hash);

let lifecycle = ctx.store.lookup_pod_lifecycle(&owner_ns_name, hash, seq);
if let Some(patch) = lifecycle.to_annotation_patch() {
if let Some(patch) = to_annotation_patch(&lifecycle) {
info!("applying lifecycle annotations (hash={hash}, seq={seq})");
if pod.metadata.annotations.is_none() {
patches.push(PatchOperation::Add(AddOperation {
Expand Down Expand Up @@ -170,3 +172,13 @@ fn into_pod_review(resp: AdmissionResponse) -> AdmissionReview<corev1::Pod> {
response: Some(resp),
}
}

fn to_annotation_patch(pld: &PodLifecycleData) -> Option<PatchOperation> {
match pld {
PodLifecycleData::Empty | PodLifecycleData::Running(_) => None,
PodLifecycleData::Finished(start_ts, end_ts) => Some(PatchOperation::Add(AddOperation {
path: format!("/metadata/annotations/{}", escape(LIFETIME_ANNOTATION_KEY)),
value: Value::String(format!("{}", end_ts - start_ts)),
})),
}
}
Loading

0 comments on commit c196c0a

Please sign in to comment.