Skip to content

Commit 60bb94f

Browse files
committed
fix(znode): use v2 types
1 parent d905a72 commit 60bb94f

6 files changed

Lines changed: 185 additions & 87 deletions

File tree

rust/operator-binary/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ pub mod crd;
4444
mod listener;
4545
mod operations;
4646
mod service;
47-
mod utils;
4847
mod webhooks;
4948
mod zk_controller;
5049
mod znode_controller;

rust/operator-binary/src/utils.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

rust/operator-binary/src/zk_controller/build/discovery.rs

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
1-
use std::{collections::BTreeSet, num::TryFromIntError};
1+
use std::{collections::BTreeSet, num::TryFromIntError, str::FromStr};
22

33
use snafu::{OptionExt, ResultExt, Snafu};
44
use stackable_operator::{
55
builder::{configmap::ConfigMapBuilder, meta::ObjectMetaBuilder},
6-
commons::product_image_selection::ResolvedProductImage,
76
crd::listener,
87
k8s_openapi::api::core::v1::ConfigMap,
9-
kube::{Resource, ResourceExt, runtime::reflector::ObjectRef},
8+
kube::{Resource, runtime::reflector::ObjectRef},
9+
v2::{
10+
HasName, HasUid, NameIsValidLabelValue,
11+
builder::meta::ownerreference_from_resource,
12+
kvp::label::recommended_labels,
13+
types::operator::{ControllerName, ProductVersion, RoleGroupName},
14+
},
1015
};
1116

1217
use crate::{
13-
crd::{ZOOKEEPER_SERVER_PORT_NAME, ZookeeperRole, security::ZookeeperSecurity, v1alpha1},
14-
utils::build_recommended_labels,
15-
zk_controller::validate::ValidatedCluster,
18+
crd::{ZOOKEEPER_SERVER_PORT_NAME, security::ZookeeperSecurity},
19+
zk_controller::validate::{ValidatedCluster, operator_name, product_name},
20+
znode_controller::validate::ValidatedZnode,
1621
};
1722

1823
type Result<T, E = Error> = std::result::Result<T, E>;
1924

