Skip to content

Commit

Permalink
fix: empty replicaset metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
itnpeople committed Jun 13, 2022
1 parent 66bf1d8 commit 1d5b5c3
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions src/app/backend/model/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,37 +129,41 @@ func GetWorkloadCumulativeMetrics(cluster string, namespace string, resource str
} else {
return nil, errors.New(fmt.Sprintf("unsupported resource '%s'", resource))
}
names := []string{}
for _, pd := range pods {
names = append(names, pd.ObjectMeta.Name)
}
if len(pods) > 0 {
names := []string{}
for _, pd := range pods {
names = append(names, pd.ObjectMeta.Name)
}

metricsClient := clientSet.NewCumulativeMetricsClient()
metricsClient := clientSet.NewCumulativeMetricsClient()

selector := client.CumulativeMetricsResourceSelector{
Pods: names,
Namespace: namespace,
Function: "AVG",
}
selector := client.CumulativeMetricsResourceSelector{
Pods: names,
Namespace: namespace,
Function: "AVG",
}

result := CumulativeMetrics{}
// invoke metrics-scraper api
metrics, err := metricsClient.Get(selector)
if err != nil {
return nil, err
}
result.Metrics = metrics
result := CumulativeMetrics{}
// invoke metrics-scraper api
metrics, err := metricsClient.Get(selector)
if err != nil {
return nil, err
}
result.Metrics = metrics

// get request, limit
for _, c := range podSpec.Containers {
result.Limits.CPU = result.Limits.CPU + c.Resources.Limits.Cpu().MilliValue()
result.Limits.Memory = result.Limits.Memory + c.Resources.Limits.Memory().Value()
result.Requests.CPU = result.Requests.CPU + c.Resources.Requests.Cpu().MilliValue()
result.Requests.Memory = result.Requests.Memory + c.Resources.Requests.Memory().Value()
}

// get request, limit
for _, c := range podSpec.Containers {
result.Limits.CPU = result.Limits.CPU + c.Resources.Limits.Cpu().MilliValue()
result.Limits.Memory = result.Limits.Memory + c.Resources.Limits.Memory().Value()
result.Requests.CPU = result.Requests.CPU + c.Resources.Requests.Cpu().MilliValue()
result.Requests.Memory = result.Requests.Memory + c.Resources.Requests.Memory().Value()
return &result, nil
} else {
return &CumulativeMetrics{}, nil
}

return &result, nil

}

// get pod list with metrics
Expand Down

0 comments on commit 1d5b5c3

Please sign in to comment.