Skip to content
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

Support specifying externalTrafficPolicy in Services created by listener-operator #773

Merged
merged 14 commits into from
May 21, 2024
43 changes: 18 additions & 25 deletions crates/stackable-operator/src/commons/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,19 @@ use crate::builder::pod::volume::ListenerOperatorVolumeSourceBuilder;
)]
#[serde(rename_all = "camelCase")]
pub struct ListenerClassSpec {
pub service_type: ServiceType,
pub service_type: KubernetesServiceType,

/// Annotations that should be added to the Service object.
#[serde(default)]
pub service_annotations: BTreeMap<String, String>,
}

/// The method used to access the services.
#[derive(Serialize, Deserialize, Clone, Copy, Debug, JsonSchema, PartialEq, Eq)]
pub enum ServiceType {
//
// Please note that this represents a Kubernetes type, so the name of the enum variant needs to exactly match the
// Kubernetes service type.
#[derive(Serialize, Deserialize, Clone, Copy, Debug, JsonSchema, PartialEq, Eq, strum::Display)]
pub enum KubernetesServiceType {
sbernauer marked this conversation as resolved.
Show resolved Hide resolved
/// Reserve a port on each node.
NodePort,
/// Provision a dedicated load balancer.
Expand All @@ -68,18 +71,17 @@ pub enum ServiceType {
ClusterIP,
}

impl ServiceType {
pub fn to_kubernetes_literal(&self) -> String {
match self {
ServiceType::NodePort => "NodePort".to_owned(),
ServiceType::LoadBalancer => "LoadBalancer".to_owned(),
ServiceType::ClusterIP => "ClusterIP".to_owned(),
}
}
}

#[derive(Serialize, Deserialize, Clone, Debug, Default, JsonSchema, PartialEq, Eq)]
pub enum TrafficPolicy {
/// Service Internal Traffic Policy enables internal traffic restrictions to only route internal traffic to endpoints
/// within the node the traffic originated from. The "internal" traffic here refers to traffic originated from Pods in
/// the current cluster. This can help to reduce costs and improve performance.
/// See [Kubernetes docs](https://kubernetes.io/docs/concepts/services-networking/service-traffic-policy/).
//
// Please note that this represents a Kubernetes type, so the name of the enum variant needs to exactly match the
// Kubernetes traffic policy.
#[derive(
Serialize, Deserialize, Clone, Debug, Default, JsonSchema, PartialEq, Eq, strum::Display,
)]
pub enum KubernetesTrafficPolicy {
/// Obscures the client source IP and may cause a second hop to another node, but allows Kubernetes to spread the load between all nodes.
#[default]
sbernauer marked this conversation as resolved.
Show resolved Hide resolved
Cluster,
Expand All @@ -88,15 +90,6 @@ pub enum TrafficPolicy {
Local,
}

impl TrafficPolicy {
pub fn to_kubernetes_literal(&self) -> String {
match self {
TrafficPolicy::Cluster => "Cluster".to_string(),
TrafficPolicy::Local => "Local".to_string(),
}
}
}

/// Exposes a set of pods to the outside world.
///
/// Essentially a Stackable extension of a Kubernetes Service. Compared to a Service, a Listener changes three things:
Expand Down Expand Up @@ -134,7 +127,7 @@ pub struct ListenerSpec {

/// `externalTrafficPolicy` that should be set on the [`Service`] object.
#[serde(default)]
pub service_external_traffic_policy: TrafficPolicy,
pub service_external_traffic_policy: KubernetesTrafficPolicy,
}

impl ListenerSpec {
Expand Down