Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: resourcesync status #168

Merged
merged 6 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/.idea
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ clap = { version = "4.5", features = ["derive", "help", "env", "std"] }
futures = "0.3"
kube = { version = "0.87.2", features = ["runtime", "derive", "unstable-runtime"] }
kube-derive = "0.87.2"
k8s-openapi = { version = "0.20.0", features = ["v1_26"] }
k8s-openapi = { version = "0.20.0", features = ["v1_26", "schemars"] }
kubert = { version = "0.21.2", features = [
"clap",
"runtime",
Expand Down
37 changes: 33 additions & 4 deletions manifests/crd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,39 @@ spec:
status:
nullable: true
properties:
demo:
type: string
required:
- demo
conditions:
items:
description: Condition contains details for one aspect of the current state of this API Resource.
properties:
lastTransitionTime:
description: lastTransitionTime is the last time the condition transitioned from one status to another. This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: message is a human readable message indicating details about the transition. This may be an empty string.
type: string
observedGeneration:
description: observedGeneration represents the .metadata.generation that the condition was set based upon. For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date with respect to the current state of the instance.
format: int64
type: integer
reason:
description: reason contains a programmatic identifier indicating the reason for the condition's last transition. Producers of specific condition types may define expected values and meanings for this field, and whether the values are considered a guaranteed API. The value should be a CamelCase string. This field may not be empty.
type: string
status:
description: status of the condition, one of True, False, Unknown.
type: string
type:
description: type of condition in CamelCase or in foo.example.com/CamelCase.
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
nullable: true
type: array
type: object
required:
- spec
Expand Down
163 changes: 136 additions & 27 deletions src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{sync::Arc, time::Duration};

use futures::StreamExt;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::OwnerReference;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::{Condition, OwnerReference, Time};
use k8s_openapi::chrono::Utc;
use kube::api::DeleteParams;
use kube::api::Patch::Merge;
use kube::runtime::{predicates, reflector, WatchStreamExt};
Expand All @@ -14,6 +13,8 @@ use kube::{
Api, Client, Resource, ResourceExt,
};
use serde_json::json;
use std::string::ToString;
use std::{sync::Arc, time::Duration};
#[allow(unused_imports)]
use tracing::{debug, error, info, warn};

Expand All @@ -22,18 +23,27 @@ use util::{WithItemAdded, WithItemRemoved};
use crate::mapping::{apply_mappings, clone_resource};
use crate::remote_watcher_manager::RemoteWatcherManager;
use crate::resource_extensions::NamespacedApi;
use crate::resources::ResourceSyncStatus;
use crate::{requeue_after, resources::ResourceSync, util, Error, Result, FINALIZER};

static RESOURCE_SYNC_FAILING_CONDITION: &str = "ResourceSyncFailing";

pub struct Context {
pub client: Client,
pub remote_watcher_manager: RemoteWatcherManager,
}

macro_rules! apply_patch_params {
() => {
PatchParams::apply(&ResourceSync::group(&())).force()
};
}

async fn reconcile_deleted_resource(
resource_sync: Arc<ResourceSync>,
name: &str,
target_api: NamespacedApi,
parent_api: Api<ResourceSync>,
parent_api: &Api<ResourceSync>,
ctx: Arc<Context>,
) -> Result<Action> {
if !resource_sync.has_target_finalizer() {
Expand Down Expand Up @@ -88,7 +98,7 @@ async fn reconcile_deleted_resource(
async fn add_target_finalizer(
resource_sync: Arc<ResourceSync>,
name: &str,
parent_api: Api<ResourceSync>,
parent_api: &Api<ResourceSync>,
) -> Result<Action> {
let patched_finalizers = resource_sync
.finalizers_clone_or_empty()
Expand Down Expand Up @@ -161,7 +171,7 @@ async fn reconcile_normally(

debug!(?target, "produced target object");

let ssapply = PatchParams::apply(&ResourceSync::group(&())).force();
let ssapply = apply_patch_params!();
target_api
.patch(&target_ref.name, &ssapply, &Patch::Apply(&target))
.await?;
Expand All @@ -185,31 +195,85 @@ async fn reconcile(resource_sync: Arc<ResourceSync>, ctx: Arc<Context>) -> Resul
.name
.to_owned()
.ok_or(Error::NameRequired)?;
info!(?name, "running reconciler");
let parent_api = resource_sync.api(ctx.client.clone());

debug!(?resource_sync.spec, "got");
let local_ns = resource_sync.namespace().ok_or(Error::NamespaceRequired)?;
let result = {
let resource_sync = Arc::clone(&resource_sync);

let target_api = resource_sync
.spec
.target
.api_for(ctx.client.clone(), &local_ns)
.await?;
let source_api = resource_sync
.spec
.source
.api_for(ctx.client.clone(), &local_ns)
.await?;
let parent_api = resource_sync.api(ctx.client.clone());
info!(?name, "running reconciler");

match resource_sync {
resource_sync if resource_sync.has_been_deleted() => {
reconcile_deleted_resource(resource_sync, &name, target_api, parent_api, ctx).await
debug!(?resource_sync.spec, "got");
let local_ns = resource_sync.namespace().ok_or(Error::NamespaceRequired)?;

let target_api = resource_sync
.spec
.target
.api_for(ctx.client.clone(), &local_ns)
.await?;
let source_api = resource_sync
.spec
.source
.api_for(ctx.client.clone(), &local_ns)
.await?;

match resource_sync {
resource_sync if resource_sync.has_been_deleted() => {
reconcile_deleted_resource(resource_sync, &name, target_api, &parent_api, ctx).await
}
resource_sync if !resource_sync.has_target_finalizer() => {
add_target_finalizer(resource_sync, &name, &parent_api).await
}
_ => reconcile_normally(resource_sync, &name, source_api, target_api, ctx).await,
}
resource_sync if !resource_sync.has_target_finalizer() => {
add_target_finalizer(resource_sync, &name, parent_api).await
};

let status = match &result {
Err(err) => {
let sync_failing_condition = Condition {
last_transition_time: sync_failing_transition_time(&(resource_sync.status)),
message: err.to_string(),
observed_generation: resource_sync.metadata.generation,
reason: RESOURCE_SYNC_FAILING_CONDITION.to_string(),
status: "True".to_string(),
type_: RESOURCE_SYNC_FAILING_CONDITION.to_string(),
};

Some(ResourceSyncStatus {
conditions: Some(vec![sync_failing_condition]),
})
}
_ => reconcile_normally(resource_sync, &name, source_api, target_api, ctx).await,
_ => None,
};

if status != resource_sync.status {
parent_api
.patch_status(
&name,
&PatchParams::default(),
&Merge(json!({"status": status})),
)
.await?;
}

result
}

fn sync_failing_transition_time(status: &Option<ResourceSyncStatus>) -> Time {
let now = Time(Utc::now());

match status {
None => now,
Some(status) => match &status.conditions {
None => now,
Some(conditions) => {
let sync_failing_condition = conditions
.iter()
.find(|c| c.type_ == RESOURCE_SYNC_FAILING_CONDITION);
sync_failing_condition
.map(|c| c.last_transition_time.clone())
.unwrap_or(now)
}
},
}
}

Expand Down Expand Up @@ -257,4 +321,49 @@ pub async fn run(client: Client) -> Result<()> {
}

#[cfg(test)]
mod tests {}
mod tests {
use super::{sync_failing_transition_time, RESOURCE_SYNC_FAILING_CONDITION};
use crate::resources::ResourceSyncStatus;
use chrono::{TimeDelta, TimeZone};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::Condition;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::Time;
use once_cell::sync::Lazy;
use rstest::rstest;

static NOW: Lazy<Time> = Lazy::new(|| Time(chrono::Utc::now()));
static EPOCH: Lazy<Time> = Lazy::new(|| Time(chrono::Utc.timestamp_opt(0, 0).unwrap()));

#[rstest]
#[case::none(None, &NOW)]
#[case::no_conditions(Some(ResourceSyncStatus::default()), &NOW)]
#[case::empty_conditions(Some(ResourceSyncStatus{conditions: Some(vec![])}), &NOW)]
#[case::condition_already_present(
Some(
ResourceSyncStatus{
conditions: Some(
vec![
Condition{
last_transition_time: EPOCH.clone(),
type_: RESOURCE_SYNC_FAILING_CONDITION.to_string(),
message: "".to_string(),
reason: "".to_string(),
observed_generation: None,
status: "".to_string()
}
]
)
}
),
&NOW
)]
#[tokio::test]
async fn test_sync_failing_transition_time(
#[case] status: Option<ResourceSyncStatus>,
#[case] expected: &Time,
) {
let result = sync_failing_transition_time(&status);
let diff = result.0 - expected.0;

assert!(diff.le(&TimeDelta::minutes(1)))
}
}
5 changes: 3 additions & 2 deletions src/resources.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::{
CustomResourceDefinition, CustomResourceValidation, JSONSchemaProps,
};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::Condition;
use kube::{
core::{gvk::ParseGroupVersionError, GroupVersionKind, TypeMeta},
CustomResource,
Expand Down Expand Up @@ -33,10 +34,10 @@ pub struct Mapping {
pub to_field_path: Option<String>,
}

#[derive(Deserialize, Serialize, Clone, Debug, Default, JsonSchema)]
#[derive(Deserialize, Serialize, Clone, Debug, Default, JsonSchema, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ResourceSyncStatus {
pub demo: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops!

pub conditions: Option<Vec<Condition>>,
}

#[derive(Deserialize, Serialize, Clone, Debug, Default, JsonSchema, Hash, PartialEq, Eq)]
Expand Down
Loading