Skip to content

Conversation

@yiannistri
Copy link
Contributor

@yiannistri yiannistri commented Jun 11, 2025

This is part of a fix for rancher/turtles#1329
Fixes: #279

This PR adds a reconciler that ensures a fleet annotation (rancher/webhook#932) is present in namespaces of CAPI clusters.

@yiannistri yiannistri moved this to PR to be reviewed in CAPI / Turtles Jun 11, 2025
@yiannistri yiannistri self-assigned this Jun 11, 2025
Copy link

@Danil-Grigorev Danil-Grigorev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @yiannistr, that looks great. Couple of comments

Comment on lines 305 to 316
// If no other clusters are found in this namespace, remove the fleet annotation.
if other_clusters.items.is_empty() {
let patch = json!({
"metadata": {
"annotations": {
FLEET_WORKSPACE_ANNOTATION: null
}
}
});
namespaces.patch_metadata(&namespace_name,&PatchParams::default(), &Patch::Merge(&patch)).await?;
debug!("Removed fleet annotation from namespace {}.", namespace_name);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but I think this can also be a part of to_bundle method in

Ok(Some(FleetClusterBundle {
template_sources: TemplateSources::new(self),
fleet: self.to_cluster(config.spec.cluster.as_ref()),
fleet_group: self.to_group(config.spec.cluster.as_ref()),
mapping: self.to_bundle_ns_mapping(config.spec.cluster.as_ref()),
#[cfg(feature = "agent-initiated")]
cluster_registration_token: self
.to_cluster_registration_token(config.spec.cluster.as_ref()),
config,
}))
Just needs to add a Namespace object to the FleetClusterBundle, and then use patch method on it as on the rest of the bundle objects. Will need a ResourceDiff implementation for Namespace though (we only care about annotations there).

@yiannistri yiannistri moved this from PR to be reviewed to In Progress (8 max) in CAPI / Turtles Jun 12, 2025
@yiannistri yiannistri force-pushed the reconcile-fleet-annotation branch from 1e3c345 to 809a21c Compare June 18, 2025 11:04
Comment on lines 221 to 228

// List all other clusters in this namespace
let namespaces = Api::<Namespace>::all(ctx.client.clone());
let namespace_name = mapping.namespace().unwrap_or_default();
let clusters = Api::<Cluster>::namespaced(ctx.client.clone(), &namespace_name);
let other_clusters = clusters
.list(&ListParams::default().fields(&format!("metadata.namespace={},metadata.name!={}", namespace_name, self.fleet.name_any())))
.await?;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// List all other clusters in this namespace
let namespaces = Api::<Namespace>::all(ctx.client.clone());
let namespace_name = mapping.namespace().unwrap_or_default();
let clusters = Api::<Cluster>::namespaced(ctx.client.clone(), &namespace_name);
let other_clusters = clusters
.list(&ListParams::default().fields(&format!("metadata.namespace={},metadata.name!={}", namespace_name, self.fleet.name_any())))
.await?;

I think other_clusters can be cloned and reused, to avoid additional list request, as you do down below. That may require moving this outside of if let Some(mapping) block, otherwise this logic will only be executed on a cluster referencing a cluster-class from another namespace, which I don’t think is the goal here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Danil-Grigorev doesn'tother_clusters use the namespace of the BundleNamespaceMapping as opposed to the namespace of the cluster?

let ns = mapping.namespace();
let other_clusters = ctx
.client
.list::<Cluster>(
&ListParams::default(),
&scope::Namespace::from(ns.clone().unwrap_or_default()),
)
.await?;

Copy link

@Danil-Grigorev Danil-Grigorev Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right, thanks, it shouldn’t be the ns of bundle ns mapping but a cluster or a fleet cluster instead (got misled by the code a bit). Still, this code block should be moved outside of let Some( case.

Maybe also a simple addition to e2e would be good here? Something in a sense of kubectl wait namespace <ns> json-path=<annotation-present>. This functionality is quite critical and would allow users to manually create FleetWorkspace on demand from UI or API, even in case of no further development in the repo. This should close rancher/turtles#1329 as a best effort.

@yiannistri yiannistri force-pushed the reconcile-fleet-annotation branch 4 times, most recently from 0a270d8 to 73853dc Compare June 18, 2025 14:04
@yiannistri yiannistri force-pushed the reconcile-fleet-annotation branch from 73853dc to 3f9c3ad Compare June 18, 2025 14:37
@Danil-Grigorev Danil-Grigorev added this pull request to the merge queue Jun 18, 2025
Merged via the queue into rancher:main with commit b31f868 Jun 18, 2025
18 of 20 checks passed
@yiannistri yiannistri deleted the reconcile-fleet-annotation branch June 23, 2025 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create fleet workspace for clusters outside of fleet-local and fleet-default namespace

2 participants