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 #562

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Added

- Support specifying externalTrafficPolicy in Services created by listener-operator ([#562]).

[#562]: https://github.com/stackabletech/operator-rs/pull/562

### Changed

- Bump all dependencies (including kube and k8s-openapi) ([#632]).
Expand Down
2 changes: 2 additions & 0 deletions src/cluster_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
client::{Client, GetApi},
commons::{
cluster_operation::ClusterOperation,
listener::Listener,
resources::{
ComputeResource, ResourceRequirementsExt, ResourceRequirementsType,
LIMIT_REQUEST_RATIO_CPU, LIMIT_REQUEST_RATIO_MEMORY,
Expand Down Expand Up @@ -165,6 +166,7 @@ impl ClusterResource for Service {}
impl ClusterResource for ServiceAccount {}
impl ClusterResource for RoleBinding {}
impl ClusterResource for Secret {}
impl ClusterResource for Listener {}

impl ClusterResource for Job {
fn pod_spec(&self) -> Option<&PodSpec> {
Expand Down
35 changes: 35 additions & 0 deletions src/commons/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,38 @@ pub enum ServiceType {
ClusterIP,
}

impl ServiceType {
pub fn to_kubernetes_literal(&self) -> String {
match self {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [clippy] reported by reviewdog 🐶

error[E0004]: non-exhaustive patterns: `&commons::listener::ServiceType::ClusterIP` not covered
  --> src/commons/listener.rs:40:15
   |
40 |         match self {
   |               ^^^^ pattern `&commons::listener::ServiceType::ClusterIP` not covered
   |
note: `commons::listener::ServiceType` defined here
  --> src/commons/listener.rs:35:5
   |
29 | pub enum ServiceType {
   |          -----------
...
35 |     ClusterIP,
   |     ^^^^^^^^^ not covered
   = note: the matched value is of type `&commons::listener::ServiceType`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
   |
42 ~             ServiceType::LoadBalancer => "LoadBalancer".to_string(),
43 ~             &commons::listener::ServiceType::ClusterIP => todo!(),
   |

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [clippy] reported by reviewdog 🐶

error[E0004]: non-exhaustive patterns: `&commons::listener::ServiceType::ClusterIP` not covered
  --> src/commons/listener.rs:40:15
   |
40 |         match self {
   |               ^^^^ pattern `&commons::listener::ServiceType::ClusterIP` not covered
   |
note: `commons::listener::ServiceType` defined here
  --> src/commons/listener.rs:35:5
   |
29 | pub enum ServiceType {
   |          -----------
...
35 |     ClusterIP,
   |     ^^^^^^^^^ not covered
   = note: the matched value is of type `&commons::listener::ServiceType`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
   |
42 ~             ServiceType::LoadBalancer => "LoadBalancer".to_string(),
43 ~             &commons::listener::ServiceType::ClusterIP => todo!(),
   |

ServiceType::NodePort => "NodePort".to_string(),
ServiceType::LoadBalancer => "LoadBalancer".to_string(),
}
}
}

#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema, PartialEq, Eq)]
pub enum TrafficPolicy {
/// Obscures the client source IP and may cause a second hop to another node, but allows Kubernetes to spread the load between all nodes.
Cluster,
/// Preserves the client source IP and avoid a second hop for LoadBalancer and NodePort type Services, but makes clients responsible for spreading the load.
Local,
}

impl Default for TrafficPolicy {
fn default() -> Self {
TrafficPolicy::Cluster
}
}

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 [`Service`], [`Listener`] changes two things:
Expand All @@ -61,6 +93,9 @@ pub struct ListenerSpec {
/// Whether incoming traffic should also be directed to `Pod`s that are not `Ready`.
#[schemars(default = "Self::default_publish_not_ready_addresses")]
pub publish_not_ready_addresses: Option<bool>,
/// `externalTrafficPolicy` that should be set on the [`Service`] object.
#[serde(default)]
pub service_external_traffic_policy: TrafficPolicy,
}

impl ListenerSpec {
Expand Down
Loading