From 956ec8bc6b896bad6c9e17b2526076c08e482fc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natalie=20Klestrup=20R=C3=B6ijezon?= Date: Thu, 31 Oct 2024 12:39:30 +0100 Subject: [PATCH 1/3] Add "smart" preferred address types --- .../src/commons/listener.rs | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/crates/stackable-operator/src/commons/listener.rs b/crates/stackable-operator/src/commons/listener.rs index d7e590f3a..bfa0f7167 100644 --- a/crates/stackable-operator/src/commons/listener.rs +++ b/crates/stackable-operator/src/commons/listener.rs @@ -65,11 +65,13 @@ pub struct ListenerClassSpec { pub service_external_traffic_policy: KubernetesTrafficPolicy, /// Whether addresses should prefer using the IP address (`IP`) or the hostname (`Hostname`). + /// Can also be set to `HostnameConservative`, which will use `IP` for `NodePort` service types, but `Hostname` for everything else. /// /// The other type will be used if the preferred type is not available. - /// By default `Hostname` is used. + /// + /// Defaults to `HostnameConservative`. #[serde(default = "ListenerClassSpec::default_preferred_address_type")] - pub preferred_address_type: AddressType, + pub preferred_address_type: PreferredAddressType, } impl ListenerClassSpec { @@ -77,8 +79,13 @@ impl ListenerClassSpec { KubernetesTrafficPolicy::Local } - const fn default_preferred_address_type() -> AddressType { - AddressType::Hostname + const fn default_preferred_address_type() -> PreferredAddressType { + PreferredAddressType::HostnameConservative + } + + /// Resolves [`Self::preferred_address_type`]'s "smart" modes depending on the rest of `self`. + pub fn resolve_preferred_address_type(&self) -> AddressType { + self.preferred_address_type.resolve(self) } } @@ -197,14 +204,41 @@ pub struct ListenerIngress { pub ports: BTreeMap, } +/// The type of a given address. #[derive(Serialize, Deserialize, Clone, Copy, Debug, JsonSchema, PartialEq, Eq)] #[serde(rename_all = "PascalCase")] pub enum AddressType { + /// A resolvable DNS hostname. Hostname, + /// A resolved IP address. #[serde(rename = "IP")] Ip, } +/// A mode for deciding the preferred [`AddressType`]. +/// +/// These can vary depending on the rest of the [`ListenerClass`]. +#[derive(Serialize, Deserialize, Clone, Copy, Debug, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "PascalCase")] +pub enum PreferredAddressType { + /// Like [`AddressType::Hostname`], but prefers [`AddressType::Ip`] for [`ServiceType::NodePort`], since their hostnames are less likely to be resolvable. + HostnameConservative, + #[serde(untagged)] + AddressType(AddressType), +} + +impl PreferredAddressType { + pub fn resolve(self, listener_class: &ListenerClassSpec) -> AddressType { + match self { + PreferredAddressType::AddressType(tpe) => tpe, + PreferredAddressType::HostnameConservative => match listener_class.service_type { + ServiceType::NodePort => AddressType::Ip, + _ => AddressType::Hostname, + }, + } + } +} + /// Informs users about Listeners that are bound by a given Pod. /// /// This is not expected to be created or modified by users. It will be created by From 854d8ab1098d04fe8a2a5a1ad0fa8cb0c2b83069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natalie=20Klestrup=20R=C3=B6ijezon?= Date: Thu, 31 Oct 2024 14:24:28 +0100 Subject: [PATCH 2/3] Flatten AddressType into PreferredAddressType due to structuralism issues --- crates/stackable-operator/src/commons/listener.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/stackable-operator/src/commons/listener.rs b/crates/stackable-operator/src/commons/listener.rs index bfa0f7167..abcda4293 100644 --- a/crates/stackable-operator/src/commons/listener.rs +++ b/crates/stackable-operator/src/commons/listener.rs @@ -210,6 +210,7 @@ pub struct ListenerIngress { pub enum AddressType { /// A resolvable DNS hostname. Hostname, + /// A resolved IP address. #[serde(rename = "IP")] Ip, @@ -219,22 +220,27 @@ pub enum AddressType { /// /// These can vary depending on the rest of the [`ListenerClass`]. #[derive(Serialize, Deserialize, Clone, Copy, Debug, JsonSchema, PartialEq, Eq)] -#[serde(rename_all = "PascalCase")] pub enum PreferredAddressType { /// Like [`AddressType::Hostname`], but prefers [`AddressType::Ip`] for [`ServiceType::NodePort`], since their hostnames are less likely to be resolvable. HostnameConservative, - #[serde(untagged)] - AddressType(AddressType), + + // Like the respective variants of AddressType. Ideally we would refer to them instead of copy/pasting, but that breaks due to upstream issues: + // - https://github.com/GREsau/schemars/issues/222 + // - https://github.com/kube-rs/kube/issues/1622 + Hostname, + #[serde(rename = "IP")] + Ip, } impl PreferredAddressType { pub fn resolve(self, listener_class: &ListenerClassSpec) -> AddressType { match self { - PreferredAddressType::AddressType(tpe) => tpe, PreferredAddressType::HostnameConservative => match listener_class.service_type { ServiceType::NodePort => AddressType::Ip, _ => AddressType::Hostname, }, + PreferredAddressType::Hostname => AddressType::Hostname, + PreferredAddressType::Ip => AddressType::Ip, } } } From 48422eaa993ace249aa37e84bbc8e0fe3a35881c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natalie=20Klestrup=20R=C3=B6ijezon?= Date: Thu, 31 Oct 2024 14:42:40 +0100 Subject: [PATCH 3/3] Changelog --- crates/stackable-operator/CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/stackable-operator/CHANGELOG.md b/crates/stackable-operator/CHANGELOG.md index 890264263..ab5f0db32 100644 --- a/crates/stackable-operator/CHANGELOG.md +++ b/crates/stackable-operator/CHANGELOG.md @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Added + +- Add new `PreferredAddressType::HostnameConservative` ([#903]). + +### Changed + +- BREAKING: Split `ListenerClass.spec.preferred_address_type` into a new `PreferredAddressType` type. Use `resolve_preferred_address_type()` to access the `AddressType` as before. ([#903]) + +[#903]: https://github.com/stackabletech/operator-rs/pull/903 + ## [0.80.0] - 2024-10-23 ### Changed