|
| 1 | +//! The validated cluster model and the steps that produce it. |
| 2 | +//! |
| 3 | +//! [`ValidatedCluster`] carries everything the build steps need, resolved once during |
| 4 | +//! [`validate`] (after [`dereference`]) so downstream code never re-derives it or |
| 5 | +//! touches the raw [`v1alpha1::KafkaCluster`] spec. The reconcile loop that consumes |
| 6 | +//! it lives in [`crate::kafka_controller`]. |
| 7 | +
|
| 8 | +use std::{borrow::Cow, collections::BTreeMap}; |
| 9 | + |
| 10 | +use stackable_operator::{ |
| 11 | + commons::product_image_selection::ResolvedProductImage, |
| 12 | + kube::{Resource, api::ObjectMeta}, |
| 13 | + v2::types::{ |
| 14 | + kubernetes::{NamespaceName, Uid}, |
| 15 | + operator::ClusterName, |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +pub(crate) mod build; |
| 20 | +pub(crate) mod dereference; |
| 21 | +pub(crate) mod validate; |
| 22 | + |
| 23 | +use crate::{ |
| 24 | + crd::{ |
| 25 | + KafkaPodDescriptor, MetadataManager, |
| 26 | + authorization::KafkaAuthorizationConfig, |
| 27 | + role::{AnyConfig, AnyConfigOverrides, KafkaRole}, |
| 28 | + security::KafkaTlsSecurity, |
| 29 | + v1alpha1, |
| 30 | + }, |
| 31 | + framework::role_utils::RoleGroupConfig, |
| 32 | +}; |
| 33 | + |
| 34 | +pub type RoleGroupName = String; |
| 35 | + |
| 36 | +/// The validated cluster. Carries everything the build steps need, resolved once |
| 37 | +/// here so downstream code never re-derives it or touches the raw spec. |
| 38 | +/// |
| 39 | +/// The cluster identity (`name`, `namespace`, `uid`) is captured here so that owner |
| 40 | +/// references for child objects can be built straight from this struct (via its |
| 41 | +/// [`Resource`] impl) without threading the raw [`v1alpha1::KafkaCluster`] around. |
| 42 | +/// This mirrors the hive-/opensearch-operator's `ValidatedCluster`. |
| 43 | +pub struct ValidatedCluster { |
| 44 | + /// `ObjectMeta` carrying `name`, `namespace` and `uid`, so this struct can act as the |
| 45 | + /// owner [`Resource`] for child objects. |
| 46 | + metadata: ObjectMeta, |
| 47 | + pub name: ClusterName, |
| 48 | + pub namespace: NamespaceName, |
| 49 | + pub image: ResolvedProductImage, |
| 50 | + pub cluster_config: ValidatedClusterConfig, |
| 51 | + pub role_group_configs: BTreeMap<KafkaRole, BTreeMap<RoleGroupName, ValidatedRoleGroupConfig>>, |
| 52 | +} |
| 53 | + |
| 54 | +impl ValidatedCluster { |
| 55 | + pub fn new( |
| 56 | + name: ClusterName, |
| 57 | + namespace: NamespaceName, |
| 58 | + uid: Uid, |
| 59 | + image: ResolvedProductImage, |
| 60 | + cluster_config: ValidatedClusterConfig, |
| 61 | + role_group_configs: BTreeMap<KafkaRole, BTreeMap<RoleGroupName, ValidatedRoleGroupConfig>>, |
| 62 | + ) -> Self { |
| 63 | + Self { |
| 64 | + metadata: ObjectMeta { |
| 65 | + name: Some(name.to_string()), |
| 66 | + namespace: Some(namespace.to_string()), |
| 67 | + uid: Some(uid.to_string()), |
| 68 | + ..ObjectMeta::default() |
| 69 | + }, |
| 70 | + name, |
| 71 | + namespace, |
| 72 | + image, |
| 73 | + cluster_config, |
| 74 | + role_group_configs, |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// Cluster-wide settings resolved during validation and dereferencing. |
| 80 | +/// |
| 81 | +/// Everything the build steps need is resolved here so they never have to read the |
| 82 | +/// raw [`v1alpha1::KafkaCluster`] spec. |
| 83 | +pub struct ValidatedClusterConfig { |
| 84 | + pub kafka_security: KafkaTlsSecurity, |
| 85 | + pub authorization_config: Option<KafkaAuthorizationConfig>, |
| 86 | + pub pod_descriptors: Vec<KafkaPodDescriptor>, |
| 87 | + pub metadata_manager: MetadataManager, |
| 88 | + |
| 89 | + /// Whether the operator must not generate broker ids itself, because the user |
| 90 | + /// supplied a `broker_id_pod_config_map_name`. Resolved from the raw spec during |
| 91 | + /// validation so the config-map builder never has to read it. |
| 92 | + pub disable_broker_id_generation: bool, |
| 93 | +} |
| 94 | + |
| 95 | +/// Lets [`ValidatedCluster`] act as the owner [`Resource`] for child objects, so owner |
| 96 | +/// references are built from it (via the captured `metadata`) rather than the raw CR. |
| 97 | +impl Resource for ValidatedCluster { |
| 98 | + type DynamicType = <v1alpha1::KafkaCluster as Resource>::DynamicType; |
| 99 | + type Scope = <v1alpha1::KafkaCluster as Resource>::Scope; |
| 100 | + |
| 101 | + fn kind(dt: &Self::DynamicType) -> Cow<'_, str> { |
| 102 | + v1alpha1::KafkaCluster::kind(dt) |
| 103 | + } |
| 104 | + |
| 105 | + fn group(dt: &Self::DynamicType) -> Cow<'_, str> { |
| 106 | + v1alpha1::KafkaCluster::group(dt) |
| 107 | + } |
| 108 | + |
| 109 | + fn version(dt: &Self::DynamicType) -> Cow<'_, str> { |
| 110 | + v1alpha1::KafkaCluster::version(dt) |
| 111 | + } |
| 112 | + |
| 113 | + fn plural(dt: &Self::DynamicType) -> Cow<'_, str> { |
| 114 | + v1alpha1::KafkaCluster::plural(dt) |
| 115 | + } |
| 116 | + |
| 117 | + fn meta(&self) -> &ObjectMeta { |
| 118 | + &self.metadata |
| 119 | + } |
| 120 | + |
| 121 | + fn meta_mut(&mut self) -> &mut ObjectMeta { |
| 122 | + &mut self.metadata |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/// A validated, merged Kafka role-group config. |
| 127 | +/// |
| 128 | +/// The merged config fragment is wrapped in [`AnyConfig`] and the merged |
| 129 | +/// `configOverrides` in [`AnyConfigOverrides`], so a single role-agnostic type |
| 130 | +/// carries both broker and controller role groups (their concrete config and |
| 131 | +/// override types differ). Produced via the local-`framework` |
| 132 | +/// [`with_validated_config`](crate::framework::role_utils::with_validated_config). |
| 133 | +pub type ValidatedRoleGroupConfig = RoleGroupConfig< |
| 134 | + AnyConfig, |
| 135 | + stackable_operator::role_utils::JavaCommonConfig, |
| 136 | + AnyConfigOverrides, |
| 137 | +>; |
0 commit comments