Skip to content

Commit

Permalink
Add Serialize to ObjecList and add field-selector and jsonpath example (
Browse files Browse the repository at this point in the history
#760)

* Add the example to demonstrate the use of field-selector and jsonpath

Signed-off-by: ChinYing-Li <chinying.li@mail.utoronto.ca>

* Modify the dynamic_jsonpath example to have a more advanced jsonpath

Signed-off-by: ChinYing-Li <chinying.li@mail.utoronto.ca>

Co-authored-by: Eirik A <sszynrae@gmail.com>
  • Loading branch information
ChinYing-Li and clux committed Dec 18, 2021
1 parent 17ebf42 commit 155859b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ validator = { version = "0.14.0", features = ["derive"] }
anyhow = "1.0.44"
env_logger = "0.9.0"
futures = "0.3.17"
jsonpath_lib = "0.3.0"
kube = { path = "../kube", version = "^0.65.0", default-features = false, features = ["admission"] }
kube-derive = { path = "../kube-derive", version = "^0.65.0", default-features = false } # only needed to opt out of schema
k8s-openapi = { version = "0.13.1", default-features = false }
Expand Down Expand Up @@ -100,6 +101,10 @@ path = "deployment_reflector.rs"
name = "dynamic_api"
path = "dynamic_api.rs"

[[example]]
name = "dynamic_jsonpath"
path = "dynamic_jsonpath.rs"

[[example]]
name = "dynamic_pod"
path = "dynamic_pod.rs"
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ cargo run --example job_api
cargo run --example log_stream
cargo run --example pod_api
cargo run --example dynamic_api
cargo run --example dynamic_jsonpath
NAMESPACE=dev cargo run --example log_stream -- kafka-manager-7d4f4bd8dc-f6c44
```

Expand Down
32 changes: 32 additions & 0 deletions examples/dynamic_jsonpath.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use jsonpath_lib;
use k8s_openapi::api::core::v1::Pod;
use kube::{
api::{Api, ListParams},
Client,
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
std::env::set_var("RUST_LOG", "info,kube=debug");
env_logger::init();
let client = Client::try_default().await?;

// Equivalent to `kubectl get pods --all-namespace \
// -o jsonpath='{.items[*].spec.containers[*].image}'`
let field_selector = std::env::var("FIELD_SELECTOR").unwrap_or_default();
let jsonpath = format!(
"{}{}",
"$",
std::env::var("JSONPATH").unwrap_or_else(|_| ".items[*].spec.containers[*].image".into())
);

let pods: Api<Pod> = Api::<Pod>::all(client);
let list_params = ListParams::default().fields(&*field_selector);
let list = pods.list(&list_params).await?;

// Use the given JSONPATH to filter the ObjectList
let list_json = serde_json::to_value(&list)?;
let res = jsonpath_lib::select(&list_json, &*jsonpath).unwrap();
println!("\t\t {:?}", res);
Ok(())
}
2 changes: 1 addition & 1 deletion kube-core/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::borrow::Cow;
/// and is generally produced from list/watch/delete collection queries on an [`Resource`](super::Resource).
///
/// This is almost equivalent to [`k8s_openapi::List<T>`](k8s_openapi::List), but iterable.
#[derive(Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct ObjectList<T>
where
T: Clone,
Expand Down

0 comments on commit 155859b

Please sign in to comment.