Skip to content

Commit 6ecceab

Browse files
committed
refactor: use v2 ConfigMapName, SecretName, ListenerName
1 parent e560c93 commit 6ecceab

8 files changed

Lines changed: 81 additions & 65 deletions

File tree

extra/crds.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ spec:
7878
- Which ca.crt to use when validating the other server
7979
8080
Defaults to `tls`
81+
maxLength: 253
82+
minLength: 1
83+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
8184
type: string
8285
serverSecretClass:
8386
default: tls
@@ -88,7 +91,10 @@ spec:
8891
- Which cert the servers should use to authenticate themselves against the client
8992
9093
Defaults to `tls`.
94+
maxLength: 253
95+
minLength: 1
9196
nullable: true
97+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
9298
type: string
9399
type: object
94100
vectorAggregatorConfigMapName:
@@ -97,7 +103,10 @@ spec:
97103
It must contain the key `ADDRESS` with the address of the Vector aggregator.
98104
Follow the [logging tutorial](https://docs.stackable.tech/home/nightly/tutorials/logging-vector-aggregator)
99105
to learn how to configure log aggregation with Vector.
106+
maxLength: 253
107+
minLength: 1
100108
nullable: true
109+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
101110
type: string
102111
type: object
103112
clusterOperation:
@@ -727,6 +736,9 @@ spec:
727736
listenerClass:
728737
default: cluster-internal
729738
description: This field controls which [ListenerClass](https://docs.stackable.tech/home/nightly/listener-operator/listenerclass.html) is used to expose the ZooKeeper servers.
739+
maxLength: 253
740+
minLength: 1
741+
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
730742
type: string
731743
podDisruptionBudget:
732744
default:

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

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::str::FromStr;
2+
13
use serde::{Deserialize, Serialize};
24
use snafu::{OptionExt, Snafu};
35
use stackable_operator::{
@@ -21,7 +23,13 @@ use stackable_operator::{
2123
shared::time::Duration,
2224
status::condition::{ClusterCondition, HasStatusCondition},
2325
utils::cluster_info::KubernetesClusterInfo,
24-
v2::{config_overrides::KeyValueConfigOverrides, role_utils::JavaCommonConfig},
26+
v2::{
27+
config_overrides::KeyValueConfigOverrides,
28+
role_utils::JavaCommonConfig,
29+
types::kubernetes::{
30+
ConfigMapName, ListenerClassName, ListenerName, NamespaceName, ServiceName,
31+
},
32+
},
2533
versioned::versioned,
2634
};
2735
use strum::{Display, EnumIter, EnumString, IntoEnumIterator};
@@ -38,8 +46,9 @@ pub mod tls;
3846
///
3947
/// Lives in the `crd` module (rather than the controller build tree) because it is shared by both
4048
/// controllers and by [`ZookeeperCluster::server_role_listener_fqdn`].
41-
pub fn role_listener_name(cluster_name: &str, zk_role: &ZookeeperRole) -> String {
42-
format!("{cluster_name}-{zk_role}")
49+
pub fn role_listener_name(cluster_name: &str, zk_role: &ZookeeperRole) -> ListenerName {
50+
ListenerName::from_str(&format!("{cluster_name}-{zk_role}"))
51+
.expect("the role listener name should be a valid Listener name")
4352
}
4453

4554
pub const APP_NAME: &str = "zookeeper";
@@ -135,7 +144,7 @@ pub mod versioned {
135144

136145
/// This field controls which [ListenerClass](DOCS_BASE_URL_PLACEHOLDER/listener-operator/listenerclass.html) is used to expose the ZooKeeper servers.
137146
#[serde(default = "default_listener_class")]
138-
pub listener_class: String,
147+
pub listener_class: ListenerClassName,
139148
}
140149

141150
#[derive(Clone, Deserialize, Debug, Eq, JsonSchema, PartialEq, Serialize)]
@@ -151,7 +160,7 @@ pub mod versioned {
151160
/// Follow the [logging tutorial](DOCS_BASE_URL_PLACEHOLDER/tutorials/logging-vector-aggregator)
152161
/// to learn how to configure log aggregation with Vector.
153162
#[serde(skip_serializing_if = "Option::is_none")]
154-
pub vector_aggregator_config_map_name: Option<String>,
163+
pub vector_aggregator_config_map_name: Option<ConfigMapName>,
155164

156165
/// TLS encryption settings for ZooKeeper (server, quorum).
157166
/// Read more in the [encryption usage guide](DOCS_BASE_URL_PLACEHOLDER/zookeeper/usage_guide/encryption).
@@ -325,8 +334,8 @@ pub enum ZookeeperRole {
325334
///
326335
/// Used for service discovery.
327336
pub struct ZookeeperPodRef {
328-
pub namespace: String,
329-
pub role_group_headless_service_name: String,
337+
pub namespace: NamespaceName,
338+
pub role_group_headless_service_name: ServiceName,
330339
pub pod_name: String,
331340
pub zookeeper_myid: u16,
332341
}
@@ -339,8 +348,9 @@ fn cluster_config_default() -> v1alpha1::ZookeeperClusterConfig {
339348
}
340349
}
341350

342-
fn default_listener_class() -> String {
343-
DEFAULT_LISTENER_CLASS.to_owned()
351+
pub(crate) fn default_listener_class() -> ListenerClassName {
352+
ListenerClassName::from_str(DEFAULT_LISTENER_CLASS)
353+
.expect("the default listener class should be a valid ListenerClass name")
344354
}
345355

346356
impl Default for ZookeeperServerRoleConfig {
@@ -469,7 +479,8 @@ mod tests {
469479
.cluster_config
470480
.tls
471481
.as_ref()
472-
.and_then(|tls| tls.server_secret_class.as_deref())
482+
.and_then(|tls| tls.server_secret_class.as_ref())
483+
.map(AsRef::as_ref)
473484
}
474485

475486
fn get_quorum_secret_class(zk: &v1alpha1::ZookeeperCluster) -> &str {
@@ -479,7 +490,7 @@ mod tests {
479490
.as_ref()
480491
.unwrap()
481492
.quorum_secret_class
482-
.as_str()
493+
.as_ref()
483494
}
484495

485496
#[test]
@@ -497,11 +508,11 @@ mod tests {
497508
serde_yaml::from_str(input).expect("illegal test input");
498509
assert_eq!(
499510
get_server_secret_class(&zookeeper),
500-
tls::server_tls_default().as_deref()
511+
tls::server_tls_default().as_ref().map(AsRef::as_ref)
501512
);
502513
assert_eq!(
503514
get_quorum_secret_class(&zookeeper),
504-
tls::quorum_tls_default().as_str()
515+
tls::quorum_tls_default().as_ref()
505516
);
506517

507518
let input = r#"
@@ -525,7 +536,7 @@ mod tests {
525536
);
526537
assert_eq!(
527538
get_quorum_secret_class(&zookeeper),
528-
tls::quorum_tls_default().as_str()
539+
tls::quorum_tls_default().as_ref()
529540
);
530541

531542
let input = r#"
@@ -545,7 +556,7 @@ mod tests {
545556
assert_eq!(get_server_secret_class(&zookeeper), None);
546557
assert_eq!(
547558
get_quorum_secret_class(&zookeeper),
548-
tls::quorum_tls_default().as_str()
559+
tls::quorum_tls_default().as_ref()
549560
);
550561

551562
let input = r#"
@@ -564,7 +575,7 @@ mod tests {
564575
serde_yaml::from_str(input).expect("illegal test input");
565576
assert_eq!(
566577
get_server_secret_class(&zookeeper),
567-
tls::server_tls_default().as_deref()
578+
tls::server_tls_default().as_ref().map(AsRef::as_ref)
568579
);
569580
assert_eq!(
570581
get_quorum_secret_class(&zookeeper),
@@ -588,11 +599,11 @@ mod tests {
588599

589600
assert_eq!(
590601
get_server_secret_class(&zookeeper),
591-
tls::server_tls_default().as_deref()
602+
tls::server_tls_default().as_ref().map(AsRef::as_ref)
592603
);
593604
assert_eq!(
594605
get_quorum_secret_class(&zookeeper),
595-
tls::quorum_tls_default()
606+
tls::quorum_tls_default().as_ref()
596607
);
597608

598609
let input = r#"
@@ -611,7 +622,7 @@ mod tests {
611622
serde_yaml::from_str(input).expect("illegal test input");
612623
assert_eq!(
613624
get_server_secret_class(&zookeeper),
614-
tls::server_tls_default().as_deref()
625+
tls::server_tls_default().as_ref().map(AsRef::as_ref)
615626
);
616627
assert_eq!(
617628
get_quorum_secret_class(&zookeeper),
@@ -638,7 +649,7 @@ mod tests {
638649
);
639650
assert_eq!(
640651
get_quorum_secret_class(&zookeeper),
641-
tls::quorum_tls_default().as_str()
652+
tls::quorum_tls_default().as_ref()
642653
);
643654
}
644655

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! and helper functions
55
//!
66
//! This is required due to overlaps between TLS encryption and e.g. mTLS authentication or Kerberos
7-
use std::collections::BTreeMap;
7+
use std::{collections::BTreeMap, str::FromStr};
88

99
use snafu::{ResultExt, Snafu};
1010
use stackable_operator::{
@@ -23,6 +23,7 @@ use stackable_operator::{
2323
crd::authentication::core,
2424
k8s_openapi::api::core::v1::Volume,
2525
shared::time::Duration,
26+
v2::types::{common::Port, kubernetes::SecretClassName},
2627
};
2728

2829
use crate::{
@@ -52,8 +53,8 @@ pub enum Error {
5253
/// Helper struct combining TLS settings for server and quorum with the resolved AuthenticationClasses
5354
pub struct ZookeeperSecurity {
5455
resolved_authentication_classes: DereferencedAuthenticationClasses,
55-
server_secret_class: Option<String>,
56-
quorum_secret_class: String,
56+
server_secret_class: Option<SecretClassName>,
57+
quorum_secret_class: SecretClassName,
5758
}
5859

5960
impl ZookeeperSecurity {
@@ -130,11 +131,11 @@ impl ZookeeperSecurity {
130131
}
131132

132133
/// Return the ZooKeeper (secure) client port depending on tls or authentication settings.
133-
pub fn client_port(&self) -> u16 {
134+
pub fn client_port(&self) -> Port {
134135
if self.tls_enabled() {
135-
Self::SECURE_CLIENT_PORT
136+
Port::from(Self::SECURE_CLIENT_PORT)
136137
} else {
137-
Self::CLIENT_PORT
138+
Port::from(Self::CLIENT_PORT)
138139
}
139140
}
140141

@@ -170,7 +171,7 @@ impl ZookeeperSecurity {
170171
pod_builder
171172
.add_volume(Self::create_quorum_tls_volume(
172173
tls_volume_name,
173-
&self.quorum_secret_class,
174+
self.quorum_secret_class.as_ref(),
174175
requested_secret_lifetime,
175176
)?)
176177
.context(AddVolumeSnafu)?;
@@ -279,19 +280,19 @@ impl ZookeeperSecurity {
279280
}
280281

281282
/// Returns the `SecretClass` provided in a `AuthenticationClass` for TLS.
282-
fn get_tls_secret_class(&self) -> Option<&String> {
283+
fn get_tls_secret_class(&self) -> Option<&str> {
283284
self.resolved_authentication_classes
284285
.get_tls_authentication_class()
285286
.and_then(|auth_class| match &auth_class.spec.provider {
286287
core::v1alpha1::AuthenticationClassProvider::Tls(tls) => {
287-
tls.client_cert_secret_class.as_ref()
288+
tls.client_cert_secret_class.as_deref()
288289
}
289290
core::v1alpha1::AuthenticationClassProvider::Ldap(_)
290291
| core::v1alpha1::AuthenticationClassProvider::Oidc(_)
291292
| core::v1alpha1::AuthenticationClassProvider::Static(_)
292293
| core::v1alpha1::AuthenticationClassProvider::Kerberos(_) => None,
293294
})
294-
.or(self.server_secret_class.as_ref())
295+
.or(self.server_secret_class.as_ref().map(AsRef::as_ref))
295296
}
296297

297298
/// Creates ephemeral volumes to mount the `SecretClass` with the listener-volume scope into the Pods.
@@ -352,8 +353,11 @@ impl ZookeeperSecurity {
352353
pub fn new_for_tests() -> Self {
353354
ZookeeperSecurity {
354355
resolved_authentication_classes: DereferencedAuthenticationClasses::new_for_tests(),
355-
server_secret_class: Some("tls".to_owned()),
356-
quorum_secret_class: "tls".to_string(),
356+
server_secret_class: Some(
357+
SecretClassName::from_str("tls").expect("'tls' is a valid SecretClass name"),
358+
),
359+
quorum_secret_class: SecretClassName::from_str("tls")
360+
.expect("'tls' is a valid SecretClass name"),
357361
}
358362
}
359363
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use std::str::FromStr;
2+
13
use serde::{Deserialize, Serialize};
24
use stackable_operator::{
35
schemars::{self, JsonSchema},
6+
v2::types::kubernetes::SecretClassName,
47
versioned::versioned,
58
};
69

@@ -19,7 +22,7 @@ pub mod versioned {
1922
///
2023
/// Defaults to `tls`
2124
#[serde(default = "quorum_tls_default")]
22-
pub quorum_secret_class: String,
25+
pub quorum_secret_class: SecretClassName,
2326

2427
/// The [SecretClass](DOCS_BASE_URL_PLACEHOLDER/secret-operator/secretclass) to use for
2528
/// client connections. This setting controls:
@@ -31,7 +34,7 @@ pub mod versioned {
3134
default = "server_tls_default",
3235
skip_serializing_if = "Option::is_none"
3336
)]
34-
pub server_secret_class: Option<String>,
37+
pub server_secret_class: Option<SecretClassName>,
3538
}
3639
}
3740

@@ -44,11 +47,12 @@ pub fn default_zookeeper_tls() -> Option<v1alpha1::ZookeeperTls> {
4447
}
4548

4649
/// Helper methods to provide defaults in the CRDs and tests
47-
pub fn server_tls_default() -> Option<String> {
48-
Some(TLS_DEFAULT_SECRET_CLASS.into())
50+
pub fn server_tls_default() -> Option<SecretClassName> {
51+
Some(quorum_tls_default())
4952
}
5053

5154
/// Helper methods to provide defaults in the CRDs and tests
52-
pub fn quorum_tls_default() -> String {
53-
TLS_DEFAULT_SECRET_CLASS.into()
55+
pub fn quorum_tls_default() -> SecretClassName {
56+
SecretClassName::from_str(TLS_DEFAULT_SECRET_CLASS)
57+
.expect("the default TLS secret class should be a valid SecretClass name")
5458
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn build_role_listener(
3636
.with_labels(cluster.recommended_labels(&role_group_name))
3737
.build(),
3838
spec: listener::v1alpha1::ListenerSpec {
39-
class_name: Some(cluster.cluster_config.listener_class.clone()),
39+
class_name: Some(cluster.cluster_config.listener_class.to_string()),
4040
ports: Some(listener_ports(&cluster.cluster_config.zookeeper_security)),
4141
..listener::v1alpha1::ListenerSpec::default()
4242
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ pub fn build_server_rolegroup_statefulset(
207207
);
208208

209209
let listener_pvc = build_role_listener_pvc(
210-
&role_listener_name(cluster.name.as_ref(), &ZookeeperRole::Server),
210+
role_listener_name(cluster.name.as_ref(), &ZookeeperRole::Server).as_ref(),
211211
&unversioned_recommended_labels,
212212
)?;
213213

@@ -329,7 +329,7 @@ pub fn build_server_rolegroup_statefulset(
329329
})
330330
.add_container_port(
331331
ZOOKEEPER_SERVER_PORT_NAME,
332-
zookeeper_security.client_port() as i32,
332+
i32::from(zookeeper_security.client_port()),
333333
)
334334
.add_container_port(ZOOKEEPER_LEADER_PORT_NAME, ZOOKEEPER_LEADER_PORT as i32)
335335
.add_container_port(ZOOKEEPER_ELECTION_PORT_NAME, ZOOKEEPER_ELECTION_PORT as i32)

0 commit comments

Comments
 (0)