@@ -41,7 +41,10 @@ use stackable_operator::{
4141 rollout:: check_statefulset_rollout_complete,
4242 } ,
4343 utils:: cluster_info:: KubernetesClusterInfo ,
44- v2:: types:: { kubernetes:: NamespaceName , operator:: ClusterName } ,
44+ v2:: types:: {
45+ kubernetes:: { NamespaceName , Uid } ,
46+ operator:: ClusterName ,
47+ } ,
4548} ;
4649use strum:: { EnumDiscriminants , IntoEnumIterator , IntoStaticStr } ;
4750
@@ -74,6 +77,10 @@ pub const CONTAINER_IMAGE_BASE_NAME: &str = "hadoop";
7477/// the controller.
7578#[ derive( Clone , Debug ) ]
7679pub struct ValidatedCluster {
80+ /// The cluster's object metadata (name, namespace and uid). Kept private and only
81+ /// exposed via the [`Resource`] implementation so this type can act as the owner
82+ /// when building owned objects.
83+ metadata : ObjectMeta ,
7784 /// The logical (and Kubernetes object) name of the cluster.
7885 pub name : ClusterName ,
7986 /// The cluster namespace, used to build kerberos principals.
@@ -85,6 +92,33 @@ pub struct ValidatedCluster {
8592}
8693
8794impl ValidatedCluster {
95+ pub fn new (
96+ name : ClusterName ,
97+ namespace : NamespaceName ,
98+ uid : Uid ,
99+ image : ResolvedProductImage ,
100+ cluster_config : ValidatedClusterConfig ,
101+ role_groups : BTreeMap < HdfsNodeRole , BTreeMap < String , ValidatedRoleGroupConfig > > ,
102+ role_configs : BTreeMap < HdfsNodeRole , ValidatedRoleConfig > ,
103+ ) -> Self {
104+ Self {
105+ metadata : ObjectMeta {
106+ name : Some ( name. to_string ( ) ) ,
107+ namespace : Some ( namespace. to_string ( ) ) ,
108+ // The uid is required so this type can produce valid owner references
109+ // (Kubernetes rejects owner references without a uid).
110+ uid : Some ( uid. to_string ( ) ) ,
111+ ..ObjectMeta :: default ( )
112+ } ,
113+ name,
114+ namespace,
115+ image,
116+ cluster_config,
117+ role_groups,
118+ role_configs,
119+ }
120+ }
121+
88122 /// Builds the [`HdfsPodRef`]s expected for every pod of the given `role`, across
89123 /// all of its role groups.
90124 ///
@@ -119,6 +153,39 @@ impl ValidatedCluster {
119153 }
120154}
121155
156+ /// Lets [`ValidatedCluster`] be used as the owner [`Resource`] (e.g. in
157+ /// [`ObjectMetaBuilder::ownerreference_from_resource`]). The kind/group/version/plural
158+ /// are delegated to [`v1alpha1::HdfsCluster`] so the generated owner references are
159+ /// identical to the ones built from the raw cluster object.
160+ impl Resource for ValidatedCluster {
161+ type DynamicType = <v1alpha1:: HdfsCluster as Resource >:: DynamicType ;
162+ type Scope = <v1alpha1:: HdfsCluster as Resource >:: Scope ;
163+
164+ fn kind ( dt : & Self :: DynamicType ) -> std:: borrow:: Cow < ' _ , str > {
165+ v1alpha1:: HdfsCluster :: kind ( dt)
166+ }
167+
168+ fn group ( dt : & Self :: DynamicType ) -> std:: borrow:: Cow < ' _ , str > {
169+ v1alpha1:: HdfsCluster :: group ( dt)
170+ }
171+
172+ fn version ( dt : & Self :: DynamicType ) -> std:: borrow:: Cow < ' _ , str > {
173+ v1alpha1:: HdfsCluster :: version ( dt)
174+ }
175+
176+ fn plural ( dt : & Self :: DynamicType ) -> std:: borrow:: Cow < ' _ , str > {
177+ v1alpha1:: HdfsCluster :: plural ( dt)
178+ }
179+
180+ fn meta ( & self ) -> & ObjectMeta {
181+ & self . metadata
182+ }
183+
184+ fn meta_mut ( & mut self ) -> & mut ObjectMeta {
185+ & mut self . metadata
186+ }
187+ }
188+
122189/// Cluster-wide settings resolved once during validation, so the build steps no
123190/// longer need the raw `HdfsCluster` to render config.
124191#[ derive( Clone , Debug ) ]
@@ -415,14 +482,14 @@ pub async fn reconcile_hdfs(
415482 // to avoid the compiler error "E0716 (temporary value dropped while borrowed)".
416483 let mut metadata = ObjectMetaBuilder :: new ( ) ;
417484 let metadata = metadata
418- . name_and_namespace ( hdfs )
485+ . name_and_namespace ( & validated_cluster )
419486 . name ( rolegroup_ref. object_name ( ) )
420- . ownerreference_from_resource ( hdfs , None , Some ( true ) )
487+ . ownerreference_from_resource ( & validated_cluster , None , Some ( true ) )
421488 . with_context ( |_| ObjectMissingMetadataForOwnerRefSnafu {
422489 obj_ref : ObjectRef :: from_obj ( hdfs) ,
423490 } ) ?
424491 . with_recommended_labels ( & build_recommended_labels (
425- hdfs ,
492+ & validated_cluster ,
426493 RESOURCE_MANAGER_HDFS_CONTROLLER ,
427494 & resolved_product_image. app_version_label_value ,
428495 & rolegroup_ref. role ,
@@ -523,7 +590,6 @@ pub async fn reconcile_hdfs(
523590 . namenode_listener_refs ( client)
524591 . await
525592 . context ( CollectDiscoveryConfigSnafu ) ?,
526- hdfs,
527593 )
528594 . context ( BuildDiscoveryConfigMapSnafu ) ?;
529595
@@ -714,6 +780,7 @@ kind: HdfsCluster
714780metadata:
715781 name: hdfs
716782 namespace: default
783+ uid: c2c8c5c0-0b5a-4b1e-9f3e-1a2b3c4d5e6f
717784spec:
718785 image:
719786 productVersion: 3.4.0
0 commit comments