Skip to content

Commit fd0033d

Browse files
author
Arvind Thirumurugan
committed
add reportNamespace to MetricCollector API
Signed-off-by: Arvind Thirumurugan <[email protected]>
1 parent 1be8c94 commit fd0033d

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

apis/placement/v1beta1/metriccollector_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ type MetricCollectorSpec struct {
5757
// +required
5858
// +kubebuilder:validation:Pattern=`^https?://.*$`
5959
PrometheusURL string `json:"prometheusUrl"`
60+
61+
// ReportNamespace is the namespace in the hub cluster where the MetricCollectorReport will be created.
62+
// This should be the fleet-member-{clusterName} namespace.
63+
// Example: fleet-member-cluster-1
64+
// +required
65+
ReportNamespace string `json:"reportNamespace"`
6066
}
6167

6268
// MetricsEndpointSpec defines how to access the metrics endpoint.ctor.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
apiVersion: cluster.kubernetes-fleet.io/v1beta1
22
kind: MemberCluster
33
metadata:
4-
name: kind-cluster-3
4+
name: kind-cluster-1
55
spec:
66
identity:
7-
name: fleet-member-agent-cluster-3
7+
name: fleet-member-agent-cluster-1
88
kind: ServiceAccount
99
namespace: fleet-system
1010
apiGroup: ""

pkg/controllers/metriccollector/controller.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"sigs.k8s.io/controller-runtime/pkg/predicate"
3333

3434
placementv1beta1 "github.com/kubefleet-dev/kubefleet/apis/placement/v1beta1"
35-
"github.com/kubefleet-dev/kubefleet/pkg/utils"
3635
)
3736

3837
const (
@@ -156,21 +155,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
156155

157156
// syncReportToHub syncs the MetricCollectorReport to the hub cluster
158157
func (r *Reconciler) syncReportToHub(ctx context.Context, mc *placementv1beta1.MetricCollector) error {
159-
// Extract cluster name from collected metrics
160-
// All metrics are guaranteed to have the same ClusterName
161-
if len(mc.Status.CollectedMetrics) == 0 {
162-
klog.V(4).InfoS("No metrics collected, skipping report sync", "metricCollector", klog.KObj(mc))
163-
return nil
158+
// Use the reportNamespace from the MetricCollector spec
159+
reportNamespace := mc.Spec.ReportNamespace
160+
if reportNamespace == "" {
161+
return fmt.Errorf("reportNamespace is not set in MetricCollector spec")
164162
}
165163

166-
clusterName := mc.Status.CollectedMetrics[0].ClusterName
167-
if clusterName == "" {
168-
return fmt.Errorf("clusterName is empty in collected metrics")
169-
}
170-
171-
// Calculate the namespace using the same pattern as Work objects
172-
reportNamespace := fmt.Sprintf(utils.NamespaceNameFormat, clusterName)
173-
174164
// Create or update MetricCollectorReport on hub
175165
report := &placementv1beta1.MetricCollectorReport{
176166
ObjectMeta: metav1.ObjectMeta{
@@ -202,7 +192,7 @@ func (r *Reconciler) syncReportToHub(ctx context.Context, mc *placementv1beta1.M
202192
klog.ErrorS(err, "Failed to create MetricCollectorReport", "report", klog.KObj(report))
203193
return err
204194
}
205-
klog.V(2).InfoS("Created MetricCollectorReport on hub", "report", klog.KObj(report), "clusterName", clusterName)
195+
klog.V(2).InfoS("Created MetricCollectorReport on hub", "report", klog.KObj(report), "reportNamespace", reportNamespace)
206196
return nil
207197
}
208198
return err
@@ -221,7 +211,7 @@ func (r *Reconciler) syncReportToHub(ctx context.Context, mc *placementv1beta1.M
221211
klog.ErrorS(err, "Failed to update MetricCollectorReport", "report", klog.KObj(existingReport))
222212
return err
223213
}
224-
klog.V(2).InfoS("Updated MetricCollectorReport on hub", "report", klog.KObj(existingReport), "clusterName", clusterName)
214+
klog.V(2).InfoS("Updated MetricCollectorReport on hub", "report", klog.KObj(existingReport), "reportNamespace", reportNamespace)
225215
return nil
226216
}
227217

0 commit comments

Comments
 (0)