Skip to content

Commit 8116051

Browse files
committed
Cleaning up and renaming vars. First working example
1 parent ff8ebe0 commit 8116051

2 files changed

Lines changed: 37 additions & 64 deletions

File tree

rust/operator-binary/src/controller.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ pub async fn reconcile_hive(
449449
.context(ApplyRoleBindingSnafu)?;
450450

451451
let mut ss_cond_builder = StatefulSetConditionBuilder::default();
452-
452+
let mut listeners = Vec::<Listener>::new();
453453
// for each rg a service needs to be build for hive
454454
for (rolegroup_name, rolegroup_config) in metastore_config.iter() {
455455
let rolegroup = hive.metastore_rolegroup_ref(rolegroup_name);
@@ -480,14 +480,14 @@ pub async fn reconcile_hive(
480480
&rbac_sa.name_any(),
481481
)?;
482482

483-
let rg_group_listener = build_group_listener(
483+
let rg_group_listener: Listener = build_group_listener(
484484
hive,
485485
&resolved_product_image,
486486
&rolegroup,
487487
config.listener_class,
488488
)?;
489-
490-
cluster_resources
489+
// TODO: Listener name might be wrong
490+
let listener = cluster_resources
491491
.add(client, rg_group_listener)
492492
.await
493493
.with_context(|_| ApplyGroupListenerSnafu {
@@ -501,6 +501,8 @@ pub async fn reconcile_hive(
501501
rolegroup: rolegroup.clone(),
502502
})?;
503503

504+
listeners.push(listener);
505+
504506
cluster_resources
505507
.add(client, rg_configmap)
506508
.await
@@ -531,8 +533,9 @@ pub async fn reconcile_hive(
531533
// std's SipHasher is deprecated, and DefaultHasher is unstable across Rust releases.
532534
// We don't /need/ stability, but it's still nice to avoid spurious changes where possible.
533535
let mut discovery_hash = FnvHasher::with_key(0);
536+
534537
for discovery_cm in
535-
discovery::build_discovery_configmaps(client, hive, hive, &resolved_product_image, None)
538+
discovery::build_discovery_configmaps(hive, hive, &resolved_product_image, None, &listeners)
536539
.await
537540
.context(BuildDiscoveryConfigSnafu)?
538541
{

rust/operator-binary/src/discovery.rs

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use snafu::{OptionExt, ResultExt, Snafu};
44
use stackable_operator::{
55
builder::{configmap::ConfigMapBuilder, meta::ObjectMetaBuilder},
66
commons::product_image_selection::ResolvedProductImage,
7+
crd::listener,
78
k8s_openapi::api::core::v1::{ConfigMap, Endpoints, Service},
89
kube::{Resource, runtime::reflector::ObjectRef},
910
};
1011

1112
use crate::{
1213
controller::build_recommended_labels,
13-
crd::{HIVE_PORT, HIVE_PORT_NAME, HiveRole, v1alpha1},
14+
crd::{HIVE_PORT_NAME, HiveRole, v1alpha1},
1415
};
1516

1617
#[derive(Snafu, Debug)]
@@ -31,10 +32,10 @@ pub enum Error {
3132
source: stackable_operator::builder::configmap::Error,
3233
obj_ref: ObjectRef<v1alpha1::HiveCluster>,
3334
},
34-
#[snafu(display("could not find service [{obj_ref}] port [{port_name}]"))]
35+
#[snafu(display("could not find port [{port_name}]"))]
3536
NoServicePort {
3637
port_name: String,
37-
obj_ref: ObjectRef<Service>,
38+
//obj_ref: ObjectRef<Service>,
3839
},
3940
#[snafu(display("service [{obj_ref}] port [{port_name}] does not have a nodePort "))]
4041
NoNodePort {
@@ -60,33 +61,25 @@ pub enum Error {
6061
/// Builds discovery [`ConfigMap`]s for connecting to a [`v1alpha1::HiveCluster`] for all expected
6162
/// scenarios.
6263
pub async fn build_discovery_configmaps(
63-
client: &stackable_operator::client::Client,
6464
owner: &impl Resource<DynamicType = ()>,
6565
hive: &v1alpha1::HiveCluster,
6666
resolved_product_image: &ResolvedProductImage,
6767
chroot: Option<&str>,
68+
listeners: &[listener::v1alpha1::Listener],
6869
) -> Result<Vec<ConfigMap>, Error> {
6970
let name = owner
7071
.meta()
7172
.name
7273
.as_ref()
7374
.context(InvalidOwnerNameForDiscoveryConfigMapSnafu)?;
74-
let namespace = owner
75-
.meta()
76-
.namespace
77-
.as_deref()
78-
.context(NoNamespaceSnafu)?;
79-
let cluster_domain = &client.kubernetes_cluster_info.cluster_domain;
75+
8076
let discovery_configmaps = vec![build_discovery_configmap(
8177
name,
8278
owner,
8379
hive,
8480
resolved_product_image,
8581
chroot,
86-
vec![(
87-
format!("{name}.{namespace}.svc.{cluster_domain}"),
88-
HIVE_PORT,
89-
)],
82+
listener_hosts(listeners, &HIVE_PORT_NAME)?,
9083
)?];
9184

9285
Ok(discovery_configmaps)
@@ -95,7 +88,7 @@ pub async fn build_discovery_configmaps(
9588
/// Build a discovery [`ConfigMap`] containing information about how to connect to a certain
9689
/// [`v1alpha1::HiveCluster`].
9790
///
98-
/// `hosts` will usually come from the cluster role service or [`nodeport_hosts`].
91+
/// `hosts` will usually come from the cluster role service or [`listener_hosts`].
9992
fn build_discovery_configmap(
10093
name: &str,
10194
owner: &impl Resource<DynamicType = ()>,
@@ -140,52 +133,29 @@ fn build_discovery_configmap(
140133
})
141134
}
142135

143-
/// Lists all nodes currently hosting Pods participating in the [`Service`]
144-
async fn nodeport_hosts(
145-
client: &stackable_operator::client::Client,
146-
svc: &Service,
136+
fn listener_hosts(
137+
listeners: &[listener::v1alpha1::Listener],
147138
port_name: &str,
148139
) -> Result<impl IntoIterator<Item = (String, u16)>, Error> {
149-
let svc_port = svc
150-
.spec
151-
.as_ref()
152-
.and_then(|svc_spec| {
153-
svc_spec
154-
.ports
155-
.as_ref()?
156-
.iter()
157-
.find(|port| port.name.as_deref() == Some(HIVE_PORT_NAME))
140+
listeners
141+
.iter()
142+
.flat_map(|listener| {
143+
listener
144+
.status
145+
.as_ref()
146+
.and_then(|s| s.ingress_addresses.as_deref())
158147
})
159-
.context(NoServicePortSnafu {
160-
port_name,
161-
obj_ref: ObjectRef::from_obj(svc),
162-
})?;
163-
164-
let node_port = svc_port.node_port.context(NoNodePortSnafu {
165-
port_name,
166-
obj_ref: ObjectRef::from_obj(svc),
167-
})?;
168-
let endpoints = client
169-
.get::<Endpoints>(
170-
svc.metadata.name.as_deref().context(NoNameSnafu)?,
171-
svc.metadata
172-
.namespace
173-
.as_deref()
174-
.context(NoNamespaceSnafu)?,
175-
)
176-
.await
177-
.with_context(|_| FindEndpointsSnafu {
178-
svc: ObjectRef::from_obj(svc),
179-
})?;
180-
let nodes = endpoints
181-
.subsets
182-
.into_iter()
183148
.flatten()
184-
.flat_map(|subset| subset.addresses)
185-
.flatten()
186-
.flat_map(|addr| addr.node_name);
187-
let addrs = nodes
188-
.map(|node| Ok((node, node_port.try_into().context(InvalidNodePortSnafu)?)))
189-
.collect::<Result<BTreeSet<_>, _>>()?;
190-
Ok(addrs)
149+
.map(|addr| {
150+
Ok((
151+
addr.address.clone(),
152+
addr.ports
153+
.get(port_name)
154+
.copied()
155+
.context(NoServicePortSnafu { port_name })?
156+
.try_into()
157+
.context(InvalidNodePortSnafu)?,
158+
))
159+
})
160+
.collect::<Result<Vec<_>, _>>()
191161
}

0 commit comments

Comments
 (0)