Skip to content

Commit 8dfd7d6

Browse files
committed
clean up port retrieval methods
1 parent aabbecf commit 8dfd7d6

File tree

2 files changed

+90
-171
lines changed

2 files changed

+90
-171
lines changed

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

Lines changed: 67 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ pub const SSL_CLIENT_XML: &str = "ssl-client.xml";
6565
pub const HBASE_CLUSTER_DISTRIBUTED: &str = "hbase.cluster.distributed";
6666
pub const HBASE_ROOTDIR: &str = "hbase.rootdir";
6767

68-
pub const HBASE_UI_PORT_NAME_HTTP: &str = "ui-http";
69-
pub const HBASE_UI_PORT_NAME_HTTPS: &str = "ui-https";
70-
pub const HBASE_REST_PORT_NAME_HTTP: &str = "rest-http";
71-
pub const HBASE_REST_PORT_NAME_HTTPS: &str = "rest-https";
72-
pub const HBASE_METRICS_PORT_NAME: &str = "metrics";
68+
const HBASE_UI_PORT_NAME_HTTP: &str = "ui-http";
69+
const HBASE_UI_PORT_NAME_HTTPS: &str = "ui-https";
70+
const HBASE_REST_PORT_NAME_HTTP: &str = "rest-http";
71+
const HBASE_REST_PORT_NAME_HTTPS: &str = "rest-https";
72+
const HBASE_METRICS_PORT_NAME: &str = "metrics";
7373

7474
pub const HBASE_MASTER_PORT: u16 = 16000;
7575
// HBase always uses 16010, regardless of http or https. On 2024-01-17 we decided in Arch-meeting that we want to stick
@@ -517,78 +517,6 @@ impl v1alpha1::HbaseCluster {
517517
.as_ref()
518518
.map(|a| a.tls_secret_class.clone())
519519
}
520-
521-
/// Returns required port name and port number tuples depending on the role.
522-
/// Hbase versions 2.6.* will have two ports for each role. The metrics are available over the
523-
/// UI port.
524-
pub fn ports(&self, role: &HbaseRole) -> Vec<(String, u16)> {
525-
match role {
526-
HbaseRole::Master => vec![
527-
("master".to_string(), HBASE_MASTER_PORT),
528-
(self.ui_port_name(), HBASE_MASTER_UI_PORT),
529-
],
530-
HbaseRole::RegionServer => vec![
531-
("regionserver".to_string(), HBASE_REGIONSERVER_PORT),
532-
(self.ui_port_name(), HBASE_REGIONSERVER_UI_PORT),
533-
],
534-
HbaseRole::RestServer => vec![
535-
(
536-
if self.has_https_enabled() {
537-
HBASE_REST_PORT_NAME_HTTPS
538-
} else {
539-
HBASE_REST_PORT_NAME_HTTP
540-
}
541-
.to_string(),
542-
HBASE_REST_PORT,
543-
),
544-
(self.ui_port_name(), HBASE_REST_UI_PORT),
545-
],
546-
}
547-
}
548-
549-
/// Returns required metrics port name and metrics port number tuples depending on the role.
550-
/// The metrics are available over the UI port.
551-
pub fn metrics_ports(&self, role: &HbaseRole) -> Vec<(String, u16)> {
552-
match role {
553-
HbaseRole::Master => vec![(
554-
HBASE_METRICS_PORT_NAME.to_owned(),
555-
HBASE_MASTER_METRICS_PORT,
556-
)],
557-
HbaseRole::RegionServer => vec![(
558-
HBASE_METRICS_PORT_NAME.to_owned(),
559-
HBASE_REGIONSERVER_METRICS_PORT,
560-
)],
561-
HbaseRole::RestServer => {
562-
vec![(HBASE_METRICS_PORT_NAME.to_owned(), HBASE_REST_METRICS_PORT)]
563-
}
564-
}
565-
}
566-
567-
pub fn service_port(&self, role: &HbaseRole) -> u16 {
568-
match role {
569-
HbaseRole::Master => HBASE_MASTER_PORT,
570-
HbaseRole::RegionServer => HBASE_REGIONSERVER_PORT,
571-
HbaseRole::RestServer => HBASE_REST_PORT,
572-
}
573-
}
574-
575-
pub fn metrics_port(&self, role: &HbaseRole) -> u16 {
576-
match role {
577-
HbaseRole::Master => HBASE_MASTER_METRICS_PORT,
578-
HbaseRole::RegionServer => HBASE_REGIONSERVER_METRICS_PORT,
579-
HbaseRole::RestServer => HBASE_REST_METRICS_PORT,
580-
}
581-
}
582-
583-
/// Name of the port used by the Web UI, which depends on HTTPS usage
584-
pub fn ui_port_name(&self) -> String {
585-
if self.has_https_enabled() {
586-
HBASE_UI_PORT_NAME_HTTPS
587-
} else {
588-
HBASE_UI_PORT_NAME_HTTP
589-
}
590-
.to_string()
591-
}
592520
}
593521

