Skip to content

Commit 84e6209

Browse files
committed
refactor: use ConfigMapName, SecretName, ListenerName in CRD
1 parent ec020a6 commit 84e6209

10 files changed

Lines changed: 61 additions & 34 deletions

File tree

extra/crds.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ spec:
381381
A reference to a Secret. The Secret needs to contain a key `nifiSensitivePropsKey`.
382382
If `autoGenerate` is false and this object is missing, the Operator will raise an error.
383383
The encryption key needs to be at least 12 characters long.
384+
maxLength: 253
385+
minLength: 1
386+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
384387
type: string
385388
required:
386389
- keySecret
@@ -396,6 +399,9 @@ spec:
396399
This only affects client connections and is used to
397400
control which certificate the servers should use to
398401
authenticate themselves against the client.
402+
maxLength: 253
403+
minLength: 1
404+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
399405
type: string
400406
type: object
401407
vectorAggregatorConfigMapName:
@@ -404,7 +410,10 @@ spec:
404410
It must contain the key `ADDRESS` with the address of the Vector aggregator.
405411
Follow the [logging tutorial](https://docs.stackable.tech/home/nightly/tutorials/logging-vector-aggregator)
406412
to learn how to configure log aggregation with Vector.
413+
maxLength: 253
414+
minLength: 1
407415
nullable: true
416+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
408417
type: string
409418
zookeeperConfigMapName:
410419
description: |-
@@ -415,6 +424,9 @@ spec:
415424
416425
The Kubernetes provider will be used if this field is unset. Kubernetes is only supported for NiFi 2.x and newer,
417426
NiFi 1.x requires ZooKeeper.
427+
maxLength: 253
428+
minLength: 1
429+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
418430
type: string
419431
required:
420432
- authentication
@@ -1345,6 +1357,9 @@ spec:
13451357
properties:
13461358
listenerClass:
13471359
default: cluster-internal
1360+
maxLength: 253
1361+
minLength: 1
1362+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
13481363
type: string
13491364
podDisruptionBudget:
13501365
default:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub(crate) mod test_support {
170170
.cluster_config
171171
.sensitive_properties
172172
.key_secret
173-
.clone(),
173+
.to_string(),
174174
server_tls_secret_class: nifi.server_tls_secret_class().to_string(),
175175
extra_volumes: nifi.spec.cluster_config.extra_volumes.clone(),
176176
reporting_task_pod_overrides: nifi

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ pub fn build(clustering_backend: &NifiClusteringBackend) -> String {
4343

4444
#[cfg(test)]
4545
mod tests {
46+
use std::str::FromStr;
47+
48+
use stackable_operator::v2::types::kubernetes::ConfigMapName;
49+
4650
use super::*;
4751

4852
#[test]
@@ -57,7 +61,8 @@ mod tests {
5761
#[test]
5862
fn test_build_state_management_xml_zookeeper() {
5963
let xml = build(&NifiClusteringBackend::ZooKeeper {
60-
zookeeper_config_map_name: "my-zk".to_string(),
64+
zookeeper_config_map_name: ConfigMapName::from_str("my-zk")
65+
.expect("'my-zk' is a valid ConfigMap name"),
6166
});
6267
assert!(xml.contains("zk-provider"));
6368
assert!(xml.contains("ZooKeeperStateProvider"));

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use stackable_operator::{
77
crd::listener::v1alpha1::{Listener, ListenerPort, ListenerSpec},
88
k8s_openapi::api::core::v1::PersistentVolumeClaim,
99
kvp::Labels,
10-
v2::builder::meta::ownerreference_from_resource,
10+
v2::{builder::meta::ownerreference_from_resource, types::kubernetes::ListenerClassName},
1111
};
1212

1313
use crate::{
@@ -28,7 +28,7 @@ pub enum Error {
2828

2929
pub fn build_group_listener(
3030
cluster: &ValidatedCluster,
31-
listener_class: String,
31+
listener_class: ListenerClassName,
3232
listener_group_name: String,
3333
) -> Result<Listener, Error> {
3434
Ok(Listener {
@@ -39,7 +39,7 @@ pub fn build_group_listener(
3939
.with_labels(cluster.recommended_labels_role_level())
4040
.build(),
4141
spec: ListenerSpec {
42-
class_name: Some(listener_class),
42+
class_name: Some(listener_class.to_string()),
4343
ports: Some(vec![ListenerPort {
4444
name: HTTPS_PORT_NAME.into(),
4545
port: HTTPS_PORT.into(),

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ pub enum Error {
8484
source: crate::controller::build::git_sync::Error,
8585
},
8686

87-
#[snafu(display("invalid Vector aggregator discovery ConfigMap name"))]
88-
ParseVectorAggregatorConfigMapName {
89-
source: stackable_operator::v2::macros::attributed_string_type::Error,
90-
},
91-
9287
#[snafu(display(
9388
"the Vector aggregator discovery ConfigMap name is required when the Vector agent is enabled"
9489
))]
@@ -138,16 +133,13 @@ pub fn validate(
138133
.check_for_nifi_version(&image.product_version)
139134
.context(InvalidSensitivePropertiesAlgorithmSnafu)?;
140135

141-
// The Vector aggregator discovery ConfigMap name is validated here so an invalid name fails
142-
// up-front. It is only required when the Vector agent is enabled for a role group.
136+
// The Vector aggregator discovery ConfigMap name is validated by the CRD's typed field. It is
137+
// only required when the Vector agent is enabled for a role group.
143138
let vector_aggregator_config_map_name = nifi
144139
.spec
145140
.cluster_config
146141
.vector_aggregator_config_map_name
147-
.as_deref()
148-
.map(ConfigMapName::from_str)
149-
.transpose()
150-
.context(ParseVectorAggregatorConfigMapNameSnafu)?;
142+
.clone();
151143

152144
let role_group_configs =
153145
build_role_group_configs(nifi, &image, &vector_aggregator_config_map_name)?;
@@ -178,7 +170,7 @@ pub fn validate(
178170
.cluster_config
179171
.sensitive_properties
180172
.key_secret
181-
.clone(),
173+
.to_string(),
182174
server_tls_secret_class: nifi.server_tls_secret_class().to_string(),
183175
extra_volumes: nifi.spec.cluster_config.extra_volumes.clone(),
184176
reporting_task_pod_overrides: nifi

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ pub mod sensitive_properties;
55
pub mod storage;
66
pub mod tls;
77

8+
use std::str::FromStr;
9+
810
use affinity::get_affinity;
911
use authorization::NifiAuthorization;
1012
use sensitive_properties::NifiSensitivePropertiesConfig;
@@ -34,7 +36,11 @@ use stackable_operator::{
3436
shared::time::Duration,
3537
status::condition::{ClusterCondition, HasStatusCondition},
3638
utils::crds::{raw_object_list_schema, raw_object_schema},
37-
v2::{config_overrides::KeyValueConfigOverrides, role_utils::JavaCommonConfig},
39+
v2::{
40+
config_overrides::KeyValueConfigOverrides,
41+
role_utils::JavaCommonConfig,
42+
types::kubernetes::{ConfigMapName, ListenerClassName, SecretClassName},
43+
},
3844
versioned::versioned,
3945
};
4046
use tls::NifiTls;
@@ -133,7 +139,7 @@ pub mod versioned {
133139
/// Follow the [logging tutorial](DOCS_BASE_URL_PLACEHOLDER/tutorials/logging-vector-aggregator)
134140
/// to learn how to configure log aggregation with Vector.
135141
#[serde(skip_serializing_if = "Option::is_none")]
136-
pub vector_aggregator_config_map_name: Option<String>,
142+
pub vector_aggregator_config_map_name: Option<ConfigMapName>,
137143

138144
#[serde(flatten)]
139145
pub clustering_backend: NifiClusteringBackend,
@@ -171,7 +177,7 @@ pub mod versioned {
171177
///
172178
/// The Kubernetes provider will be used if this field is unset. Kubernetes is only supported for NiFi 2.x and newer,
173179
/// NiFi 1.x requires ZooKeeper.
174-
zookeeper_config_map_name: String,
180+
zookeeper_config_map_name: ConfigMapName,
175181
},
176182
Kubernetes {},
177183
}
@@ -207,7 +213,7 @@ impl v1alpha1::NifiCluster {
207213
}
208214

209215
/// Return user provided server TLS settings
210-
pub fn server_tls_secret_class(&self) -> &str {
216+
pub fn server_tls_secret_class(&self) -> &SecretClassName {
211217
&self.spec.cluster_config.tls.server_secret_class
212218
}
213219
}
@@ -465,7 +471,7 @@ pub struct NifiNodeRoleConfig {
465471
pub common: GenericRoleConfig,
466472

467473
#[serde(default = "node_default_listener_class")]
468-
pub listener_class: String,
474+
pub listener_class: ListenerClassName,
469475
}
470476

471477
impl Default for NifiNodeRoleConfig {
@@ -477,8 +483,9 @@ impl Default for NifiNodeRoleConfig {
477483
}
478484
}
479485

480-
fn node_default_listener_class() -> String {
481-
"cluster-internal".to_string()
486+
fn node_default_listener_class() -> ListenerClassName {
487+
ListenerClassName::from_str("cluster-internal")
488+
.expect("'cluster-internal' is a valid listener class name")
482489
}
483490

484491
#[cfg(test)]

rust/operator-binary/src/crd/sensitive_properties.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use serde::{Deserialize, Serialize};
22
use snafu::Snafu;
3-
use stackable_operator::schemars::{self, JsonSchema};
3+
use stackable_operator::{
4+
schemars::{self, JsonSchema},
5+
v2::types::kubernetes::SecretName,
6+
};
47

58
#[derive(Snafu, Debug)]
69
pub enum Error {
@@ -14,13 +17,13 @@ pub enum Error {
1417
/// NiFi supports encrypting sensitive properties in processors as they are written to disk.
1518
/// You can configure the encryption algorithm and the key to use.
1619
/// You can also let the operator generate an encryption key for you.
17-
#[derive(Clone, Debug, Default, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
20+
#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
1821
#[serde(rename_all = "camelCase")]
1922
pub struct NifiSensitivePropertiesConfig {
2023
/// A reference to a Secret. The Secret needs to contain a key `nifiSensitivePropsKey`.
2124
/// If `autoGenerate` is false and this object is missing, the Operator will raise an error.
2225
/// The encryption key needs to be at least 12 characters long.
23-
pub key_secret: String,
26+
pub key_secret: SecretName,
2427

2528
/// Whether to generate the `keySecret` if it is missing.
2629
/// Defaults to `false`.

rust/operator-binary/src/crd/tls.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
use std::str::FromStr;
2+
13
use serde::{Deserialize, Serialize};
2-
use stackable_operator::schemars::{self, JsonSchema};
4+
use stackable_operator::{
5+
schemars::{self, JsonSchema},
6+
v2::types::kubernetes::SecretClassName,
7+
};
38

49
#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
510
#[serde(rename_all = "camelCase")]
@@ -8,7 +13,7 @@ pub struct NifiTls {
813
/// control which certificate the servers should use to
914
/// authenticate themselves against the client.
1015
#[serde(default = "NifiTls::default_server_secret_class")]
11-
pub server_secret_class: String,
16+
pub server_secret_class: SecretClassName,
1217
}
1318

1419
impl Default for NifiTls {
@@ -20,7 +25,7 @@ impl Default for NifiTls {
2025
}
2126

2227
impl NifiTls {
23-
fn default_server_secret_class() -> String {
24-
"tls".to_owned()
28+
fn default_server_secret_class() -> SecretClassName {
29+
SecretClassName::from_str("tls").expect("'tls' is a valid secret class name")
2530
}
2631
}

rust/operator-binary/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ fn references_config_map(
216216
let references_zookeeper_config_map = match &nifi.spec.cluster_config.clustering_backend {
217217
NifiClusteringBackend::ZooKeeper {
218218
zookeeper_config_map_name,
219-
} => *zookeeper_config_map_name == config_map.name_any(),
219+
} => zookeeper_config_map_name.to_string() == config_map.name_any(),
220220
NifiClusteringBackend::Kubernetes {} => false,
221221
};
222222
let references_authorization_config_map = references_authorization_config_map(nifi, config_map);

rust/operator-binary/src/security/sensitive_key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ pub(crate) async fn check_or_generate_sensitive_key(
3434
let sensitive_config = &nifi.spec.cluster_config.sensitive_properties;
3535

3636
match client
37-
.get_opt::<Secret>(&sensitive_config.key_secret, namespace.as_ref())
37+
.get_opt::<Secret>(sensitive_config.key_secret.as_ref(), namespace.as_ref())
3838
.await
3939
.context(SensitiveKeySecretSnafu)?
4040
{
4141
Some(_) => Ok(false),
4242
None => {
4343
if !sensitive_config.auto_generate {
4444
return Err(Error::SensitiveKeySecretMissing {
45-
name: sensitive_config.key_secret.clone(),
45+
name: sensitive_config.key_secret.to_string(),
4646
namespace: namespace.to_string(),
4747
});
4848
}

0 commit comments

Comments
 (0)