-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmod.rs
More file actions
590 lines (520 loc) · 21.6 KB
/
mod.rs
File metadata and controls
590 lines (520 loc) · 21.6 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
pub mod affinity;
pub mod authentication;
pub mod authorization;
pub mod sensitive_properties;
pub mod tls;
use std::collections::BTreeMap;
use affinity::get_affinity;
use authorization::NifiAuthorization;
use sensitive_properties::NifiSensitivePropertiesConfig;
use serde::{Deserialize, Serialize};
use snafu::{OptionExt, ResultExt, Snafu};
use stackable_operator::{
commons::{
affinity::StackableAffinity,
cluster_operation::ClusterOperation,
product_image_selection::ProductImage,
resources::{
CpuLimitsFragment, MemoryLimitsFragment, NoRuntimeLimits, NoRuntimeLimitsFragment,
PvcConfig, PvcConfigFragment, Resources, ResourcesFragment,
},
},
config::{
fragment::{self, Fragment, ValidationError},
merge::Merge,
},
config_overrides::{KeyValueConfigOverrides, KeyValueOverridesProvider},
crd::{authentication::core as auth_core, git_sync},
deep_merger::ObjectOverrides,
k8s_openapi::{
api::core::v1::{PodTemplateSpec, Volume},
apimachinery::pkg::api::resource::Quantity,
},
kube::{CustomResource, ResourceExt, runtime::reflector::ObjectRef},
memory::MemoryQuantity,
product_config_utils::{self, Configuration},
product_logging::{self, spec::Logging},
role_utils::{GenericRoleConfig, JavaCommonConfig, Role, RoleGroupRef},
schemars::{self, JsonSchema},
shared::time::Duration,
status::condition::{ClusterCondition, HasStatusCondition},
utils::crds::{raw_object_list_schema, raw_object_schema},
versioned::versioned,
};
use tls::NifiTls;
pub const APP_NAME: &str = "nifi";
pub const HTTPS_PORT_NAME: &str = "https";
pub const HTTPS_PORT: u16 = 8443;
pub const PROTOCOL_PORT_NAME: &str = "protocol";
pub const PROTOCOL_PORT: u16 = 9088;
pub const BALANCE_PORT_NAME: &str = "balance";
pub const BALANCE_PORT: u16 = 6243;
pub const METRICS_PORT_NAME: &str = "metrics";
pub const METRICS_PORT: u16 = 8081;
pub const STACKABLE_LOG_DIR: &str = "/stackable/log";
pub const STACKABLE_LOG_CONFIG_DIR: &str = "/stackable/log_config";
pub const MAX_NIFI_LOG_FILES_SIZE: MemoryQuantity = MemoryQuantity::from_mebi(10.0);
const DEFAULT_NODE_GRACEFUL_SHUTDOWN_TIMEOUT: Duration = Duration::from_minutes_unchecked(5);
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("the NiFi role [{role}] is missing from spec"))]
MissingNifiRole { role: String },
#[snafu(display("fragment validation failure"))]
FragmentValidationFailure { source: ValidationError },
}
#[versioned(
version(name = "v1alpha1"),
crates(
kube_core = "stackable_operator::kube::core",
kube_client = "stackable_operator::kube::client",
k8s_openapi = "stackable_operator::k8s_openapi",
schemars = "stackable_operator::schemars",
versioned = "stackable_operator::versioned"
)
)]
pub mod versioned {
/// A NiFi cluster stacklet. This resource is managed by the Stackable operator for Apache NiFi.
/// Find more information on how to use it and the resources that the operator generates in the
/// [operator documentation](DOCS_BASE_URL_PLACEHOLDER/nifi/).
#[versioned(crd(
group = "nifi.stackable.tech",
shortname = "nifi",
status = "NifiStatus",
namespaced,
))]
#[derive(Clone, CustomResource, Debug, Deserialize, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NifiClusterSpec {
/// Settings that affect all roles and role groups.
/// The settings in the `clusterConfig` are cluster wide settings that do not need to be configurable at role or role group level.
pub cluster_config: v1alpha1::NifiClusterConfig,
// no doc - docs in Role struct.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub nodes: Option<Role<NifiConfigFragment, NifiConfigOverrides, NifiNodeRoleConfig, JavaCommonConfig>>,
// no doc - docs in ProductImage struct.
pub image: ProductImage,
// no doc - docs in ClusterOperation struct.
#[serde(default)]
pub cluster_operation: ClusterOperation,
// no doc - docs in ObjectOverrides struct.
#[serde(default)]
pub object_overrides: ObjectOverrides,
}
#[derive(Clone, Debug, Deserialize, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NifiClusterConfig {
/// Authentication options for NiFi (required).
/// Read more about authentication in the [security documentation](DOCS_BASE_URL_PLACEHOLDER/nifi/usage_guide/security#authentication).
// We don't add `#[serde(default)]` here, as we require authentication
pub authentication: Vec<auth_core::v1alpha1::ClientAuthenticationDetails>,
/// Authorization options.
/// Learn more in the [NiFi authorization usage guide](DOCS_BASE_URL_PLACEHOLDER/nifi/usage-guide/security#authorization).
#[serde(default)]
pub authorization: NifiAuthorization,
/// Configuration of allowed proxies e.g. load balancers or Kubernetes Ingress. Using a proxy that is not allowed by NiFi results
/// in a failed host header check.
#[serde(default)]
pub host_header_check: HostHeaderCheckConfig,
/// TLS configuration options for the server.
#[serde(default)]
pub tls: NifiTls,
// no doc - docs in NifiSensitivePropertiesConfig struct.
pub sensitive_properties: NifiSensitivePropertiesConfig,
/// Name of the Vector aggregator [discovery ConfigMap](DOCS_BASE_URL_PLACEHOLDER/concepts/service_discovery).
/// It must contain the key `ADDRESS` with the address of the Vector aggregator.
/// Follow the [logging tutorial](DOCS_BASE_URL_PLACEHOLDER/tutorials/logging-vector-aggregator)
/// to learn how to configure log aggregation with Vector.
#[serde(skip_serializing_if = "Option::is_none")]
pub vector_aggregator_config_map_name: Option<String>,
#[serde(flatten)]
pub clustering_backend: NifiClusteringBackend,
/// The `customComponentsGitSync` setting allows configuring custom components to mount via `git-sync`.
/// Learn more in the documentation for
/// [Loading custom components](DOCS_BASE_URL_PLACEHOLDER/nifi/usage_guide/custom-components.html#git_sync).
#[serde(default)]
pub custom_components_git_sync: Vec<git_sync::v1alpha1::GitSync>,
/// Extra volumes similar to `.spec.volumes` on a Pod to mount into every container, this can be useful to for
/// example make client certificates, keytabs or similar things available to processors. These volumes will be
/// mounted into all pods at `/stackable/userdata/{volumename}`.
/// See also the [external files usage guide](DOCS_BASE_URL_PLACEHOLDER/nifi/usage_guide/extra-volumes).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[schemars(schema_with = "raw_object_list_schema")]
pub extra_volumes: Vec<Volume>,
// Docs are on the struct
#[serde(default)]
pub create_reporting_task_job: CreateReportingTaskJob,
}
// This is flattened in for backwards compatibility reasons, `zookeeper_config_map_name` already existed and used to be mandatory.
// For v1alpha2, consider migrating this to a tagged enum for consistency.
#[derive(Clone, Debug, Deserialize, JsonSchema, PartialEq, Serialize)]
#[serde(untagged)]
pub enum NifiClusteringBackend {
#[serde(rename_all = "camelCase")]
ZooKeeper {
/// NiFi can either use ZooKeeper or Kubernetes for managing its cluster state. To use ZooKeeper, provide the name of the
/// ZooKeeper [discovery ConfigMap](DOCS_BASE_URL_PLACEHOLDER/concepts/service_discovery) here.
/// When using the [Stackable operator for Apache ZooKeeper](DOCS_BASE_URL_PLACEHOLDER/zookeeper/)
/// to deploy a ZooKeeper cluster, this will simply be the name of your ZookeeperCluster resource.
///
/// The Kubernetes provider will be used if this field is unset. Kubernetes is only supported for NiFi 2.x and newer,
/// NiFi 1.x requires ZooKeeper.
zookeeper_config_map_name: String,
},
Kubernetes {},
}
}
impl HasStatusCondition for v1alpha1::NifiCluster {
fn conditions(&self) -> Vec<ClusterCondition> {
match &self.status {
Some(status) => status.conditions.clone(),
None => vec![],
}
}
}
impl v1alpha1::NifiCluster {
/// Metadata about a metastore rolegroup
pub fn node_rolegroup_ref(&self, group_name: impl Into<String>) -> RoleGroupRef<Self> {
RoleGroupRef {
cluster: ObjectRef::from_obj(self),
role: NifiRole::Node.to_string(),
role_group: group_name.into(),
}
}
pub fn role_config(&self, role: &NifiRole) -> Option<&NifiNodeRoleConfig> {
match role {
NifiRole::Node => self.spec.nodes.as_ref().map(|n| &n.role_config),
}
}
/// Return user provided server TLS settings
pub fn server_tls_secret_class(&self) -> &str {
&self.spec.cluster_config.tls.server_secret_class
}
/// Retrieve and merge resource configs for role and role groups
pub fn merged_config(&self, role: &NifiRole, role_group: &str) -> Result<NifiConfig, Error> {
// Initialize the result with all default values as baseline
let conf_defaults = NifiConfig::default_config(&self.name_any(), role);
let role = self.spec.nodes.as_ref().context(MissingNifiRoleSnafu {
role: role.to_string(),
})?;
// Retrieve role resource config
let mut conf_role = role.config.config.to_owned();
// Retrieve rolegroup specific resource config
let mut conf_rolegroup = role
.role_groups
.get(role_group)
.map(|rg| rg.config.config.clone())
.unwrap_or_default();
// Merge more specific configs into default config
// Hierarchy is:
// 1. RoleGroup
// 2. Role
// 3. Default
conf_role.merge(&conf_defaults);
conf_rolegroup.merge(&conf_role);
tracing::debug!("Merged config: {:?}", conf_rolegroup);
fragment::validate(conf_rolegroup).context(FragmentValidationFailureSnafu)
}
}
#[derive(Clone, Debug, Deserialize, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct HostHeaderCheckConfig {
/// Allow all proxy hosts by turning off host header validation.
/// See <https://github.com/stackabletech/docker-images/pull/694>
#[serde(default = "default_allow_all")]
pub allow_all: bool,
/// List of proxy hosts to add to the default allow list deployed by SDP containing Kubernetes Services utilized by NiFi.
#[serde(default)]
pub additional_allowed_hosts: Vec<String>,
}
impl Default for HostHeaderCheckConfig {
fn default() -> Self {
Self {
allow_all: default_allow_all(),
additional_allowed_hosts: Vec::default(),
}
}
}
pub fn default_allow_all() -> bool {
true
}
/// This section creates a `create-reporting-task` Kubernetes Job, which enables the export of
/// Prometheus metrics within NiFi.
#[derive(Clone, Debug, Deserialize, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateReportingTaskJob {
/// Wether the Kubernetes Job should be created, defaults to true. It can be helpful to disable
/// the Job, e.g. when you configOverride an authentication mechanism, which the Job currently
/// can't use to authenticate against NiFi.
#[serde(default = "CreateReportingTaskJob::default_enabled")]
pub enabled: bool,
/// Here you can define a
/// [PodTemplateSpec](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.27/#podtemplatespec-v1-core)
/// to override any property that can be set on the Pod of the create-reporting-task Kubernetes Job.
/// Read the
/// [Pod overrides documentation](DOCS_BASE_URL_PLACEHOLDER/concepts/overrides#pod-overrides)
/// for more information.
#[serde(default)]
#[schemars(schema_with = "raw_object_schema")]
pub pod_overrides: PodTemplateSpec,
}
impl Default for CreateReportingTaskJob {
fn default() -> Self {
Self {
enabled: Self::default_enabled(),
pod_overrides: Default::default(),
}
}
}
impl CreateReportingTaskJob {
const fn default_enabled() -> bool {
true
}
}
#[derive(strum::Display)]
#[strum(serialize_all = "camelCase")]
pub enum NifiRole {
#[strum(serialize = "node")]
Node,
}
#[derive(Clone, Debug, Default, Deserialize, JsonSchema, Serialize)]
pub struct NifiStatus {
pub deployed_version: Option<String>,
#[serde(default)]
pub conditions: Vec<ClusterCondition>,
}
#[derive(
Clone,
Debug,
Deserialize,
strum::Display,
Eq,
strum::EnumIter,
JsonSchema,
Ord,
PartialEq,
PartialOrd,
Serialize,
)]
#[serde(rename_all = "kebab-case")]
#[strum(serialize_all = "kebab-case")]
pub enum Container {
Prepare,
Vector,
Nifi,
GitSync,
}
#[derive(Clone, Debug, Default, Fragment, JsonSchema, PartialEq)]
#[fragment_attrs(
derive(
Clone,
Debug,
Default,
Deserialize,
Merge,
JsonSchema,
PartialEq,
Serialize
),
serde(rename_all = "camelCase")
)]
pub struct NifiConfig {
#[fragment_attrs(serde(default))]
pub logging: Logging<Container>,
/// Resource usage is configured here, this includes CPU usage, memory usage and disk storage usage.
/// The default CPU request and limit are 500m and 2000m respectively.
/// The default memory limit is 4GB.
#[fragment_attrs(serde(default))]
pub resources: Resources<NifiStorageConfig, NoRuntimeLimits>,
#[fragment_attrs(serde(default))]
pub affinity: StackableAffinity,
/// Time period Pods have to gracefully shut down, e.g. `30m`, `1h` or `2d`. Consult the operator documentation for details.
#[fragment_attrs(serde(default))]
pub graceful_shutdown_timeout: Option<Duration>,
/// Request secret (currently only autoTls certificates) lifetime from the secret operator, e.g. `7d`, or `30d`.
/// Please note that this can be shortened by the `maxCertificateLifetime` setting on the SecretClass issuing the TLS certificate.
#[fragment_attrs(serde(default))]
pub requested_secret_lifetime: Option<Duration>,
}
impl NifiConfig {
// Auto TLS certificate lifetime
const DEFAULT_NODE_SECRET_LIFETIME: Duration = Duration::from_days_unchecked(1);
pub fn default_config(cluster_name: &str, role: &NifiRole) -> NifiConfigFragment {
NifiConfigFragment {
logging: product_logging::spec::default_logging(),
resources: ResourcesFragment {
cpu: CpuLimitsFragment {
min: Some(Quantity("500m".to_string())),
max: Some(Quantity("2000m".to_string())),
},
memory: MemoryLimitsFragment {
limit: Some(Quantity("4096Mi".to_string())),
runtime_limits: NoRuntimeLimitsFragment {},
},
storage: NifiStorageConfigFragment {
flowfile_repo: PvcConfigFragment {
capacity: Some(Quantity("1024Mi".to_string())),
storage_class: None,
selectors: None,
},
provenance_repo: PvcConfigFragment {
capacity: Some(Quantity("2048Mi".to_string())),
storage_class: None,
selectors: None,
},
database_repo: PvcConfigFragment {
capacity: Some(Quantity("1024Mi".to_string())),
storage_class: None,
selectors: None,
},
content_repo: PvcConfigFragment {
capacity: Some(Quantity("4096Mi".to_string())),
storage_class: None,
selectors: None,
},
state_repo: PvcConfigFragment {
capacity: Some(Quantity("1024Mi".to_string())),
storage_class: None,
selectors: None,
},
filebased_repo: PvcConfigFragment {
capacity: Some(Quantity("16Mi".to_string())),
storage_class: None,
selectors: None,
},
},
},
affinity: get_affinity(cluster_name, role),
graceful_shutdown_timeout: Some(DEFAULT_NODE_GRACEFUL_SHUTDOWN_TIMEOUT),
requested_secret_lifetime: Some(Self::DEFAULT_NODE_SECRET_LIFETIME),
}
}
}
impl Configuration for NifiConfigFragment {
type Configurable = v1alpha1::NifiCluster;
fn compute_env(
&self,
_resource: &Self::Configurable,
_role_name: &str,
) -> Result<BTreeMap<String, Option<String>>, product_config_utils::Error> {
Ok(BTreeMap::new())
}
fn compute_cli(
&self,
_resource: &Self::Configurable,
_role_name: &str,
) -> Result<BTreeMap<String, Option<String>>, product_config_utils::Error> {
Ok(BTreeMap::new())
}
fn compute_files(
&self,
_resource: &Self::Configurable,
_role_name: &str,
_file: &str,
) -> Result<BTreeMap<String, Option<String>>, product_config_utils::Error> {
Ok(BTreeMap::new())
}
}
#[derive(Clone, Debug, Default, JsonSchema, PartialEq, Fragment)]
#[fragment_attrs(
derive(
Clone,
Debug,
Default,
Deserialize,
Merge,
JsonSchema,
PartialEq,
Serialize
),
serde(rename_all = "camelCase")
)]
pub struct NifiStorageConfig {
/// [The FlowFile Repository](https://nifi.apache.org/docs/nifi-docs/html/nifi-in-depth.html#flowfile-repository)
/// is where NiFi keeps track of the state and metadata of FlowFiles as they traverse the data flow.
/// The repository ensures durability, reliability, and recoverability of data in case of system failures or interruptions.
///
/// Default size: 1GB
#[fragment_attrs(serde(default))]
pub flowfile_repo: PvcConfig,
/// [The Provenance Repository](https://nifi.apache.org/docs/nifi-docs/html/nifi-in-depth.html#provenance-repository)
/// is where the history of each FlowFile is stored.
/// This history is used to provide the Data Lineage (also known as the Chain of Custody) of each piece of data.
///
/// Default size: 2GB
#[fragment_attrs(serde(default))]
pub provenance_repo: PvcConfig,
/// Default size: 1GB
#[fragment_attrs(serde(default))]
pub database_repo: PvcConfig,
/// [The Content Repository](https://nifi.apache.org/docs/nifi-docs/html/nifi-in-depth.html#content-repository)
/// is simply a place in local storage where the content of all FlowFiles exists and it is typically the largest of the Repositories.
///
/// Default size: 4GB
#[fragment_attrs(serde(default))]
pub content_repo: PvcConfig,
/// Default size: 1GB
#[fragment_attrs(serde(default))]
pub state_repo: PvcConfig,
/// Used as persistence for file-based authorization. This contains the users.xml and authorizations.xml files.
/// Default size: 16MB
#[fragment_attrs(serde(default))]
pub filebased_repo: PvcConfig,
}
/// Typed config overrides for NiFi configuration files.
///
/// Each field corresponds to a configuration file that supports key-value overrides.
/// The field names match the actual file names used by NiFi.
#[derive(Clone, Debug, Default, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NifiConfigOverrides {
/// Overrides for the `bootstrap.conf` file.
#[serde(rename = "bootstrap.conf")]
pub bootstrap_conf: Option<KeyValueConfigOverrides>,
/// Overrides for the `nifi.properties` file.
#[serde(rename = "nifi.properties")]
pub nifi_properties: Option<KeyValueConfigOverrides>,
/// Overrides for the `security.properties` file.
#[serde(rename = "security.properties")]
pub security_properties: Option<KeyValueConfigOverrides>,
}
impl KeyValueOverridesProvider for NifiConfigOverrides {
fn get_key_value_overrides(&self, file: &str) -> BTreeMap<String, Option<String>> {
match file {
"bootstrap.conf" => self
.bootstrap_conf
.as_ref()
.map(|kv| kv.as_overrides())
.unwrap_or_default(),
"nifi.properties" => self
.nifi_properties
.as_ref()
.map(|kv| kv.as_overrides())
.unwrap_or_default(),
"security.properties" => self
.security_properties
.as_ref()
.map(|kv| kv.as_overrides())
.unwrap_or_default(),
_ => BTreeMap::new(),
}
}
}
#[derive(Clone, Debug, Deserialize, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NifiNodeRoleConfig {
#[serde(flatten)]
pub common: GenericRoleConfig,
#[serde(default = "node_default_listener_class")]
pub listener_class: String,
}
impl Default for NifiNodeRoleConfig {
fn default() -> Self {
NifiNodeRoleConfig {
listener_class: node_default_listener_class(),
common: Default::default(),
}
}
}
fn node_default_listener_class() -> String {
"cluster-internal".to_string()
}