594522
pub fn merged_env(rolegroup_config: Option<&BTreeMap<String, String>>) -> Vec<EnvVar> {
@@ -789,6 +717,68 @@ impl HbaseRole {
789717
};
790718
Ok(pvc)
791719
}
720+
721+
/// Returns required port name and port number tuples depending on the role.
722+
/// Hbase versions 2.6.* will have two ports for each role. The metrics are available over the
723+
/// UI port.
724+
pub fn ports(&self, hbase: &v1alpha1::HbaseCluster) -> Vec<(String, u16)> {
725+
vec![
726+
(self.data_port_name(hbase), self.data_port()),
727+
(Self::ui_port_name(hbase).to_string(), self.ui_port()),
728+
]
729+
}
730+
731+
pub fn data_port(&self) -> u16 {
732+
match self {
733+
HbaseRole::Master => HBASE_MASTER_PORT,
734+
HbaseRole::RegionServer => HBASE_REGIONSERVER_PORT,
735+
HbaseRole::RestServer => HBASE_REST_PORT,
736+
}
737+
}
738+
739+
pub fn data_port_name(&self, hbase: &v1alpha1::HbaseCluster) -> String {
740+
match self {
741+
HbaseRole::Master | HbaseRole::RegionServer => self.to_string(),
742+
HbaseRole::RestServer => {
743+
if hbase.has_https_enabled() {
744+
HBASE_REST_PORT_NAME_HTTPS.to_owned()
745+
} else {
746+
HBASE_REST_PORT_NAME_HTTP.to_owned()
747+
}
748+
}
749+
}
750+
}
751+
752+
pub fn ui_port(&self) -> u16 {
753+
match self {
754+
HbaseRole::Master => HBASE_MASTER_UI_PORT,
755+
HbaseRole::RegionServer => HBASE_REGIONSERVER_UI_PORT,
756+
HbaseRole::RestServer => HBASE_REST_UI_PORT,
757+
}
758+
}
759+
760+
/// Name of the port used by the Web UI, which depends on HTTPS usage
761+
pub fn ui_port_name(hbase: &v1alpha1::HbaseCluster) -> &str {
762+
if hbase.has_https_enabled() {
763+
HBASE_UI_PORT_NAME_HTTPS
764+
} else {
765+
HBASE_UI_PORT_NAME_HTTP
766+
}
767+
}
768+
769+
pub fn metrics_port(&self) -> u16 {
770+
match self {
771+
HbaseRole::Master => HBASE_MASTER_METRICS_PORT,
772+
HbaseRole::RegionServer => HBASE_REGIONSERVER_METRICS_PORT,
773+
HbaseRole::RestServer => HBASE_REST_METRICS_PORT,
774+
}
775+
}
776+
777+
pub fn metrics_port_name(&self) -> &str {
778+
match self {
779+
_ => HBASE_METRICS_PORT_NAME,
780+
}
781+
}
792782
}
793783

