Skip to content

Commit 9f3e992

Browse files
committed
Migrate to DeserializeGuard
Part of stackabletech/issues#211
1 parent 2f634b8 commit 9f3e992

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

rust/operator-binary/src/controller.rs

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use stackable_operator::{
4242
DeepMerge,
4343
},
4444
kube::{
45+
core::{error_boundary, DeserializeGuard},
4546
runtime::{controller::Action, reflector::ObjectRef},
4647
Resource as KubeResource, ResourceExt,
4748
},
@@ -143,6 +144,11 @@ pub struct Ctx {
143144
#[strum_discriminants(derive(IntoStaticStr))]
144145
#[allow(clippy::enum_variant_names)]
145146
pub enum Error {
147+
#[snafu(display("OpaCluster object is invalid"))]
148+
InvalidOpaCluster {
149+
source: error_boundary::InvalidObject,
150+
},
151+
146152
#[snafu(display("object does not define meta name"))]
147153
NoName,
148154

@@ -349,9 +355,18 @@ pub struct OpaClusterConfigDecisionLog {
349355
console: bool,
350356
}
351357

352-
pub async fn reconcile_opa(opa: Arc<OpaCluster>, ctx: Arc<Ctx>) -> Result<Action> {
358+
pub async fn reconcile_opa(
359+
opa: Arc<DeserializeGuard<OpaCluster>>,
360+
ctx: Arc<Ctx>,
361+
) -> Result<Action> {
353362
tracing::info!("Starting reconcile");
354-
let opa_ref = ObjectRef::from_obj(opa.as_ref());
363+
let opa = opa
364+
.0
365+
.as_ref()
366+
.map_err(error_boundary::InvalidObject::clone)
367+
.context(InvalidOpaClusterSnafu)?;
368+
let opa_ref = ObjectRef::from_obj(opa);
369+
355370
let client = &ctx.client;
356371
let resolved_product_image = opa
357372
.spec
@@ -371,7 +386,7 @@ pub async fn reconcile_opa(opa: Arc<OpaCluster>, ctx: Arc<Ctx>) -> Result<Action
371386
let validated_config = validate_all_roles_and_groups_config(
372387
&resolved_product_image.product_version,
373388
&transform_all_roles_to_config(
374-
opa.as_ref(),
389+
opa,
375390
[(
376391
opa_role.to_string(),
377392
(
@@ -395,11 +410,11 @@ pub async fn reconcile_opa(opa: Arc<OpaCluster>, ctx: Arc<Ctx>) -> Result<Action
395410
.map(Cow::Borrowed)
396411
.unwrap_or_default();
397412

398-
let vector_aggregator_address = resolve_vector_aggregator_address(&opa, client)
413+
let vector_aggregator_address = resolve_vector_aggregator_address(opa, client)
399414
.await
400415
.context(ResolveVectorAggregatorAddressSnafu)?;
401416

402-
let server_role_service = build_server_role_service(&opa, &resolved_product_image)?;
417+
let server_role_service = build_server_role_service(opa, &resolved_product_image)?;
403418
// required for discovery config map later
404419
let server_role_service = cluster_resources
405420
.add(client, server_role_service)
@@ -410,8 +425,8 @@ pub async fn reconcile_opa(opa: Arc<OpaCluster>, ctx: Arc<Ctx>) -> Result<Action
410425
.get_required_labels()
411426
.context(BuildLabelSnafu)?;
412427

413-
let (rbac_sa, rbac_rolebinding) = build_rbac_resources(opa.as_ref(), APP_NAME, required_labels)
414-
.context(BuildRbacResourcesSnafu)?;
428+
let (rbac_sa, rbac_rolebinding) =
429+
build_rbac_resources(opa, APP_NAME, required_labels).context(BuildRbacResourcesSnafu)?;
415430

416431
let rbac_sa = cluster_resources
417432
.add(client, rbac_sa)
@@ -436,15 +451,15 @@ pub async fn reconcile_opa(opa: Arc<OpaCluster>, ctx: Arc<Ctx>) -> Result<Action
436451
.context(FailedToResolveConfigSnafu)?;
437452

438453
let rg_configmap = build_server_rolegroup_config_map(
439-
&opa,
454+
opa,
440455
&resolved_product_image,
441456
&rolegroup,
442457
&merged_config,
443458
vector_aggregator_address.as_deref(),
444459
)?;
445-
let rg_service = build_rolegroup_service(&opa, &resolved_product_image, &rolegroup)?;
460+
let rg_service = build_rolegroup_service(opa, &resolved_product_image, &rolegroup)?;
446461
let rg_daemonset = build_server_rolegroup_daemonset(
447-
&opa,
462+
opa,
448463
&resolved_product_image,
449464
&opa_role,
450465
&rolegroup,
@@ -498,13 +513,9 @@ pub async fn reconcile_opa(opa: Arc<OpaCluster>, ctx: Arc<Ctx>) -> Result<Action
498513
.context(ApplyPatchRoleGroupDaemonSetSnafu { rolegroup })?;
499514
}
500515

501-
for discovery_cm in build_discovery_configmaps(
502-
opa.as_ref(),
503-
opa.as_ref(),
504-
&resolved_product_image,
505-
&server_role_service,
506-
)
507-
.context(BuildDiscoveryConfigSnafu)?
516+
for discovery_cm in
517+
build_discovery_configmaps(opa, opa, &resolved_product_image, &server_role_service)
518+
.context(BuildDiscoveryConfigSnafu)?
508519
{
509520
cluster_resources
510521
.add(client, discovery_cm)
@@ -516,14 +527,11 @@ pub async fn reconcile_opa(opa: Arc<OpaCluster>, ctx: Arc<Ctx>) -> Result<Action
516527
ClusterOperationsConditionBuilder::new(&opa.spec.cluster_operation);
517528

518529
let status = OpaClusterStatus {
519-
conditions: compute_conditions(
520-
opa.as_ref(),
521-
&[&ds_cond_builder, &cluster_operation_cond_builder],
522-
),
530+
conditions: compute_conditions(opa, &[&ds_cond_builder, &cluster_operation_cond_builder]),
523531
};
524532

525533
client
526-
.apply_patch_status(OPERATOR_NAME, &*opa, &status)
534+
.apply_patch_status(OPERATOR_NAME, opa, &status)
527535
.await
528536
.context(ApplyStatusSnafu)?;
529537

@@ -996,8 +1004,17 @@ fn build_server_rolegroup_daemonset(
9961004
})
9971005
}
9981006

999-
pub fn error_policy(_obj: Arc<OpaCluster>, _error: &Error, _ctx: Arc<Ctx>) -> Action {
1000-
Action::requeue(*Duration::from_secs(5))
1007+
pub fn error_policy(
1008+
_obj: Arc<DeserializeGuard<OpaCluster>>,
1009+
error: &Error,
1010+
_ctx: Arc<Ctx>,
1011+
) -> Action {
1012+
match error {
1013+
// root object is invalid, will be requeued when modified anyway
1014+
Error::InvalidOpaCluster { .. } => Action::await_change(),
1015+
1016+
_ => Action::requeue(*Duration::from_secs(10)),
1017+
}
10011018
}
10021019

10031020
fn build_config_file(merged_config: &OpaConfig) -> String {

rust/operator-binary/src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use stackable_operator::{
1212
core::v1::{ConfigMap, Service},
1313
},
1414
kube::{
15+
core::DeserializeGuard,
1516
runtime::{watcher, Controller},
1617
Api,
1718
},
@@ -108,10 +109,10 @@ async fn create_controller(
108109
opa_bundle_builder_image: String,
109110
user_info_fetcher_image: String,
110111
) {
111-
let opa_api: Api<OpaCluster> = watch_namespace.get_api(&client);
112-
let daemonsets_api: Api<DaemonSet> = watch_namespace.get_api(&client);
113-
let configmaps_api: Api<ConfigMap> = watch_namespace.get_api(&client);
114-
let services_api: Api<Service> = watch_namespace.get_api(&client);
112+
let opa_api: Api<DeserializeGuard<OpaCluster>> = watch_namespace.get_api(&client);
113+
let daemonsets_api: Api<DeserializeGuard<DaemonSet>> = watch_namespace.get_api(&client);
114+
let configmaps_api: Api<DeserializeGuard<ConfigMap>> = watch_namespace.get_api(&client);
115+
let services_api: Api<DeserializeGuard<Service>> = watch_namespace.get_api(&client);
115116

116117
let controller = Controller::new(opa_api, watcher::Config::default())
117118
.owns(daemonsets_api, watcher::Config::default())

0 commit comments

Comments
 (0)