Skip to content

Commit 5aa8f83

Browse files
authored
Update json-patch to 2.0.0 (#1507)
Signed-off-by: song <[email protected]>
1 parent 7ee3298 commit 5aa8f83

File tree

7 files changed

+32
-12
lines changed

7 files changed

+32
-12
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ hyper-openssl = "0.10.2"
5757
hyper-rustls = { version = "0.27.0", default-features = false }
5858
hyper-socks2 = { version = "0.9.0", default-features = false }
5959
hyper-timeout = "0.5.1"
60-
json-patch = "1.0.0"
60+
json-patch = "2.0.0"
61+
jsonptr = "0.4.7"
6162
jsonpath-rust = "0.5.0"
6263
k8s-openapi = { version = "0.22.0", default-features = false }
6364
openssl = "0.10.36"

examples/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ garde = { version = "0.18.0", default-features = false, features = ["derive"] }
2828
anyhow.workspace = true
2929
futures = { workspace = true, features = ["async-await"] }
3030
jsonpath-rust.workspace = true
31+
jsonptr.workspace = true
3132
kube = { path = "../kube", version = "^0.91.0", default-features = false, features = ["admission"] }
3233
kube-derive = { path = "../kube-derive", version = "^0.91.0", default-features = false } # only needed to opt out of schema
3334
k8s-openapi.workspace = true

examples/admission_controller.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use jsonptr::Pointer;
12
use kube::core::{
23
admission::{AdmissionRequest, AdmissionResponse, AdmissionReview},
34
DynamicObject, Resource, ResourceExt,
45
};
5-
use std::{convert::Infallible, error::Error};
6+
7+
use kube::runtime::finalizer;
8+
use std::{convert::Infallible, error::Error, str::FromStr};
69
use tracing::*;
710
use warp::{reply, Filter, Reply};
811

@@ -75,13 +78,13 @@ fn mutate(res: AdmissionResponse, obj: &DynamicObject) -> Result<AdmissionRespon
7578
// Ensure labels exist before adding a key to it
7679
if obj.meta().labels.is_none() {
7780
patches.push(json_patch::PatchOperation::Add(json_patch::AddOperation {
78-
path: "/metadata/labels".into(),
81+
path: Pointer::new(["metadata", "labels"]),
7982
value: serde_json::json!({}),
8083
}));
8184
}
8285
// Add our label
8386
patches.push(json_patch::PatchOperation::Add(json_patch::AddOperation {
84-
path: "/metadata/labels/admission".into(),
87+
path: Pointer::new(["metadata", "labels", "admission"]),
8588
value: serde_json::Value::String("modified-by-admission-controller".into()),
8689
}));
8790
Ok(res.with_patch(json_patch::Patch(patches))?)

kube-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ k8s-openapi = { workspace = true, features = ["latest"] }
4141
assert-json-diff.workspace = true
4242
kube = { path = "../kube", version = "<1.0.0, >=0.53.0" }
4343
serde_yaml.workspace = true
44+
jsonptr.workspace = true

kube-core/src/admission.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,12 @@ pub enum Operation {
223223
/// .into_review();
224224
///
225225
/// use json_patch::{AddOperation, Patch, PatchOperation};
226+
/// use jsonptr::Pointer;
226227
///
227228
/// // A response adding a label to the resource.
228229
/// let _: AdmissionReview<_> = AdmissionResponse::from(&req)
229230
/// .with_patch(Patch(vec![PatchOperation::Add(AddOperation {
230-
/// path: "/metadata/labels/my-label".to_owned(),
231+
/// path: Pointer::new(["metadata","labels","my-label"]),
231232
/// value: serde_json::Value::String("my-value".to_owned()),
232233
/// })]))
233234
/// .unwrap()

kube-runtime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ tokio = { workspace = true, features = ["time"] }
4141
tokio-util = { workspace = true, features = ["time"] }
4242
tracing.workspace = true
4343
json-patch.workspace = true
44+
jsonptr.workspace = true
4445
serde_json.workspace = true
4546
thiserror.workspace = true
4647
backoff.workspace = true

kube-runtime/src/finalizer.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
use crate::controller::Action;
33
use futures::{TryFuture, TryFutureExt};
44
use json_patch::{AddOperation, PatchOperation, RemoveOperation, TestOperation};
5+
use jsonptr::Pointer;
56
use kube_client::{
67
api::{Patch, PatchParams},
78
Api, Resource, ResourceExt,
89
};
10+
911
use serde::{de::DeserializeOwned, Serialize};
10-
use std::{error::Error as StdError, fmt::Debug, sync::Arc};
12+
use std::{error::Error as StdError, fmt::Debug, str::FromStr, sync::Arc};
1113
use thiserror::Error;
1214

1315
#[derive(Debug, Error)]
@@ -25,6 +27,8 @@ where
2527
RemoveFinalizer(#[source] kube_client::Error),
2628
#[error("object has no name")]
2729
UnnamedObject,
30+
#[error("invalid finalizer")]
31+
InvalidFinalizer,
2832
}
2933

3034
struct FinalizerState {
@@ -138,10 +142,14 @@ where
138142
// `Test` ensures that we fail instead of deleting someone else's finalizer
139143
// (in which case a new `Cleanup` event will be sent)
140144
PatchOperation::Test(TestOperation {
141-
path: finalizer_path.clone(),
145+
path: Pointer::from_str(finalizer_path.as_str())
146+
.map_err(|_err| Error::InvalidFinalizer)?,
142147
value: finalizer_name.into(),
143148
}),
144-
PatchOperation::Remove(RemoveOperation { path: finalizer_path }),
149+
PatchOperation::Remove(RemoveOperation {
150+
path: Pointer::from_str(finalizer_path.as_str())
151+
.map_err(|_err| Error::InvalidFinalizer)?,
152+
}),
145153
])),
146154
)
147155
.await
@@ -156,11 +164,13 @@ where
156164
let patch = json_patch::Patch(if obj.finalizers().is_empty() {
157165
vec![
158166
PatchOperation::Test(TestOperation {
159-
path: "/metadata/finalizers".to_string(),
167+
path: Pointer::from_str("/metadata/finalizers")
168+
.map_err(|_err| Error::InvalidFinalizer)?,
160169
value: serde_json::Value::Null,
161170
}),
162171
PatchOperation::Add(AddOperation {
163-
path: "/metadata/finalizers".to_string(),
172+
path: Pointer::from_str("/metadata/finalizers")
173+
.map_err(|_err| Error::InvalidFinalizer)?,
164174
value: vec![finalizer_name].into(),
165175
}),
166176
]
@@ -170,11 +180,13 @@ where
170180
// https://github.com/kube-rs/kube/issues/964#issuecomment-1197311254),
171181
// so we need to fail and retry if anyone else has added the finalizer in the meantime
172182
PatchOperation::Test(TestOperation {
173-
path: "/metadata/finalizers".to_string(),
183+
path: Pointer::from_str("/metadata/finalizers")
184+
.map_err(|_err| Error::InvalidFinalizer)?,
174185
value: obj.finalizers().into(),
175186
}),
176187
PatchOperation::Add(AddOperation {
177-
path: "/metadata/finalizers/-".to_string(),
188+
path: Pointer::from_str("/metadata/finalizers/-")
189+
.map_err(|_err| Error::InvalidFinalizer)?,
178190
value: finalizer_name.into(),
179191
}),
180192
]

0 commit comments

Comments
 (0)