feat: K8s apply function now correctly emulates kubectl apply behavior by either creating or updating resources #55

Merged
johnride merged 2 commits from feat/k8sApply into master 2025-06-09 20:19:57 +00:00
2 changed files with 17 additions and 9 deletions
Showing only changes of commit 703fc5c216 - Show all commits

View File

@ -4,7 +4,7 @@ use harmony::{
maestro::Maestro,
modules::{
lamp::{LAMPConfig, LAMPScore},
monitoring::monitoring_alerting::{AlertChannel, MonitoringAlertingStackScore},
monitoring::monitoring_alerting::MonitoringAlertingStackScore,
},
topology::{K8sAnywhereTopology, Url},
};
@ -43,9 +43,6 @@ async fn main() {
.await
.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();
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 kube::{
Api, Client, Config, Error, Resource,
api::PostParams,
api::{Patch, PatchParams, PostParams},
config::{KubeConfigOptions, Kubeconfig},
};
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
K: Resource + Clone + std::fmt::Debug + DeserializeOwned + serde::Serialize,
<K as Resource>::Scope: ApplyStrategy<K>,
@ -29,12 +32,20 @@ impl K8sClient {
debug!(
"Applying resource {:?} with ns {:?}",
resource.meta().name,
ns
namespace
);
trace!("{:#?}", serde_json::to_string(resource));
let api: Api<K> = <<K as Resource>::Scope as ApplyStrategy<K>>::get_api(&self.client, ns);
api.create(&PostParams::default(), &resource).await
let api: Api<K> = <<K as Resource>::Scope as ApplyStrategy<K>>::get_api(&self.client, namespace);
// 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>