Skip to content

Commit 5475664

Browse files
committed
refactor: move gitsync components to ValidatedRoleGroupConfig
1 parent 034d8d8 commit 5475664

11 files changed

Lines changed: 100 additions & 75 deletions

File tree

rust/operator-binary/src/controller/build/config_map.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::{
1313
controller::{
1414
ValidatedCluster,
1515
build::{
16-
git_sync,
1716
properties::{
1817
ConfigFileName, authorizers, bootstrap_conf, login_identity_providers,
1918
nifi_properties, product_logging, security_properties, state_management_xml,
@@ -58,9 +57,6 @@ pub enum Error {
5857

5958
#[snafu(display("the cluster has no rolegroup [{role_group}] in role [{role}]"))]
6059
MissingRoleGroup { role: String, role_group: String },
61-
62-
#[snafu(display("failed to build git-sync resources"))]
63-
BuildGitSyncResources { source: git_sync::Error },
6460
}
6561

6662
type Result<T, E = Error> = std::result::Result<T, E>;
@@ -86,8 +82,6 @@ pub fn build_rolegroup_config_map(
8682
})?;
8783

8884
let proxy_hosts = proxy_hosts::compute_proxy_hosts(cluster, cluster_info);
89-
let git_sync_resources =
90-
git_sync::build_git_sync_resources(cluster, rg).context(BuildGitSyncResourcesSnafu)?;
9185

9286
let mut cm_builder = ConfigMapBuilder::new();
9387

@@ -112,11 +106,11 @@ pub fn build_rolegroup_config_map(
112106
)
113107
.add_data(
114108
ConfigFileName::NifiProperties.to_string(),
115-
nifi_properties::build(cluster, rg, &proxy_hosts, &git_sync_resources).with_context(
116-
|_| BuildNifiPropertiesSnafu {
109+
nifi_properties::build(cluster, rg, &proxy_hosts).with_context(|_| {
110+
BuildNifiPropertiesSnafu {
117111
rolegroup: role_group_name.clone(),
118-
},
119-
)?,
112+
}
113+
})?,
120114
)
121115
.add_data(
122116
ConfigFileName::StateManagementXml.to_string(),
Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//! Builds the git-sync resources (volumes, mounts, containers) for a NiFi Node rolegroup.
22
33
use snafu::{ResultExt, Snafu};
4-
use stackable_operator::{crd::git_sync, k8s_openapi::api::core::v1::EnvVar};
4+
use stackable_operator::{
5+
commons::product_image_selection::ResolvedProductImage, crd::git_sync,
6+
k8s_openapi::api::core::v1::EnvVar, v2::builder::pod::container::EnvVarSet,
7+
};
58

69
use crate::{
7-
controller::{
8-
ValidatedCluster, ValidatedRoleGroupConfig, build::resource::statefulset::LOG_VOLUME_NAME,
9-
},
10-
crd::Container,
10+
controller::build::resource::statefulset::LOG_VOLUME_NAME,
11+
crd::{Container, NifiConfig},
1112
};
1213

1314
#[derive(Snafu, Debug)]
@@ -21,18 +22,24 @@ type Result<T, E = Error> = std::result::Result<T, E>;
2122
/// Builds the [`git_sync::v1alpha2::GitSyncResources`] for a single Node rolegroup. The env vars
2223
/// and logging configuration differ per rolegroup, so the resources are computed per rolegroup
2324
/// rather than once for the whole cluster.
25+
///
26+
/// Called from the [`validate`](crate::controller::validate) step; the result is stored on the
27+
/// [`ValidatedRoleGroupConfig`](crate::controller::ValidatedRoleGroupConfig) and consumed by the
28+
/// downstream builders.
2429
pub fn build_git_sync_resources(
25-
cluster: &ValidatedCluster,
26-
rg: &ValidatedRoleGroupConfig,
30+
custom_components_git_sync: &[git_sync::v1alpha2::GitSync],
31+
image: &ResolvedProductImage,
32+
config: &NifiConfig,
33+
env_overrides: &EnvVarSet,
2734
) -> Result<git_sync::v1alpha2::GitSyncResources> {
28-
let env_vars: Vec<EnvVar> = rg.env_overrides.clone().into();
35+
let env_vars: Vec<EnvVar> = env_overrides.clone().into();
2936
git_sync::v1alpha2::GitSyncResources::new(
30-
&cluster.cluster_config.custom_components_git_sync,
31-
&cluster.image,
37+
custom_components_git_sync,
38+
image,
3239
&env_vars,
3340
&[],
3441
LOG_VOLUME_NAME,
35-
&rg.config.logging.for_container(&Container::GitSync),
42+
&config.logging.for_container(&Container::GitSync),
3643
)
3744
.context(InvalidGitSyncSpecSnafu)
3845
}

rust/operator-binary/src/controller/build/properties.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ pub(crate) mod test_support {
130130
let nifi: v1alpha1::NifiCluster =
131131
serde_yaml::from_str(MINIMAL_NIFI_YAML).expect("invalid test YAML");
132132

133-
let role_group_configs = build_role_group_configs(&nifi, &None)
134-
.expect("role group configs should merge for minimal fixture");
135-
136133
let image = ResolvedProductImage {
137134
product_version: "2.9.0".to_string(),
138135
app_version_label_value: "2.9.0".parse::<LabelValue>().unwrap(),
@@ -141,6 +138,9 @@ pub(crate) mod test_support {
141138
pull_secrets: None,
142139
};
143140

141+
let role_group_configs = build_role_group_configs(&nifi, &image, &None)
142+
.expect("role group configs should merge for minimal fixture");
143+
144144
let name = ClusterName::from_str("simple-nifi").expect("valid cluster name");
145145
let namespace = NamespaceName::from_str("default").expect("valid namespace");
146146
let uid = Uid::from_str("e6ac237d-a6d4-43a1-8135-f36506110912").expect("valid uid");
@@ -180,11 +180,6 @@ pub(crate) mod test_support {
180180
.pod_overrides
181181
.clone(),
182182
host_header_check: nifi.spec.cluster_config.host_header_check.clone(),
183-
custom_components_git_sync: nifi
184-
.spec
185-
.cluster_config
186-
.custom_components_git_sync
187-
.clone(),
188183
},
189184
)
190185
}
@@ -199,10 +194,4 @@ pub(crate) mod test_support {
199194
})
200195
.expect("minimal_validated_cluster must contain a 'default' role group")
201196
}
202-
203-
/// Build an empty [`GitSyncResources`] (no git-sync configured).
204-
pub fn empty_git_sync_resources()
205-
-> stackable_operator::crd::git_sync::v1alpha2::GitSyncResources {
206-
stackable_operator::crd::git_sync::v1alpha2::GitSyncResources::default()
207-
}
208197
}

rust/operator-binary/src/controller/build/properties/bootstrap_conf.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ mod tests {
5555

5656
use super::*;
5757
use crate::{
58-
controller::validate::build_role_group_configs,
58+
controller::validate::{build_role_group_configs, test_resolved_product_image},
5959
crd::{NifiRole, v1alpha1},
6060
};
6161

@@ -64,7 +64,8 @@ mod tests {
6464
serde_yaml::from_str(nifi_cluster).expect("illegal test input");
6565

6666
let role_group_configs =
67-
build_role_group_configs(&nifi, &None).expect("failed to build role group configs");
67+
build_role_group_configs(&nifi, &test_resolved_product_image(), &None)
68+
.expect("failed to build role group configs");
6869
let rg = role_group_configs
6970
.get(&NifiRole::Node)
7071
.and_then(|groups| {

rust/operator-binary/src/controller/build/properties/nifi_properties.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::collections::BTreeMap;
44

55
use snafu::{ResultExt, ensure};
6-
use stackable_operator::{crd::git_sync, memory::MemoryQuantity};
6+
use stackable_operator::memory::MemoryQuantity;
77

88
use super::format_properties;
99
use crate::{
@@ -32,8 +32,8 @@ pub fn build(
3232
cluster: &ValidatedCluster,
3333
rg: &ValidatedRoleGroupConfig,
3434
proxy_hosts: &str,
35-
git_sync_resources: &git_sync::v1alpha2::GitSyncResources,
3635
) -> Result<String, Error> {
36+
let git_sync_resources = &rg.git_sync_resources;
3737
let product_version = &cluster.image.product_version;
3838
let auth_config = &cluster.cluster_config.authentication;
3939
let resource_config = &rg.config.resources;
@@ -590,9 +590,7 @@ fn storage_quantity_to_nifi(quantity: MemoryQuantity) -> String {
590590
mod tests {
591591
use super::*;
592592
use crate::{
593-
controller::build::properties::test_support::{
594-
default_rg, empty_git_sync_resources, minimal_validated_cluster,
595-
},
593+
controller::build::properties::test_support::{default_rg, minimal_validated_cluster},
596594
crd::HTTPS_PORT,
597595
};
598596

@@ -602,9 +600,8 @@ mod tests {
602600
fn test_stable_keys_present() {
603601
let cluster = minimal_validated_cluster();
604602
let rg = default_rg(&cluster);
605-
let git_sync = empty_git_sync_resources();
606603

607-
let props = build(&cluster, rg, "*", &git_sync).expect("build should succeed");
604+
let props = build(&cluster, rg, "*").expect("build should succeed");
608605

609606
// HTTPS port
610607
assert!(
@@ -645,7 +642,7 @@ mod tests {
645642
use stackable_operator::v2::types::operator::RoleGroupName;
646643

647644
use crate::{
648-
controller::validate::build_role_group_configs,
645+
controller::validate::{build_role_group_configs, test_resolved_product_image},
649646
crd::{NifiRole, v1alpha1},
650647
};
651648

@@ -674,7 +671,8 @@ mod tests {
674671
"#;
675672
let nifi: v1alpha1::NifiCluster = serde_yaml::from_str(yaml).expect("invalid test YAML");
676673
let mut role_group_configs =
677-
build_role_group_configs(&nifi, &None).expect("failed to build role group configs");
674+
build_role_group_configs(&nifi, &test_resolved_product_image(), &None)
675+
.expect("failed to build role group configs");
678676
let default_rg_name = "default"
679677
.parse::<RoleGroupName>()
680678
.expect("valid role-group name");
@@ -683,17 +681,21 @@ mod tests {
683681
.and_then(|groups| groups.remove(&default_rg_name))
684682
.expect("default role group must exist");
685683

686-
// Build a cluster with this rg substituted in
684+
// Build a cluster with this rg substituted in, then borrow it back for the build call
685+
// (`ValidatedRoleGroupConfig` is not `Clone`).
687686
let mut cluster = minimal_validated_cluster();
688687
cluster
689688
.role_group_configs
690689
.get_mut(&NifiRole::Node)
691690
.unwrap()
692-
.insert(default_rg_name, rg.clone());
691+
.insert(default_rg_name.clone(), rg);
692+
let rg = cluster
693+
.role_group_configs
694+
.get(&NifiRole::Node)
695+
.and_then(|groups| groups.get(&default_rg_name))
696+
.expect("default role group must exist");
693697

694-
let git_sync = empty_git_sync_resources();
695-
let props =
696-
build(&cluster, &rg, "*", &git_sync).expect("build with override should succeed");
698+
let props = build(&cluster, rg, "*").expect("build with override should succeed");
697699

698700
assert!(
699701
props.contains("some.custom.key=some-custom-value"),

rust/operator-binary/src/controller/build/properties/security_properties.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ mod tests {
4949
pod_overrides: Default::default(),
5050
product_specific_common_config: JavaCommonConfig::default(),
5151
vector_container: None,
52+
git_sync_resources: Default::default(),
5253
}
5354
}
5455

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use stackable_operator::{
1414
},
1515
},
1616
constants::RESTART_CONTROLLER_ENABLED_LABEL,
17-
crd::{authentication::oidc::v1alpha1::AuthenticationProvider, git_sync},
17+
crd::authentication::oidc::v1alpha1::AuthenticationProvider,
1818
k8s_openapi::{
1919
DeepMerge,
2020
api::{
@@ -171,7 +171,6 @@ pub(crate) async fn build_node_rolegroup_statefulset(
171171
rolling_update_supported: bool,
172172
replicas: Option<i32>,
173173
service_account_name: &str,
174-
git_sync_resources: &git_sync::v1alpha2::GitSyncResources,
175174
) -> Result<StatefulSet> {
176175
tracing::debug!("Building statefulset");
177176

@@ -180,6 +179,7 @@ pub(crate) async fn build_node_rolegroup_statefulset(
180179
let resolved_product_image = &cluster.image;
181180
let authentication_config = &cluster.cluster_config.authentication;
182181
let authorization_config = &cluster.cluster_config.authorization;
182+
let git_sync_resources = &rg.git_sync_resources;
183183

184184
// Type-safe names for this role group's resources (StatefulSet, ConfigMap, headless Service).
185185
let resource_names = cluster.resource_names(role_group_name);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ pub(crate) mod validate;
5252
/// Produced from the result of
5353
/// [`with_validated_config`](stackable_operator::v2::role_utils::with_validated_config) in the
5454
/// [`validate`] step; downstream builders consume this rather than the raw `NifiCluster`.
55-
#[derive(Clone, Debug)]
5655
pub struct ValidatedRoleGroupConfig {
5756
/// The desired number of replicas (defaulted to 1 during validation).
5857
///
@@ -75,6 +74,11 @@ pub struct ValidatedRoleGroupConfig {
7574
/// ConfigMap name), validated up-front in the [`validate`] step. `None` when the Vector agent
7675
/// is disabled for this role group.
7776
pub vector_container: Option<VectorContainerLogConfig>,
77+
/// The git-sync resources (containers, volumes, mounts) for this role group, resolved from the
78+
/// cluster's `customComponentsGitSync` specs up-front in the [`validate`] step. The env vars and
79+
/// logging config differ per role group, so these are computed per role group. Consumed by both
80+
/// the StatefulSet builder and the `nifi.properties` builder.
81+
pub git_sync_resources: git_sync::v1alpha2::GitSyncResources,
7882
}
7983

8084
/// The validated NifiCluster: everything `reconcile_nifi` needs after dereferencing,
@@ -107,8 +111,6 @@ pub struct ValidatedClusterConfig {
107111
pub authentication: NifiAuthenticationConfig,
108112
/// The cluster authorization settings.
109113
pub authorization: ResolvedNifiAuthorizationConfig,
110-
/// The git-sync specs, resolved into git-sync resources at build time.
111-
pub custom_components_git_sync: Vec<git_sync::v1alpha2::GitSync>,
112114
/// The clustering backend (ZooKeeper or Kubernetes), copied from the spec.
113115
pub clustering_backend: v1alpha1::NifiClusteringBackend,
114116
/// The host-header-check config, resolved into the proxy hosts allow-list at build time.

0 commit comments

Comments
 (0)