@@ -3,7 +3,7 @@ use stackable_operator::{
33 builder:: meta:: ObjectMetaBuilder ,
44 commons:: product_image_selection:: ResolvedProductImage ,
55 k8s_openapi:: api:: core:: v1:: { Service , ServicePort , ServiceSpec } ,
6- kvp:: { Label , Labels } ,
6+ kvp:: { Annotations , Labels } ,
77 role_utils:: RoleGroupRef ,
88} ;
99
@@ -34,20 +34,20 @@ pub enum Error {
3434pub fn build_node_rolegroup_headless_service (
3535 superset : & v1alpha1:: SupersetCluster ,
3636 resolved_product_image : & ResolvedProductImage ,
37- rolegroup : & RoleGroupRef < v1alpha1:: SupersetCluster > ,
37+ rolegroup_ref : & RoleGroupRef < v1alpha1:: SupersetCluster > ,
3838) -> Result < Service , Error > {
3939 let headless_service = Service {
4040 metadata : ObjectMetaBuilder :: new ( )
4141 . name_and_namespace ( superset)
42- . name ( rolegroup_headless_service_name ( rolegroup ) )
42+ . name ( rolegroup_ref . rolegroup_headless_service_name ( ) )
4343 . ownerreference_from_resource ( superset, None , Some ( true ) )
4444 . context ( ObjectMissingMetadataForOwnerRefSnafu ) ?
4545 . with_recommended_labels ( build_recommended_labels (
4646 superset,
4747 SUPERSET_CONTROLLER_NAME ,
4848 & resolved_product_image. app_version_label_value ,
49- & rolegroup . role ,
50- & rolegroup . role_group ,
49+ & rolegroup_ref . role ,
50+ & rolegroup_ref . role_group ,
5151 ) )
5252 . context ( MetadataBuildSnafu ) ?
5353 . build ( ) ,
@@ -60,8 +60,8 @@ pub fn build_node_rolegroup_headless_service(
6060 Labels :: role_group_selector (
6161 superset,
6262 APP_NAME ,
63- & rolegroup . role ,
64- & rolegroup . role_group ,
63+ & rolegroup_ref . role ,
64+ & rolegroup_ref . role_group ,
6565 )
6666 . context ( LabelBuildSnafu ) ?
6767 . into ( ) ,
@@ -78,23 +78,24 @@ pub fn build_node_rolegroup_headless_service(
7878pub fn build_node_rolegroup_metrics_service (
7979 superset : & v1alpha1:: SupersetCluster ,
8080 resolved_product_image : & ResolvedProductImage ,
81- rolegroup : & RoleGroupRef < v1alpha1:: SupersetCluster > ,
81+ rolegroup_ref : & RoleGroupRef < v1alpha1:: SupersetCluster > ,
8282) -> Result < Service , Error > {
8383 let metrics_service = Service {
8484 metadata : ObjectMetaBuilder :: new ( )
8585 . name_and_namespace ( superset)
86- . name ( rolegroup_metrics_service_name ( rolegroup ) )
86+ . name ( rolegroup_ref . rolegroup_metrics_service_name ( ) )
8787 . ownerreference_from_resource ( superset, None , Some ( true ) )
8888 . context ( ObjectMissingMetadataForOwnerRefSnafu ) ?
8989 . with_recommended_labels ( build_recommended_labels (
9090 superset,
9191 SUPERSET_CONTROLLER_NAME ,
9292 & resolved_product_image. app_version_label_value ,
93- & rolegroup . role ,
94- & rolegroup . role_group ,
93+ & rolegroup_ref . role ,
94+ & rolegroup_ref . role_group ,
9595 ) )
9696 . context ( MetadataBuildSnafu ) ?
97- . with_label ( Label :: try_from ( ( "prometheus.io/scrape" , "true" ) ) . context ( LabelBuildSnafu ) ?)
97+ . with_labels ( prometheus_labels ( ) )
98+ . with_annotations ( prometheus_annotations ( ) )
9899 . build ( ) ,
99100 spec : Some ( ServiceSpec {
100101 // Internal communication does not need to be exposed
@@ -105,8 +106,8 @@ pub fn build_node_rolegroup_metrics_service(
105106 Labels :: role_group_selector (
106107 superset,
107108 APP_NAME ,
108- & rolegroup . role ,
109- & rolegroup . role_group ,
109+ & rolegroup_ref . role ,
110+ & rolegroup_ref . role_group ,
110111 )
111112 . context ( LabelBuildSnafu ) ?
112113 . into ( ) ,
@@ -120,22 +121,6 @@ pub fn build_node_rolegroup_metrics_service(
120121 Ok ( metrics_service)
121122}
122123
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-
139124fn metrics_ports ( ) -> Vec < ServicePort > {
140125 vec ! [ ServicePort {
141126 name: Some ( METRICS_PORT_NAME . to_string( ) ) ,
@@ -153,3 +138,22 @@ fn service_ports() -> Vec<ServicePort> {
153138 ..ServicePort :: default ( )
154139 } ]
155140}
141+ /// Common labels for Prometheus
142+ fn prometheus_labels ( ) -> Labels {
143+ Labels :: try_from ( [ ( "prometheus.io/scrape" , "true" ) ] ) . expect ( "should be a valid label" )
144+ }
145+
146+ /// Common annotations for Prometheus
147+ ///
148+ /// These annotations can be used in a ServiceMonitor.
149+ ///
150+ /// see also <https://github.com/prometheus-community/helm-charts/blob/prometheus-27.32.0/charts/prometheus/values.yaml#L983-L1036>
151+ fn prometheus_annotations ( ) -> Annotations {
152+ Annotations :: try_from ( [
153+ ( "prometheus.io/path" . to_owned ( ) , "/metrics" . to_owned ( ) ) ,
154+ ( "prometheus.io/port" . to_owned ( ) , METRICS_PORT . to_string ( ) ) ,
155+ ( "prometheus.io/scheme" . to_owned ( ) , "http" . to_owned ( ) ) ,
156+ ( "prometheus.io/scrape" . to_owned ( ) , "true" . to_owned ( ) ) ,
157+ ] )
158+ . expect ( "should be valid annotations" )
159+ }
0 commit comments