{"id":11843,"date":"2025-09-14T10:25:48","date_gmt":"2025-09-14T10:25:48","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=11843"},"modified":"2025-09-14T13:21:29","modified_gmt":"2025-09-14T13:21:29","slug":"a-simple-rust-application-using-the-kube-client","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/a-simple-rust-application-using-the-kube-client\/","title":{"rendered":"A simple Rust application using the Kube client"},"content":{"rendered":"<p>Rust has a kubernetes client crate which allows us to write code against Kubernetes via the client (i.e. instead of calling out to kubectl itself).<\/p>\n<p>Create yourself a binary Rust application with the following dependencies<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;dependencies]\r\nk8s-openapi = { version = &quot;0.26.0&quot;, default-features = false, features = &#x5B;&quot;v1_32&quot;] }\r\nkube = { version = &quot;2.0.1&quot;, features = &#x5B;&quot;runtime&quot;, &quot;client&quot;] }\r\ntokio = { version = &quot;1.30&quot;, features = &#x5B;&quot;full&quot;] }\r\nanyhow = &quot;1.0&quot;\r\n<\/pre>\n<p>Note: Check the <em>k8s-openapi<\/em> features match you installed kubernetes, run <em>kubectl version<\/em> to check your server version and use this.<\/p>\n<p>This is very much a starter post, so I&#8217;m going to just change main.rs to simply instantiate a client and get all pods running across all namespaces<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nuse kube::{Api, Client};\r\nuse k8s_openapi::api::core::v1::Pod;\r\nuse kube::runtime::reflector::Lookup;\r\n\r\n#&#x5B;tokio::main]\r\nasync fn main() -&gt; anyhow::Result&lt;()&gt; {\r\n\r\n  let client = Client::try_default().await?;\r\n  let pods: Api&lt;Pod&gt; = Api::all(client);\r\n\r\n  let pod_list = pods.list(&amp;Default::default()).await?;\r\n\r\n  for p in pod_list {\r\n    println!(&quot;Pod name: {:?}&quot;, p.name().expect(&quot;Pod name missing&quot;));\r\n  }\r\n\r\n  Ok(())\r\n}\r\n<\/pre>\n<p>If you want to use the <em>default<\/em> namespace then change the line <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlet pods: Api&lt;Pod&gt; = Api::all(client);\r\n<\/pre>\n<p>to <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlet pods: Api&lt;Pod&gt; = Api::default_namespaced(client);\r\n<\/pre>\n<p>or if you want to get pods from a specific namespace use<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlet pods: Api&lt;Pod&gt; = Api::namespaced(client, &quot;mynamespace&quot;);\r\n<\/pre>\n<p>Note: Ofcourse you could use <em>&#8220;default&#8221;<\/em> for the default namespace or <em>&#8220;&#8221;<\/em> for all namespaces in place of <em>&#8220;mynamespace&#8221;<\/em>.<\/p>\n<p><strong>Code<\/strong><\/p>\n<p>Code is available on <a href=\"https:\/\/github.com\/putridparrot\/blog-projects\/tree\/master\/rust_getpods\" target=\"_blank\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rust has a kubernetes client crate which allows us to write code against Kubernetes via the client (i.e. instead of calling out to kubectl itself). Create yourself a binary Rust application with the following dependencies &#x5B;dependencies] k8s-openapi = { version = &quot;0.26.0&quot;, default-features = false, features = &#x5B;&quot;v1_32&quot;] } kube = { version = &quot;2.0.1&quot;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[314,191],"tags":[],"class_list":["post-11843","post","type-post","status-publish","format-standard","hentry","category-kubernetes","category-rust"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=11843"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11843\/revisions"}],"predecessor-version":[{"id":11848,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11843\/revisions\/11848"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=11843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=11843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=11843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}