|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 | // Copyright Open Network Fabric Authors |
3 | 3 |
|
| 4 | +use chrono::{TimeZone, Utc}; |
| 5 | +use config::GenId; |
| 6 | +use config::converters::k8s::ToK8sConversionError; |
4 | 7 | use tokio::sync::mpsc::Sender; |
5 | 8 |
|
6 | | -use config::{ExternalConfig, GwConfig}; |
7 | | -use k8s_intf::client::WatchError; |
8 | | -use k8s_intf::watch_gateway_agent_crd; |
9 | | -use tracing::error; |
| 9 | +use config::converters::k8s::status::dataplane_status::DataplaneStatusForK8sConversion; |
| 10 | +use config::{ExternalConfig, GwConfig, internal::status::DataplaneStatus}; |
| 11 | +use k8s_intf::client::{PatchError, WatchError, patch_gateway_status, watch_gateway_agent_crd}; |
| 12 | +use k8s_intf::gateway_agent_crd::GatewayAgentStatus; |
| 13 | +use tracing::{debug, error}; |
10 | 14 |
|
11 | 15 | use crate::processor::proc::{ConfigChannelRequest, ConfigRequest, ConfigResponse}; |
12 | 16 |
|
13 | 17 | #[derive(Debug, thiserror::Error)] |
14 | 18 | pub enum K8sClientError { |
15 | 19 | #[error("K8s client exited early")] |
16 | 20 | EarlyTermination, |
17 | | - #[error("K8s client could not get hostname: {0}")] |
18 | | - HostnameError(#[from] std::io::Error), |
19 | 21 | #[error("K8s watch failed: {0}")] |
20 | 22 | WatchError(#[from] WatchError), |
| 23 | + #[error("Failed to convert dataplane status to k8s format: {0}")] |
| 24 | + StatusConversionError(#[from] ToK8sConversionError), |
| 25 | + #[error("Failed to patch k8s gateway status: {0}")] |
| 26 | + PatchStatusError(#[from] PatchError), |
21 | 27 | } |
22 | 28 |
|
23 | | -pub async fn k8s_start_client( |
24 | | - hostname: &str, |
25 | | - tx: Sender<ConfigChannelRequest>, |
26 | | -) -> Result<(), K8sClientError> { |
27 | | - watch_gateway_agent_crd(hostname, async move |ga| { |
28 | | - let external_config = ExternalConfig::try_from(ga); |
29 | | - match external_config { |
30 | | - Ok(external_config) => { |
31 | | - let gw_config = Box::new(GwConfig::new(external_config)); |
32 | | - |
33 | | - let (req, rx) = ConfigChannelRequest::new(ConfigRequest::ApplyConfig(gw_config)); |
34 | | - let tx_result = tx.send(req).await; |
35 | | - if let Err(e) = tx_result { |
36 | | - error!("Failure sending request to config processor: {e}"); |
37 | | - } |
38 | | - match rx.await { |
39 | | - Err(e) => error!("Failure receiving from config processor: {e}"), |
40 | | - Ok(response) => match response { |
41 | | - ConfigResponse::ApplyConfig(Err(e)) => { |
42 | | - error!("Failed to apply config: {e}"); |
| 29 | +async fn get_dataplane_status( |
| 30 | + tx: &Sender<ConfigChannelRequest>, |
| 31 | +) -> Result<DataplaneStatus, MgmtStatusError> { |
| 32 | + let (req, rx) = ConfigChannelRequest::new(ConfigRequest::GetDataplaneStatus); |
| 33 | + tx.send(req).await.map_err(|_| { |
| 34 | + MgmtStatusError::FetchStatusError("Failure relaying status fetch request".to_string()) |
| 35 | + })?; |
| 36 | + let response = rx.await.map_err(|_| { |
| 37 | + MgmtStatusError::FetchStatusError( |
| 38 | + "Failure receiving status from config processor".to_string(), |
| 39 | + ) |
| 40 | + })?; |
| 41 | + |
| 42 | + match response { |
| 43 | + ConfigResponse::GetDataplaneStatus(status) => Ok(*status), |
| 44 | + _ => unreachable!(), |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +async fn get_current_config_generation( |
| 49 | + tx: &Sender<ConfigChannelRequest>, |
| 50 | +) -> Result<GenId, MgmtStatusError> { |
| 51 | + let (req, rx) = ConfigChannelRequest::new(ConfigRequest::GetGeneration); |
| 52 | + tx.send(req).await.map_err(|_| { |
| 53 | + MgmtStatusError::FetchStatusError("Failure relaying get generation request".to_string()) |
| 54 | + })?; |
| 55 | + let response = rx.await.map_err(|_| { |
| 56 | + MgmtStatusError::FetchStatusError( |
| 57 | + "Failure receiving config generation from processor".to_string(), |
| 58 | + ) |
| 59 | + })?; |
| 60 | + match response { |
| 61 | + ConfigResponse::GetGeneration(opt_genid) => { |
| 62 | + opt_genid.ok_or(MgmtStatusError::NoConfigApplied) |
| 63 | + } |
| 64 | + _ => unreachable!(), |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[derive(Debug, thiserror::Error)] |
| 69 | +enum MgmtStatusError { |
| 70 | + #[error("Failed to fetch dataplane status: {0}")] |
| 71 | + FetchStatusError(String), |
| 72 | + #[error("No config is currently applied")] |
| 73 | + NoConfigApplied, |
| 74 | +} |
| 75 | + |
| 76 | +pub struct K8sClient { |
| 77 | + hostname: String, |
| 78 | +} |
| 79 | + |
| 80 | +impl K8sClient { |
| 81 | + pub fn new(hostname: &str) -> Self { |
| 82 | + Self { |
| 83 | + hostname: hostname.to_string(), |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + pub async fn init(&self) -> Result<(), K8sClientError> { |
| 88 | + // Reset the config generation and applied time in K8s |
| 89 | + patch_gateway_status( |
| 90 | + &self.hostname, |
| 91 | + &GatewayAgentStatus { |
| 92 | + agent_version: Some("(none: agentless)".to_string()), |
| 93 | + last_applied_gen: Some(0), |
| 94 | + last_applied_time: Some( |
| 95 | + Utc.timestamp_opt(0, 0) |
| 96 | + .unwrap() |
| 97 | + .to_rfc3339_opts(chrono::SecondsFormat::Nanos, true), |
| 98 | + ), |
| 99 | + state: None, |
| 100 | + }, |
| 101 | + ) |
| 102 | + .await?; |
| 103 | + Ok(()) |
| 104 | + } |
| 105 | + |
| 106 | + pub async fn k8s_start_config_watch( |
| 107 | + &self, |
| 108 | + tx: Sender<ConfigChannelRequest>, |
| 109 | + ) -> Result<(), K8sClientError> { |
| 110 | + // Clone this here so that the closure does not try to borrow self |
| 111 | + // and cause K8sClient to not be Send for 'static but only a specific |
| 112 | + // lifetime |
| 113 | + let hostname = self.hostname.clone(); |
| 114 | + watch_gateway_agent_crd(&hostname.clone(), async move |ga| { |
| 115 | + let external_config = ExternalConfig::try_from(ga); |
| 116 | + match external_config { |
| 117 | + Ok(external_config) => { |
| 118 | + let genid = external_config.genid; |
| 119 | + let current_genid = match get_current_config_generation(&tx).await { |
| 120 | + Ok(id) => id, |
| 121 | + Err(e) => match e { |
| 122 | + MgmtStatusError::NoConfigApplied => 0, |
| 123 | + _ => { |
| 124 | + error!("Failed to get current config generation: {e}"); |
| 125 | + return; |
| 126 | + } |
43 | 127 | } |
44 | | - ConfigResponse::ApplyConfig(Ok(())) => {} |
45 | | - _ => unreachable!(), |
46 | | - }, |
47 | | - }; |
48 | | - } |
49 | | - Err(e) => { |
50 | | - error!("Failed to convert K8sGatewayAgent to ExternalConfig: {e}"); |
| 128 | + }; |
| 129 | + if current_genid == genid { |
| 130 | + debug!("Not applying config, configuration generation unchanged (old={current_genid}, new={genid})"); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + let gw_config = Box::new(GwConfig::new(external_config)); |
| 135 | + |
| 136 | + let (req, rx) = |
| 137 | + ConfigChannelRequest::new(ConfigRequest::ApplyConfig(gw_config)); |
| 138 | + let tx_result = tx.send(req).await; |
| 139 | + if let Err(e) = tx_result { |
| 140 | + error!("Failure sending request to config processor: {e}"); |
| 141 | + } |
| 142 | + match rx.await { |
| 143 | + Err(e) => error!("Failure receiving from config processor: {e}"), |
| 144 | + Ok(response) => match response { |
| 145 | + ConfigResponse::ApplyConfig(Err(e)) => { |
| 146 | + error!("Failed to apply config: {e}"); |
| 147 | + } |
| 148 | + ConfigResponse::ApplyConfig(Ok(())) => { |
| 149 | + let last_applied_time = Some(chrono::Utc::now()); |
| 150 | + let k8s_status = match GatewayAgentStatus::try_from( |
| 151 | + &DataplaneStatusForK8sConversion { |
| 152 | + last_applied_gen: Some(genid), |
| 153 | + last_applied_time: last_applied_time.as_ref(), |
| 154 | + last_collected_time: None, |
| 155 | + status: None, |
| 156 | + }, |
| 157 | + ) { |
| 158 | + Ok(v) => Some(v), |
| 159 | + Err(e) => { error!("Unable to build object to patch k8s status with applied generation: {e}"); None } |
| 160 | + |
| 161 | + }; |
| 162 | + |
| 163 | + if let Some(k8s_status) = k8s_status { |
| 164 | + match patch_gateway_status(&hostname, &k8s_status).await { |
| 165 | + Ok(()) => {}, |
| 166 | + Err(e) => {error!("Unable to patch k8s last_applied_gen and timestamp: {e}"); } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + _ => unreachable!(), |
| 171 | + }, |
| 172 | + }; |
| 173 | + } |
| 174 | + Err(e) => { |
| 175 | + error!("Failed to convert K8sGatewayAgent to ExternalConfig: {e}"); |
| 176 | + } |
51 | 177 | } |
| 178 | + }) |
| 179 | + .await?; |
| 180 | + Err(K8sClientError::EarlyTermination) |
| 181 | + } |
| 182 | + |
| 183 | + pub async fn k8s_start_status_update( |
| 184 | + &self, |
| 185 | + tx: Sender<ConfigChannelRequest>, |
| 186 | + status_update_interval: &std::time::Duration, |
| 187 | + ) -> Result<(), K8sClientError> { |
| 188 | + // Clone this here so that the closure does not try to borrow self |
| 189 | + // and cause K8sClient to not be Send for 'static but only a specific |
| 190 | + // lifetime |
| 191 | + let hostname = self.hostname.clone(); |
| 192 | + loop { |
| 193 | + let status = get_dataplane_status(&tx).await; |
| 194 | + |
| 195 | + let status = match status { |
| 196 | + Ok(status) => status, |
| 197 | + Err(err) => { |
| 198 | + error!("Failed to fetch dataplane status: {}", err); |
| 199 | + continue; |
| 200 | + } |
| 201 | + }; |
| 202 | + |
| 203 | + let k8s_status = GatewayAgentStatus::try_from(&DataplaneStatusForK8sConversion { |
| 204 | + last_applied_gen: None, |
| 205 | + last_applied_time: None, |
| 206 | + last_collected_time: Some(&chrono::Utc::now()), |
| 207 | + status: Some(&status), |
| 208 | + })?; |
| 209 | + patch_gateway_status(&hostname, &k8s_status).await?; |
| 210 | + |
| 211 | + // Process status update |
| 212 | + tokio::time::sleep(*status_update_interval).await; |
52 | 213 | } |
53 | | - }) |
54 | | - .await?; |
55 | | - Err(K8sClientError::EarlyTermination) |
| 214 | + } |
56 | 215 | } |
0 commit comments