/knative-rs

Rust implementation of Knative custom resource definitions and objects.

Primary LanguageRustApache License 2.0Apache-2.0

Knative

CI Crates.io

A Rust implementation of Knative and Knative Eventing custom resource defintions and objects, leveraging kube-rs.

This implementation is incomplete and should be considered pre-alpha. It contains only a small subset of the full specification.

Currently, you can use this crate to manage the status of a custom event source CustomResource in accordance with knative's expectations.

use schemars::JsonSchema;
use serde::{Serialize, Deserialize};
use kube::{Api, CustomResource, Resource, ResourceExt};
use kube::runtime::controller::{Action, Context};
use knative::source_types::{
    SourceSpec,
    SourceStatus,
    SourceCondition,
    SinkManager,
};
use std::sync::Arc;

#[derive(Serialize, Deserialize, CustomResource, Clone, Debug, JsonSchema)]
#[kube(group = "mysource.dev", version = "v1", kind = "MySource", status = "MySourceStatus", namespaced)]
struct MySourceSpec {
    #[serde(flatten)]
    source_spec: SourceSpec,
}

#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
struct MySourceStatus {
    #[serde(flatten)]
    source_status: SourceStatus<SourceCondition>,
}

#[derive(Clone)]
struct Data {
    client: kube::Client,
}

async fn reconcile(my_resource: Arc<MySource>, ctx: Context<Data>) -> Result<Action, kube::Error> {
    let client = ctx.get_ref().client.clone();
    let api = Api::<MySource>::namespaced(
        client.clone(),
        my_resource.namespace().as_ref().unwrap()
    );
    let mut resource = api.get_status(&my_resource.name()).await?;

    if let Some(ref mut status) = resource.status {
        status.source_status.mark_sink("http://hardcoded-sink".parse().unwrap());

        // ...set the K_SINK environment variable on the receive-adapter that this controller manages

        // ...patch the new status with the api
    }

    Ok(Action::requeue(std::time::Duration::from_secs(60 * 60)))
}

Additional reference usage of this crate is currently WIP!