Skip to content

Commit fb8acfe

Browse files
committed
refactor: move to v2 types, clusterresources, pdbs, container builders
1 parent f1a8361 commit fb8acfe

5 files changed

Lines changed: 65 additions & 105 deletions

File tree

rust/operator-binary/src/controller/build/resource/pdb.rs

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,36 @@
1-
use snafu::{ResultExt, Snafu};
21
use stackable_operator::{
3-
builder::pdb::PodDisruptionBudgetBuilder, client::Client, cluster_resources::ClusterResources,
4-
commons::pdb::PdbConfig, kube::ResourceExt,
2+
commons::pdb::PdbConfig, k8s_openapi::api::policy::v1::PodDisruptionBudget,
3+
v2::builder::pdb::pod_disruption_budget_builder_with_role,
54
};
65

76
use crate::{
8-
OPERATOR_NAME,
9-
crd::{APP_NAME, NifiRole, v1alpha1},
10-
nifi_controller::NIFI_CONTROLLER_NAME,
7+
controller::{ValidatedCluster, controller_name, operator_name, product_name},
8+
crd::NifiRole,
119
};
1210

13-
#[derive(Snafu, Debug)]
14-
pub enum Error {
15-
#[snafu(display("Cannot create PodDisruptionBudget for role [{role}]"))]
16-
CreatePdb {
17-
source: stackable_operator::builder::pdb::Error,
18-
role: String,
19-
},
20-
#[snafu(display("Cannot apply PodDisruptionBudget [{name}]"))]
21-
ApplyPdb {
22-
source: stackable_operator::cluster_resources::Error,
23-
name: String,
24-
},
25-
}
26-
27-
pub async fn add_pdbs(
11+
/// Builds the [`PodDisruptionBudget`] for the given `role`, or `None` if PDBs are disabled.
12+
pub fn build_pdb(
2813
pdb: &PdbConfig,
29-
nifi: &v1alpha1::NifiCluster,
14+
cluster: &ValidatedCluster,
3015
role: &NifiRole,
31-
client: &Client,
32-
cluster_resources: &mut ClusterResources<'_>,
33-
) -> Result<(), Error> {
16+
) -> Option<PodDisruptionBudget> {
3417
if !pdb.enabled {
35-
return Ok(());
18+
return None;
3619
}
3720
let max_unavailable = pdb.max_unavailable.unwrap_or(match role {
3821
NifiRole::Node => max_unavailable_nodes(),
3922
});
40-
let pdb = PodDisruptionBudgetBuilder::new_with_role(
41-
nifi,
42-
APP_NAME,
43-
&role.to_string(),
44-
OPERATOR_NAME,
45-
NIFI_CONTROLLER_NAME,
23+
let pdb = pod_disruption_budget_builder_with_role(
24+
cluster,
25+
&product_name(),
26+
&ValidatedCluster::role_name(),
27+
&operator_name(),
28+
&controller_name(),
4629
)
47-
.with_context(|_| CreatePdbSnafu {
48-
role: role.to_string(),
49-
})?
5030
.with_max_unavailable(max_unavailable)
5131
.build();
52-
let pdb_name = pdb.name_any();
53-
cluster_resources
54-
.add(client, pdb)
55-
.await
56-
.with_context(|_| ApplyPdbSnafu { name: pdb_name })?;
5732

58-
Ok(())
33+
Some(pdb)
5934
}
6035

6136
fn max_unavailable_nodes() -> u16 {

rust/operator-binary/src/controller/build/resource/reporting_task.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use stackable_operator::{
3030
self,
3131
meta::ObjectMetaBuilder,
3232
pod::{
33-
PodBuilder, container::ContainerBuilder, resources::ResourceRequirementsBuilder,
33+
PodBuilder, resources::ResourceRequirementsBuilder,
3434
security::PodSecurityContextBuilder, volume::SecretFormat,
3535
},
3636
},
@@ -46,8 +46,11 @@ use stackable_operator::{
4646
shared::time::Duration,
4747
utils::cluster_info::KubernetesClusterInfo,
4848
v2::{
49-
builder::meta::ownerreference_from_resource,
50-
types::{kubernetes::NamespaceName, operator::RoleGroupName},
49+
builder::{meta::ownerreference_from_resource, pod::container::new_container_builder},
50+
types::{
51+
kubernetes::{ContainerName, NamespaceName},
52+
operator::RoleGroupName,
53+
},
5154
},
5255
};
5356

@@ -62,19 +65,13 @@ use crate::{
6265

6366
const REPORTING_TASK_CERT_VOLUME_NAME: &str = "tls";
6467
const REPORTING_TASK_CERT_VOLUME_MOUNT: &str = "/stackable/cert";
65-
const REPORTING_TASK_CONTAINER_NAME: &str = "reporting-task";
68+
stackable_operator::constant!(REPORTING_TASK_CONTAINER_NAME: ContainerName = "reporting-task");
6669

6770
#[derive(Snafu, Debug)]
6871
pub enum Error {
6972
#[snafu(display("object defines no name"))]
7073
ObjectHasNoName,
7174

72-
#[snafu(display("illegal container name: [{container_name}]"))]
73-
IllegalContainerName {
74-
source: stackable_operator::builder::pod::container::Error,
75-
container_name: String,
76-
},
77-
7875
#[snafu(display("failed to add Authentication Volumes and VolumeMounts"))]
7976
AddAuthVolumes {
8077
source: crate::security::authentication::Error,
@@ -147,7 +144,10 @@ fn reporting_task_role_group() -> RoleGroupName {
147144

148145
/// Return the name of the reporting task Service.
149146
pub fn build_reporting_task_service_name(nifi_cluster_name: &str) -> String {
150-
format!("{nifi_cluster_name}-{REPORTING_TASK_CONTAINER_NAME}")
147+
format!(
148+
"{nifi_cluster_name}-{container}",
149+
container = &*REPORTING_TASK_CONTAINER_NAME
150+
)
151151
}
152152

153153
/// Return the FQDN (with namespace, domain) of the reporting task.
@@ -284,11 +284,7 @@ fn build_reporting_task_job(
284284
format!("-m {METRICS_PORT}"),
285285
format!("-c {REPORTING_TASK_CERT_VOLUME_MOUNT}/ca.crt"),
286286
];
287-
let mut cb = ContainerBuilder::new(REPORTING_TASK_CONTAINER_NAME).with_context(|_| {
288-
IllegalContainerNameSnafu {
289-
container_name: REPORTING_TASK_CONTAINER_NAME.to_string(),
290-
}
291-
})?;
287+
let mut cb = new_container_builder(&REPORTING_TASK_CONTAINER_NAME);
292288
cb.image_from_product_image(resolved_product_image)
293289
.command(vec!["sh".to_string(), "-c".to_string()])
294290
.args(vec![args.join(" ")])

rust/operator-binary/src/controller/build/resource/statefulset.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use stackable_operator::{
99
self,
1010
meta::ObjectMetaBuilder,
1111
pod::{
12-
PodBuilder, container::ContainerBuilder, resources::ResourceRequirementsBuilder,
12+
PodBuilder, resources::ResourceRequirementsBuilder,
1313
security::PodSecurityContextBuilder, volume::SecretFormat,
1414
},
1515
},
@@ -40,7 +40,10 @@ use stackable_operator::{
4040
},
4141
utils::{COMMON_BASH_TRAP_FUNCTIONS, cluster_info::KubernetesClusterInfo},
4242
v2::{
43-
builder::{meta::ownerreference_from_resource, pod::container::EnvVarSet},
43+
builder::{
44+
meta::ownerreference_from_resource,
45+
pod::container::{EnvVarSet, new_container_builder},
46+
},
4447
product_logging::framework::vector_container,
4548
types::{
4649
kubernetes::{ContainerName, VolumeName},
@@ -92,12 +95,6 @@ pub enum Error {
9295
#[snafu(display("object defines no name"))]
9396
ObjectHasNoName,
9497

95-
#[snafu(display("illegal container name: [{container_name}]"))]
96-
IllegalContainerName {
97-
source: stackable_operator::builder::pod::container::Error,
98-
container_name: String,
99-
},
100-
10198
#[snafu(display("failed to add Authentication Volumes and VolumeMounts"))]
10299
AddAuthVolumes {
103100
source: crate::security::authentication::Error,
@@ -152,6 +149,10 @@ const LOG_CONFIG_VOLUME_NAME: &str = "log-config";
152149
/// git-sync container, see [`crate::controller::build::git_sync`]).
153150
pub(crate) const LOG_VOLUME_NAME: &str = "log";
154151

152+
// Container names. These must match the corresponding (kebab-cased) `crate::crd::Container`
153+
// variants, which key the per-container logging config.
154+
stackable_operator::constant!(PREPARE_CONTAINER_NAME: ContainerName = "prepare");
155+
stackable_operator::constant!(NIFI_CONTAINER_NAME: ContainerName = "nifi");
155156
stackable_operator::constant!(VECTOR_CONTAINER_NAME: ContainerName = "vector");
156157

157158
// Typed `VolumeName`s for the Vector container's log-config and log volumes. They reuse the
@@ -256,7 +257,7 @@ pub(crate) async fn build_node_rolegroup_statefulset(
256257

257258
let sensitive_key_secret = &nifi.spec.cluster_config.sensitive_properties.key_secret;
258259

259-
let prepare_container_name = Container::Prepare.to_string();
260+
let prepare_container_name = PREPARE_CONTAINER_NAME.to_string();
260261
let mut prepare_args = vec![];
261262

262263
if let Some(ContainerLogConfig {
@@ -328,12 +329,7 @@ pub(crate) async fn build_node_rolegroup_statefulset(
328329
"config-utils template /stackable/nifi/conf/security.properties".to_string(),
329330
]);
330331

331-
let mut container_prepare =
332-
ContainerBuilder::new(&prepare_container_name).with_context(|_| {
333-
IllegalContainerNameSnafu {
334-
container_name: prepare_container_name.to_string(),
335-
}
336-
})?;
332+
let mut container_prepare = new_container_builder(&PREPARE_CONTAINER_NAME);
337333

338334
container_prepare
339335
.image_from_product_image(resolved_product_image)
@@ -379,13 +375,7 @@ pub(crate) async fn build_node_rolegroup_statefulset(
379375
.build(),
380376
);
381377

382-
let nifi_container_name = Container::Nifi.to_string();
383-
let mut container_nifi_builder =
384-
ContainerBuilder::new(&nifi_container_name).with_context(|_| {
385-
IllegalContainerNameSnafu {
386-
container_name: nifi_container_name,
387-
}
388-
})?;
378+
let mut container_nifi_builder = new_container_builder(&NIFI_CONTAINER_NAME);
389379

390380
let nifi_args = vec![formatdoc! {"
391381
{COMMON_BASH_TRAP_FUNCTIONS}

rust/operator-binary/src/controller/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,17 @@ impl ValidatedCluster {
214214
}
215215

216216
/// The product name (`nifi`) as a type-safe label value.
217-
fn product_name() -> ProductName {
217+
pub(crate) fn product_name() -> ProductName {
218218
ProductName::from_str(APP_NAME).expect("'nifi' is a valid product name")
219219
}
220220

221221
/// The operator name as a type-safe label value.
222-
fn operator_name() -> OperatorName {
222+
pub(crate) fn operator_name() -> OperatorName {
223223
OperatorName::from_str(OPERATOR_NAME).expect("the operator name is a valid label value")
224224
}
225225

226226
/// The controller name as a type-safe label value.
227-
fn controller_name() -> ControllerName {
227+
pub(crate) fn controller_name() -> ControllerName {
228228
ControllerName::from_str(NIFI_CONTROLLER_NAME)
229229
.expect("the controller name is a valid label value")
230230
}

rust/operator-binary/src/nifi_controller.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use snafu::{OptionExt, ResultExt, Snafu};
77
use stackable_operator::{
88
cli::OperatorEnvironmentOptions,
99
client::Client,
10-
cluster_resources::{ClusterResourceApplyStrategy, ClusterResources},
10+
cluster_resources::ClusterResourceApplyStrategy,
1111
commons::rbac::build_rbac_resources,
1212
kube::{
13-
Resource, ResourceExt,
13+
ResourceExt,
1414
core::{DeserializeGuard, error_boundary},
1515
runtime::controller::Action,
1616
},
@@ -21,7 +21,7 @@ use stackable_operator::{
2121
compute_conditions, operations::ClusterOperationsConditionBuilder,
2222
statefulset::StatefulSetConditionBuilder,
2323
},
24-
v2::types::operator::RoleGroupName,
24+
v2::{cluster_resources::cluster_resources_new, types::operator::RoleGroupName},
2525
};
2626
use strum::{EnumDiscriminants, IntoStaticStr};
2727
use tracing::Instrument;
@@ -32,12 +32,12 @@ use crate::{
3232
build,
3333
build::resource::{
3434
listener::{build_group_listener, group_listener_name},
35-
pdb::add_pdbs,
35+
pdb::build_pdb,
3636
reporting_task::build_maybe_reporting_task,
3737
service::{build_rolegroup_headless_service, build_rolegroup_metrics_service},
3838
statefulset::build_node_rolegroup_statefulset,
3939
},
40-
dereference,
40+
controller_name, dereference, operator_name, product_name,
4141
upgrade::{self, ClusterVersionUpdateState},
4242
validate,
4343
},
@@ -71,11 +71,6 @@ pub enum Error {
7171
#[snafu(display("failed to validate cluster"))]
7272
ValidateCluster { source: validate::Error },
7373

74-
#[snafu(display("failed to create cluster resources"))]
75-
CreateClusterResources {
76-
source: stackable_operator::cluster_resources::Error,
77-
},
78-
7974
#[snafu(display("failed to delete orphaned resources"))]
8075
DeleteOrphanedResources {
8176
source: stackable_operator::cluster_resources::Error,
@@ -147,9 +142,9 @@ pub enum Error {
147142
source: stackable_operator::commons::rbac::Error,
148143
},
149144

150-
#[snafu(display("failed to create PodDisruptionBudget"))]
151-
FailedToCreatePdb {
152-
source: crate::controller::build::resource::pdb::Error,
145+
#[snafu(display("failed to apply PodDisruptionBudget"))]
146+
ApplyPdb {
147+
source: stackable_operator::cluster_resources::Error,
153148
},
154149

155150
#[snafu(display("failed to get required labels"))]
@@ -249,15 +244,16 @@ pub async fn reconcile_nifi(
249244
}
250245
// end todo
251246

252-
let mut cluster_resources = ClusterResources::new(
253-
APP_NAME,
254-
OPERATOR_NAME,
255-
NIFI_CONTROLLER_NAME,
256-
&nifi.object_ref(&()),
247+
let mut cluster_resources = cluster_resources_new(
248+
&product_name(),
249+
&operator_name(),
250+
&controller_name(),
251+
&validated_cluster.name,
252+
&validated_cluster.namespace,
253+
&validated_cluster.uid,
257254
ClusterResourceApplyStrategy::from(&nifi.spec.cluster_operation),
258255
&nifi.spec.object_overrides,
259-
)
260-
.context(CreateClusterResourcesSnafu)?;
256+
);
261257

262258
if let NifiAuthenticationConfig::Oidc { .. } = authentication_config {
263259
check_or_generate_oidc_admin_password(client, nifi, &validated_cluster.namespace)
@@ -392,9 +388,12 @@ pub async fn reconcile_nifi(
392388
listener_class,
393389
}) = role_config
394390
{
395-
add_pdbs(pdb, nifi, &nifi_role, client, &mut cluster_resources)
396-
.await
397-
.context(FailedToCreatePdbSnafu)?;
391+
if let Some(pdb) = build_pdb(pdb, &validated_cluster, &nifi_role) {
392+
cluster_resources
393+
.add(client, pdb)
394+
.await
395+
.context(ApplyPdbSnafu)?;
396+
}
398397

399398
let role_group_listener = build_group_listener(
400399
&validated_cluster,

0 commit comments

Comments
 (0)