@@ -29,6 +29,7 @@ import (
2929 ctrl "sigs.k8s.io/controller-runtime"
3030 "sigs.k8s.io/controller-runtime/pkg/builder"
3131 "sigs.k8s.io/controller-runtime/pkg/client"
32+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3233 "sigs.k8s.io/controller-runtime/pkg/predicate"
3334
3435 placementv1beta1 "github.com/kubefleet-dev/kubefleet/apis/placement/v1beta1"
@@ -37,6 +38,9 @@ import (
3738const (
3839 // defaultCollectionInterval is the interval for collecting metrics (30 seconds)
3940 defaultCollectionInterval = 30 * time .Second
41+
42+ // metricCollectorFinalizer is the finalizer for cleaning up MetricCollectorReport
43+ metricCollectorFinalizer = "kubernetes-fleet.io/metric-collector-report-cleanup"
4044)
4145
4246// Reconciler reconciles a MetricCollector object
@@ -74,6 +78,38 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
7478 return ctrl.Result {}, err
7579 }
7680
81+ // Handle deletion - cleanup MetricCollectorReport on hub
82+ if ! mc .DeletionTimestamp .IsZero () {
83+ if controllerutil .ContainsFinalizer (mc , metricCollectorFinalizer ) {
84+ klog .V (2 ).InfoS ("Cleaning up MetricCollectorReport on hub" , "metricCollector" , req .Name )
85+
86+ // Delete MetricCollectorReport from hub cluster
87+ if err := r .deleteReportFromHub (ctx , mc ); err != nil {
88+ klog .ErrorS (err , "Failed to delete MetricCollectorReport from hub" , "metricCollector" , req .Name )
89+ return ctrl.Result {}, err
90+ }
91+
92+ // Remove finalizer
93+ controllerutil .RemoveFinalizer (mc , metricCollectorFinalizer )
94+ if err := r .MemberClient .Update (ctx , mc ); err != nil {
95+ klog .ErrorS (err , "Failed to remove finalizer" , "metricCollector" , req .Name )
96+ return ctrl.Result {}, err
97+ }
98+ klog .V (2 ).InfoS ("Successfully cleaned up MetricCollectorReport" , "metricCollector" , req .Name )
99+ }
100+ return ctrl.Result {}, nil
101+ }
102+
103+ // Add finalizer if not present
104+ if ! controllerutil .ContainsFinalizer (mc , metricCollectorFinalizer ) {
105+ controllerutil .AddFinalizer (mc , metricCollectorFinalizer )
106+ if err := r .MemberClient .Update (ctx , mc ); err != nil {
107+ klog .ErrorS (err , "Failed to add finalizer" , "metricCollector" , req .Name )
108+ return ctrl.Result {}, err
109+ }
110+ klog .V (2 ).InfoS ("Added finalizer to MetricCollector" , "metricCollector" , req .Name )
111+ }
112+
77113 // Collect metrics from Prometheus
78114 collectedMetrics , collectErr := r .collectFromPrometheus (ctx , mc )
79115
@@ -214,6 +250,34 @@ func (r *Reconciler) syncReportToHub(ctx context.Context, mc *placementv1beta1.M
214250 return nil
215251}
216252
253+ // deleteReportFromHub deletes the MetricCollectorReport from the hub cluster
254+ func (r * Reconciler ) deleteReportFromHub (ctx context.Context , mc * placementv1beta1.MetricCollector ) error {
255+ // Use the reportNamespace from the MetricCollector spec
256+ reportNamespace := mc .Spec .ReportNamespace
257+ if reportNamespace == "" {
258+ klog .V (2 ).InfoS ("reportNamespace is not set, skipping deletion" , "metricCollector" , mc .Name )
259+ return nil
260+ }
261+
262+ // Try to delete MetricCollectorReport on hub
263+ report := & placementv1beta1.MetricCollectorReport {}
264+ err := r .HubClient .Get (ctx , client.ObjectKey {Name : mc .Name , Namespace : reportNamespace }, report )
265+ if err != nil {
266+ if errors .IsNotFound (err ) {
267+ klog .V (2 ).InfoS ("MetricCollectorReport not found on hub, already deleted" , "report" , mc .Name , "namespace" , reportNamespace )
268+ return nil
269+ }
270+ return fmt .Errorf ("failed to get MetricCollectorReport: %w" , err )
271+ }
272+
273+ if err := r .HubClient .Delete (ctx , report ); err != nil && ! errors .IsNotFound (err ) {
274+ return fmt .Errorf ("failed to delete MetricCollectorReport: %w" , err )
275+ }
276+
277+ klog .InfoS ("Deleted MetricCollectorReport from hub" , "report" , mc .Name , "namespace" , reportNamespace )
278+ return nil
279+ }
280+
217281// SetupWithManager sets up the controller with the Manager.
218282func (r * Reconciler ) SetupWithManager (mgr ctrl.Manager ) error {
219283 r .recorder = mgr .GetEventRecorderFor ("metriccollector-controller" )
0 commit comments