-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathvalidate.rs
More file actions
431 lines (378 loc) · 15.9 KB
/
Copy pathvalidate.rs
File metadata and controls
431 lines (378 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! The validate step in the NifiCluster controller
//!
//! Synchronously validates inputs that don't require Kubernetes API calls. Produces
//! [`ValidatedCluster`], consumed by the rest of `reconcile_nifi`.
use std::{collections::BTreeMap, str::FromStr as _};
use snafu::{OptionExt, ResultExt, Snafu};
use stackable_operator::{
cli::OperatorEnvironmentOptions,
commons::product_image_selection,
config::fragment,
kube::ResourceExt as _,
product_logging::spec::Logging,
role_utils::CommonConfiguration,
v2::{
builder::pod::container::{EnvVarName, EnvVarSet},
controller_utils::{self, get_cluster_name, get_uid},
product_logging::framework::{
VectorContainerLogConfig, validate_logging_configuration_for_container,
},
role_utils::with_validated_config,
types::{
kubernetes::ConfigMapName,
operator::{ProductVersion, RoleGroupName},
},
},
};
use strum::{EnumDiscriminants, IntoStaticStr};
use super::{
NifiRoleGroupConfig, ValidatedCluster, ValidatedClusterConfig, ValidatedLogging,
ValidatedNifiConfig, ValidatedRoleConfig, ValidatedSensitiveProperties,
};
use crate::{
controller::{build::git_sync::build_git_sync_resources, dereference::DereferencedObjects},
crd::{Container, NifiConfig, NifiRole, v1alpha1},
security::{
authentication::{self, NifiAuthenticationConfig},
authorization::ResolvedNifiAuthorizationConfig,
},
};
/// The base name of the NiFi product image, used to resolve the fully-qualified image reference.
const CONTAINER_IMAGE_BASE_NAME: &str = "nifi";
#[derive(Snafu, Debug, EnumDiscriminants)]
#[strum_discriminants(derive(IntoStaticStr))]
#[allow(clippy::enum_variant_names)]
pub enum Error {
#[snafu(display("failed to resolve product image"))]
ResolveProductImage {
source: product_image_selection::Error,
},
#[snafu(display("object has no nodes defined"))]
NoNodesDefined,
#[snafu(display("failed to get the cluster name"))]
GetClusterName { source: controller_utils::Error },
#[snafu(display("failed to get the UID"))]
GetUid { source: controller_utils::Error },
#[snafu(display("invalid NiFi authentication configuration"))]
InvalidAuthenticationConfig { source: authentication::Error },
#[snafu(display("failed to validate the rolegroup config fragment"))]
ValidateRoleGroupConfig { source: fragment::ValidationError },
#[snafu(display("the role-group name {role_group:?} is invalid"))]
ParseRoleGroupName {
source: stackable_operator::v2::macros::attributed_string_type::Error,
role_group: String,
},
#[snafu(display("environment variable name {name:?} is invalid"))]
ParseEnvVarName {
source: stackable_operator::v2::macros::attributed_string_type::Error,
name: String,
},
#[snafu(display("failed to build git-sync resources"))]
BuildGitSyncResources {
source: crate::controller::build::git_sync::Error,
},
#[snafu(display(
"the Vector aggregator discovery ConfigMap name is required when the Vector agent is enabled"
))]
MissingVectorAggregatorConfigMapName,
#[snafu(display("failed to validate logging configuration"))]
ValidateLoggingConfig {
source: stackable_operator::v2::product_logging::framework::Error,
},
}
type Result<T, E = Error> = std::result::Result<T, E>;
/// Validates the cluster spec and the dereferenced inputs.
pub fn validate(
nifi: &v1alpha1::NifiCluster,
dereferenced_objects: &DereferencedObjects,
operator_environment: &OperatorEnvironmentOptions,
) -> Result<ValidatedCluster> {
let image = nifi
.spec
.image
.resolve(
CONTAINER_IMAGE_BASE_NAME,
&operator_environment.image_repository,
crate::built_info::PKG_VERSION,
)
.context(ResolveProductImageSnafu)?;
let name = get_cluster_name(nifi).context(GetClusterNameSnafu)?;
let authentication_config =
NifiAuthenticationConfig::validate(&name, &dereferenced_objects.authentication_classes)
.context(InvalidAuthenticationConfigSnafu)?;
let authorization_config = ResolvedNifiAuthorizationConfig::validate(
&nifi.spec.cluster_config.authorization,
&dereferenced_objects.authorization,
);
let sensitive_properties_algorithm = nifi
.spec
.cluster_config
.sensitive_properties
.algorithm
.clone()
.unwrap_or_default();
// The Vector aggregator discovery ConfigMap name is validated by the CRD's typed field. It is
// only required when the Vector agent is enabled for a role group.
let vector_aggregator_config_map_name = nifi
.spec
.cluster_config
.vector_aggregator_config_map_name
.clone();
let role_group_configs =
build_role_group_configs(nifi, &image, &vector_aggregator_config_map_name)?;
// Per-role config (PDB + listener class), extracted here so downstream builders source it from
// the `ValidatedCluster` rather than the raw `NifiCluster`. The `nodes` role is mandatory
// (already enforced by `build_role_group_configs` above), so this is always present.
let role_config = nifi
.role_config(&NifiRole::Node)
.map(|role_config| ValidatedRoleConfig {
pdb: role_config.common.pod_disruption_budget.clone(),
listener_class: role_config.listener_class.clone(),
})
.context(NoNodesDefinedSnafu)?;
let namespace = dereferenced_objects.namespace.clone();
let cluster_domain = dereferenced_objects.cluster_domain.clone();
let uid = get_uid(nifi).context(GetUidSnafu)?;
// `app_version_label_value` is constructed to be a valid label value, so it is always a valid
// `ProductVersion`. It is used for the `app.kubernetes.io/version` label on built resources.
let product_version = ProductVersion::from_str(&image.app_version_label_value)
.expect("the app version label value is a valid product version");
Ok(ValidatedCluster::new(
name,
namespace,
cluster_domain,
uid,
image,
product_version,
role_config,
role_group_configs,
ValidatedClusterConfig {
authentication: authentication_config,
authorization: authorization_config,
clustering_backend: nifi.spec.cluster_config.clustering_backend.clone(),
sensitive_properties: ValidatedSensitiveProperties {
algorithm: sensitive_properties_algorithm,
key_secret: nifi
.spec
.cluster_config
.sensitive_properties
.key_secret
.clone(),
auto_generate: nifi.spec.cluster_config.sensitive_properties.auto_generate,
},
server_tls_secret_class: nifi.server_tls_secret_class().clone(),
extra_volumes: nifi.spec.cluster_config.extra_volumes.clone(),
host_header_check: nifi.spec.cluster_config.host_header_check.clone(),
},
))
}
pub(crate) fn build_role_group_configs(
nifi: &v1alpha1::NifiCluster,
image: &product_image_selection::ResolvedProductImage,
vector_aggregator_config_map_name: &Option<ConfigMapName>,
) -> Result<BTreeMap<NifiRole, BTreeMap<RoleGroupName, NifiRoleGroupConfig>>> {
let role = nifi.spec.nodes.as_ref().context(NoNodesDefinedSnafu)?;
let default_config = NifiConfig::default_config(&nifi.name_any(), &NifiRole::Node);
let mut groups: BTreeMap<RoleGroupName, NifiRoleGroupConfig> = BTreeMap::new();
for (rg_name, rg) in &role.role_groups {
let role_group_name =
RoleGroupName::from_str(rg_name).with_context(|_| ParseRoleGroupNameSnafu {
role_group: rg_name.clone(),
})?;
let validated = with_validated_config::<NifiConfig, _, _, _, _>(rg, role, &default_config)
.context(ValidateRoleGroupConfigSnafu)?;
let CommonConfiguration {
config,
config_overrides,
env_overrides,
cli_overrides,
pod_overrides,
product_specific_common_config,
} = validated.config;
// Convert the merged env-override HashMap into an EnvVarSet, validating each name
// eagerly. Keys are unique (HashMap), so insertion order is irrelevant.
let mut env_overrides_set = EnvVarSet::new();
for (name, value) in env_overrides {
env_overrides_set = env_overrides_set.with_value(
&EnvVarName::from_str(&name)
.context(ParseEnvVarNameSnafu { name: name.clone() })?,
value,
);
}
// Validate the logging config (NiFi + optional Vector container) up-front so an invalid
// custom log ConfigMap name, or a missing Vector aggregator discovery ConfigMap name, fails
// during validation rather than at resource-build time.
let logging = validate_logging(&config.logging, vector_aggregator_config_map_name)?;
// The git-sync resources depend on this role group's env-var overrides and logging config,
// so they are resolved (and validated) per role group up-front rather than at build time.
let git_sync_resources = build_git_sync_resources(
&nifi.spec.cluster_config.custom_components_git_sync,
image,
&config,
&env_overrides_set,
)
.context(BuildGitSyncResourcesSnafu)?;
groups.insert(
role_group_name,
NifiRoleGroupConfig {
replicas: validated.replicas,
config: ValidatedNifiConfig::from_merged(config, logging, git_sync_resources),
config_overrides,
env_overrides: env_overrides_set,
cli_overrides,
pod_overrides,
product_specific_common_config,
},
);
}
let mut role_group_configs = BTreeMap::new();
role_group_configs.insert(NifiRole::Node, groups);
Ok(role_group_configs)
}
/// Validates the logging configuration for the NiFi (and optional Vector) container.
///
/// `vector_aggregator_config_map_name` is the discovery ConfigMap name of the Vector aggregator;
/// it is required (and validated) only when the Vector agent is enabled.
fn validate_logging(
logging: &Logging<Container>,
vector_aggregator_config_map_name: &Option<ConfigMapName>,
) -> Result<ValidatedLogging> {
let nifi_container = validate_logging_configuration_for_container(logging, &Container::Nifi)
.context(ValidateLoggingConfigSnafu)?;
let prepare_container =
validate_logging_configuration_for_container(logging, &Container::Prepare)
.context(ValidateLoggingConfigSnafu)?;
let vector_container = if logging.enable_vector_agent {
let vector_aggregator_config_map_name = vector_aggregator_config_map_name
.clone()
.context(MissingVectorAggregatorConfigMapNameSnafu)?;
Some(VectorContainerLogConfig {
log_config: validate_logging_configuration_for_container(logging, &Container::Vector)
.context(ValidateLoggingConfigSnafu)?,
vector_aggregator_config_map_name,
})
} else {
None
};
Ok(ValidatedLogging {
nifi_container,
prepare_container,
vector_container,
enable_vector_agent: logging.enable_vector_agent,
})
}
/// A minimal resolved product image (NiFi 2.9.0) for tests that need to build role-group configs.
#[cfg(test)]
pub(crate) fn test_resolved_product_image() -> product_image_selection::ResolvedProductImage {
product_image_selection::ResolvedProductImage {
product_version: "2.9.0".to_string(),
app_version_label_value: "2.9.0".parse().expect("valid label value"),
image: "oci.stackable.tech/sdp/nifi:2.9.0-stackable0.0.0-dev".to_string(),
image_pull_policy: "IfNotPresent".to_string(),
pull_secrets: None,
}
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use stackable_operator::v2::types::kubernetes::ConfigMapName;
use super::*;
/// A NiFi cluster with the Vector agent enabled at the Node role level.
const NIFI_VECTOR_ENABLED_YAML: &str = r#"
apiVersion: nifi.stackable.tech/v1alpha1
kind: NifiCluster
metadata:
name: simple-nifi
namespace: default
spec:
image:
productVersion: 2.9.0
clusterConfig:
authentication:
- authenticationClass: nifi-admin-credentials-simple
sensitiveProperties:
keySecret: simple-nifi-sensitive-property-key
autoGenerate: true
nodes:
config:
logging:
enableVectorAgent: true
roleGroups:
default:
replicas: 1
"#;
/// A minimal NiFi cluster with the Vector agent disabled (the default).
const NIFI_VECTOR_DISABLED_YAML: &str = r#"
apiVersion: nifi.stackable.tech/v1alpha1
kind: NifiCluster
metadata:
name: simple-nifi
namespace: default
spec:
image:
productVersion: 2.9.0
clusterConfig:
authentication:
- authenticationClass: nifi-admin-credentials-simple
sensitiveProperties:
keySecret: simple-nifi-sensitive-property-key
autoGenerate: true
nodes:
roleGroups:
default:
replicas: 1
"#;
fn default_rg(
configs: &BTreeMap<NifiRole, BTreeMap<RoleGroupName, NifiRoleGroupConfig>>,
) -> &NifiRoleGroupConfig {
configs[&NifiRole::Node]
.get(&RoleGroupName::from_str("default").expect("valid role-group name"))
.expect("the 'default' role group must exist")
}
#[test]
fn vector_container_is_validated_when_agent_enabled() {
let nifi: v1alpha1::NifiCluster =
serde_yaml::from_str(NIFI_VECTOR_ENABLED_YAML).expect("invalid test YAML");
let aggregator = Some(ConfigMapName::from_str("nifi-vector-aggregator-discovery").unwrap());
let configs = build_role_group_configs(&nifi, &test_resolved_product_image(), &aggregator)
.expect("role group configs should validate");
let vector = default_rg(&configs)
.config
.logging
.vector_container
.as_ref()
.expect("the Vector container config should be present when the agent is enabled");
assert_eq!(
"nifi-vector-aggregator-discovery",
vector.vector_aggregator_config_map_name.to_string()
);
}
#[test]
fn vector_agent_enabled_without_aggregator_name_fails() {
let nifi: v1alpha1::NifiCluster =
serde_yaml::from_str(NIFI_VECTOR_ENABLED_YAML).expect("invalid test YAML");
// `NifiRoleGroupConfig` is not `Debug` (its `config` holds non-`Debug` git-sync resources),
// so match on the result rather than using `expect_err` (which would require `Ok` to be
// `Debug`).
let result = build_role_group_configs(&nifi, &test_resolved_product_image(), &None);
assert!(matches!(
result,
Err(Error::MissingVectorAggregatorConfigMapName)
));
}
#[test]
fn no_vector_container_when_agent_disabled() {
let nifi: v1alpha1::NifiCluster =
serde_yaml::from_str(NIFI_VECTOR_DISABLED_YAML).expect("invalid test YAML");
// The aggregator name is not required when the Vector agent is disabled.
let configs = build_role_group_configs(&nifi, &test_resolved_product_image(), &None)
.expect("role group configs should validate");
assert!(
default_rg(&configs)
.config
.logging
.vector_container
.is_none()
);
}
}