Skip to content

Commit

Permalink
Update json-patch to 2.0.0
Browse files Browse the repository at this point in the history
Signed-off-by: song <tinysong1226@gmail.com>
  • Loading branch information
bobsongplus committed Jun 3, 2024
1 parent cdab287 commit 5ec957e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ hyper-openssl = "0.10.2"
hyper-rustls = { version = "0.27.0", default-features = false }
hyper-socks2 = { version = "0.9.0", default-features = false }
hyper-timeout = "0.5.1"
json-patch = "1.0.0"
json-patch = "2.0.0"
jsonptr = "0.4.7"
jsonpath-rust = "0.5.0"
k8s-openapi = { version = "0.22.0", default-features = false }
openssl = "0.10.36"
Expand Down
1 change: 1 addition & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ garde = { version = "0.18.0", default-features = false, features = ["derive"] }
anyhow.workspace = true
futures = { workspace = true, features = ["async-await"] }
jsonpath-rust.workspace = true
jsonptr.workspace = true
kube = { path = "../kube", version = "^0.91.0", default-features = false, features = ["admission"] }
kube-derive = { path = "../kube-derive", version = "^0.91.0", default-features = false } # only needed to opt out of schema
k8s-openapi.workspace = true
Expand Down
7 changes: 4 additions & 3 deletions examples/admission_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use kube::core::{
admission::{AdmissionRequest, AdmissionResponse, AdmissionReview},
DynamicObject, Resource, ResourceExt,
};
use std::{convert::Infallible, error::Error};
use jsonptr::Pointer;
use std::{convert::Infallible, error::Error, str::FromStr};
use tracing::*;
use warp::{reply, Filter, Reply};

Expand Down Expand Up @@ -75,13 +76,13 @@ fn mutate(res: AdmissionResponse, obj: &DynamicObject) -> Result<AdmissionRespon
// Ensure labels exist before adding a key to it
if obj.meta().labels.is_none() {
patches.push(json_patch::PatchOperation::Add(json_patch::AddOperation {
path: "/metadata/labels".into(),
path: Pointer::from_str("/metadata/labels")..map_err(|_err|Error::InvalidFinalizer)?,
value: serde_json::json!({}),
}));
}
// Add our label
patches.push(json_patch::PatchOperation::Add(json_patch::AddOperation {
path: "/metadata/labels/admission".into(),
path: Pointer::from_str("/metadata/labels/admission")..map_err(|_err|Error::InvalidFinalizer)?,
value: serde_json::Value::String("modified-by-admission-controller".into()),
}));
Ok(res.with_patch(json_patch::Patch(patches))?)
Expand Down
1 change: 1 addition & 0 deletions kube-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tokio = { workspace = true, features = ["time"] }
tokio-util = { workspace = true, features = ["time"] }
tracing.workspace = true
json-patch.workspace = true
jsonptr.workspace = true
serde_json.workspace = true
thiserror.workspace = true
backoff.workspace = true
Expand Down
29 changes: 21 additions & 8 deletions kube-runtime/src/finalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
use crate::controller::Action;
use futures::{TryFuture, TryFutureExt};
use json_patch::{AddOperation, PatchOperation, RemoveOperation, TestOperation};
use jsonptr::Pointer;
use kube_client::{
api::{Patch, PatchParams},
Api, Resource, ResourceExt,
};

use serde::{de::DeserializeOwned, Serialize};
use std::{error::Error as StdError, fmt::Debug, sync::Arc};
use std::{error::Error as StdError, fmt::Debug, str::FromStr, sync::Arc};
use thiserror::Error;

#[derive(Debug, Error)]
Expand All @@ -25,6 +27,9 @@ where
RemoveFinalizer(#[source] kube_client::Error),
#[error("object has no name")]
UnnamedObject,
#[error("invalid finalizer")]
InvalidFinalizer,

}

struct FinalizerState {
Expand Down Expand Up @@ -89,6 +94,12 @@ impl FinalizerState {
///
/// `reconcile` should take the object that the [`Event`] contains, rather than trying to reuse `obj`, since it may have been updated.
///
/// # Panics
///
/// This function will panic if `Pointer::from_str(finalizer_path).unwrap()`
/// returns an `Err`. This can happen if the `finalizer_path` is not a valid
/// string for creating a `Pointer`.
///
/// # Errors
///
/// [`Event::Apply`] and [`Event::Cleanup`] are both fallible, their errors are passed through as [`Error::ApplyFailed`]
Expand Down Expand Up @@ -129,7 +140,7 @@ where
// Short-circuit, so that we keep the finalizer if cleanup fails
.map_err(Error::CleanupFailed)?;
// Cleanup was successful, remove the finalizer so that deletion can continue
let finalizer_path = format!("/metadata/finalizers/{finalizer_i}");
let finalizer_path: &str = "/metadata/finalizers/{finalizer_i}";
api.patch::<K>(
&name,
&PatchParams::default(),
Expand All @@ -138,10 +149,12 @@ where
// `Test` ensures that we fail instead of deleting someone else's finalizer
// (in which case a new `Cleanup` event will be sent)
PatchOperation::Test(TestOperation {
path: finalizer_path.clone(),
path: Pointer::from_str(finalizer_path).map_err(|_err|Error::InvalidFinalizer)?,
value: finalizer_name.into(),
}),
PatchOperation::Remove(RemoveOperation { path: finalizer_path }),
PatchOperation::Remove(RemoveOperation {
path: Pointer::from_str(finalizer_path).map_err(|_err|Error::InvalidFinalizer)?,
}),
])),
)
.await
Expand All @@ -156,11 +169,11 @@ where
let patch = json_patch::Patch(if obj.finalizers().is_empty() {
vec![
PatchOperation::Test(TestOperation {
path: "/metadata/finalizers".to_string(),
path: Pointer::from_str("/metadata/finalizers").map_err(|_err|Error::InvalidFinalizer)?,
value: serde_json::Value::Null,
}),
PatchOperation::Add(AddOperation {
path: "/metadata/finalizers".to_string(),
path: Pointer::from_str("/metadata/finalizers").map_err(|_err|Error::InvalidFinalizer)?,
value: vec![finalizer_name].into(),
}),
]
Expand All @@ -170,11 +183,11 @@ where
// https://github.com/kube-rs/kube/issues/964#issuecomment-1197311254),
// so we need to fail and retry if anyone else has added the finalizer in the meantime
PatchOperation::Test(TestOperation {
path: "/metadata/finalizers".to_string(),
path: Pointer::from_str("/metadata/finalizers").map_err(|_err|Error::InvalidFinalizer)?,
value: obj.finalizers().into(),
}),
PatchOperation::Add(AddOperation {
path: "/metadata/finalizers/-".to_string(),
path: Pointer::from_str("/metadata/finalizers/-").map_err(|_err|Error::InvalidFinalizer)?,
value: finalizer_name.into(),
}),
]
Expand Down

0 comments on commit 5ec957e

Please sign in to comment.