Skip to main content

Context

Trait Context 

Source
pub trait Context {
    type Resource: Resource + Send + Sync + 'static;
    type Error: Error;

    const FINALIZER_NAME: Option<&'static str> = None;

    // Required method
    fn apply<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        client: Client,
        resource: &'life1 Self::Resource,
        metadata: &'life2 mut TraceMetadata,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Action>, Self::Error>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;

    // Provided methods
    fn cleanup<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        client: Client,
        resource: &'life1 Self::Resource,
        metadata: &'life2 mut TraceMetadata,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Action>, Self::Error>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn success_action(&self, resource: &Self::Resource) -> Action { ... }
    fn error_action(
        self: Arc<Self>,
        resource: Arc<Self::Resource>,
        err: &Error<Self::Error>,
        consecutive_errors: u32,
    ) -> Action { ... }
    fn failure_event(
        &self,
        resource: &Self::Resource,
        phase: Phase,
        err: &Error<Self::Error>,
    ) -> Option<Event> { ... }
}
Expand description

The Context trait should be implemented in order to provide callbacks for events that happen to resources watched by a Controller.

Provided Associated Constants§

Source

const FINALIZER_NAME: Option<&'static str> = None

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.

If this is None (the default), a finalizer will not be used, and cleanup events will not be reported.

Required Associated Types§

Source

type Resource: Resource + Send + Sync + 'static

The type of Kubernetes resource that will be watched by the Controller this context is passed to

Source

type Error: Error

The error type which will be returned by the apply and cleanup methods

Required Methods§

Source

fn apply<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, client: Client, resource: &'life1 Self::Resource, metadata: &'life2 mut TraceMetadata, ) -> Pin<Box<dyn Future<Output = Result<Option<Action>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

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 will be called to find the action to perform.

metadata is state for this reconciliation pass, which can be used to annotate its tracing span, time steps of the work for metrics, and publish events about the resource.

What this returns determines the Outcome reported to the controller’s observer, as described in Outcome::of_result.

Provided Methods§

Source

fn cleanup<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, client: Client, resource: &'life1 Self::Resource, metadata: &'life2 mut TraceMetadata, ) -> Pin<Box<dyn Future<Output = Result<Option<Action>, Self::Error>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

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 will be called to find the action to perform.

metadata and the return value are treated as for apply.

Note that this method will only be called if a finalizer is used.

Source

fn success_action(&self, resource: &Self::Resource) -> Action

This method is called when a call to apply or 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.

Source

fn error_action( self: Arc<Self>, resource: Arc<Self::Resource>, err: &Error<Self::Error>, consecutive_errors: u32, ) -> Action

This method is called when a call to apply or 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.

Source

fn failure_event( &self, resource: &Self::Resource, phase: Phase, err: &Error<Self::Error>, ) -> Option<Event>

This method is called when a reconciliation pass fails, if the controller has an event recorder, to determine the Kubernetes event to publish on the resource. Return None to publish nothing, for instance for errors that are an expected part of waiting on something else.

The default implementation publishes a Warning event with reason ReconcileFailed (or CleanupFailed, if the resource is being deleted), and the error and its causes as the note. Repeats of an identical event are aggregated, as described in the events module; this means that errors which include something that varies from one attempt to the next (such as a timestamp or request ID) create a new event per attempt, so should be rephrased here.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§