4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
- v1 "github.com/karmada-io/dashboard/cmd/metrics-scraper/app/db"
7
+
8
+ "github.com/karmada-io/dashboard/cmd/metrics-scraper/app/db"
8
9
"github.com/karmada-io/dashboard/pkg/client"
9
10
"github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
10
11
@@ -21,32 +22,32 @@ import (
21
22
type SaveRequest struct {
22
23
appName string
23
24
podName string
24
- data * v1 .ParsedData
25
+ data * db .ParsedData
25
26
result chan error
26
27
}
27
28
28
- func FetchMetrics (ctx context.Context , appName string , requests chan SaveRequest ) (map [string ]* v1 .ParsedData , []string , error ) {
29
+ func FetchMetrics (ctx context.Context , appName string , requests chan SaveRequest ) (map [string ]* db .ParsedData , []string , error ) {
29
30
kubeClient := client .InClusterClient ()
30
31
podsMap , errors := getKarmadaPods (ctx , appName ) // Pass context here
31
32
if len (podsMap ) == 0 && len (errors ) > 0 {
32
33
return nil , errors , fmt .Errorf ("no pods found" )
33
34
}
34
- allMetrics := make (map [string ]* v1 .ParsedData )
35
+ allMetrics := make (map [string ]* db .ParsedData )
35
36
var mu sync.Mutex
36
37
var wg sync.WaitGroup
37
38
for clusterName , pods := range podsMap {
38
39
for _ , pod := range pods {
39
40
wg .Add (1 )
40
- go func (ctx context.Context , pod v1 .PodInfo , clusterName string ) {
41
+ go func (ctx context.Context , pod db .PodInfo , clusterName string ) {
41
42
defer wg .Done ()
42
43
select {
43
44
case <- ctx .Done ():
44
45
return
45
46
default :
46
47
}
47
- var jsonMetrics * v1 .ParsedData
48
+ var jsonMetrics * db .ParsedData
48
49
var err error
49
- if appName == v1 .KarmadaAgent {
50
+ if appName == db .KarmadaAgent {
50
51
jsonMetrics , err = getKarmadaAgentMetrics (ctx , pod .Name , clusterName , requests )
51
52
if err != nil {
52
53
mu .Lock ()
@@ -55,12 +56,12 @@ func FetchMetrics(ctx context.Context, appName string, requests chan SaveRequest
55
56
return
56
57
}
57
58
} else {
58
- port := v1 .SchedulerPort
59
- if appName == v1 .KarmadaControllerManager {
60
- port = v1 .ControllerManagerPort
59
+ port := db .SchedulerPort
60
+ if appName == db .KarmadaControllerManager {
61
+ port = db .ControllerManagerPort
61
62
}
62
63
metricsOutput , err := kubeClient .CoreV1 ().RESTClient ().Get ().
63
- Namespace (v1 .Namespace ).
64
+ Namespace (db .Namespace ).
64
65
Resource ("pods" ).
65
66
SubResource ("proxy" ).
66
67
Name (fmt .Sprintf ("%s:%s" , pod .Name , port )).
@@ -101,12 +102,12 @@ func FetchMetrics(ctx context.Context, appName string, requests chan SaveRequest
101
102
return allMetrics , errors , nil
102
103
}
103
104
104
- func getKarmadaPods (ctx context.Context , appName string ) (map [string ][]v1 .PodInfo , []string ) {
105
+ func getKarmadaPods (ctx context.Context , appName string ) (map [string ][]db .PodInfo , []string ) {
105
106
kubeClient := client .InClusterClient ()
106
- podsMap := make (map [string ][]v1 .PodInfo )
107
+ podsMap := make (map [string ][]db .PodInfo )
107
108
var errors []string
108
109
109
- if appName == v1 .KarmadaAgent {
110
+ if appName == db .KarmadaAgent {
110
111
karmadaClient := client .InClusterKarmadaClient ()
111
112
clusters , err := karmadaClient .ClusterV1alpha1 ().Clusters ().List (ctx , metav1.ListOptions {})
112
113
if err != nil {
@@ -125,7 +126,7 @@ func getKarmadaPods(ctx context.Context, appName string) (map[string][]v1.PodInf
125
126
}
126
127
}
127
128
} else {
128
- pods , err := kubeClient .CoreV1 ().Pods (v1 .Namespace ).List (ctx , metav1.ListOptions {
129
+ pods , err := kubeClient .CoreV1 ().Pods (db .Namespace ).List (ctx , metav1.ListOptions {
129
130
LabelSelector : fmt .Sprintf ("app=%s" , appName ),
130
131
})
131
132
if err != nil {
@@ -134,14 +135,14 @@ func getKarmadaPods(ctx context.Context, appName string) (map[string][]v1.PodInf
134
135
}
135
136
136
137
for _ , pod := range pods .Items {
137
- podsMap [appName ] = append (podsMap [appName ], v1 .PodInfo {Name : pod .Name })
138
+ podsMap [appName ] = append (podsMap [appName ], db .PodInfo {Name : pod .Name })
138
139
}
139
140
}
140
141
141
142
return podsMap , errors
142
143
}
143
144
144
- func getClusterPods (ctx context.Context , cluster * v1alpha1.Cluster ) ([]v1 .PodInfo , error ) {
145
+ func getClusterPods (ctx context.Context , cluster * v1alpha1.Cluster ) ([]db .PodInfo , error ) {
145
146
fmt .Printf ("Getting pods for cluster: %s\n " , cluster .Name )
146
147
147
148
kubeconfigPath := os .Getenv ("KUBECONFIG" )
@@ -172,17 +173,17 @@ func getClusterPods(ctx context.Context, cluster *v1alpha1.Cluster) ([]v1.PodInf
172
173
173
174
fmt .Printf ("Found %d pods in cluster %s\n " , len (podList .Items ), cluster .Name )
174
175
175
- var podInfos []v1 .PodInfo
176
+ var podInfos []db .PodInfo
176
177
for _ , pod := range podList .Items {
177
- podInfos = append (podInfos , v1 .PodInfo {
178
+ podInfos = append (podInfos , db .PodInfo {
178
179
Name : pod .Name ,
179
180
})
180
181
}
181
182
182
183
return podInfos , nil
183
184
}
184
185
185
- func getKarmadaAgentMetrics (ctx context.Context , podName string , clusterName string , requests chan SaveRequest ) (* v1 .ParsedData , error ) {
186
+ func getKarmadaAgentMetrics (ctx context.Context , podName string , clusterName string , requests chan SaveRequest ) (* db .ParsedData , error ) {
186
187
kubeClient := client .InClusterKarmadaClient ()
187
188
clusters , err := kubeClient .ClusterV1alpha1 ().Clusters ().List (ctx , metav1.ListOptions {})
188
189
if err != nil {
@@ -232,15 +233,15 @@ func getKarmadaAgentMetrics(ctx context.Context, podName string, clusterName str
232
233
return nil , fmt .Errorf ("failed to retrieve metrics: %v" , err )
233
234
}
234
235
235
- var parsedData * v1 .ParsedData
236
+ var parsedData * db .ParsedData
236
237
if isJSON (metricsOutput ) {
237
- parsedData = & v1 .ParsedData {}
238
+ parsedData = & db .ParsedData {}
238
239
err = json .Unmarshal (metricsOutput , parsedData )
239
240
if err != nil {
240
241
return nil , fmt .Errorf ("failed to unmarshal JSON metrics: %v" , err )
241
242
}
242
243
} else {
243
- var parsedDataPtr * v1 .ParsedData
244
+ var parsedDataPtr * db .ParsedData
244
245
parsedDataPtr , err = parseMetricsToJSON (string (metricsOutput ))
245
246
if err != nil {
246
247
return nil , fmt .Errorf ("failed to parse metrics to JSON: %v" , err )
@@ -251,7 +252,7 @@ func getKarmadaAgentMetrics(ctx context.Context, podName string, clusterName str
251
252
// Send save request to the database worker
252
253
select {
253
254
case requests <- SaveRequest {
254
- appName : v1 .KarmadaAgent ,
255
+ appName : db .KarmadaAgent ,
255
256
podName : podName ,
256
257
data : parsedData ,
257
258
result : nil , // Not waiting for result
0 commit comments