-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_utils.go
349 lines (298 loc) · 9.45 KB
/
handler_utils.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package monitor
import (
"context"
"fmt"
"strings"
"time"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/controllers/user/workload"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
prometheusReqTimeout = 30 * time.Second
)
var (
defaultQueryDuring = "5m"
defaultTo = "now"
defaultFrom = "now-" + defaultQueryDuring
)
func newClusterGraphInputParser(input v3.QueryGraphInput) *clusterGraphInputParser {
return &clusterGraphInputParser{
Input: &input,
}
}
type clusterGraphInputParser struct {
Input *v3.QueryGraphInput
ClusterName string
Start time.Time
End time.Time
Step time.Duration
Conditions []*types.QueryCondition
}
func (p *clusterGraphInputParser) parse() (err error) {
if p.Input.MetricParams == nil {
p.Input.MetricParams = make(map[string]string)
}
p.Start, p.End, p.Step, err = parseTimeParams(p.Input.From, p.Input.To, p.Input.Interval)
if err != nil {
return err
}
return p.parseFilter()
}
func (p *clusterGraphInputParser) parseFilter() error {
if p.Input.Filters == nil {
return fmt.Errorf("must have clusterId filter")
}
p.ClusterName = p.Input.Filters["clusterId"]
if p.ClusterName == "" {
return fmt.Errorf("clusterId is empty")
}
for name, value := range p.Input.Filters {
p.Conditions = append(p.Conditions, types.NewConditionFromString(name, types.ModifierEQ, value))
}
return nil
}
func newProjectGraphInputParser(input v3.QueryGraphInput) *projectGraphInputParser {
return &projectGraphInputParser{
Input: &input,
}
}
type projectGraphInputParser struct {
Input *v3.QueryGraphInput
ProjectID string
ClusterName string
Start time.Time
End time.Time
Step time.Duration
Conditions []*types.QueryCondition
}
func (p *projectGraphInputParser) parse() (err error) {
if p.Input.MetricParams == nil {
p.Input.MetricParams = make(map[string]string)
}
p.Start, p.End, p.Step, err = parseTimeParams(p.Input.From, p.Input.To, p.Input.Interval)
if err != nil {
return err
}
return p.parseFilter()
}
func (p *projectGraphInputParser) parseFilter() error {
if p.Input.Filters == nil {
return fmt.Errorf("must have projectId filter")
}
p.ProjectID = p.Input.Filters["projectId"]
if p.ProjectID == "" {
return fmt.Errorf("projectId is empty")
}
if p.ClusterName, _ = ref.Parse(p.ProjectID); p.ClusterName == "" {
return fmt.Errorf("clusterName is empty")
}
for name, value := range p.Input.Filters {
p.Conditions = append(p.Conditions, types.NewConditionFromString(name, types.ModifierEQ, value))
}
return nil
}
type authChecker struct {
ProjectID string
Input *v3.QueryGraphInput
UserContext *config.UserContext
WorkloadController workload.CommonController
}
func newAuthChecker(ctx context.Context, userContext *config.UserContext, input *v3.QueryGraphInput, projectID string) *authChecker {
return &authChecker{
ProjectID: projectID,
Input: input,
UserContext: userContext,
WorkloadController: workload.NewWorkloadController(ctx, userContext.UserOnlyContext(), nil),
}
}
func (a *authChecker) check() error {
return a.parseNamespace()
}
func (a *authChecker) parseNamespace() error {
if a.Input.MetricParams["namespace"] != "" {
if !a.isAuthorizeNamespace() {
return fmt.Errorf("could not query unauthorize namespace")
}
return nil
}
nss, err := a.getAuthroizeNamespace()
if err != nil {
return err
}
a.Input.MetricParams["namespace"] = nss
return nil
}
func (a *authChecker) isAuthorizeNamespace() bool {
ns, err := a.UserContext.Core.Namespaces(metav1.NamespaceAll).Get(a.Input.MetricParams["namespace"], metav1.GetOptions{})
if err != nil {
logrus.Errorf("get namespace %s info failed, %v", a.Input.MetricParams["namespace"], err)
return false
}
return ns.Annotations[projectIDAnn] == a.ProjectID
}
func (a *authChecker) getAuthroizeNamespace() (string, error) {
nss, err := a.UserContext.Core.Namespaces(metav1.NamespaceAll).List(metav1.ListOptions{})
if err != nil {
return "", fmt.Errorf("list namespace failed, %v", err)
}
var authNs []string
for _, v := range nss.Items {
if v.Annotations[projectIDAnn] == a.ProjectID {
authNs = append(authNs, v.Name)
}
}
return strings.Join(authNs, "|"), nil
}
func getAuthToken(userContext *config.UserContext, appName, namespace string) (string, error) {
sa, err := userContext.Core.ServiceAccounts(namespace).Get(appName, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("get service account %s:%s for monitor failed, %v", namespace, appName, err)
}
var secretName string
if secretName = sa.Secrets[0].Name; secretName == "" {
return "", fmt.Errorf("get secret from service account %s:%s for monitor failed, secret name is empty", namespace, appName)
}
secret, err := userContext.Core.Secrets(namespace).Get(secretName, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("get secret %s:%s for monitor failed, %v", namespace, secretName, err)
}
return string(secret.Data["token"]), nil
}
func parseMetricParams(userContext *config.UserContext, nodeLister v3.NodeLister, resourceType, clusterName string, metricParams map[string]string) (map[string]string, error) {
newMetricParams := make(map[string]string)
for k, v := range metricParams {
newMetricParams[k] = v
}
var ip string
var err error
switch resourceType {
case ResourceNode:
instance := newMetricParams["instance"]
if instance == "" {
return nil, fmt.Errorf("instance in metric params is empty")
}
ip, err = nodeName2InternalIP(nodeLister, clusterName, instance)
if err != nil {
return newMetricParams, err
}
case ResourceWorkload:
workloadName := newMetricParams["workloadName"]
rcType, ns, name, err := parseWorkloadName(workloadName)
if err != nil {
return newMetricParams, err
}
var podOwners []string
if workloadName != "" {
if rcType == workload.ReplicaSetType || rcType == workload.ReplicationControllerType || rcType == workload.DaemonSetType || rcType == workload.StatefulSetType || rcType == workload.JobType || rcType == workload.CronJobType {
podOwners = []string{name}
}
if rcType == workload.DeploymentType {
rcs, err := userContext.Apps.ReplicaSets(ns).List(metav1.ListOptions{})
if err != nil {
return newMetricParams, fmt.Errorf("list replicasets failed, %v", err)
}
for _, rc := range rcs.Items {
if rc.OwnerReferences != nil && strings.ToLower(rc.OwnerReferences[0].Kind) == workload.DeploymentType && rc.OwnerReferences[0].Name == name {
podOwners = append(podOwners, rc.Name)
}
}
rcType = workload.ReplicaSetType
}
var podNames []string
pods, err := userContext.Core.Pods(ns).List(metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("list pod failed, %v", err)
}
for _, pod := range pods.Items {
podRefName := pod.OwnerReferences[0].Name
podRefKind := pod.OwnerReferences[0].Kind
if pod.OwnerReferences != nil && contains(podRefName, podOwners...) && strings.ToLower(podRefKind) == rcType {
podNames = append(podNames, pod.Name)
}
}
newMetricParams["podName"] = strings.Join(podNames, "|")
}
case ResourcePod:
podName := newMetricParams["podName"]
if podName == "" {
return nil, fmt.Errorf("pod name is empty")
}
ns, name := ref.Parse(podName)
newMetricParams["namespace"] = ns
newMetricParams["podName"] = name
case ResourceContainer:
podName := newMetricParams["podName"]
if podName == "" {
return nil, fmt.Errorf("pod name is empty")
}
ns, name := ref.Parse(podName)
newMetricParams["namespace"] = ns
newMetricParams["podName"] = name
containerName := newMetricParams["containerName"]
if containerName == "" {
return nil, fmt.Errorf("container name is empty")
}
}
newMetricParams["instance"] = ip + ".*"
return newMetricParams, nil
}
func replaceParams(metricParams map[string]string, expr string) string {
var replacer []string
for k, v := range metricParams {
replacer = append(replacer, "$"+k)
replacer = append(replacer, v)
}
srp := strings.NewReplacer(replacer...)
return srp.Replace(expr)
}
func parseTimeParams(from, to, interval string) (start, end time.Time, step time.Duration, err error) {
if from == "" {
from = defaultFrom
}
if to == "" {
to = defaultTo
}
timeRange := NewTimeRange(from, to)
start, err = timeRange.ParseFrom()
if err != nil {
err = fmt.Errorf("parse param from value %s failed, %v", from, err)
return
}
end, err = timeRange.ParseTo()
if err != nil {
err = fmt.Errorf("parse param to value %s failed, %v", to, err)
return
}
i, err := getIntervalFrom(interval, defaultMinInterval)
if err != nil {
err = fmt.Errorf("parse param interval value %s failed, %v", i, err)
return
}
intervalCalculator := newIntervalCalculator(&IntervalOptions{MinInterval: i})
calInterval := intervalCalculator.Calculate(timeRange, i)
step = time.Duration(int64(calInterval.Value))
return
}
func parseWorkloadName(id string) (typeName, namespace, name string, err error) {
arr := strings.Split(id, ":")
if len(arr) < 3 {
return "", "", "", fmt.Errorf("invalid workload name: %s", id)
}
return arr[0], arr[1], arr[2], nil
}
func contains(str string, arr ...string) bool {
for _, v := range arr {
if v == str {
return true
}
}
return false
}
func isInstanceGraph(graphType string) bool {
return graphType == "singlestat"
}