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

Add "smart" preferred address types #903

Merged
merged 3 commits into from
Nov 5, 2024
Merged
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
10 changes: 10 additions & 0 deletions crates/stackable-operator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 44 additions & 4 deletions crates/stackable-operator/src/commons/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,27 @@ 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 {
const fn default_service_external_traffic_policy() -> KubernetesTrafficPolicy {
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)
}
}

Expand Down Expand Up @@ -197,14 +204,47 @@ pub struct ListenerIngress {
pub ports: BTreeMap<String, i32>,
}

/// 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)]
pub enum PreferredAddressType {
/// Like [`AddressType::Hostname`], but prefers [`AddressType::Ip`] for [`ServiceType::NodePort`], since their hostnames are less likely to be resolvable.
HostnameConservative,

// 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::HostnameConservative => match listener_class.service_type {
ServiceType::NodePort => AddressType::Ip,
_ => AddressType::Hostname,
},
PreferredAddressType::Hostname => AddressType::Hostname,
PreferredAddressType::Ip => AddressType::Ip,
}
sbernauer marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// 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
Expand Down