feat: K8s apply function now correctly emulates kubectl apply behavior by either creating or updating resources
Some checks failed
Run Check Script / check (push) Failing after 44s
Run Check Script / check (pull_request) Failing after 43s

This commit is contained in:
Jean-Gabriel Gill-Couture 2025-06-09 14:35:04 -04:00
parent bf7a6d590c
commit 703fc5c216
2 changed files with 17 additions and 9 deletions

View File

@ -4,7 +4,7 @@ use harmony::{
maestro::Maestro, maestro::Maestro,
modules::{ modules::{
lamp::{LAMPConfig, LAMPScore}, lamp::{LAMPConfig, LAMPScore},
monitoring::monitoring_alerting::{AlertChannel, MonitoringAlertingStackScore}, monitoring::monitoring_alerting::MonitoringAlertingStackScore,
}, },
topology::{K8sAnywhereTopology, Url}, topology::{K8sAnywhereTopology, Url},
}; };
@ -43,9 +43,6 @@ async fn main() {
.await .await
.unwrap(); .unwrap();
let url = url::Url::parse("https://discord.com/api/webhooks/dummy_channel/dummy_token")
.expect("invalid URL");
let mut monitoring_stack_score = MonitoringAlertingStackScore::new(); let mut monitoring_stack_score = MonitoringAlertingStackScore::new();
monitoring_stack_score.namespace = Some(lamp_stack.config.namespace.clone()); monitoring_stack_score.namespace = Some(lamp_stack.config.namespace.clone());

View File

@ -2,7 +2,7 @@ use derive_new::new;
use k8s_openapi::{ClusterResourceScope, NamespaceResourceScope}; use k8s_openapi::{ClusterResourceScope, NamespaceResourceScope};
use kube::{ use kube::{
Api, Client, Config, Error, Resource, Api, Client, Config, Error, Resource,
api::PostParams, api::{Patch, PatchParams, PostParams},
config::{KubeConfigOptions, Kubeconfig}, config::{KubeConfigOptions, Kubeconfig},
}; };
use log::{debug, error, trace}; use log::{debug, error, trace};
@ -20,7 +20,10 @@ impl K8sClient {
}) })
} }
pub async fn apply<K>(&self, resource: &K, ns: Option<&str>) -> Result<K, Error> /// Apply a resource in namespace
///
/// See `kubectl apply` for more information on the expected behavior of this function
pub async fn apply<K>(&self, resource: &K, namespace: Option<&str>) -> Result<K, Error>
where where
K: Resource + Clone + std::fmt::Debug + DeserializeOwned + serde::Serialize, K: Resource + Clone + std::fmt::Debug + DeserializeOwned + serde::Serialize,
<K as Resource>::Scope: ApplyStrategy<K>, <K as Resource>::Scope: ApplyStrategy<K>,
@ -29,12 +32,20 @@ impl K8sClient {
debug!( debug!(
"Applying resource {:?} with ns {:?}", "Applying resource {:?} with ns {:?}",
resource.meta().name, resource.meta().name,
ns namespace
); );
trace!("{:#?}", serde_json::to_string(resource)); trace!("{:#?}", serde_json::to_string(resource));
let api: Api<K> = <<K as Resource>::Scope as ApplyStrategy<K>>::get_api(&self.client, ns); let api: Api<K> = <<K as Resource>::Scope as ApplyStrategy<K>>::get_api(&self.client, namespace);
api.create(&PostParams::default(), &resource).await // api.create(&PostParams::default(), &resource).await
let patch_params = PatchParams::apply("harmony");
let name = resource
.meta()
.name
.as_ref()
.expect("K8s Resource should have a name");
api.patch(name, &patch_params, &Patch::Apply(resource))
.await
} }
pub async fn apply_many<K>(&self, resource: &Vec<K>, ns: Option<&str>) -> Result<Vec<K>, Error> pub async fn apply_many<K>(&self, resource: &Vec<K>, ns: Option<&str>) -> Result<Vec<K>, Error>