Skip to content

Commit

Permalink
refactor: remove useless Context
Browse files Browse the repository at this point in the history
Signed-off-by: iGxnon <[email protected]>
  • Loading branch information
iGxnon committed Sep 7, 2023
1 parent 8a7b9f1 commit db5fa90
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
23 changes: 4 additions & 19 deletions operator-k8s/src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@ use crate::consts::DEFAULT_REQUEUE_DURATION;
/// Cluster controller
pub(crate) mod cluster;

/// The common context
pub(crate) struct Context<C> {
/// The controller held by this context
controller: C,
}

impl<C> Context<C> {
/// Constructor
pub(crate) fn new(controller: C) -> Self {
Self { controller }
}
}

/// Metrics labeled
pub(crate) trait MetricsLabeled {
/// Label
Expand Down Expand Up @@ -75,26 +62,24 @@ where
fn handle_error(&self, resource: &Arc<R>, err: &Self::Error);

/// The reconcile function used in kube::runtime::Controller
async fn reconcile(resource: Arc<R>, ctx: Arc<Context<Self>>) -> Result<Action, Self::Error> {
let controller = &ctx.controller;
async fn reconcile(resource: Arc<R>, controller: Arc<Self>) -> Result<Action, Self::Error> {
let _timer = controller.metrics().record_duration();
controller.reconcile_once(&resource).await?;
Ok(Action::requeue(DEFAULT_REQUEUE_DURATION))
}

/// The on_error function used in kube::runtime::Controller
fn on_error(resource: Arc<R>, err: &Self::Error, ctx: Arc<Context<Self>>) -> Action {
let controller = &ctx.controller;
fn on_error(resource: Arc<R>, err: &Self::Error, controller: Arc<Self>) -> Action {
controller.metrics().record_failed_count(&err.labels());
controller.handle_error(&resource, err);
Action::requeue(DEFAULT_REQUEUE_DURATION)
}

/// Run this controller
async fn run(ctx: Arc<Context<Self>>, api: Api<R>) {
async fn run(controller: Arc<Self>, api: Api<R>) {
kube::runtime::Controller::new(api, WatcherConfig::default())
.shutdown_on_signal()
.run(Self::reconcile, Self::on_error, ctx)
.run(Self::reconcile, Self::on_error, controller)
.filter_map(|res| async move { res.ok() })
.for_each(|_| futures::future::ready(()))
.await;
Expand Down
8 changes: 4 additions & 4 deletions operator-k8s/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use utils::migration::ApiVersion;

use crate::config::{Config, Namespace};
use crate::controller::cluster::{ClusterMetrics, Controller as ClusterController};
use crate::controller::{Context, Controller, Metrics};
use crate::controller::{Controller, Metrics};
use crate::crd::Cluster;
use crate::router::{healthz, metrics, sidecar_state};
use crate::sidecar_state::SidecarState;
Expand Down Expand Up @@ -84,12 +84,12 @@ impl Operator {
let metrics = ClusterMetrics::new();
let registry = Registry::new();
metrics.register(&registry)?;
let ctx = Arc::new(Context::new(ClusterController {
let controller = Arc::new(ClusterController {
kube_client,
cluster_suffix: self.config.cluster_suffix.clone(),
metrics,
}));
let mut controller = ClusterController::run(ctx, cluster_api);
});
let mut controller = ClusterController::run(controller, cluster_api);

let web_server = self.web_server(status_tx, registry);

Expand Down

0 comments on commit db5fa90

Please sign in to comment.