-
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Serialize to ObjecList and add field-selector and jsonpath example (
#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
1 parent
17ebf42
commit 155859b
Showing
4 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters