From ec74d04dedd95d1f2a9ae2a31a3c97a0120f971b Mon Sep 17 00:00:00 2001 From: Sebastian Bernauer Date: Sun, 5 Mar 2023 19:25:18 +0100 Subject: [PATCH 1/2] Support specifying externalTrafficPolicy in Services created by listener-operator --- CHANGELOG.md | 6 ++++++ src/cluster_resources.rs | 2 ++ src/commons/listener.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c97b70b..93f101a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 + ## [0.36.1] - 2023-02-27 ### Fixed diff --git a/src/cluster_resources.rs b/src/cluster_resources.rs index e129299c..5211b899 100644 --- a/src/cluster_resources.rs +++ b/src/cluster_resources.rs @@ -7,6 +7,7 @@ use std::{ use crate::{ client::{Client, GetApi}, + commons::listener::Listener, error::{Error, OperatorResult}, k8s_openapi::{ api::{ @@ -50,6 +51,7 @@ impl ClusterResource for StatefulSet {} impl ClusterResource for ServiceAccount {} impl ClusterResource for RoleBinding {} impl ClusterResource for Secret {} +impl ClusterResource for Listener {} /// A structure containing the cluster resources. /// diff --git a/src/commons/listener.rs b/src/commons/listener.rs index 0681c469..691ffae6 100644 --- a/src/commons/listener.rs +++ b/src/commons/listener.rs @@ -33,6 +33,38 @@ pub enum ServiceType { LoadBalancer, } +impl ServiceType { + pub fn to_kubernetes_literal(&self) -> String { + match self { + 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 should have good overall load-spreading. + Cluster, + /// Preserves the client source IP and avoid a second hop for LoadBalancer and NodePort type Services, but risks potentially imbalanced traffic spreading. + 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: @@ -59,6 +91,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, + /// `externalTrafficPolicy` that should be set on the [`Service`] object. + #[serde(default)] + pub service_external_traffic_policy: TrafficPolicy, } impl ListenerSpec { From 837f98845320a334d06c321de998d5f4bff5a2ee Mon Sep 17 00:00:00 2001 From: Sebastian Bernauer Date: Tue, 8 Aug 2023 07:37:36 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Natalie --- src/commons/listener.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commons/listener.rs b/src/commons/listener.rs index 691ffae6..49ccb9c8 100644 --- a/src/commons/listener.rs +++ b/src/commons/listener.rs @@ -44,9 +44,9 @@ impl ServiceType { #[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 should have good overall load-spreading. + /// 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 risks potentially imbalanced traffic spreading. + /// 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, }