forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_graph_action.go
160 lines (138 loc) · 5.34 KB
/
project_graph_action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package monitor
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/parse"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/clustermanager"
monitorutil "github.com/rancher/rancher/pkg/monitoring"
"github.com/rancher/rancher/pkg/ref"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
mgmtclientv3 "github.com/rancher/types/client/management/v3"
"github.com/rancher/types/config/dialer"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func NewProjectGraphHandler(dialerFactory dialer.Factory, clustermanager *clustermanager.Manager) *ProjectGraphHandler {
return &ProjectGraphHandler{
dialerFactory: dialerFactory,
clustermanager: clustermanager,
}
}
type ProjectGraphHandler struct {
dialerFactory dialer.Factory
clustermanager *clustermanager.Manager
}
func (h *ProjectGraphHandler) QuerySeriesAction(actionName string, action *types.Action, apiContext *types.APIContext) error {
var queryGraphInput v3.QueryGraphInput
actionInput, err := parse.ReadBody(apiContext.Request)
if err != nil {
return err
}
if err = convert.ToObj(actionInput, &queryGraphInput); err != nil {
return err
}
inputParser := newProjectGraphInputParser(queryGraphInput)
if err = inputParser.parse(); err != nil {
return err
}
clusterName := inputParser.ClusterName
userContext, err := h.clustermanager.UserContext(clusterName)
if err != nil {
return fmt.Errorf("get usercontext failed, %v", err)
}
check := newAuthChecker(apiContext.Request.Context(), userContext, inputParser.Input, inputParser.ProjectID)
if err = check.check(); err != nil {
return err
}
reqContext, cancel := context.WithTimeout(context.Background(), prometheusReqTimeout)
defer cancel()
var svcName, svcNamespace, svcPort, token string
var queries []*PrometheusQuery
if inputParser.Input.Filters["resourceType"] == "istioproject" {
if inputParser.Input.MetricParams["namespace"] == "" {
return fmt.Errorf("no namespace found")
}
svcName, svcNamespace, svcPort = monitorutil.IstioPrometheusEndpoint()
mgmtClient := h.clustermanager.ScaledContext.Management
istioGraphs, err := mgmtClient.ClusterMonitorGraphs(clusterName).List(metav1.ListOptions{LabelSelector: "component=istio,level=project"})
if err != nil {
return fmt.Errorf("list istio graph failed, %v", err)
}
for _, graph := range istioGraphs.Items {
_, projectName := ref.Parse(inputParser.ProjectID)
refName := getRefferenceGraphName(projectName, graph.Name)
monitorMetrics, err := graph2Metrics(userContext, mgmtClient, clusterName, graph.Spec.ResourceType, refName, graph.Spec.MetricsSelector, graph.Spec.DetailsMetricsSelector, inputParser.Input.MetricParams, inputParser.Input.IsDetails)
if err != nil {
return err
}
queries = append(queries, metrics2PrometheusQuery(monitorMetrics, inputParser.Start, inputParser.End, inputParser.Step, isInstanceGraph(graph.Spec.GraphType))...)
}
} else {
prometheusName, prometheusNamespace := monitorutil.ClusterMonitoringInfo()
token, err = getAuthToken(userContext, prometheusName, prometheusNamespace)
if err != nil {
return err
}
svcName, svcNamespace, svcPort = monitorutil.ClusterPrometheusEndpoint()
var graphs []mgmtclientv3.ProjectMonitorGraph
err = access.List(apiContext, apiContext.Version, mgmtclientv3.ProjectMonitorGraphType, &types.QueryOptions{Conditions: inputParser.Conditions}, &graphs)
if err != nil {
return err
}
mgmtClient := h.clustermanager.ScaledContext.Management
for _, graph := range graphs {
g := graph
_, projectName := ref.Parse(graph.ProjectID)
refName := getRefferenceGraphName(projectName, graph.Name)
monitorMetrics, err := graph2Metrics(userContext, mgmtClient, clusterName, g.ResourceType, refName, graph.MetricsSelector, graph.DetailsMetricsSelector, inputParser.Input.MetricParams, inputParser.Input.IsDetails)
if err != nil {
return err
}
queries = append(queries, metrics2PrometheusQuery(monitorMetrics, inputParser.Start, inputParser.End, inputParser.Step, isInstanceGraph(g.GraphType))...)
}
}
prometheusQuery, err := NewPrometheusQuery(reqContext, clusterName, token, svcNamespace, svcName, svcPort, h.dialerFactory, userContext)
if err != nil {
return err
}
seriesSlice, err := prometheusQuery.Do(queries)
if err != nil {
logrus.WithError(err).Warn("query series failed")
return httperror.NewAPIError(httperror.ServerError, "Failed to obtain metrics. The metrics service may not be available.")
}
if seriesSlice == nil {
apiContext.WriteResponse(http.StatusNoContent, nil)
return nil
}
collection := v3.QueryProjectGraphOutput{Type: "collection"}
for k, v := range seriesSlice {
graphName, _, _ := parseID(k)
queryGraph := v3.QueryProjectGraph{
GraphName: graphName,
Series: parseResponse(v),
}
collection.Data = append(collection.Data, queryGraph)
}
res, err := json.Marshal(collection)
if err != nil {
return fmt.Errorf("marshal query series result failed, %v", err)
}
apiContext.Response.Write(res)
return nil
}
func parseResponse(seriesSlice []*TimeSeries) []*v3.TimeSeries {
var series []*v3.TimeSeries
for _, v := range seriesSlice {
series = append(series, &v3.TimeSeries{
Name: v.Name,
Points: v.Points,
})
}
return series
}