Skip to content

Commit f0dd42b

Browse files
committed
refactor: add private metadata to ValidatedCluster to use for ownerreferences
1 parent 11c014d commit f0dd42b

3 files changed

Lines changed: 67 additions & 10 deletions

File tree

rust/operator-binary/src/airflow_controller.rs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,71 @@ pub struct ValidatedClusterConfig {
143143
/// role group before any resources are created.
144144
#[derive(Clone, Debug)]
145145
pub struct ValidatedCluster {
146+
/// `ObjectMeta` carrying `name`, `namespace` and `uid`, captured during validation, so this
147+
/// struct can stand in as the owner [`Resource`] for child objects.
148+
metadata: ObjectMeta,
146149
pub image: ResolvedProductImage,
147150
pub cluster_config: ValidatedClusterConfig,
148151
pub role_groups: BTreeMap<AirflowRole, BTreeMap<String, AirflowRoleGroupConfig>>,
149152
pub role_configs: BTreeMap<AirflowRole, ValidatedRoleConfig>,
150153
}
151154

155+
impl ValidatedCluster {
156+
pub fn new(
157+
airflow: &v1alpha2::AirflowCluster,
158+
image: ResolvedProductImage,
159+
cluster_config: ValidatedClusterConfig,
160+
role_groups: BTreeMap<AirflowRole, BTreeMap<String, AirflowRoleGroupConfig>>,
161+
role_configs: BTreeMap<AirflowRole, ValidatedRoleConfig>,
162+
) -> Self {
163+
Self {
164+
// Capture only the identity fields needed to own child objects.
165+
metadata: ObjectMeta {
166+
name: Some(airflow.name_any()),
167+
namespace: airflow.namespace(),
168+
uid: airflow.uid(),
169+
..ObjectMeta::default()
170+
},
171+
image,
172+
cluster_config,
173+
role_groups,
174+
role_configs,
175+
}
176+
}
177+
}
178+
179+
/// Lets [`ValidatedCluster`] stand in for the raw [`v1alpha2::AirflowCluster`] when building owner
180+
/// references and metadata for child objects. Kind/group/version are delegated to the CRD; the
181+
/// `metadata` (name, namespace, uid) is captured during validation.
182+
impl Resource for ValidatedCluster {
183+
type DynamicType = <v1alpha2::AirflowCluster as Resource>::DynamicType;
184+
type Scope = <v1alpha2::AirflowCluster as Resource>::Scope;
185+
186+
fn kind(dt: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
187+
v1alpha2::AirflowCluster::kind(dt)
188+
}
189+
190+
fn group(dt: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
191+
v1alpha2::AirflowCluster::group(dt)
192+
}
193+
194+
fn version(dt: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
195+
v1alpha2::AirflowCluster::version(dt)
196+
}
197+
198+
fn plural(dt: &Self::DynamicType) -> std::borrow::Cow<'_, str> {
199+
v1alpha2::AirflowCluster::plural(dt)
200+
}
201+
202+
fn meta(&self) -> &ObjectMeta {
203+
&self.metadata
204+
}
205+
206+
fn meta_mut(&mut self) -> &mut ObjectMeta {
207+
&mut self.metadata
208+
}
209+
}
210+
152211
#[derive(Snafu, Debug, EnumDiscriminants)]
153212
#[strum_discriminants(derive(IntoStaticStr))]
154213
pub enum Error {
@@ -568,7 +627,6 @@ pub async fn reconcile_airflow(
568627
})?;
569628

570629
let rg_configmap = config_map::build_rolegroup_config_map(
571-
airflow,
572630
&validated_cluster,
573631
&rolegroup,
574632
&validated_rg_config.config_overrides,
@@ -645,7 +703,6 @@ async fn build_executor_template(
645703
};
646704

647705
let rg_configmap = config_map::build_rolegroup_config_map(
648-
airflow,
649706
validated_cluster,
650707
&rolegroup,
651708
// The kubernetes-executor pod template does not apply webserver_config.py overrides

rust/operator-binary/src/controller/build/config_map.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ pub enum Error {
5757

5858
/// The rolegroup [`ConfigMap`] configures the rolegroup based on the configuration given by the administrator
5959
pub fn build_rolegroup_config_map(
60-
airflow: &v1alpha2::AirflowCluster,
6160
validated_cluster: &ValidatedCluster,
6261
rolegroup: &RoleGroupRef<v1alpha2::AirflowCluster>,
6362
config_overrides: &AirflowConfigOverrides,
@@ -78,12 +77,12 @@ pub fn build_rolegroup_config_map(
7877
cm_builder
7978
.metadata(
8079
ObjectMetaBuilder::new()
81-
.name_and_namespace(airflow)
80+
.name_and_namespace(validated_cluster)
8281
.name(rolegroup.object_name())
83-
.ownerreference_from_resource(airflow, None, Some(true))
82+
.ownerreference_from_resource(validated_cluster, None, Some(true))
8483
.context(ObjectMissingMetadataForOwnerRefSnafu)?
8584
.with_recommended_labels(&build_recommended_labels(
86-
airflow,
85+
validated_cluster,
8786
AIRFLOW_CONTROLLER_NAME,
8887
&validated_cluster.image.app_version_label_value,
8988
&rolegroup.role,

rust/operator-binary/src/controller/validate.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,17 @@ pub fn validate_cluster(
9898
authorization_config,
9999
} = dereferenced;
100100

101-
Ok(ValidatedCluster {
102-
image: resolved_product_image,
103-
cluster_config: ValidatedClusterConfig {
101+
Ok(ValidatedCluster::new(
102+
airflow,
103+
resolved_product_image,
104+
ValidatedClusterConfig {
104105
executor: airflow.spec.executor.clone(),
105106
authentication_config,
106107
authorization_config,
107108
},
108109
role_groups,
109110
role_configs,
110-
})
111+
))
111112
}
112113

113114
/// Validate and merge one role group against its role, via the shared

0 commit comments

Comments
 (0)