2025
#[derive(Snafu, Debug)]
2126
pub enum Error {
22-
#[snafu(display("object is missing metadata to build owner reference"))]
23-
ObjectMissingMetadataForOwnerRef {
24-
source: stackable_operator::builder::meta::Error,
25-
},
26-
2727
#[snafu(display("chroot path {} was relative (must be absolute)", chroot))]
2828
RelativeChroot { chroot: String },
2929

30-
#[snafu(display("object has no namespace associated"))]
31-
NoNamespace,
32-
3330
#[snafu(display("{listener} does not have a port with the name {port_name:?}"))]
3431
PortNotFound {
3532
port_name: String,
@@ -51,11 +48,6 @@ pub enum Error {
5148
BuildConfigMap {
5249
source: stackable_operator::builder::configmap::Error,
5350
},
54-
55-
#[snafu(display("failed to build object meta data"))]
56-
ObjectMeta {
57-
source: stackable_operator::builder::meta::Error,
58-
},
5951
}
6052

6153
/// Build the discovery [`ConfigMap`] for the cluster controller from the
@@ -72,35 +64,33 @@ pub fn build_discovery_configmap(
7264
validated_cluster,
7365
&validated_cluster.namespace,
7466
controller_name,
67+
&validated_cluster.product_version,
7568
listener,
7669
None,
77-
&validated_cluster.image,
7870
&validated_cluster.cluster_config.zookeeper_security,
7971
)
8072
}
8173

8274
/// Build the discovery [`ConfigMap`] for the znode controller.
8375
///
8476
/// The ConfigMap is owned by, and placed in the namespace of, the
85-
/// [`ZookeeperZnode`](v1alpha1::ZookeeperZnode). The `image` and `zookeeper_security` originate from
86-
/// the referenced cluster, while `chroot` isolates the znode within the shared ZooKeeper ensemble.
77+
/// [`ValidatedZnode`]. The product version and `zookeeper_security` originate from the referenced
78+
/// cluster (via the validated znode), while `chroot` isolates the znode within the shared ZooKeeper
79+
/// ensemble.
8780
pub fn build_znode_discovery_configmap(
88-
znode: &v1alpha1::ZookeeperZnode,
81+
validated_znode: &ValidatedZnode,
8982
controller_name: &str,
9083
listener: listener::v1alpha1::Listener,
9184
chroot: &str,
92-
image: &ResolvedProductImage,
93-
zookeeper_security: &ZookeeperSecurity,
9485
) -> Result<ConfigMap> {
95-
let namespace = znode.namespace().context(NoNamespaceSnafu)?;
9686
build_discovery_configmap_for_owner(
97-
znode,
98-
namespace,
87+
validated_znode,
88+
&validated_znode.namespace,
9989
controller_name,
90+
&validated_znode.product_version,
10091
listener,
10192
Some(chroot),
102-
image,
103-
zookeeper_security,
93+
&validated_znode.zookeeper_security,
10494
)
10595
}
10696

@@ -111,15 +101,23 @@ pub fn build_znode_discovery_configmap(
111101
/// controller, or the [`ZookeeperZnode`](v1alpha1::ZookeeperZnode) for the znode controller) and
112102
/// `namespace` is where the ConfigMap is placed.
113103
fn build_discovery_configmap_for_owner(
114-
owner: &impl Resource<DynamicType = ()>,
104+
owner: &(impl Resource<DynamicType = ()> + HasName + HasUid + NameIsValidLabelValue),
115105
namespace: impl Into<String>,
116106
controller_name: &str,
107+
product_version: &ProductVersion,
117108
listener: listener::v1alpha1::Listener,
118109
chroot: Option<&str>,
119-
image: &ResolvedProductImage,
120110
zookeeper_security: &ZookeeperSecurity,
121111
) -> Result<ConfigMap> {
122-
let name = owner.name_unchecked();
112+
let name = owner.to_name();
113+
114+
// The discovery ConfigMap is a role-level resource of the `server` role, conventionally
115+
// labelled with the `discovery` role group. The controller name differs between the cluster and
116+
// znode controllers, so it is passed in and validated into the type-safe newtype here.
117+
let controller_name = ControllerName::from_str(controller_name)
118+
.expect("the controller name is a valid label value");
119+
let role_group_name =
120+
RoleGroupName::from_str("discovery").expect("'discovery' is a valid role group name");
123121

124122
let listener_addresses = listener_addresses(&listener, ZOOKEEPER_SERVER_PORT_NAME)?;
125123

@@ -143,16 +141,16 @@ fn build_discovery_configmap_for_owner(
143141
ObjectMetaBuilder::new()
144142
.name(name)
145143
.namespace(namespace)
146-
.ownerreference_from_resource(owner, None, Some(true))
147-
.context(ObjectMissingMetadataForOwnerRefSnafu)?
148-
.with_recommended_labels(&build_recommended_labels(
144+
.ownerreference(ownerreference_from_resource(owner, None, Some(true)))
145+
.with_labels(recommended_labels(
149146
owner,
150-
controller_name,
151-
&image.app_version_label_value,
152-
&ZookeeperRole::Server.to_string(),
153-
"discovery",
147+
&product_name(),
148+
product_version,
149+
&operator_name(),
150+
&controller_name,
151+
&ValidatedCluster::role_name(),
152+
&role_group_name,
154153
))
155-
.context(ObjectMetaSnafu)?
156154
.build(),
157155
)
158156
.add_data("ZOOKEEPER", conn_str)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,12 @@ impl ValidatedCluster {
220220
}
221221

222222
/// The product name (`zookeeper`) as a type-safe label value.
223-
fn product_name() -> ProductName {
223+
pub(crate) fn product_name() -> ProductName {
224224
ProductName::from_str(APP_NAME).expect("'zookeeper' is a valid product name")
225225
}
226226

227227
/// The operator name as a type-safe label value.
228-
fn operator_name() -> OperatorName {
228+
pub(crate) fn operator_name() -> OperatorName {
229229
OperatorName::from_str(OPERATOR_NAME).expect("the operator name is a valid label value")
230230
}
231231

rust/operator-binary/src/znode_controller.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use snafu::{OptionExt, ResultExt, Snafu};
88
use stackable_operator::{
99
cli::OperatorEnvironmentOptions,
1010
cluster_resources::{ClusterResourceApplyStrategy, ClusterResources},
11-
commons::product_image_selection::ResolvedProductImage,
1211
crd::listener,
1312
k8s_openapi::api::core::v1::ConfigMap,
1413
kube::{
@@ -32,7 +31,7 @@ use crate::{
3231
};
3332

3433
mod dereference;
35-
mod validate;
34+
pub(crate) mod validate;
3635

3736
pub const ZNODE_CONTROLLER_NAME: &str = "znode";
3837
pub const ZNODE_FULL_CONTROLLER_NAME: &str = concatcp!(ZNODE_CONTROLLER_NAME, '.', OPERATOR_NAME);
@@ -232,18 +231,15 @@ pub async fn reconcile_znode(
232231
match ev {
233232
finalizer::Event::Apply(znode) => {
234233
let dereferenced = dereferenced_objects.context(DereferenceSnafu)?;
235-
let validate::ValidatedZnode {
236-
image,
237-
zookeeper_security,
238-
} = validate::validate(&znode, &dereferenced, &ctx.operator_environment)
239-
.context(ValidateClusterSnafu)?;
234+
let validated_znode =
235+
validate::validate(&znode, &dereferenced, &ctx.operator_environment)
236+
.context(ValidateClusterSnafu)?;
240237
reconcile_apply(
241238
client,
242239
&znode,
240+
&validated_znode,
243241
dereferenced.zk,
244-
&zookeeper_security,
245242
&znode_path,
246-
&image,
247243
)
248244
.await
249245
}
@@ -276,10 +272,9 @@ pub async fn reconcile_znode(
276272
async fn reconcile_apply(
277273
client: &stackable_operator::client::Client,
278274
znode: &v1alpha1::ZookeeperZnode,
275+
validated_znode: &validate::ValidatedZnode,
279276
zk: v1alpha1::ZookeeperCluster,
280-
zookeeper_security: &ZookeeperSecurity,
281277
znode_path: &str,
282-
image: &ResolvedProductImage,
283278
) -> Result<controller::Action> {
284279
let mut cluster_resources = ClusterResources::new(
285280
APP_NAME,
@@ -292,7 +287,11 @@ async fn reconcile_apply(
292287
.context(ZnodeMissingExpectedKeysSnafu { znode })?;
293288

294289
znode_mgmt::ensure_znode_exists(
295-
&zk_mgmt_addr(&zk, zookeeper_security, &client.kubernetes_cluster_info)?,
290+
&zk_mgmt_addr(
291+
&zk,
292+
&validated_znode.zookeeper_security,
293+
&client.kubernetes_cluster_info,
294+
)?,
296295
znode_path,
297296
)
298297
.await
@@ -315,12 +314,10 @@ async fn reconcile_apply(
315314
})?;
316315

317316
let discovery_cm = build_znode_discovery_configmap(
318-
znode,
317+
validated_znode,
319318
ZNODE_CONTROLLER_NAME,
320319
listener,
321320
znode_path,
322-
image,
323-
zookeeper_security,
324321
)
325322
.context(BuildDiscoveryConfigMapSnafu)?;
326323

0 commit comments

Comments
 (0)