-
Notifications
You must be signed in to change notification settings - Fork 1
Generalize update handlers and add the ReadOnlyRepositoryWithFactory trait
#76
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
e843641
49d4188
f1c31fa
527f861
e478ddd
1ae136c
7598655
92b783d
9dc51f5
c266fd0
0e8d14a
e08b61d
6acaa1f
454a518
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| pub mod composed_update_handler; | ||
| pub mod logging_update_handler; | ||
|
|
||
| use async_trait::async_trait; | ||
| use kube::Resource; | ||
| use kube::runtime::watcher; | ||
|
|
||
| #[async_trait] | ||
| pub trait ResourceUpdateHandler<S>: Send + Sync | ||
| where | ||
| S: Resource + Send + Sync, | ||
| { | ||
| async fn handle_update(&self, result: &Result<S, watcher::Error>) -> (); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| use crate::services::backends::kubernetes::resource_update_handler::ResourceUpdateHandler; | ||
| use async_trait::async_trait; | ||
| use kube::Resource; | ||
| use std::fmt::Debug; | ||
|
|
||
| pub struct ComposedUpdateHandler<T> { | ||
| handlers: Vec<Box<dyn ResourceUpdateHandler<T>>>, | ||
| } | ||
|
|
||
| impl<T> ComposedUpdateHandler<T> { | ||
| pub fn new() -> Self { | ||
| Self { handlers: Vec::new() } | ||
| } | ||
|
|
||
| pub fn add_handler(mut self, handler: Box<dyn ResourceUpdateHandler<T>>) -> Self { | ||
| self.handlers.push(handler); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl<T> ResourceUpdateHandler<T> for ComposedUpdateHandler<T> | ||
| where | ||
| T: Resource + Debug + Send + Sync + 'static, | ||
| { | ||
| async fn handle_update(&self, result: &Result<T, kube::runtime::watcher::Error>) -> () { | ||
s-vitaliy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for handler in &self.handlers { | ||
| handler.handle_update(result).await; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,26 @@ pub trait ReadOnlyRepository<Key, Entity>: Send + Sync { | |
| async fn get(&self, key: Key) -> Result<Entity, Self::ReadError>; | ||
| } | ||
|
|
||
| #[async_trait] | ||
| pub trait ValueFactory<Key, Entity>: Send + Sync { | ||
| type CreateError; | ||
|
|
||
| async fn create(&self, key: &Key) -> Result<Entity, Self::CreateError>; | ||
| } | ||
|
|
||
| #[async_trait] | ||
| /// Represents a repository for policies | ||
| pub trait ReadOnlyRepositoryWithFactory<Key, Entity>: Send + Sync { | ||
|
||
| type ReadError; | ||
|
|
||
| /// Retrieves a policy by id | ||
| async fn get( | ||
|
||
| &self, | ||
| key: Key, | ||
| create_new: &dyn ValueFactory<Key, Entity, CreateError = Self::ReadError>, | ||
| ) -> Result<Entity, Self::ReadError>; | ||
| } | ||
|
|
||
| #[async_trait] | ||
| /// Represents a repository for policies | ||
| pub trait CanDelete<Key, Entity>: Send + Sync { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.