|
| 1 | +use clap::ValueEnum; |
| 2 | +use snafu::ResultExt; |
| 3 | +use stackable_operator::{ |
| 4 | + k8s_openapi::api::core::v1::Node, |
| 5 | + kube::{Api, Client, api::ListParams}, |
| 6 | +}; |
| 7 | +use tokio::sync::OnceCell; |
| 8 | +use tracing::{debug, info, instrument}; |
| 9 | + |
| 10 | +pub static LISTENER_CLASS_PRESET: OnceCell<ListenerClassPreset> = OnceCell::const_new(); |
| 11 | + |
| 12 | +/// Represents the `preset` value in the Listener Operator Helm Chart |
| 13 | +#[derive(Copy, Clone, Debug, ValueEnum)] |
| 14 | +pub enum ListenerClassPreset { |
| 15 | + None, |
| 16 | + StableNodes, |
| 17 | + EphemeralNodes, |
| 18 | +} |
| 19 | + |
| 20 | +impl ListenerClassPreset { |
| 21 | + pub fn as_helm_values(&self) -> String { |
| 22 | + let preset_value = match self { |
| 23 | + Self::None => "none", |
| 24 | + Self::StableNodes => "stable-nodes", |
| 25 | + Self::EphemeralNodes => "ephemeral-nodes", |
| 26 | + }; |
| 27 | + format!("preset: {preset_value}") |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +#[instrument] |
| 32 | +pub async fn determine_and_store_listener_class_preset(from_cli: Option<&ListenerClassPreset>) { |
| 33 | + if let Some(from_cli) = from_cli { |
| 34 | + LISTENER_CLASS_PRESET |
| 35 | + .set(*from_cli) |
| 36 | + .expect("LISTENER_CLASS_PRESET should be unset"); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + let kubernetes_environment = guess_kubernetes_environment().await.unwrap_or_else(|err| { |
| 41 | + info!("failed to determine Kubernetes environment, using defaults: {err:#?}"); |
| 42 | + KubernetesEnvironment::Unknown |
| 43 | + }); |
| 44 | + let listener_class_preset = match kubernetes_environment { |
| 45 | + // Kind does not support LoadBalancers out of the box, so avoid that |
| 46 | + KubernetesEnvironment::Kind => ListenerClassPreset::StableNodes, |
| 47 | + // LoadBalancer support in k3s is optional, so let's be better safe than sorry and not use |
| 48 | + // them |
| 49 | + KubernetesEnvironment::K3s => ListenerClassPreset::StableNodes, |
| 50 | + // Weekly node rotations and LoadBalancer support |
| 51 | + KubernetesEnvironment::Ionos => ListenerClassPreset::EphemeralNodes, |
| 52 | + // Don't pin nodes and assume we have LoadBalancer support |
| 53 | + KubernetesEnvironment::Unknown => ListenerClassPreset::EphemeralNodes, |
| 54 | + }; |
| 55 | + debug!( |
| 56 | + preset = ?listener_class_preset, |
| 57 | + kubernetes.environment = ?kubernetes_environment, |
| 58 | + "Using ListenerClass preset" |
| 59 | + ); |
| 60 | + |
| 61 | + LISTENER_CLASS_PRESET |
| 62 | + .set(listener_class_preset) |
| 63 | + .expect("LISTENER_CLASS_PRESET should be unset"); |
| 64 | +} |
| 65 | + |
| 66 | +#[derive(Debug)] |
| 67 | +enum KubernetesEnvironment { |
| 68 | + Kind, |
| 69 | + K3s, |
| 70 | + Ionos, |
| 71 | + Unknown, |
| 72 | +} |
| 73 | + |
| 74 | +/// Tries to guess what Kubernetes environment stackablectl is connecting to. |
| 75 | +/// |
| 76 | +/// Returns an error in case anything goes wrong. This could e.g. be the case in case no |
| 77 | +/// Kubernetes context is configured, stackablectl is missing RBAC permission to retrieve nodes or |
| 78 | +/// simply a network error. |
| 79 | +#[instrument] |
| 80 | +async fn guess_kubernetes_environment() -> Result<KubernetesEnvironment, snafu::Whatever> { |
| 81 | + let client = Client::try_default() |
| 82 | + .await |
| 83 | + .whatever_context("failed to construct Kubernetes client")?; |
| 84 | + let node_api: Api<Node> = Api::all(client); |
| 85 | + let nodes = node_api |
| 86 | + .list(&ListParams::default()) |
| 87 | + .await |
| 88 | + .whatever_context("failed to list Kubernetes nodes")?; |
| 89 | + |
| 90 | + for node in nodes { |
| 91 | + if let Some(spec) = node.spec { |
| 92 | + if let Some(provider_id) = spec.provider_id { |
| 93 | + if provider_id.starts_with("kind://") { |
| 94 | + return Ok(KubernetesEnvironment::Kind); |
| 95 | + } else if provider_id.starts_with("k3s://") { |
| 96 | + return Ok(KubernetesEnvironment::K3s); |
| 97 | + } else if provider_id.starts_with("ionos://") { |
| 98 | + return Ok(KubernetesEnvironment::Ionos); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + Ok(KubernetesEnvironment::Unknown) |
| 105 | +} |
0 commit comments