kube_client/discovery/mod.rs
1//! High-level utilities for runtime API discovery.
2
3use crate::{Client, Result};
4pub use kube_core::discovery::{ApiCapabilities, ApiResource, Scope, verbs};
5use kube_core::{DynamicObject, TypeMeta, gvk::GroupVersionKind};
6use std::collections::HashMap;
7mod apigroup;
8pub mod oneshot;
9pub use apigroup::ApiGroup;
10mod parse;
11
12// re-export one-shots
13pub use oneshot::{group, pinned_group, pinned_kind};
14
15/// How the Discovery client decides what api groups to scan
16enum DiscoveryMode {
17 /// Only allow explicitly listed apigroups
18 Allow(Vec<String>),
19 /// Allow all apigroups except the ones listed
20 Block(Vec<String>),
21}
22
23impl DiscoveryMode {
24 fn is_queryable(&self, group: &String) -> bool {
25 match &self {
26 Self::Allow(allowed) => allowed.contains(group),
27 Self::Block(blocked) => !blocked.contains(group),
28 }
29 }
30}
31
32/// A caching client for running API discovery against the Kubernetes API.
33///
34/// This simplifies the required querying and type matching, and stores the responses
35/// for each discovered api group and exposes helpers to access them.
36///
37/// The discovery process varies in complexity depending on:
38/// - how much you know about the kind(s) and group(s) you are interested in
39/// - how many groups you are interested in
40///
41/// Discovery can be performed on:
42/// - all api groups (default)
43/// - a subset of api groups (by setting Discovery::filter)
44///
45/// To make use of discovered apis, extract one or more [`ApiGroup`]s from it,
46/// or resolve a precise one using [`Discovery::resolve_gvk`](crate::discovery::Discovery::resolve_gvk).
47///
48/// If caching of results is __not required__, then a simpler [`oneshot`](crate::discovery::oneshot) discovery system can be used.
49///
50/// [`ApiGroup`]: crate::discovery::ApiGroup
51#[cfg_attr(docsrs, doc(cfg(feature = "client")))]
52pub struct Discovery {
53 client: Client,
54 groups: HashMap<String, ApiGroup>,
55 mode: DiscoveryMode,
56}
57
58/// Caching discovery interface
59///
60/// Builds an internal map of its cache
61impl Discovery {
62 /// Construct a caching api discovery client
63 #[must_use]
64 pub fn new(client: Client) -> Self {
65 let groups = HashMap::new();
66 let mode = DiscoveryMode::Block(vec![]);
67 Self { client, groups, mode }
68 }
69
70 /// Configure the discovery client to only look for the listed apigroups
71 #[must_use]
72 pub fn filter(mut self, allow: &[&str]) -> Self {
73 self.mode = DiscoveryMode::Allow(allow.iter().map(ToString::to_string).collect());
74 self
75 }
76
77 /// Configure the discovery client to look for all apigroups except the listed ones
78 #[must_use]
79 pub fn exclude(mut self, deny: &[&str]) -> Self {
80 self.mode = DiscoveryMode::Block(deny.iter().map(ToString::to_string).collect());
81 self
82 }
83
84 /// Runs or re-runs the configured discovery algorithm and updates/populates the cache
85 ///
86 /// The cache is empty cleared when this is started. By default, every api group found is checked,
87 /// causing `N+2` queries to the api server (where `N` is number of api groups).
88 ///
89 /// **Note**: Consider using [`Discovery::run_aggregated`] instead, which only requires
90 /// 2 API calls regardless of the number of groups (requires Kubernetes 1.26+).
91 ///
92 /// ```no_run
93 /// use kube::{Client, api::{Api, DynamicObject}, discovery::{Discovery, verbs, Scope}, ResourceExt};
94 /// #[tokio::main]
95 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
96 /// let client = Client::try_default().await?;
97 /// let discovery = Discovery::new(client.clone()).run().await?;
98 /// for group in discovery.groups() {
99 /// for (ar, caps) in group.recommended_resources() {
100 /// if !caps.supports_operation(verbs::LIST) {
101 /// continue;
102 /// }
103 /// let api: Api<DynamicObject> = Api::all_with(client.clone(), &ar);
104 /// // can now api.list() to emulate kubectl get all --all
105 /// for obj in api.list(&Default::default()).await? {
106 /// println!("{} {}: {}", ar.api_version, ar.kind, obj.name_any());
107 /// }
108 /// }
109 /// }
110 /// Ok(())
111 /// }
112 /// ```
113 /// See a bigger example in [examples/dynamic.api](https://github.com/kube-rs/kube/blob/main/examples/dynamic_api.rs)
114 pub async fn run(mut self) -> Result<Self> {
115 self.groups.clear();
116 let api_groups = self.client.list_api_groups().await?;
117 // query regular groups + crds under /apis
118 for g in api_groups.groups {
119 let key = g.name.clone();
120 if self.mode.is_queryable(&key) {
121 let apigroup = ApiGroup::query_apis(&self.client, g).await?;
122 self.groups.insert(key, apigroup);
123 }
124 }
125 // query core versions under /api
126 let corekey = ApiGroup::CORE_GROUP.to_string();
127 if self.mode.is_queryable(&corekey) {
128 let coreapis = self.client.list_core_api_versions().await?;
129 let apigroup = ApiGroup::query_core(&self.client, coreapis).await?;
130 self.groups.insert(corekey, apigroup);
131 }
132 Ok(self)
133 }
134
135 /// Runs discovery using the Aggregated Discovery API
136 ///
137 /// This method uses the Aggregated Discovery API (available since Kubernetes 1.26, stable in 1.30)
138 /// to fetch all API resources in just 2 requests instead of N+2 requests.
139 ///
140 /// The Aggregated Discovery API provides all resource information in the response to `/api` and `/apis`
141 /// when the appropriate Accept header is set, eliminating the need to query each group individually.
142 ///
143 /// ```no_run
144 /// use kube::{Client, api::{Api, DynamicObject}, discovery::{Discovery, verbs, Scope}, ResourceExt};
145 /// #[tokio::main]
146 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
147 /// let client = Client::try_default().await?;
148 /// // Uses only 2 API calls instead of N+2
149 /// let discovery = Discovery::new(client.clone()).run_aggregated().await?;
150 /// for group in discovery.groups() {
151 /// for (ar, caps) in group.recommended_resources() {
152 /// if !caps.supports_operation(verbs::LIST) {
153 /// continue;
154 /// }
155 /// let api: Api<DynamicObject> = Api::all_with(client.clone(), &ar);
156 /// for obj in api.list(&Default::default()).await? {
157 /// println!("{} {}: {}", ar.api_version, ar.kind, obj.name_any());
158 /// }
159 /// }
160 /// }
161 /// Ok(())
162 /// }
163 /// ```
164 ///
165 /// # Requirements
166 /// - Kubernetes 1.26+ (beta) or 1.30+ (stable)
167 ///
168 /// # Note
169 /// If the server does not support Aggregated Discovery, this will return an error.
170 /// Consider falling back to [`Discovery::run`] in that case.
171 pub async fn run_aggregated(mut self) -> Result<Self> {
172 self.groups.clear();
173
174 // Query /apis for all non-core groups (single request)
175 let apis_discovery = self.client.list_api_groups_aggregated().await?;
176 for ag in apis_discovery.items {
177 let key = ag
178 .metadata
179 .as_ref()
180 .and_then(|m| m.name.clone())
181 .unwrap_or_default();
182 if self.mode.is_queryable(&key) {
183 let apigroup = ApiGroup::from_v2(ag)?;
184 self.groups.insert(key, apigroup);
185 }
186 }
187
188 // Query /api for core group (single request)
189 let corekey = ApiGroup::CORE_GROUP.to_string();
190 if self.mode.is_queryable(&corekey) {
191 let core_discovery = self.client.list_core_api_versions_aggregated().await?;
192 // Core group is the first (and usually only) item
193 if let Some(core_ag) = core_discovery.items.into_iter().next() {
194 let apigroup = ApiGroup::from_v2(core_ag)?;
195 self.groups.insert(corekey, apigroup);
196 }
197 }
198
199 Ok(self)
200 }
201}
202
203/// Interface to the Discovery cache
204impl Discovery {
205 /// Returns iterator over all served groups
206 pub fn groups(&self) -> impl Iterator<Item = &ApiGroup> {
207 self.groups.values()
208 }
209
210 /// Returns a sorted vector of all served groups
211 ///
212 /// This vector is in kubectl's normal alphabetical group order
213 pub fn groups_alphabetical(&self) -> Vec<&ApiGroup> {
214 let mut values: Vec<_> = self.groups().collect();
215 // collect to maintain kubectl order of groups
216 values.sort_by_key(|g| g.name());
217 values
218 }
219
220 /// Returns the [`ApiGroup`] for a given group if served
221 pub fn get(&self, group: &str) -> Option<&ApiGroup> {
222 self.groups.get(group)
223 }
224
225 /// Check if a group is served by the apiserver
226 pub fn has_group(&self, group: &str) -> bool {
227 self.groups.contains_key(group)
228 }
229
230 /// Finds an [`ApiResource`] and its [`ApiCapabilities`] after discovery by matching a GVK
231 ///
232 /// This is for quick extraction after having done a complete discovery.
233 /// If you are only interested in a single kind, consider [`oneshot::pinned_kind`](crate::discovery::pinned_kind).
234 pub fn resolve_gvk(&self, gvk: &GroupVersionKind) -> Option<(ApiResource, ApiCapabilities)> {
235 self.get(&gvk.group)?
236 .versioned_resources(&gvk.version)
237 .into_iter()
238 .find(|res| res.0.kind == gvk.kind)
239 }
240
241 /// Finds an [`ApiResource`] and its [`ApiCapabilities`] from a [`TypeMeta`] after discovery
242 ///
243 /// A convenience wrapper around [`Discovery::resolve_gvk`] for when you have a [`TypeMeta`]
244 /// (e.g. taken from a [`DynamicObject`]) rather than a [`GroupVersionKind`]. Returns `None` if
245 /// the `apiVersion` cannot be parsed or the kind was not discovered.
246 pub fn resolve_typemeta(&self, tm: &TypeMeta) -> Option<(ApiResource, ApiCapabilities)> {
247 let gvk = GroupVersionKind::try_from(tm).ok()?;
248 self.resolve_gvk(&gvk)
249 }
250
251 /// Finds an [`ApiResource`] and its [`ApiCapabilities`] for a [`DynamicObject`] after discovery
252 ///
253 /// This makes it easy to build an [`Api`](crate::Api) for an object obtained dynamically
254 /// (e.g. from an admission request or another watch) without manually assembling a
255 /// [`GroupVersionKind`]:
256 ///
257 /// ```no_run
258 /// use kube::{Client, api::{Api, DynamicObject}, discovery::Discovery};
259 /// #[tokio::main]
260 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
261 /// let client = Client::try_default().await?;
262 /// let object: DynamicObject = todo!("an object obtained at runtime");
263 /// let discovery = Discovery::new(client.clone()).run().await?;
264 /// if let Some((ar, _caps)) = discovery.resolve_object(&object) {
265 /// let api: Api<DynamicObject> = match object.metadata.namespace.as_deref() {
266 /// Some(ns) => Api::namespaced_with(client.clone(), ns, &ar),
267 /// None => Api::all_with(client.clone(), &ar),
268 /// };
269 /// // now `api` can be used to interact with the object
270 /// let _ = api;
271 /// }
272 /// Ok(())
273 /// }
274 /// ```
275 ///
276 /// Returns `None` if the object has no [`TypeMeta`], its `apiVersion` cannot be parsed, or the
277 /// kind was not discovered.
278 pub fn resolve_object(&self, obj: &DynamicObject) -> Option<(ApiResource, ApiCapabilities)> {
279 self.resolve_typemeta(obj.types.as_ref()?)
280 }
281}
282
283#[cfg(test)]
284mod tests {
285 use super::*;
286 use crate::client::Body;
287 use http::{Request, Response};
288 use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
289 use kube_core::{
290 DynamicObject, TypeMeta,
291 discovery::v2::{
292 APIGroupDiscovery, APIResourceDiscovery, APIVersionDiscovery, GroupVersionKind as V2Gvk,
293 },
294 };
295 use tower_test::mock;
296
297 // Build a Discovery cache serving a single apps/v1 Deployment, with a dummy client
298 // (resolve_typemeta / resolve_object never touch the client).
299 fn test_discovery() -> Discovery {
300 let ag = APIGroupDiscovery {
301 metadata: Some(ObjectMeta {
302 name: Some("apps".to_string()),
303 ..Default::default()
304 }),
305 versions: vec![APIVersionDiscovery {
306 version: Some("v1".to_string()),
307 resources: vec![APIResourceDiscovery {
308 resource: Some("deployments".to_string()),
309 response_kind: Some(V2Gvk {
310 group: None,
311 version: None,
312 kind: Some("Deployment".to_string()),
313 }),
314 scope: Some("Namespaced".to_string()),
315 verbs: vec!["get".to_string(), "list".to_string()],
316 ..Default::default()
317 }],
318 freshness: Some("Current".to_string()),
319 }],
320 };
321 let mut groups = HashMap::new();
322 groups.insert("apps".to_string(), ApiGroup::from_v2(ag).unwrap());
323 let (mock_service, _handle) = mock::pair::<Request<Body>, Response<Body>>();
324 let client = Client::new(mock_service, "default");
325 Discovery {
326 client,
327 groups,
328 mode: DiscoveryMode::Block(vec![]),
329 }
330 }
331
332 #[tokio::test]
333 async fn resolve_typemeta_finds_resource() {
334 let discovery = test_discovery();
335 let tm = TypeMeta {
336 api_version: "apps/v1".to_string(),
337 kind: "Deployment".to_string(),
338 };
339 let (ar, _caps) = discovery.resolve_typemeta(&tm).expect("known typemeta resolves");
340 assert_eq!(ar.group, "apps");
341 assert_eq!(ar.version, "v1");
342 assert_eq!(ar.kind, "Deployment");
343 }
344
345 #[tokio::test]
346 async fn resolve_object_finds_resource() {
347 let discovery = test_discovery();
348 let obj = DynamicObject {
349 types: Some(TypeMeta {
350 api_version: "apps/v1".to_string(),
351 kind: "Deployment".to_string(),
352 }),
353 metadata: Default::default(),
354 data: Default::default(),
355 };
356 let (ar, _caps) = discovery
357 .resolve_object(&obj)
358 .expect("object with types resolves");
359 assert_eq!(ar.kind, "Deployment");
360 assert_eq!(ar.api_version, "apps/v1");
361 }
362
363 #[tokio::test]
364 async fn resolve_object_without_typemeta_is_none() {
365 let discovery = test_discovery();
366 let obj = DynamicObject {
367 types: None,
368 metadata: Default::default(),
369 data: Default::default(),
370 };
371 assert!(discovery.resolve_object(&obj).is_none());
372 }
373
374 #[tokio::test]
375 async fn resolve_typemeta_unknown_kind_is_none() {
376 let discovery = test_discovery();
377 let tm = TypeMeta {
378 api_version: "apps/v1".to_string(),
379 kind: "DoesNotExist".to_string(),
380 };
381 assert!(discovery.resolve_typemeta(&tm).is_none());
382 }
383}