-
-
Notifications
You must be signed in to change notification settings - Fork 3
Listener: Use headless and metrics service #651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maltesander
wants to merge
4
commits into
main
Choose a base branch
from
fix/split-headless-metrics-service
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ mod crd; | |
mod env_vars; | ||
mod operations; | ||
mod product_logging; | ||
mod service; | ||
mod util; | ||
|
||
mod built_info { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
use std::collections::BTreeMap; | ||
|
||
use snafu::{ResultExt, Snafu}; | ||
use stackable_operator::{ | ||
builder::meta::ObjectMetaBuilder, | ||
k8s_openapi::api::core::v1::{Service, ServicePort, ServiceSpec}, | ||
kvp::{Label, ObjectLabels}, | ||
role_utils::RoleGroupRef, | ||
}; | ||
|
||
use crate::crd::{ | ||
AirflowRole, HTTP_PORT, HTTP_PORT_NAME, METRICS_PORT, METRICS_PORT_NAME, v1alpha1, | ||
}; | ||
|
||
pub const METRICS_SERVICE_SUFFIX: &str = "metrics"; | ||
pub const HEADLESS_SERVICE_SUFFIX: &str = "headless"; | ||
|
||
#[derive(Snafu, Debug)] | ||
pub enum Error { | ||
#[snafu(display("object is missing metadata to build owner reference"))] | ||
ObjectMissingMetadataForOwnerRef { | ||
source: stackable_operator::builder::meta::Error, | ||
}, | ||
|
||
#[snafu(display("failed to build Metadata"))] | ||
MetadataBuild { | ||
source: stackable_operator::builder::meta::Error, | ||
}, | ||
|
||
#[snafu(display("failed to build Labels"))] | ||
LabelBuild { | ||
source: stackable_operator::kvp::LabelError, | ||
}, | ||
} | ||
|
||
/// The rolegroup headless [`Service`] is a service that allows direct access to the instances of a certain rolegroup | ||
/// This is mostly useful for internal communication between peers, or for clients that perform client-side load balancing. | ||
pub fn build_rolegroup_headless_service( | ||
airflow: &v1alpha1::AirflowCluster, | ||
rolegroup_ref: &RoleGroupRef<v1alpha1::AirflowCluster>, | ||
object_labels: ObjectLabels<v1alpha1::AirflowCluster>, | ||
selector: BTreeMap<String, String>, | ||
) -> Result<Service, Error> { | ||
let ports = headless_service_ports(); | ||
|
||
let metadata = ObjectMetaBuilder::new() | ||
.name_and_namespace(airflow) | ||
.name(rolegroup_headless_service_name( | ||
&rolegroup_ref.object_name(), | ||
)) | ||
.ownerreference_from_resource(airflow, None, Some(true)) | ||
.context(ObjectMissingMetadataForOwnerRefSnafu)? | ||
.with_recommended_labels(object_labels) | ||
.context(MetadataBuildSnafu)? | ||
.build(); | ||
|
||
let service_spec = ServiceSpec { | ||
// Internal communication does not need to be exposed | ||
type_: Some("ClusterIP".to_string()), | ||
cluster_ip: Some("None".to_string()), | ||
ports: Some(ports), | ||
selector: Some(selector), | ||
publish_not_ready_addresses: Some(true), | ||
..ServiceSpec::default() | ||
}; | ||
|
||
Ok(Service { | ||
metadata, | ||
spec: Some(service_spec), | ||
status: None, | ||
}) | ||
} | ||
|
||
/// The rolegroup metrics [`Service`] is a service that exposes metrics and a prometheus scraping label. | ||
pub fn build_rolegroup_metrics_service( | ||
airflow: &v1alpha1::AirflowCluster, | ||
rolegroup_ref: &RoleGroupRef<v1alpha1::AirflowCluster>, | ||
object_labels: ObjectLabels<v1alpha1::AirflowCluster>, | ||
selector: BTreeMap<String, String>, | ||
) -> Result<Service, Error> { | ||
let ports = metrics_service_ports(); | ||
|
||
let prometheus_label = | ||
Label::try_from(("prometheus.io/scrape", "true")).context(LabelBuildSnafu)?; | ||
|
||
let metadata = ObjectMetaBuilder::new() | ||
.name_and_namespace(airflow) | ||
.name(rolegroup_metrics_service_name(&rolegroup_ref.object_name())) | ||
.ownerreference_from_resource(airflow, None, Some(true)) | ||
.context(ObjectMissingMetadataForOwnerRefSnafu)? | ||
.with_recommended_labels(object_labels) | ||
.context(MetadataBuildSnafu)? | ||
.with_label(prometheus_label) | ||
.build(); | ||
|
||
let service_spec = ServiceSpec { | ||
// Internal communication does not need to be exposed | ||
type_: Some("ClusterIP".to_string()), | ||
cluster_ip: Some("None".to_string()), | ||
ports: Some(ports), | ||
selector: Some(selector), | ||
publish_not_ready_addresses: Some(true), | ||
..ServiceSpec::default() | ||
}; | ||
|
||
Ok(Service { | ||
metadata, | ||
spec: Some(service_spec), | ||
status: None, | ||
}) | ||
} | ||
|
||
pub fn stateful_set_service_name( | ||
airflow_role: &AirflowRole, | ||
rolegroup_ref: &RoleGroupRef<v1alpha1::AirflowCluster>, | ||
) -> Option<String> { | ||
match airflow_role { | ||
AirflowRole::Webserver => Some(rolegroup_headless_service_name( | ||
&rolegroup_ref.object_name(), | ||
)), | ||
AirflowRole::Scheduler | AirflowRole::Worker => None, | ||
} | ||
} | ||
|
||
/// Returns the metrics rolegroup service name `<cluster>-<role>-<rolegroup>-<METRICS_SERVICE_SUFFIX>`. | ||
// TODO: Replace by operator.rs functions | ||
fn rolegroup_metrics_service_name(role_group_ref_object_name: &str) -> String { | ||
format!("{role_group_ref_object_name}-{METRICS_SERVICE_SUFFIX}") | ||
} | ||
|
||
/// Returns the headless rolegroup service name `<cluster>-<role>-<rolegroup>-<HEADLESS_SERVICE_SUFFIX>`. | ||
// TODO: Replace by operator.rs functions | ||
fn rolegroup_headless_service_name(role_group_ref_object_name: &str) -> String { | ||
Maleware marked this conversation as resolved.
Show resolved
Hide resolved
|
||
format!("{role_group_ref_object_name}-{HEADLESS_SERVICE_SUFFIX}") | ||
} | ||
|
||
fn headless_service_ports() -> Vec<ServicePort> { | ||
vec![ServicePort { | ||
name: Some(HTTP_PORT_NAME.to_string()), | ||
port: HTTP_PORT.into(), | ||
protocol: Some("TCP".to_string()), | ||
..ServicePort::default() | ||
}] | ||
} | ||
|
||
fn metrics_service_ports() -> Vec<ServicePort> { | ||
vec![ServicePort { | ||
name: Some(METRICS_PORT_NAME.to_string()), | ||
port: METRICS_PORT.into(), | ||
protocol: Some("TCP".to_string()), | ||
..ServicePort::default() | ||
}] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.