diff --git a/CHANGELOG.md b/CHANGELOG.md index 04b63093..a39fdf1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,8 @@ All notable changes to this project will be documented in this file. - Fix a longstanding problem of including empty `categories`, `shortNames` and `additionalPrinterColumns` in the CRDs, which could cause problems with GitOps tools (e.g. ArgoCD) reporting a diff in the custom resources. See [our internal issue](https://github.com/stackabletech/hdfs-operator/issues/626) and [the fix](https://github.com/kube-rs/kube/pull/2042) for details ([#814]). +- The operator now watches all resources that it creates and early-exits the reconcile action when the + cluster is marked for deletion ([#821]). [#801]: https://github.com/stackabletech/hdfs-operator/pull/801 [#806]: https://github.com/stackabletech/hdfs-operator/pull/806 @@ -36,6 +38,7 @@ All notable changes to this project will be documented in this file. [#811]: https://github.com/stackabletech/hdfs-operator/pull/811 [#814]: https://github.com/stackabletech/hdfs-operator/pull/814 [#819]: https://github.com/stackabletech/hdfs-operator/pull/819 +[#821]: https://github.com/stackabletech/hdfs-operator/pull/821 ## [26.7.0] - 2026-07-21 diff --git a/deploy/helm/hdfs-operator/templates/clusterrole-operator.yaml b/deploy/helm/hdfs-operator/templates/clusterrole-operator.yaml index 6de12303..320b5993 100644 --- a/deploy/helm/hdfs-operator/templates/clusterrole-operator.yaml +++ b/deploy/helm/hdfs-operator/templates/clusterrole-operator.yaml @@ -13,13 +13,15 @@ rules: - nodes/proxy verbs: - get - # Manage core workload resources created per HdfsCluster. - # All resources are applied via Server-Side Apply (create + patch) and tracked for - # orphan cleanup (list + delete). + # Manage core workload resources created per HdfsCluster (the ServiceAccount + # provides workload pod identity). All resources are applied via Server-Side Apply + # (create + patch), tracked for orphan cleanup (list + delete) and watched by the + # controller. - apiGroups: - "" resources: - configmaps + - serviceaccounts - services verbs: - create @@ -28,18 +30,8 @@ rules: - list - patch - watch - # serviceaccounts are applied via SSA and tracked for orphan cleanup. - - apiGroups: - - "" - resources: - - serviceaccounts - verbs: - - create - - delete - - get - - list - - patch - # rolebindings are applied via SSA and tracked for orphan cleanup. + # rolebindings are applied via SSA and tracked for orphan cleanup + # and watched by the controller. - apiGroups: - rbac.authorization.k8s.io resources: @@ -50,6 +42,7 @@ rules: - get - list - patch + - watch # statefulsets are applied via SSA, tracked for orphan cleanup. - apiGroups: - apps @@ -62,7 +55,8 @@ rules: - list - patch - watch - # poddisruptionbudgets are applied via SSA and tracked for orphan cleanup. + # poddisruptionbudgets are applied via SSA and tracked for orphan cleanup + # and watched by the controller. - apiGroups: - policy resources: @@ -73,6 +67,7 @@ rules: - get - list - patch + - watch # Required for maintaining the CRDs within the operator (including the conversion webhook info). # Also for the startup condition check before the controller can run. - apiGroups: diff --git a/rust/operator-binary/src/hdfs_controller.rs b/rust/operator-binary/src/hdfs_controller.rs index 10b9aec2..0f358590 100644 --- a/rust/operator-binary/src/hdfs_controller.rs +++ b/rust/operator-binary/src/hdfs_controller.rs @@ -84,6 +84,10 @@ pub async fn reconcile_hdfs( ) -> HdfsOperatorResult { tracing::info!("Starting reconcile"); + if hdfs.meta().deletion_timestamp.is_some() { + return Ok(Action::await_change()); + } + let hdfs = hdfs .0 .as_ref() @@ -160,13 +164,25 @@ mod test { use std::str::FromStr; use stackable_operator::{ - builder::pod::PodBuilder, commons::networking::DomainName, kube::api::ObjectMeta, - kvp::Labels, utils::cluster_info::KubernetesClusterInfo, + builder::pod::PodBuilder, + client::Client, + commons::networking::DomainName, + kube::{ + Client as KubeClient, Config, + api::ObjectMeta, + runtime::{ + controller::Action, + events::{Recorder, Reporter}, + }, + }, + kvp::Labels, + utils::cluster_info::KubernetesClusterInfo, v2::types::operator::RoleGroupName, }; use super::*; use crate::{ + HDFS_FULL_CONTROLLER_NAME, controller::build::container::ContainerConfig, test_support::{deserialize_cluster, role_group_config, validate_cluster}, }; @@ -261,4 +277,63 @@ spec: Some("group-value".to_string()) ); } + + /// The client points at a closed port, so any API call would fail the reconciliation: an `Ok` + /// proves that a cluster being deleted returns before the reconciler touches the Kubernetes + /// API, and because the spec is invalid, before the [`DeserializeGuard`] is unwrapped. + #[test] + fn reconcile_exits_early_for_deleted_cluster() { + let hdfs = serde_yaml::from_str( + r#" +apiVersion: hdfs.stackable.tech/v1alpha1 +kind: HdfsCluster +metadata: + name: hdfs + namespace: default + deletionTimestamp: "2026-08-14T12:00:00Z" +spec: {} +"#, + ) + .expect("YAML parses; the invalid spec is captured inside the DeserializeGuard"); + + let action = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .expect("current-thread tokio runtime") + .block_on(async { + let kube_client = KubeClient::try_from(Config::new( + "http://127.0.0.1:1".parse().expect("valid static URI"), + )) + .expect("client from static config"); + + let ctx = Arc::new(Ctx { + client: Client::new( + kube_client.clone(), + None, + "default".to_owned(), + KubernetesClusterInfo { + cluster_domain: DomainName::from_str("cluster.local") + .expect("valid cluster domain"), + }, + ), + event_recorder: Arc::new(Recorder::new( + kube_client, + Reporter { + controller: HDFS_FULL_CONTROLLER_NAME.to_string(), + instance: None, + }, + )), + operator_environment: OperatorEnvironmentOptions { + operator_namespace: "stackable-operators".to_owned(), + operator_service_name: "hdfs-operator".to_owned(), + image_repository: "oci.stackable.tech/sdp".to_owned(), + }, + }); + + reconcile_hdfs(Arc::new(hdfs), ctx).await + }) + .expect("a deleted cluster reconciles without any API call"); + + assert_eq!(action, Action::await_change()); + } } diff --git a/rust/operator-binary/src/main.rs b/rust/operator-binary/src/main.rs index 4ce582a0..1c2ee474 100644 --- a/rust/operator-binary/src/main.rs +++ b/rust/operator-binary/src/main.rs @@ -17,7 +17,9 @@ use stackable_operator::{ eos::EndOfSupportChecker, k8s_openapi::api::{ apps::v1::StatefulSet, - core::v1::{ConfigMap, Service}, + core::v1::{ConfigMap, Service, ServiceAccount}, + policy::v1::PodDisruptionBudget, + rbac::v1::RoleBinding, }, kube::{ Api, CustomResourceExt, ResourceExt, @@ -156,15 +158,27 @@ async fn main() -> anyhow::Result<()> { let hdfs_cluster_store = hdfs_controller.store(); let hdfs_controller = hdfs_controller .owns( - watch_namespace.get_api::>(&client), + watch_namespace.get_api::(&client), watcher::Config::default(), ) .owns( - watch_namespace.get_api::>(&client), + watch_namespace.get_api::(&client), watcher::Config::default(), ) .owns( - watch_namespace.get_api::>(&client), + watch_namespace.get_api::(&client), + watcher::Config::default(), + ) + .owns( + watch_namespace.get_api::(&client), + watcher::Config::default(), + ) + .owns( + watch_namespace.get_api::(&client), + watcher::Config::default(), + ) + .owns( + watch_namespace.get_api::(&client), watcher::Config::default(), ) .watches( diff --git a/tests/templates/kuttl/cluster-operation/60-assert.yaml b/tests/templates/kuttl/cluster-operation/60-assert.yaml new file mode 100644 index 00000000..d2247480 --- /dev/null +++ b/tests/templates/kuttl/cluster-operation/60-assert.yaml @@ -0,0 +1,170 @@ +--- +# The recreated StatefulSets must bring the cluster back to ready, and the recreated +# objects must carry an owner reference back to the HdfsCluster so that garbage +# collection still works for them. +apiVersion: kuttl.dev/v1beta1 +kind: TestAssert +metadata: + name: recreate-owned-resources +timeout: 600 +commands: + - script: kubectl -n $NAMESPACE wait --for=condition=available hdfsclusters.hdfs.stackable.tech/hdfs --timeout 601s +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: hdfs-namenode-default + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs +status: + readyReplicas: 2 + replicas: 2 +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: hdfs-datanode-default + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs +status: + readyReplicas: 1 + replicas: 1 +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: hdfs-journalnode-default + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs +status: + readyReplicas: 1 + replicas: 1 +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: hdfs-serviceaccount + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: hdfs-rolebinding + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs +--- +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: hdfs-namenode + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs +--- +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: hdfs-datanode + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs +--- +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: hdfs-journalnode + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: hdfs + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs +--- +apiVersion: v1 +kind: Service +metadata: + name: hdfs-namenode-default + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs +--- +apiVersion: v1 +kind: Service +metadata: + name: hdfs-namenode-default-metrics + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs +--- +apiVersion: v1 +kind: Service +metadata: + name: hdfs-datanode-default + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs +--- +apiVersion: v1 +kind: Service +metadata: + name: hdfs-datanode-default-metrics + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs +--- +apiVersion: v1 +kind: Service +metadata: + name: hdfs-journalnode-default + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs +--- +apiVersion: v1 +kind: Service +metadata: + name: hdfs-journalnode-default-metrics + ownerReferences: + - apiVersion: hdfs.stackable.tech/v1alpha1 + controller: true + kind: HdfsCluster + name: hdfs diff --git a/tests/templates/kuttl/cluster-operation/60-delete-owned-resources.yaml b/tests/templates/kuttl/cluster-operation/60-delete-owned-resources.yaml new file mode 100644 index 00000000..45ac09c5 --- /dev/null +++ b/tests/templates/kuttl/cluster-operation/60-delete-owned-resources.yaml @@ -0,0 +1,61 @@ +--- +# Every resource the operator applies carries an ownerReference and a `.owns()` watch +# (main.rs): deleting it must trigger a reconcile of the HdfsCluster that re-applies it, +# proving the `.owns()` routing and the ClusterRole `watch` verbs end to end. +# `.watches()` registrations can't be tested this way: the operator never recreates +# what it didn't apply. +# +# Resources are discovered by label (ClusterResources::add enforces the labels on +# everything the operator applies), so new resources and kinds are covered +# automatically. Labels over-match on derived objects, so each match must also carry +# a controller ownerReference pointing at the HdfsCluster; kinds that can never pass that +# gate are excluded up front. Recreation is proven by UID change, and a floor guard +# catches a selector that silently matches nothing. +apiVersion: kuttl.dev/v1beta1 +kind: TestStep +metadata: + name: delete-owned-resources +timeout: 300 +commands: + - script: | + set -eu + + delete_and_await_recreation() { + resource=$1 + old_uid=$(kubectl get -n "$NAMESPACE" "$resource" -o jsonpath='{.metadata.uid}') + kubectl delete -n "$NAMESPACE" "$resource" --wait=false + # Recreation is a single reconcile away, so this normally succeeds on the + # first iteration; 30s is a generous upper bound well below the step timeout. + for _ in $(seq 1 30); do + new_uid=$(kubectl get -n "$NAMESPACE" "$resource" -o jsonpath='{.metadata.uid}' 2>/dev/null || true) + if [ -n "$new_uid" ] && [ "$new_uid" != "$old_uid" ]; then + return 0 + fi + sleep 1 + done + echo "$resource was not recreated (old uid: $old_uid, current: '${new_uid:-}')" >&2 + return 1 + } + + selector="app.kubernetes.io/instance=hdfs,app.kubernetes.io/managed-by=hdfs.stackable.tech_hdfs-controller" + excluded="^(pods|persistentvolumeclaims|endpoints|events)$|^endpointslices\.|^controllerrevisions\.|^events\." + + deleted=0 + for kind in $(kubectl api-resources --verbs=list --namespaced -o name | grep -Ev "$excluded" | sort); do + for resource in $(kubectl get -n "$NAMESPACE" "$kind" -l "$selector" -o name 2>/dev/null); do + owner=$(kubectl get -n "$NAMESPACE" "$resource" -o jsonpath='{.metadata.ownerReferences[?(@.controller==true)].kind}/{.metadata.ownerReferences[?(@.controller==true)].name}' 2>/dev/null || true) + if [ "$owner" != "HdfsCluster/hdfs" ]; then + echo "skipping $resource: controller owner is '${owner:-none}', not the HdfsCluster" + continue + fi + delete_and_await_recreation "$resource" + deleted=$((deleted + 1)) + done + done + + # Guard against the sweep silently matching nothing (wrong selector, renamed + # labels): the fixture is known to produce well over this many owned resources. + if [ "$deleted" -lt 10 ]; then + echo "only $deleted labelled resources were swept - the label selector is broken" >&2 + exit 1 + fi