794784
fn default_resources(role: &HbaseRole) -> ResourcesFragment<HbaseStorageConfig, NoRuntimeLimits> {

rust/operator-binary/src/hbase_controller.rs

Lines changed: 23 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ use crate::{
7575
},
7676
crd::{
7777
APP_NAME, AnyServiceConfig, Container, HBASE_ENV_SH, HBASE_MASTER_PORT,
78-
HBASE_MASTER_UI_PORT, HBASE_REGIONSERVER_PORT, HBASE_REGIONSERVER_UI_PORT,
79-
HBASE_REST_PORT_NAME_HTTP, HBASE_REST_PORT_NAME_HTTPS, HBASE_SITE_XML, HbaseClusterStatus,
80-
HbaseRole, JVM_SECURITY_PROPERTIES_FILE, LISTENER_VOLUME_DIR, LISTENER_VOLUME_NAME,
81-
SSL_CLIENT_XML, SSL_SERVER_XML, merged_env, v1alpha1,
78+
HBASE_MASTER_UI_PORT, HBASE_REGIONSERVER_PORT, HBASE_REGIONSERVER_UI_PORT, HBASE_SITE_XML,
79+
HbaseClusterStatus, HbaseRole, JVM_SECURITY_PROPERTIES_FILE, LISTENER_VOLUME_DIR,
80+
LISTENER_VOLUME_NAME, SSL_CLIENT_XML, SSL_SERVER_XML, merged_env, v1alpha1,
8281
},
8382
discovery::build_discovery_configmap,
8483
kerberos::{
@@ -108,9 +107,6 @@ const HBASE_LOG_CONFIG_TMP_DIR: &str = "/stackable/tmp/log_config";
108107

109108
const DOCKER_IMAGE_BASE_NAME: &str = "hbase";
110109

111-
const HBASE_MASTER_PORT_NAME: &str = "master";
112-
const HBASE_REGIONSERVER_PORT_NAME: &str = "regionserver";
113-
114110
pub struct Ctx {
115111
pub client: stackable_operator::client::Client,
116112
pub product_config: ProductConfigManager,
@@ -125,21 +121,6 @@ pub enum Error {
125121
#[snafu(display("missing secret lifetime"))]
126122
MissingSecretLifetime,
127123

128-
#[snafu(display("object defines no version"))]
129-
ObjectHasNoVersion,
130-
131-
#[snafu(display("object defines no namespace"))]
132-
ObjectHasNoNamespace,
133-
134-
#[snafu(display("object defines no master role"))]
135-
NoMasterRole,
136-
137-
#[snafu(display("the HBase role [{role}] is missing from spec"))]
138-
MissingHbaseRole { role: String },
139-
140-
#[snafu(display("object defines no regionserver role"))]
141-
NoRegionServerRole,
142-
143124
#[snafu(display("failed to create cluster resources"))]
144125
CreateClusterResources {
145126
source: stackable_operator::cluster_resources::Error,
@@ -206,12 +187,6 @@ pub enum Error {
206187
cm_name: String,
207188
},
208189

209-
#[snafu(display("failed to retrieve the entry {entry} for config map {cm_name}"))]
210-
MissingConfigMapEntry {
211-
entry: &'static str,
212-
cm_name: String,
213-
},
214-
215190
#[snafu(display("failed to patch service account"))]
216191
ApplyServiceAccount {
217192
source: stackable_operator::cluster_resources::Error,
@@ -228,9 +203,6 @@ pub enum Error {
228203
role: String,
229204
},
230205

231-
#[snafu(display("failed to retrieve Hbase role group: {source}"))]
232-
UnidentifiedHbaseRoleGroup { source: crate::crd::Error },
233-
234206
#[snafu(display("failed to resolve and merge config for role and role group"))]
235207
FailedToResolveConfig { source: crate::crd::Error },
236208

@@ -289,9 +261,6 @@ pub enum Error {
289261
#[snafu(display("unknown role [{role}]"))]
290262
UnknownHbaseRole { source: ParseError, role: String },
291263

292-
#[snafu(display("authorization is only supported from HBase 2.6 onwards"))]
293-
AuthorizationNotSupported,
294-
295264
#[snafu(display("failed to configure logging"))]
296265
ConfigureLogging { source: LoggingError },
297266

@@ -742,8 +711,8 @@ fn build_rolegroup_service(
742711
rolegroup: &RoleGroupRef<v1alpha1::HbaseCluster>,
743712
resolved_product_image: &ResolvedProductImage,
744713
) -> Result<Service> {
745-
let ports = hbase
746-
.ports(hbase_role)
714+
let ports = hbase_role
715+
.ports(hbase)
747716
.into_iter()
748717
.map(|(name, value)| ServicePort {
749718
name: Some(name),
@@ -795,16 +764,12 @@ pub fn build_rolegroup_metrics_service(
795764
rolegroup: &RoleGroupRef<v1alpha1::HbaseCluster>,
796765
resolved_product_image: &ResolvedProductImage,
797766
) -> Result<Service, Error> {
798-
let ports = hbase
799-
.metrics_ports(hbase_role)
800-
.into_iter()
801-
.map(|(name, value)| ServicePort {
802-
name: Some(name),
803-
port: i32::from(value),
804-
protocol: Some("TCP".to_owned()),
805-
..ServicePort::default()
806-
})
807-
.collect();
767+
let ports = vec![ServicePort {
768+
name: Some(hbase_role.metrics_port_name().to_owned()),
769+
port: i32::from(hbase_role.metrics_port()),
770+
protocol: Some("TCP".to_owned()),
771+
..ServicePort::default()
772+
}];
808773

809774
let service_selector =
810775
Labels::role_group_selector(hbase, APP_NAME, &rolegroup.role, &rolegroup.role_group)
@@ -849,7 +814,7 @@ fn prometheus_annotations(hbase: &v1alpha1::HbaseCluster, hbase_role: &HbaseRole
849814
("prometheus.io/path".to_owned(), "/prometheus".to_owned()),
850815
(
851816
"prometheus.io/port".to_owned(),
852-
hbase.metrics_port(hbase_role).to_string(),
817+
hbase_role.metrics_port().to_string(),
853818
),
854819
(
855820
"prometheus.io/scheme".to_owned(),
@@ -879,8 +844,8 @@ fn build_rolegroup_statefulset(
879844
) -> Result<StatefulSet> {
880845
let hbase_version = &resolved_product_image.app_version_label_value;
881846

882-
let ports = hbase
883-
.ports(hbase_role)
847+
let ports = hbase_role
848+
.ports(hbase)
884849
.into_iter()
885850
.map(|(name, value)| ContainerPort {
886851
name: Some(name),
@@ -890,38 +855,12 @@ fn build_rolegroup_statefulset(
890855
})
891856
.collect();
892857

893-
let probe_template = match hbase_role {
894-
HbaseRole::Master => Probe {
895-
tcp_socket: Some(TCPSocketAction {
896-
port: IntOrString::String(HBASE_MASTER_PORT_NAME.to_string()),
897-
..TCPSocketAction::default()
898-
}),
899-
..Probe::default()
900-
},
901-
HbaseRole::RegionServer => Probe {
902-
tcp_socket: Some(TCPSocketAction {
903-
port: IntOrString::String(HBASE_REGIONSERVER_PORT_NAME.to_string()),
904-
..TCPSocketAction::default()
905-
}),
906-
..Probe::default()
907-
},
908-
HbaseRole::RestServer => Probe {
909-
// We cant use HTTPGetAction, as it returns a 401 in case kerberos is enabled, and there is currently no way
910-
// to tell Kubernetes an 401 is healthy. As an alternative we run curl ourselves and check the http status
911-
// code there.
912-
tcp_socket: Some(TCPSocketAction {
913-
port: IntOrString::String(
914-
if hbase.has_https_enabled() {
915-
HBASE_REST_PORT_NAME_HTTPS
916-
} else {
917-
HBASE_REST_PORT_NAME_HTTP
918-
}
919-
.to_string(),
920-
),
921-
..TCPSocketAction::default()
922-
}),
923-
..Probe::default()
924-
},
858+
let probe_template = Probe {
859+
tcp_socket: Some(TCPSocketAction {
860+
port: IntOrString::String(hbase_role.data_port_name(hbase)),
861+
..TCPSocketAction::default()
862+
}),
863+
..Probe::default()
925864
};
926865

927866
let startup_probe = Probe {
@@ -967,26 +906,16 @@ fn build_rolegroup_statefulset(
967906
let role_name = hbase_role.cli_role_name();
968907
let mut hbase_container = ContainerBuilder::new("hbase").expect("ContainerBuilder not created");
969908

970-
let rest_http_port_name = if hbase.has_https_enabled() {
971-
HBASE_REST_PORT_NAME_HTTPS
972-
} else {
973-
HBASE_REST_PORT_NAME_HTTP
974-
};
975-
976909
hbase_container
977910
.image_from_product_image(resolved_product_image)
978911
.command(command())
979912
.args(vec![formatdoc! {"
980913
{entrypoint} {role} {port} {port_name} {ui_port_name}",
981914
entrypoint = "/stackable/hbase/bin/hbase-entrypoint.sh".to_string(),
982915
role = role_name,
983-
port = hbase.service_port(hbase_role).to_string(),
984-
port_name = match hbase_role {
985-
HbaseRole::Master => HBASE_MASTER_PORT_NAME,
986-
HbaseRole::RegionServer => HBASE_REGIONSERVER_PORT_NAME,
987-
HbaseRole::RestServer => rest_http_port_name,
988-
},
989-
ui_port_name = hbase.ui_port_name(),
916+
port = hbase_role.data_port(),
917+
port_name = hbase_role.data_port_name(hbase),
918+
ui_port_name = HbaseRole::ui_port_name(hbase),
990919
}])
991920
.add_env_vars(merged_env)
992921
// Needed for the `containerdebug` process to log it's tracing information to.

0 commit comments

Comments
 (0)