|
| 1 | +/* |
| 2 | +Copyright 2025 The KubeFleet Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1beta1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "k8s.io/apimachinery/pkg/api/meta" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | +) |
| 23 | + |
| 24 | +// +genclient |
| 25 | +// +genclient:nonNamespaced |
| 26 | +// +kubebuilder:object:root=true |
| 27 | +// +kubebuilder:resource:scope="Cluster",shortName=mc,categories={fleet,fleet-metrics} |
| 28 | +// +kubebuilder:subresource:status |
| 29 | +// +kubebuilder:storageversion |
| 30 | +// +kubebuilder:printcolumn:JSONPath=`.metadata.generation`,name="Gen",type=string |
| 31 | +// +kubebuilder:printcolumn:JSONPath=`.status.conditions[?(@.type=="MetricCollectorReady")].status`,name="Ready",type=string |
| 32 | +// +kubebuilder:printcolumn:JSONPath=`.status.workloadsMonitored`,name="Workloads",type=integer |
| 33 | +// +kubebuilder:printcolumn:JSONPath=`.status.lastCollectionTime`,name="Last-Collection",type=date |
| 34 | +// +kubebuilder:printcolumn:JSONPath=`.metadata.creationTimestamp`,name="Age",type=date |
| 35 | + |
| 36 | +// MetricCollector is used by member-agent to scrape and collect metrics from workloads |
| 37 | +// running on the member cluster. It runs on each member cluster and collects metrics |
| 38 | +// from Prometheus-compatible endpoints. |
| 39 | +type MetricCollector struct { |
| 40 | + metav1.TypeMeta `json:",inline"` |
| 41 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 42 | + |
| 43 | + // The desired state of MetricCollector. |
| 44 | + // +required |
| 45 | + Spec MetricCollectorSpec `json:"spec"` |
| 46 | + |
| 47 | + // The observed status of MetricCollector. |
| 48 | + // +optional |
| 49 | + Status MetricCollectorStatus `json:"status,omitempty"` |
| 50 | +} |
| 51 | + |
| 52 | +// MetricCollectorSpec defines the desired state of MetricCollector. |
| 53 | +type MetricCollectorSpec struct { |
| 54 | + // PrometheusURL is the URL of the Prometheus server. |
| 55 | + // Example: http://prometheus.test-ns.svc.cluster.local:9090 |
| 56 | + // +required |
| 57 | + // +kubebuilder:validation:Pattern=`^https?://.*$` |
| 58 | + PrometheusURL string `json:"prometheusUrl"` |
| 59 | + |
| 60 | + // ReportNamespace is the namespace in the hub cluster where the MetricCollectorReport will be created. |
| 61 | + // This should be the fleet-member-{clusterName} namespace. |
| 62 | + // Example: fleet-member-cluster-1 |
| 63 | + // +required |
| 64 | + ReportNamespace string `json:"reportNamespace"` |
| 65 | +} |
| 66 | + |
| 67 | +// MetricsEndpointSpec defines how to access the metrics endpoint.ctor. |
| 68 | +type MetricCollectorStatus struct { |
| 69 | + // Conditions is an array of current observed conditions. |
| 70 | + // +optional |
| 71 | + Conditions []metav1.Condition `json:"conditions,omitempty"` |
| 72 | + |
| 73 | + // ObservedGeneration is the generation most recently observed. |
| 74 | + // +optional |
| 75 | + ObservedGeneration int64 `json:"observedGeneration,omitempty"` |
| 76 | + |
| 77 | + // WorkloadsMonitored is the count of workloads being monitored. |
| 78 | + // +optional |
| 79 | + WorkloadsMonitored int32 `json:"workloadsMonitored,omitempty"` |
| 80 | + |
| 81 | + // LastCollectionTime is when metrics were last collected. |
| 82 | + // +optional |
| 83 | + LastCollectionTime *metav1.Time `json:"lastCollectionTime,omitempty"` |
| 84 | + |
| 85 | + // CollectedMetrics contains the most recent metrics from each workload. |
| 86 | + // +optional |
| 87 | + CollectedMetrics []WorkloadMetrics `json:"collectedMetrics,omitempty"` |
| 88 | +} |
| 89 | + |
| 90 | +// WorkloadMetrics represents metrics collected from a single workload pod. |
| 91 | +type WorkloadMetrics struct { |
| 92 | + // Namespace is the namespace of the pod. |
| 93 | + // +required |
| 94 | + Namespace string `json:"namespace"` |
| 95 | + |
| 96 | + // ClusterName from the workload_health metric label. |
| 97 | + // +required |
| 98 | + ClusterName string `json:"clusterName"` |
| 99 | + |
| 100 | + // WorkloadName from the workload_health metric label (typically the deployment name). |
| 101 | + // +required |
| 102 | + WorkloadName string `json:"workloadName"` |
| 103 | + |
| 104 | + // Health indicates if the workload is healthy (true=healthy, false=unhealthy). |
| 105 | + // +required |
| 106 | + Health bool `json:"health"` |
| 107 | +} |
| 108 | + |
| 109 | +const ( |
| 110 | + // MetricCollectorConditionTypeReady indicates the collector is ready. |
| 111 | + MetricCollectorConditionTypeReady string = "MetricCollectorReady" |
| 112 | + |
| 113 | + // MetricCollectorConditionTypeCollecting indicates metrics are being collected. |
| 114 | + MetricCollectorConditionTypeCollecting string = "MetricsCollecting" |
| 115 | + |
| 116 | + // MetricCollectorConditionTypeReported indicates metrics were successfully reported to hub. |
| 117 | + MetricCollectorConditionTypeReported string = "MetricsReported" |
| 118 | +) |
| 119 | + |
| 120 | +// +kubebuilder:object:root=true |
| 121 | + |
| 122 | +// MetricCollectorList contains a list of MetricCollector. |
| 123 | +type MetricCollectorList struct { |
| 124 | + metav1.TypeMeta `json:",inline"` |
| 125 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 126 | + Items []MetricCollector `json:"items"` |
| 127 | +} |
| 128 | + |
| 129 | +// GetConditions returns the conditions of the MetricCollector. |
| 130 | +func (m *MetricCollector) GetConditions() []metav1.Condition { |
| 131 | + return m.Status.Conditions |
| 132 | +} |
| 133 | + |
| 134 | +// SetConditions sets the conditions of the MetricCollector. |
| 135 | +func (m *MetricCollector) SetConditions(conditions ...metav1.Condition) { |
| 136 | + m.Status.Conditions = conditions |
| 137 | +} |
| 138 | + |
| 139 | +// GetCondition returns the condition of the given MetricCollector. |
| 140 | +func (m *MetricCollector) GetCondition(conditionType string) *metav1.Condition { |
| 141 | + return meta.FindStatusCondition(m.Status.Conditions, conditionType) |
| 142 | +} |
| 143 | + |
| 144 | +func init() { |
| 145 | + SchemeBuilder.Register(&MetricCollector{}, &MetricCollectorList{}) |
| 146 | +} |
0 commit comments