Skip to content

Commit 3f9c3ad

Browse files
committed
fix: Add fleet annotation to namespace of CAPI cluster
1 parent 5aaff1b commit 3f9c3ad

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

config/rbac/role.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@ rules:
1919
- list
2020
- get
2121
- watch
22-
- apiGroups:
23-
- ""
24-
resources:
25-
- namespaces
26-
verbs:
2722
- create
23+
- patch
2824
- apiGroups:
2925
- events.k8s.io
3026
resources:

justfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ test-import: start-dev deploy deploy-child-cluster deploy-kindnet deploy-app &&
172172
kubectl wait pods --for=condition=Ready --timeout=150s --all --all-namespaces
173173
kubectl wait cluster --timeout=500s --for=condition=ControlPlaneReady=true docker-demo
174174
kubectl wait clusters.fleet.cattle.io --timeout=500s --for=condition=Ready=true docker-demo
175+
kubectl wait ns default --timeout=500s --for=jsonpath='{.metadata.annotations.field\.cattle\.io\/allow-fleetworkspace-creation-for-existing-namespace}=true'
175176

176177
# Full e2e test of importing cluster in fleet
177178
test-import-rke2: start-dev deploy deploy-child-rke2-cluster deploy-calico-gitrepo deploy-app

src/controller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,4 @@ fn error_policy(doc: Arc<impl kube::Resource>, error: &Error, ctx: Arc<Context>)
417417
warn!("reconcile failed: {:?}", error);
418418
ctx.metrics.reconcile_failure(doc, error);
419419
Action::requeue(Duration::from_secs(10))
420-
}
420+
}

src/controllers/cluster.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ use kube::api::{
1717
use kube::client::scope;
1818
use kube::runtime::watcher::{self, Config};
1919
use kube::{Api, Client};
20-
use kube::{Resource, api::ResourceExt, runtime::controller::Action};
20+
use kube::{Resource, api::{ResourceExt, Patch}, runtime::controller::Action};
2121
use serde::Serialize;
22-
use serde_json::Value;
23-
use tracing::info;
22+
use serde_json::{Value,json};
23+
use tracing::{info,debug};
2424

2525
use std::sync::Arc;
2626

@@ -30,6 +30,7 @@ use super::controller::{
3030
use super::{BundleResult, ClusterSyncError, ClusterSyncResult};
3131

3232
pub static CONTROLPLANE_READY_CONDITION: &str = "ControlPlaneReady";
33+
pub static FLEET_WORKSPACE_ANNOTATION: &str = "field.cattle.io/allow-fleetworkspace-creation-for-existing-namespace";
3334

3435
pub struct FleetClusterBundle {
3536
template_sources: TemplateSources,
@@ -176,6 +177,19 @@ impl FleetBundle for FleetClusterBundle {
176177
}
177178
}
178179

180+
// Ensure the fleet workspace annotation is present.
181+
let patch = json!({
182+
"metadata": {
183+
"annotations": {
184+
FLEET_WORKSPACE_ANNOTATION: "true"
185+
}
186+
}
187+
});
188+
let namespace_name = self.fleet.namespace().unwrap_or_default();
189+
let namespaces = Api::<Namespace>::all(ctx.client.clone());
190+
namespaces.patch_metadata(&namespace_name,&PatchParams::default(), &Patch::Merge(&patch)).await?;
191+
debug!("Added fleet annotation to namespace {}.", namespace_name);
192+
179193
Ok(Action::await_change())
180194
}
181195

@@ -205,6 +219,26 @@ impl FleetBundle for FleetClusterBundle {
205219
.await?;
206220
}
207221

222+
// List all other clusters in this namespace
223+
let namespaces = Api::<Namespace>::all(ctx.client.clone());
224+
let namespace_name = self.fleet.namespace().unwrap_or_default();
225+
let clusters = Api::<Cluster>::namespaced(ctx.client.clone(), &namespace_name);
226+
let other_clusters = clusters
227+
.list(&ListParams::default().fields(&format!("metadata.namespace={},metadata.name!={}", namespace_name, self.fleet.name_any())))
228+
.await?;
229+
// If no other clusters are found in this namespace, remove the fleet workspace annotation.
230+
if other_clusters.items.is_empty() {
231+
let patch = json!({
232+
"metadata": {
233+
"annotations": {
234+
FLEET_WORKSPACE_ANNOTATION: null
235+
}
236+
}
237+
});
238+
namespaces.patch_metadata(&namespace_name,&PatchParams::default(), &Patch::Merge(&patch)).await?;
239+
debug!("Removed fleet annotation from namespace {}.", namespace_name);
240+
}
241+
208242
Ok(Action::await_change())
209243
}
210244
}
@@ -285,4 +319,4 @@ impl Cluster {
285319

286320
Ok(Action::await_change())
287321
}
288-
}
322+
}

0 commit comments

Comments
 (0)