Skip to content

Commit 857379e

Browse files
committed
feat(k8s-less): augment k8s-less to write status to file
Augments the k8s-less client to write status in yaml and json each time a configuration is successfully applied. In this implementation, the state is serialized in two files: - gwstatus.json - gwstatus.yaml within a "state" directory created in the watched directory. This is to avoid needing to add another cmd line option for the directory to store the status files, which seems unnecessary at this point. Signed-off-by: Fredi Raspall <[email protected]>
1 parent 80542b6 commit 857379e

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

mgmt/src/processor/k8s_less_client.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,62 @@
44
use config::{ExternalConfig, GwConfig};
55
use futures::TryFutureExt;
66
use k8s_less::kubeless_watch_gateway_agent_crd;
7+
use std::path::PathBuf;
78
use std::sync::Arc;
9+
use tokio::fs::create_dir_all;
810
use tracing::{error, info};
911

12+
use crate::processor::k8s_client::build_gateway_status;
1013
use crate::processor::mgmt_client::{ConfigClient, ConfigProcessorError};
14+
use k8s_intf::utils::save;
1115

1216
#[derive(Debug, thiserror::Error)]
1317
pub enum K8sLessError {
1418
#[error("K8sless exited early")]
1519
EarlyTermination,
1620
#[error("Watching error: {0}")]
1721
WatchError(String),
22+
#[error("Internal error: {0}")]
23+
Internal(String),
1824
}
1925

2026
pub struct K8sLess {
2127
pathdir: String,
28+
statedir: String,
2229
client: ConfigClient,
2330
}
2431

2532
impl K8sLess {
2633
pub fn new(pathdir: &str, client: ConfigClient) -> Self {
2734
Self {
2835
pathdir: pathdir.to_string(),
36+
statedir: pathdir.to_string() + "/state",
2937
client,
3038
}
3139
}
3240

41+
async fn update_gateway_status(&self) {
42+
let Some(k8s_status) = build_gateway_status(&self.client).await else {
43+
return;
44+
};
45+
let mut state_dir = PathBuf::from(&self.statedir);
46+
state_dir.push("gwstatus");
47+
48+
let state_file = state_dir.to_str().unwrap_or_else(|| unreachable!());
49+
if let Err(e) = save(state_file, &k8s_status) {
50+
error!("Failed to save state: {e}");
51+
}
52+
}
53+
3354
pub async fn start_config_watch(k8sless: Arc<Self>) -> Result<(), K8sLessError> {
55+
// create directory to store status updates
56+
create_dir_all(&k8sless.statedir).await.map_err(|e| {
57+
K8sLessError::Internal(format!(
58+
"Failed to create directory '{}': {e}",
59+
k8sless.statedir
60+
))
61+
})?;
62+
3463
info!("Starting config watcher for directory {}", k8sless.pathdir);
3564

3665
kubeless_watch_gateway_agent_crd(&k8sless.pathdir.clone(), async move |ga| {
@@ -55,7 +84,10 @@ impl K8sLess {
5584

5685
// request the config processor to apply the config and update status on success
5786
match k8sless.client.apply_config(gwconfig).await {
58-
Ok(()) => info!("Config for generation {genid} was successfully applied"),
87+
Ok(()) => {
88+
info!("Config for generation {genid} was successfully applied. Updating status...");
89+
k8sless.update_gateway_status().await;
90+
},
5991
Err(e) => error!("Failed to apply the config for generation {genid}: {e}"),
6092
}
6193
}

0 commit comments

Comments
 (0)