|
1 | 1 | //! Resource fetching utilities |
2 | 2 | //! |
3 | | -//! Provides functions for fetching Flux resource YAML from the Kubernetes API. |
| 3 | +//! Provides functions for fetching full Kubernetes resources from the API. |
4 | 4 |
|
5 | | -use crate::models::FluxResourceKind; |
6 | | -use crate::watcher::{ |
7 | | - Alert, ArtifactGenerator, Bucket, ExternalArtifact, FluxInstance, FluxReport, GitRepository, |
8 | | - HelmChart, HelmRelease, HelmRepository, ImagePolicy, ImageRepository, ImageUpdateAutomation, |
9 | | - Kustomization, OCIRepository, Provider, Receiver, ResourceSet, ResourceSetInputProvider, |
10 | | -}; |
| 5 | +use anyhow::Context; |
11 | 6 | use kube::Api; |
| 7 | +use kube::core::DynamicObject; |
12 | 8 |
|
13 | | -/// Fetch resource YAML from the Kubernetes API |
14 | | -pub async fn fetch_resource_yaml( |
| 9 | +use crate::kube::get_api_resource_with_fallback; |
| 10 | + |
| 11 | +/// Fetch a full resource object from the Kubernetes API using discovery-backed version fallback. |
| 12 | +pub async fn fetch_resource( |
15 | 13 | client: &kube::Client, |
16 | 14 | resource_type: &str, |
17 | 15 | namespace: &str, |
18 | 16 | name: &str, |
19 | 17 | ) -> anyhow::Result<serde_json::Value> { |
20 | | - // Match resource type and fetch using appropriate API |
21 | | - macro_rules! fetch_resource { |
22 | | - ($type:ty) => {{ |
23 | | - let api: Api<$type> = Api::namespaced(client.clone(), namespace); |
24 | | - match api.get(name).await { |
25 | | - Ok(obj) => { |
26 | | - return Ok(serde_json::to_value(&obj)?); |
27 | | - } |
28 | | - Err(e) => { |
29 | | - return Err(anyhow::anyhow!("Failed to fetch {}: {}", resource_type, e)); |
30 | | - } |
31 | | - } |
32 | | - }}; |
33 | | - } |
| 18 | + let api_resource = get_api_resource_with_fallback(client, resource_type, namespace, name) |
| 19 | + .await |
| 20 | + .with_context(|| { |
| 21 | + format!( |
| 22 | + "Failed to discover API resource for {}/{} in namespace {}", |
| 23 | + resource_type, name, namespace |
| 24 | + ) |
| 25 | + })?; |
| 26 | + let api: Api<DynamicObject> = Api::namespaced_with(client.clone(), namespace, &api_resource); |
| 27 | + let obj = api.get(name).await.with_context(|| { |
| 28 | + format!( |
| 29 | + "Failed to fetch {}/{} in namespace {}", |
| 30 | + resource_type, name, namespace |
| 31 | + ) |
| 32 | + })?; |
34 | 33 |
|
35 | | - match FluxResourceKind::parse_optional(resource_type) { |
36 | | - Some(FluxResourceKind::GitRepository) => fetch_resource!(GitRepository), |
37 | | - Some(FluxResourceKind::OCIRepository) => fetch_resource!(OCIRepository), |
38 | | - Some(FluxResourceKind::HelmRepository) => fetch_resource!(HelmRepository), |
39 | | - Some(FluxResourceKind::Bucket) => fetch_resource!(Bucket), |
40 | | - Some(FluxResourceKind::HelmChart) => fetch_resource!(HelmChart), |
41 | | - Some(FluxResourceKind::ExternalArtifact) => fetch_resource!(ExternalArtifact), |
42 | | - Some(FluxResourceKind::ArtifactGenerator) => fetch_resource!(ArtifactGenerator), |
43 | | - Some(FluxResourceKind::Kustomization) => fetch_resource!(Kustomization), |
44 | | - Some(FluxResourceKind::HelmRelease) => fetch_resource!(HelmRelease), |
45 | | - Some(FluxResourceKind::ImageRepository) => fetch_resource!(ImageRepository), |
46 | | - Some(FluxResourceKind::ImagePolicy) => fetch_resource!(ImagePolicy), |
47 | | - Some(FluxResourceKind::ImageUpdateAutomation) => fetch_resource!(ImageUpdateAutomation), |
48 | | - Some(FluxResourceKind::Alert) => fetch_resource!(Alert), |
49 | | - Some(FluxResourceKind::Provider) => fetch_resource!(Provider), |
50 | | - Some(FluxResourceKind::Receiver) => fetch_resource!(Receiver), |
51 | | - Some(FluxResourceKind::ResourceSet) => fetch_resource!(ResourceSet), |
52 | | - Some(FluxResourceKind::ResourceSetInputProvider) => { |
53 | | - fetch_resource!(ResourceSetInputProvider) |
54 | | - } |
55 | | - Some(FluxResourceKind::FluxReport) => fetch_resource!(FluxReport), |
56 | | - Some(FluxResourceKind::FluxInstance) => fetch_resource!(FluxInstance), |
57 | | - None => Err(anyhow::anyhow!("Unknown resource type: {}", resource_type)), |
58 | | - } |
| 34 | + serde_json::to_value(&obj).context("Failed to serialize fetched resource") |
| 35 | +} |
| 36 | + |
| 37 | +/// Fetch resource data for the YAML view. |
| 38 | +/// |
| 39 | +/// This remains as a compatibility wrapper around the generic resource fetch path. |
| 40 | +pub async fn fetch_resource_yaml( |
| 41 | + client: &kube::Client, |
| 42 | + resource_type: &str, |
| 43 | + namespace: &str, |
| 44 | + name: &str, |
| 45 | +) -> anyhow::Result<serde_json::Value> { |
| 46 | + fetch_resource(client, resource_type, namespace, name).await |
59 | 47 | } |
0 commit comments