k8s_controller/controller.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
use std::collections::BTreeMap;
use std::error::Error;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use futures::future::FutureExt;
use futures::stream::StreamExt;
use kube::api::Api;
use kube::core::{ClusterResourceScope, NamespaceResourceScope};
use kube::{Client, Resource, ResourceExt};
use kube_runtime::controller::Action;
use kube_runtime::finalizer::{finalizer, Event};
use kube_runtime::watcher;
use rand::{thread_rng, Rng};
use tracing::{event, Level};
/// The [`Controller`] watches a set of resources, calling methods on the
/// provided [`Context`] when events occur.
pub struct Controller<Ctx: Context>
where
Ctx: Send + Sync + 'static,
Ctx::Error: Send + Sync + 'static,
Ctx::Resource: Send + Sync + 'static,
Ctx::Resource: Clone + std::fmt::Debug + serde::Serialize,
for<'de> Ctx::Resource: serde::Deserialize<'de>,
<Ctx::Resource as Resource>::DynamicType:
Eq + Clone + std::hash::Hash + std::default::Default + std::fmt::Debug + std::marker::Unpin,
{
client: kube::Client,
make_api: Box<dyn Fn(&Ctx::Resource) -> Api<Ctx::Resource> + Sync + Send + 'static>,
controller: kube_runtime::controller::Controller<Ctx::Resource>,
context: Ctx,
}
impl<Ctx: Context> Controller<Ctx>
where
Ctx: Send + Sync + 'static,
Ctx::Error: Send + Sync + 'static,
Ctx::Resource: Send + Sync + 'static,
Ctx::Resource: Clone + std::fmt::Debug + serde::Serialize,
for<'de> Ctx::Resource: serde::Deserialize<'de>,
<Ctx::Resource as Resource>::DynamicType:
Eq + Clone + std::hash::Hash + std::default::Default + std::fmt::Debug + std::marker::Unpin,
{
/// Creates a new controller for a namespaced resource using the given
/// `client`. The `context` given determines the type of resource
/// to watch (via the [`Context::Resource`] type provided as part of
/// the trait implementation). The resources to be watched will be
/// limited to resources in the given `namespace`. A [`watcher::Config`]
/// can be given to limit the resources watched (for instance,
/// `watcher::Config::default().labels("app=myapp")`).
pub fn namespaced(client: Client, context: Ctx, namespace: &str, wc: watcher::Config) -> Self
where
Ctx::Resource: Resource<Scope = NamespaceResourceScope>,
{
let make_api = {
let client = client.clone();
Box::new(move |resource: &Ctx::Resource| {
Api::<Ctx::Resource>::namespaced(client.clone(), &resource.namespace().unwrap())
})
};
let controller = kube_runtime::controller::Controller::new(
Api::<Ctx::Resource>::namespaced(client.clone(), namespace),
wc,
);
Self {
client,
make_api,
controller,
context,
}
}
/// Creates a new controller for a namespaced resource using the given
/// `client`. The `context` given determines the type of resource to
/// watch (via the [`Context::Resource`] type provided as part of the
/// trait implementation). The resources to be watched will not be
/// limited by namespace. A [`watcher::Config`] can be given to limit the
/// resources watched (for instance,
/// `watcher::Config::default().labels("app=myapp")`).
pub fn namespaced_all(client: Client, context: Ctx, wc: watcher::Config) -> Self
where
Ctx::Resource: Resource<Scope = NamespaceResourceScope>,
{
let make_api = {
let client = client.clone();
Box::new(move |resource: &Ctx::Resource| {
Api::<Ctx::Resource>::namespaced(client.clone(), &resource.namespace().unwrap())
})
};
let controller = kube_runtime::controller::Controller::new(
Api::<Ctx::Resource>::all(client.clone()),
wc,
);
Self {
client,
make_api,
controller,
context,
}
}
/// Creates a new controller for a cluster-scoped resource using the
/// given `client`. The `context` given determines the type of resource
/// to watch (via the [`Context::Resource`] type provided as part of the
/// trait implementation). A [`watcher::Config`] can be given to limit the
/// resources watched (for instance,
/// `watcher::Config::default().labels("app=myapp")`).
pub fn cluster(client: Client, context: Ctx, wc: watcher::Config) -> Self
where
Ctx::Resource: Resource<Scope = ClusterResourceScope>,
{
let make_api = {
let client = client.clone();
Box::new(move |_: &Ctx::Resource| Api::<Ctx::Resource>::all(client.clone()))
};
let controller = kube_runtime::controller::Controller::new(
Api::<Ctx::Resource>::all(client.clone()),
wc,
);
Self {
client,
make_api,
controller,
context,
}
}
/// Run the controller. This method will not return. The [`Context`]
/// given to the constructor will have its [`apply`](Context::apply)
/// method called when a resource is created or updated, and its
/// [`cleanup`](Context::cleanup) method called when a resource is about
/// to be deleted.
pub async fn run(self) {
let Self {
client,
make_api,
controller,
context,
} = self;
let backoffs = Arc::new(Mutex::new(BTreeMap::new()));
let backoffs = &backoffs;
controller
.run(
|resource, context| {
let uid = resource.uid().unwrap();
let backoffs = Arc::clone(backoffs);
context
._reconcile(client.clone(), make_api(&resource), resource)
.inspect(move |result| {
if result.is_ok() {
backoffs.lock().unwrap().remove(&uid);
}
})
},
|resource, err, context| {
let consecutive_errors = {
let uid = resource.uid().unwrap();
let mut backoffs = backoffs.lock().unwrap();
let consecutive_errors: u32 =
backoffs.get(&uid).copied().unwrap_or_default();
backoffs.insert(uid, consecutive_errors.saturating_add(1));
consecutive_errors
};
context.error_action(resource, err, consecutive_errors)
},
Arc::new(context),
)
.for_each(|reconciliation_result| async move {
let dynamic_type = Default::default();
let kind = Ctx::Resource::kind(&dynamic_type).into_owned();
match reconciliation_result {
Ok(resource) => {
event!(
Level::INFO,
resource_name = %resource.0.name,
controller = Ctx::FINALIZER_NAME,
"{} reconciliation successful.",
kind
);
}
Err(err) => event!(
Level::ERROR,
err = %err,
source = err.source(),
controller = Ctx::FINALIZER_NAME,
"{} reconciliation error.",
kind
),
}
})
.await
}
pub fn with_concurrency(mut self, concurrency: u16) -> Self {
self.controller = self
.controller
.with_config(kube_runtime::Config::default().concurrency(concurrency));
self
}
}
/// The [`Context`] trait should be implemented in order to provide callbacks
/// for events that happen to resources watched by a [`Controller`].
#[cfg_attr(not(docsrs), async_trait::async_trait)]
pub trait Context {
/// The type of Kubernetes [resource](Resource) that will be watched by
/// the [`Controller`] this context is passed to
type Resource: Resource;
/// The error type which will be returned by the [`apply`](Self::apply)
/// and [`cleanup`](Self::apply) methods
type Error: std::error::Error;
/// The name to use for the finalizer. This must be unique across
/// controllers - if multiple controllers with the same finalizer name
/// run against the same resource, unexpected behavior can occur.
const FINALIZER_NAME: &'static str;
/// This method is called when a watched resource is created or updated.
/// The [`Client`] used by the controller is passed in to allow making
/// additional API requests, as is the resource which triggered this
/// event. If this method returns `Some(action)`, the given action will
/// be performed, otherwise if `None` is returned,
/// [`success_action`](Self::success_action) will be called to find the
/// action to perform.
async fn apply(
&self,
client: Client,
resource: &Self::Resource,
) -> Result<Option<Action>, Self::Error>;
/// This method is called when a watched resource is marked for deletion.
/// The [`Client`] used by the controller is passed in to allow making
/// additional API requests, as is the resource which triggered this
/// event. If this method returns `Some(action)`, the given action will
/// be performed, otherwise if `None` is returned,
/// [`success_action`](Self::success_action) will be called to find the
/// action to perform.
async fn cleanup(
&self,
client: Client,
resource: &Self::Resource,
) -> Result<Option<Action>, Self::Error>;
/// This method is called when a call to [`apply`](Self::apply) or
/// [`cleanup`](Self::cleanup) returns `Ok(None)`. It should return the
/// default [`Action`] to perform. The default implementation will
/// requeue the event at a random time between 40 and 60 minutes in the
/// future.
fn success_action(&self, resource: &Self::Resource) -> Action {
// use a better name for the parameter name in the docs
let _resource = resource;
Action::requeue(Duration::from_secs(thread_rng().gen_range(2400..3600)))
}
/// This method is called when a call to [`apply`](Self::apply) or
/// [`cleanup`](Self::cleanup) returns `Err`. It should return the
/// default [`Action`] to perform. The error returned will be passed in
/// here, as well as a count of how many consecutive errors have happened
/// for this resource, to allow for an exponential backoff strategy. The
/// default implementation uses exponential backoff with a max of 256
/// seconds and some added randomization to avoid thundering herds.
fn error_action(
self: Arc<Self>,
resource: Arc<Self::Resource>,
err: &kube_runtime::finalizer::Error<Self::Error>,
consecutive_errors: u32,
) -> Action {
// use a better name for the parameter name in the docs
let _resource = resource;
let _err = err;
let seconds = 2u64.pow(consecutive_errors.min(7) + 1);
Action::requeue(Duration::from_millis(
thread_rng().gen_range((seconds * 500)..(seconds * 1000)),
))
}
#[doc(hidden)]
async fn _reconcile(
self: Arc<Self>,
client: Client,
api: Api<Self::Resource>,
resource: Arc<Self::Resource>,
) -> Result<Action, kube_runtime::finalizer::Error<Self::Error>>
where
Self: Send + Sync + 'static,
Self::Error: Send + Sync + 'static,
Self::Resource: Send + Sync + 'static,
Self::Resource: Clone + std::fmt::Debug + serde::Serialize,
for<'de> Self::Resource: serde::Deserialize<'de>,
<Self::Resource as Resource>::DynamicType: Eq
+ Clone
+ std::hash::Hash
+ std::default::Default
+ std::fmt::Debug
+ std::marker::Unpin,
{
let dynamic_type = Default::default();
let kind = Self::Resource::kind(&dynamic_type).into_owned();
let mut ran = false;
let res = finalizer(
&api,
Self::FINALIZER_NAME,
Arc::clone(&resource),
|event| async {
ran = true;
event!(
Level::INFO,
resource_name = %resource.name_unchecked().as_str(),
controller = Self::FINALIZER_NAME,
"Reconciling {} ({}).",
kind,
match event {
Event::Apply(_) => "apply",
Event::Cleanup(_) => "cleanup",
}
);
let action = match event {
Event::Apply(resource) => {
let action = self.apply(client, &resource).await?;
if let Some(action) = action {
action
} else {
self.success_action(&resource)
}
}
Event::Cleanup(resource) => self
.cleanup(client, &resource)
.await?
.unwrap_or_else(Action::await_change),
};
Ok(action)
},
)
.await;
if !ran {
event!(
Level::INFO,
resource_name = %resource.name_unchecked().as_str(),
controller = Self::FINALIZER_NAME,
"Reconciling {} ({}).",
kind,
if resource.meta().deletion_timestamp.is_some() {
"delete"
} else {
"init"
}
);
}
res
}
}