|
| 1 | +use snafu::{ResultExt, Snafu}; |
| 2 | +use stackable_operator::{ |
| 3 | + builder::meta::ObjectMetaBuilder, |
| 4 | + commons::product_image_selection::ResolvedProductImage, |
| 5 | + k8s_openapi::api::core::v1::{Service, ServicePort, ServiceSpec}, |
| 6 | + kvp::{Label, Labels}, |
| 7 | + role_utils::RoleGroupRef, |
| 8 | +}; |
| 9 | + |
| 10 | +use crate::{ |
| 11 | + crd::{APP_NAME, APP_PORT, APP_PORT_NAME, METRICS_PORT, METRICS_PORT_NAME, v1alpha1}, |
| 12 | + superset_controller::SUPERSET_CONTROLLER_NAME, |
| 13 | + util::build_recommended_labels, |
| 14 | +}; |
| 15 | +#[derive(Debug, Snafu)] |
| 16 | +pub enum Error { |
| 17 | + #[snafu(display("object is missing metadata to build owner reference"))] |
| 18 | + ObjectMissingMetadataForOwnerRef { |
| 19 | + source: stackable_operator::builder::meta::Error, |
| 20 | + }, |
| 21 | + #[snafu(display("failed to build Metadata"))] |
| 22 | + MetadataBuild { |
| 23 | + source: stackable_operator::builder::meta::Error, |
| 24 | + }, |
| 25 | + #[snafu(display("failed to build Labels"))] |
| 26 | + LabelBuild { |
| 27 | + source: stackable_operator::kvp::LabelError, |
| 28 | + }, |
| 29 | +} |
| 30 | + |
| 31 | +/// The rolegroup [`Service`] is a headless service that allows direct access to the instances of a certain rolegroup |
| 32 | +/// |
| 33 | +/// This is mostly useful for internal communication between peers, or for clients that perform client-side load balancing. |
| 34 | +pub fn build_node_rolegroup_headless_service( |
| 35 | + superset: &v1alpha1::SupersetCluster, |
| 36 | + resolved_product_image: &ResolvedProductImage, |
| 37 | + rolegroup: &RoleGroupRef<v1alpha1::SupersetCluster>, |
| 38 | +) -> Result<Service, Error> { |
| 39 | + let headless_service = Service { |
| 40 | + metadata: ObjectMetaBuilder::new() |
| 41 | + .name_and_namespace(superset) |
| 42 | + .name(rolegroup_headless_service_name(rolegroup)) |
| 43 | + .ownerreference_from_resource(superset, None, Some(true)) |
| 44 | + .context(ObjectMissingMetadataForOwnerRefSnafu)? |
| 45 | + .with_recommended_labels(build_recommended_labels( |
| 46 | + superset, |
| 47 | + SUPERSET_CONTROLLER_NAME, |
| 48 | + &resolved_product_image.app_version_label, |
| 49 | + &rolegroup.role, |
| 50 | + &rolegroup.role_group, |
| 51 | + )) |
| 52 | + .context(MetadataBuildSnafu)? |
| 53 | + .build(), |
| 54 | + spec: Some(ServiceSpec { |
| 55 | + // Internal communication does not need to be exposed |
| 56 | + type_: Some("ClusterIP".to_owned()), |
| 57 | + cluster_ip: Some("None".to_owned()), |
| 58 | + ports: Some(service_ports()), |
| 59 | + selector: Some( |
| 60 | + Labels::role_group_selector( |
| 61 | + superset, |
| 62 | + APP_NAME, |
| 63 | + &rolegroup.role, |
| 64 | + &rolegroup.role_group, |
| 65 | + ) |
| 66 | + .context(LabelBuildSnafu)? |
| 67 | + .into(), |
| 68 | + ), |
| 69 | + publish_not_ready_addresses: Some(true), |
| 70 | + ..ServiceSpec::default() |
| 71 | + }), |
| 72 | + status: None, |
| 73 | + }; |
| 74 | + Ok(headless_service) |
| 75 | +} |
| 76 | + |
| 77 | +/// The rolegroup metrics [`Service`] is a service that exposes metrics and a prometheus scraping label |
| 78 | +pub fn build_node_rolegroup_metrics_service( |
| 79 | + superset: &v1alpha1::SupersetCluster, |
| 80 | + resolved_product_image: &ResolvedProductImage, |
| 81 | + rolegroup: &RoleGroupRef<v1alpha1::SupersetCluster>, |
| 82 | +) -> Result<Service, Error> { |
| 83 | + let metrics_service = Service { |
| 84 | + metadata: ObjectMetaBuilder::new() |
| 85 | + .name_and_namespace(superset) |
| 86 | + .name(rolegroup_metrics_service_name(rolegroup)) |
| 87 | + .ownerreference_from_resource(superset, None, Some(true)) |
| 88 | + .context(ObjectMissingMetadataForOwnerRefSnafu)? |
| 89 | + .with_recommended_labels(build_recommended_labels( |
| 90 | + superset, |
| 91 | + SUPERSET_CONTROLLER_NAME, |
| 92 | + &resolved_product_image.app_version_label, |
| 93 | + &rolegroup.role, |
| 94 | + &rolegroup.role_group, |
| 95 | + )) |
| 96 | + .context(MetadataBuildSnafu)? |
| 97 | + .with_label(Label::try_from(("prometheus.io/scrape", "true")).context(LabelBuildSnafu)?) |
| 98 | + .build(), |
| 99 | + spec: Some(ServiceSpec { |
| 100 | + // Internal communication does not need to be exposed |
| 101 | + type_: Some("ClusterIP".to_owned()), |
| 102 | + cluster_ip: Some("None".to_owned()), |
| 103 | + ports: Some(metrics_ports()), |
| 104 | + selector: Some( |
| 105 | + Labels::role_group_selector( |
| 106 | + superset, |
| 107 | + APP_NAME, |
| 108 | + &rolegroup.role, |
| 109 | + &rolegroup.role_group, |
| 110 | + ) |
| 111 | + .context(LabelBuildSnafu)? |
| 112 | + .into(), |
| 113 | + ), |
| 114 | + publish_not_ready_addresses: Some(true), |
| 115 | + ..ServiceSpec::default() |
| 116 | + }), |
| 117 | + status: None, |
| 118 | + }; |
| 119 | + |
| 120 | + Ok(metrics_service) |
| 121 | +} |
| 122 | + |
| 123 | +/// Headless service for cluster internal purposes only. |
| 124 | +// TODO: Move to operator-rs |
| 125 | +pub fn rolegroup_headless_service_name( |
| 126 | + rolegroup: &RoleGroupRef<v1alpha1::SupersetCluster>, |
| 127 | +) -> String { |
| 128 | + format!("{name}-headless", name = rolegroup.object_name()) |
| 129 | +} |
| 130 | + |
| 131 | +/// Headless metrics service exposes Prometheus endpoint only |
| 132 | +// TODO: Move to operator-rs |
| 133 | +pub fn rolegroup_metrics_service_name( |
| 134 | + rolegroup: &RoleGroupRef<v1alpha1::SupersetCluster>, |
| 135 | +) -> String { |
| 136 | + format!("{name}-metrics", name = rolegroup.object_name()) |
| 137 | +} |
| 138 | + |
| 139 | +fn metrics_ports() -> Vec<ServicePort> { |
| 140 | + vec![ServicePort { |
| 141 | + name: Some(METRICS_PORT_NAME.to_string()), |
| 142 | + port: METRICS_PORT.into(), |
| 143 | + protocol: Some("TCP".to_string()), |
| 144 | + ..ServicePort::default() |
| 145 | + }] |
| 146 | +} |
| 147 | + |
| 148 | +fn service_ports() -> Vec<ServicePort> { |
| 149 | + vec![ServicePort { |
| 150 | + name: Some(APP_PORT_NAME.to_string()), |
| 151 | + port: APP_PORT.into(), |
| 152 | + protocol: Some("TCP".to_string()), |
| 153 | + ..ServicePort::default() |
| 154 | + }] |
| 155 | +} |
0 commit comments