diff --git a/pkg/api/monitoring/v1alpha2/types.go b/pkg/api/monitoring/v1alpha2/types.go new file mode 100644 index 0000000000000000000000000000000000000000..74700cb197ba9a795125fbe4a9a81df151ff4676 --- /dev/null +++ b/pkg/api/monitoring/v1alpha2/types.go @@ -0,0 +1,23 @@ +package v1alpha2 + +// Prometheus query api response +type APIResponse struct { + Status string `json:"status" description:"result status, one of error, success"` + Data QueryResult `json:"data" description:"actual metric result"` + ErrorType string `json:"errorType,omitempty"` + Error string `json:"error,omitempty"` + Warnings []string `json:"warnings,omitempty"` +} + +// QueryResult includes result data from a query. +type QueryResult struct { + ResultType string `json:"resultType" description:"result type, one of matrix, vector"` + Result []QueryValue `json:"result" description:"metric data including labels, time series and values"` +} + +// Time Series +type QueryValue struct { + Metric map[string]string `json:"metric,omitempty" description:"time series labels"` + Value []interface{} `json:"value,omitempty" description:"time series, values of vector type"` + Values [][]interface{} `json:"values,omitempty" description:"time series, values of matrix type"` +} diff --git a/pkg/models/metrics/metrics.go b/pkg/models/metrics/metrics.go index 64955a706af6c25bab2e7befea7990ddd5bb8d43..e81dc7554183695288341ee4e51064691c259341 100644 --- a/pkg/models/metrics/metrics.go +++ b/pkg/models/metrics/metrics.go @@ -22,6 +22,7 @@ import ( "fmt" "github.com/json-iterator/go" "k8s.io/klog" + "kubesphere.io/kubesphere/pkg/api/monitoring/v1alpha2" "kubesphere.io/kubesphere/pkg/models/workspaces" cs "kubesphere.io/kubesphere/pkg/simple/client" "net/url" @@ -29,8 +30,6 @@ import ( "strings" "sync" "time" - - "kubesphere.io/kubesphere/pkg/simple/client/prometheus" ) var jsonIter = jsoniter.ConfigCompatibleWithStandardLibrary @@ -789,9 +788,9 @@ func GetWorkspaceStatistics(workspaceName string) *Response { func (response *APIResponse) withMetricResult(time int64, value int) { response.Status = "success" - response.Data = prometheus.QueryResult{ + response.Data = v1alpha2.QueryResult{ ResultType: "vector", - Result: []prometheus.QueryValue{ + Result: []v1alpha2.QueryValue{ { Value: []interface{}{time, value}, }, diff --git a/pkg/models/metrics/namespaces.go b/pkg/models/metrics/namespaces.go index 6099596c17b244bee94c1a6272fdbd545c428dbe..981e469112bce65350f93fa0ee6317f6de290d9c 100644 --- a/pkg/models/metrics/namespaces.go +++ b/pkg/models/metrics/namespaces.go @@ -43,16 +43,19 @@ func GetNamespacesWithMetrics(namespaces []*v1.Namespace) []*v1.Namespace { for _, result := range rawMetrics.Results { for _, data := range result.Data.Result { - if ns, exist := data.Metric["namespace"]; exist { - if len(data.Value) == 2 { - for i := 0; i < len(namespaces); i++ { - if namespaces[i].Name == ns { - if namespaces[i].Annotations == nil { - namespaces[i].Annotations = make(map[string]string, 0) - } - namespaces[i].Annotations[result.MetricName] = data.Value[1].(string) - } + + ns, exist := data.Metric["namespace"] + + if !exist || len(data.Value) != 2 { + continue + } + + for _, item := range namespaces { + if item.Name == ns { + if item.Annotations == nil { + item.Annotations = make(map[string]string, 0) } + item.Annotations[result.MetricName] = data.Value[1].(string) } } } diff --git a/pkg/models/metrics/types.go b/pkg/models/metrics/types.go index 8d7fd0102698c3a202c72be677b21364f9d5ba7c..1b8023da69de37455f563d485c6d89c2a25c8c10 100644 --- a/pkg/models/metrics/types.go +++ b/pkg/models/metrics/types.go @@ -19,7 +19,7 @@ package metrics import ( - "kubesphere.io/kubesphere/pkg/simple/client/prometheus" + "kubesphere.io/kubesphere/pkg/api/monitoring/v1alpha2" "net/url" ) @@ -47,7 +47,7 @@ type RequestParams struct { type APIResponse struct { MetricName string `json:"metric_name,omitempty" description:"metric name, eg. scheduler_up_sum"` - prometheus.APIResponse + v1alpha2.APIResponse } type Response struct { diff --git a/pkg/models/metrics/util.go b/pkg/models/metrics/util.go index 3c4eef6a7b085a8d27882a3307f04faf2c7d1653..b332ced4f09bedc5a918fbfc493b7ce8baf66be6 100644 --- a/pkg/models/metrics/util.go +++ b/pkg/models/metrics/util.go @@ -20,15 +20,14 @@ package metrics import ( "k8s.io/apimachinery/pkg/labels" + "k8s.io/klog" + "kubesphere.io/kubesphere/pkg/api/monitoring/v1alpha2" "kubesphere.io/kubesphere/pkg/informers" - "kubesphere.io/kubesphere/pkg/simple/client/prometheus" "math" "sort" "strconv" "runtime/debug" - - "github.com/golang/glog" ) const ( @@ -44,8 +43,8 @@ const ( ) type FormatedMetricDataWrapper struct { - fmtMetricData prometheus.QueryResult - by func(p, q *prometheus.QueryValue) bool + fmtMetricData v1alpha2.QueryResult + by func(p, q *v1alpha2.QueryValue) bool } func (wrapper FormatedMetricDataWrapper) Len() int { @@ -64,7 +63,7 @@ func (wrapper FormatedMetricDataWrapper) Swap(i, j int) { func (rawMetrics *Response) SortBy(sortMetricName string, sortType string) (*Response, int) { defer func() { if err := recover(); err != nil { - glog.Errorln(err) + klog.Errorln(err) debug.PrintStack() } }() @@ -92,7 +91,7 @@ func (rawMetrics *Response) SortBy(sortMetricName string, sortType string) (*Res if metricItem.MetricName == sortMetricName { if sortType == ResultSortTypeAsc { // asc - sort.Sort(FormatedMetricDataWrapper{metricItem.Data, func(p, q *prometheus.QueryValue) bool { + sort.Sort(FormatedMetricDataWrapper{metricItem.Data, func(p, q *v1alpha2.QueryValue) bool { value1 := p.Value value2 := q.Value v1, _ := strconv.ParseFloat(value1[len(value1)-1].(string), 64) @@ -107,7 +106,7 @@ func (rawMetrics *Response) SortBy(sortMetricName string, sortType string) (*Res }}) } else { // desc - sort.Sort(FormatedMetricDataWrapper{metricItem.Data, func(p, q *prometheus.QueryValue) bool { + sort.Sort(FormatedMetricDataWrapper{metricItem.Data, func(p, q *v1alpha2.QueryValue) bool { value1 := p.Value value2 := q.Value v1, _ := strconv.ParseFloat(value1[len(value1)-1].(string), 64) @@ -164,7 +163,7 @@ func (rawMetrics *Response) SortBy(sortMetricName string, sortType string) (*Res for i := 0; i < len(rawMetrics.Results); i++ { re := rawMetrics.Results[i] if re.Data.ResultType == ResultTypeVector && re.Status == MetricStatusSuccess { - sortedMetric := make([]prometheus.QueryValue, len(indexMap)) + sortedMetric := make([]v1alpha2.QueryValue, len(indexMap)) for j := 0; j < len(re.Data.Result); j++ { r := re.Data.Result[j] k, exist := r.Metric[ResultItemMetricResourceName] @@ -200,7 +199,7 @@ func (fmtLevelMetric *Response) Page(pageNum string, limitNum string, maxLength if pageNum != "" { p, err := strconv.Atoi(pageNum) if err != nil { - glog.Errorln(err) + klog.Errorln(err) } else { if p > 0 { page = p @@ -216,7 +215,7 @@ func (fmtLevelMetric *Response) Page(pageNum string, limitNum string, maxLength if limitNum != "" { l, err := strconv.Atoi(limitNum) if err != nil { - glog.Errorln(err) + klog.Errorln(err) } else { if l > 0 { limit = l diff --git a/pkg/simple/client/prometheus/prometheus.go b/pkg/simple/client/prometheus/prometheus.go index 7cff474a58e15e00307134a3594b233e662a8b1b..6584e77b969d645c10e09ae0aecc546d0498134d 100644 --- a/pkg/simple/client/prometheus/prometheus.go +++ b/pkg/simple/client/prometheus/prometheus.go @@ -22,32 +22,11 @@ import ( jsoniter "github.com/json-iterator/go" "io/ioutil" "k8s.io/klog" + "kubesphere.io/kubesphere/pkg/api/monitoring/v1alpha2" "net/http" "time" ) -// Prometheus query api response -type APIResponse struct { - Status string `json:"status" description:"result status, one of error, success"` - Data QueryResult `json:"data" description:"actual metric result"` - ErrorType string `json:"errorType,omitempty"` - Error string `json:"error,omitempty"` - Warnings []string `json:"warnings,omitempty"` -} - -// QueryResult includes result data from a query. -type QueryResult struct { - ResultType string `json:"resultType" description:"result type, one of matrix, vector"` - Result []QueryValue `json:"result" description:"metric data including labels, time series and values"` -} - -// Time Series -type QueryValue struct { - Metric map[string]string `json:"metric,omitempty" description:"time series labels"` - Value []interface{} `json:"value,omitempty" description:"time series, values of vector type"` - Values [][]interface{} `json:"values,omitempty" description:"time series, values of matrix type"` -} - type PrometheusClient struct { client *http.Client endpoint string @@ -64,17 +43,17 @@ func NewPrometheusClient(options *PrometheusOptions) (*PrometheusClient, error) }, nil } -func (c *PrometheusClient) QueryToK8SPrometheus(queryType string, params string) (apiResponse APIResponse) { +func (c *PrometheusClient) QueryToK8SPrometheus(queryType string, params string) (apiResponse v1alpha2.APIResponse) { return c.query(c.endpoint, queryType, params) } -func (c *PrometheusClient) QueryToK8SSystemPrometheus(queryType string, params string) (apiResponse APIResponse) { +func (c *PrometheusClient) QueryToK8SSystemPrometheus(queryType string, params string) (apiResponse v1alpha2.APIResponse) { return c.query(c.secondaryEndpoint, queryType, params) } var jsonIter = jsoniter.ConfigCompatibleWithStandardLibrary -func (c *PrometheusClient) query(endpoint string, queryType string, params string) (apiResponse APIResponse) { +func (c *PrometheusClient) query(endpoint string, queryType string, params string) (apiResponse v1alpha2.APIResponse) { url := fmt.Sprintf("%s/api/v1/%s?%s", endpoint, queryType, params) response, err := c.client.Get(url)