I'm just getting used to this library. One thing that confuses me is how to send a request using jsonpath or field-selector flags. I could not find enough information on either how to use it or it's already supported. (search)
How do I build an equivalent kube-rs request to this command using this library?
$ kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}'
$ kubectl get pods --field-selector status.phase=='Running'
For the --field-selector equivalent, you can set the field selector string in ListParams, although it's not something we have examples of atm, you should be able to just set the string to Some("status.phase=='Running'".to_string()).
You can also cross reference with kubectl -v=9 when you make the query and look for the curl line if you have difficulties constructing the ListParams (but know that the parameters you pass in will be url encoded by kube).
For -o jsonpath, that's a kubectl-ism that we don't have support for (because it's basically running something like jq to grab particular fields of the data after the query has been done). It's potentially something we could support because we already have a jsonpath_lib that can do querying. If there's interest into it, I am happy to discuss what that feature could look like more.
Thanks for the prompt reply and detailed explanation. I'd like to try this as soon as I have some free time.
...although it's not something we have examples of atm...
Also, I'd like to send a PR that introduces new ListParams examples, If I can achieve something successfully. I'll ping you here about the situation.
It's potentially something we could support because we already have a jsonpath_lib that can do querying.
Yep, that would be great.
If the desired path is known statically, it may be easier to use typed API, because you have no chance to use wrong field path:
let pods_api = Api::<Pod>::namespaced(client.clone(), ns);
let pods = pods.list().await?;
for pod in pods {
println!("{}", pod.name());
}
But kube also provides dynamic API that can be used to achieve something similar to kubectl get --field-selector ... -ojsonpath=.... This gist (based on dynamic_watcher example) shows how you can apply arbitrary JSON paths to arbitrary resources matching arbitrary field selector.
That looks great, and easy. Perhaps the main thing we ought to do on jsonpath is simply document DynamicObject with an example on how to do jsonpath_lib queries on the resulting serde_json::Value.
Most helpful comment
If the desired path is known statically, it may be easier to use typed API, because you have no chance to use wrong field path:
But
kubealso provides dynamic API that can be used to achieve something similar tokubectl get --field-selector ... -ojsonpath=.... This gist (based ondynamic_watcherexample) shows how you can apply arbitrary JSON paths to arbitrary resources matching arbitrary field selector.