|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright Open Network Fabric Authors |
| 3 | + |
| 4 | +use futures::{StreamExt, TryStreamExt}; |
| 5 | +use kube::runtime::{WatchStreamExt, watcher}; |
| 6 | +use kube::{Api, Client}; |
| 7 | + |
| 8 | +use tracectl::trace_target; |
| 9 | +use tracing::{error, info}; |
| 10 | + |
| 11 | +use crate::gateway_agent_crd::GatewayAgent; |
| 12 | + |
| 13 | +trace_target!("k8s-client", LevelFilter::INFO, &["management"]); |
| 14 | + |
| 15 | +#[derive(Debug, thiserror::Error)] |
| 16 | +pub enum WatchError { |
| 17 | + #[error("Client error: {0}")] |
| 18 | + ClientError(#[from] kube::Error), |
| 19 | + #[error("Watcher error: {0}")] |
| 20 | + WatcherError(#[from] kube::runtime::watcher::Error), |
| 21 | +} |
| 22 | + |
| 23 | +/// Watch `GatewayAgent` CRD and call callback for all changes |
| 24 | +/// |
| 25 | +/// # Errors |
| 26 | +/// Returns an error if the watch fails to start |
| 27 | +pub async fn watch_gateway_agent_crd( |
| 28 | + gateway_object_name: &str, |
| 29 | + callback: impl AsyncFn(&GatewayAgent), |
| 30 | +) -> Result<(), WatchError> { |
| 31 | + let client = Client::try_default().await?; |
| 32 | + // Relevant gateway agent objects are in the "fab" namespace |
| 33 | + let gws: Api<GatewayAgent> = Api::namespaced(client.clone(), "fab"); |
| 34 | + |
| 35 | + info!("Starting K8s GatewayAgent watcher..."); |
| 36 | + |
| 37 | + let watch_config = watcher::Config { |
| 38 | + // The service account for this gateway only has access to its corresponding |
| 39 | + // gateway agent object, so specifically filter for that to avoid an auth error |
| 40 | + // and to not apply incorrect configurations intended for other gateways |
| 41 | + field_selector: Some(format!("metadata.name={gateway_object_name}")), |
| 42 | + // The default initial list strategy attempts to list all gateway objects via the k8s |
| 43 | + // api and then filters them locally. But, the service account for this gateway does |
| 44 | + // not have permission to list all gateway objects. Instead, we use the streaming list |
| 45 | + // initial list strategy which directly calls the k8s watch api with the appropriate |
| 46 | + // watch config that includes the field selector. |
| 47 | + initial_list_strategy: watcher::InitialListStrategy::StreamingList, |
| 48 | + ..Default::default() |
| 49 | + }; |
| 50 | + let mut stream = watcher(gws, watch_config) |
| 51 | + .default_backoff() |
| 52 | + .applied_objects() |
| 53 | + .boxed(); |
| 54 | + |
| 55 | + loop { |
| 56 | + match stream.try_next().await { |
| 57 | + Ok(Some(ga)) => callback(&ga).await, |
| 58 | + Ok(None) => {} |
| 59 | + // Should we check for retriable vs non-retriable errors here? |
| 60 | + Err(err) => { |
| 61 | + error!("Watcher error: {err}"); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments