Skip to content

Commit

Permalink
chore: Update jsonpath-rust to remove lazy_static and once_cell d…
Browse files Browse the repository at this point in the history
…ependencies, slight performance improvement (#1629)

* chore: Update jsonpath-rust to remove `lazy_static` and `once_cell` dependencies, slight performance improvement

Signed-off-by: Bryant Biggs <bryantbiggs@gmail.com>

* chore: Update example for jsoanpath changes

Signed-off-by: Bryant Biggs <bryantbiggs@gmail.com>

* fix: Extract value, not the json path

Signed-off-by: Bryant Biggs <bryantbiggs@gmail.com>

* fix: Return string value, not string encoded json value

Signed-off-by: Bryant Biggs <bryantbiggs@gmail.com>

---------

Signed-off-by: Bryant Biggs <bryantbiggs@gmail.com>
  • Loading branch information
bryantbiggs authored Nov 12, 2024
1 parent 4f1e889 commit b0538cb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async-broadcast = "0.7.0"
async-stream = "0.3.5"
async-trait = "0.1.64"
backoff = "0.4.0"
base64 = "0.22.0"
base64 = "0.22.1"
bytes = "1.1.0"
chrono = { version = "0.4.34", default-features = false }
darling = "0.20.3"
Expand All @@ -49,7 +49,7 @@ futures = { version = "0.3.17", default-features = false }
hashbrown = "0.15.0"
home = "0.5.4"
http = "1.1.0"
http-body = "1.0.0"
http-body = "1.0.1"
http-body-util = "0.1.2"
hyper = "1.2.0"
hyper-util = "0.1.9"
Expand All @@ -59,7 +59,7 @@ hyper-socks2 = { version = "0.9.0", default-features = false }
hyper-timeout = "0.5.1"
json-patch = "3"
jsonptr = "0.6"
jsonpath-rust = "0.5.0"
jsonpath-rust = "0.7.3"
k8s-openapi = { version = "0.23.0", default-features = false }
openssl = "0.10.36"
parking_lot = "0.12.0"
Expand All @@ -68,7 +68,7 @@ pin-project = "1.0.4"
proc-macro2 = "1.0.29"
quote = "1.0.10"
rand = "0.8.3"
rustls = { version = "0.23.0", default-features = false }
rustls = { version = "0.23.16", default-features = false }
rustls-pemfile = "2.0.0"
schemars = "0.8.6"
secrecy = "0.10.2"
Expand Down
8 changes: 4 additions & 4 deletions examples/dynamic_jsonpath.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{Context, Error};
use jsonpath_rust::JsonPathInst;
use jsonpath_rust::JsonPath;
use k8s_openapi::api::core::v1::Pod;
use kube::{
api::{Api, ListParams},
Expand All @@ -18,7 +18,7 @@ async fn main() -> anyhow::Result<()> {
let jsonpath = {
let path = std::env::var("JSONPATH").unwrap_or_else(|_| ".items[*].spec.containers[*].image".into());
format!("${path}")
.parse::<JsonPathInst>()
.parse::<JsonPath>()
.map_err(Error::msg)
.with_context(|| {
format!(
Expand All @@ -34,8 +34,8 @@ async fn main() -> anyhow::Result<()> {

// Use the given JSONPATH to filter the ObjectList
let list_json = serde_json::to_value(&list)?;
for res in jsonpath.find_slice(&list_json, Default::default()) {
info!("\t\t {}", *res);
for res in jsonpath.find_slice(&list_json) {
info!("\t\t {}", res.to_data());
}
Ok(())
}
2 changes: 1 addition & 1 deletion kube-client/src/api/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ enum Dirtiness {
New,
}

impl<'a, K> OccupiedEntry<'a, K> {
impl<K> OccupiedEntry<'_, K> {
/// Borrow the object
pub fn get(&self) -> &K {
&self.object
Expand Down
21 changes: 9 additions & 12 deletions kube-client/src/client/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use http::{
header::{InvalidHeaderValue, AUTHORIZATION},
HeaderValue, Request,
};
use jsonpath_rust::{path::config::JsonPathConfig, JsonPathInst};
use jsonpath_rust::JsonPath;
use secrecy::{ExposeSecret, SecretString};
use serde::{Deserialize, Serialize};
use thiserror::Error;
Expand Down Expand Up @@ -498,33 +498,30 @@ fn token_from_gcp_provider(provider: &AuthProviderConfig) -> Result<ProviderToke
}

fn extract_value(json: &serde_json::Value, context: &str, path: &str) -> Result<String, Error> {
let cfg = JsonPathConfig::default(); // no need for regex caching here
let parsed_path = path
.trim_matches(|c| c == '"' || c == '{' || c == '}')
.parse::<JsonPathInst>()
.parse::<JsonPath>()
.map_err(|err| {
Error::AuthExec(format!(
"Failed to parse {context:?} as a JsonPath: {path}\n
Error: {err}"
))
})?;

let res = parsed_path.find_slice(json, cfg);
let res = parsed_path.find_slice(json);

let Some(res) = res.into_iter().next() else {
return Err(Error::AuthExec(format!(
"Target {context:?} value {path:?} not found"
)));
};

if let Some(val) = res.as_str() {
Ok(val.to_owned())
} else {
Err(Error::AuthExec(format!(
"Target {:?} value {:?} is not a string: {:?}",
context, path, *res
)))
}
let jval = res.to_data();
let val = jval.as_str().ok_or(Error::AuthExec(format!(
"Target {context:?} value {path:?} is not a string"
)))?;

Ok(val.to_string())
}

/// ExecCredentials is used by exec-based plugins to communicate credentials to
Expand Down

0 comments on commit b0538cb

Please sign in to comment.