/
allocationprops.go
636 lines (570 loc) · 19.3 KB
/
allocationprops.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
package kubecost
import (
"fmt"
"sort"
"strings"
"github.com/tkennes/openghg/pkg/log"
"github.com/tkennes/openghg/pkg/prom"
)
const (
AllocationNilProp string = ""
AllocationClusterProp string = "cluster"
AllocationNodeProp string = "node"
AllocationContainerProp string = "container"
AllocationControllerProp string = "controller"
AllocationControllerKindProp string = "controllerKind"
AllocationNamespaceProp string = "namespace"
AllocationPodProp string = "pod"
AllocationProviderIDProp string = "providerID"
AllocationServiceProp string = "service"
AllocationLabelProp string = "label"
AllocationAnnotationProp string = "annotation"
AllocationDeploymentProp string = "deployment"
AllocationStatefulSetProp string = "statefulset"
AllocationDaemonSetProp string = "daemonset"
AllocationJobProp string = "job"
AllocationDepartmentProp string = "department"
AllocationEnvironmentProp string = "environment"
AllocationOwnerProp string = "owner"
AllocationProductProp string = "product"
AllocationTeamProp string = "team"
)
func ParseProperty(text string) (string, error) {
switch strings.TrimSpace(strings.ToLower(text)) {
case "cluster":
return AllocationClusterProp, nil
case "node":
return AllocationNodeProp, nil
case "container":
return AllocationContainerProp, nil
case "controller":
return AllocationControllerProp, nil
case "controllerkind":
return AllocationControllerKindProp, nil
case "namespace":
return AllocationNamespaceProp, nil
case "pod":
return AllocationPodProp, nil
case "providerid":
return AllocationProviderIDProp, nil
case "service":
return AllocationServiceProp, nil
case "label":
return AllocationLabelProp, nil
case "annotation":
return AllocationAnnotationProp, nil
case "deployment":
return AllocationDeploymentProp, nil
case "daemonset":
return AllocationDaemonSetProp, nil
case "statefulset":
return AllocationStatefulSetProp, nil
case "job":
return AllocationJobProp, nil
case "department":
return AllocationDepartmentProp, nil
case "environment":
return AllocationEnvironmentProp, nil
case "owner":
return AllocationOwnerProp, nil
case "product":
return AllocationProductProp, nil
case "team":
return AllocationTeamProp, nil
}
if strings.HasPrefix(text, "label:") {
label := prom.SanitizeLabelName(strings.TrimSpace(strings.TrimPrefix(text, "label:")))
return fmt.Sprintf("label:%s", label), nil
}
if strings.HasPrefix(text, "annotation:") {
annotation := prom.SanitizeLabelName(strings.TrimSpace(strings.TrimPrefix(text, "annotation:")))
return fmt.Sprintf("annotation:%s", annotation), nil
}
return AllocationNilProp, fmt.Errorf("invalid allocation property: %s", text)
}
// AllocationProperties describes a set of Kubernetes objects.
type AllocationProperties struct {
Cluster string `json:"cluster,omitempty"`
Node string `json:"node,omitempty"`
Container string `json:"container,omitempty"`
Controller string `json:"controller,omitempty"`
ControllerKind string `json:"controllerKind,omitempty"`
Namespace string `json:"namespace,omitempty"`
Pod string `json:"pod,omitempty"`
Services []string `json:"services,omitempty"`
ProviderID string `json:"providerID,omitempty"`
Labels AllocationLabels `json:"labels,omitempty"`
Annotations AllocationAnnotations `json:"annotations,omitempty"`
NamespaceLabels AllocationLabels `json:"namespaceLabels,omitempty"` // @bingen:field[version=17]
NamespaceAnnotations AllocationAnnotations `json:"namespaceAnnotations,omitempty"` // @bingen:field[version=17]
// When set to true, maintain the intersection of all labels + annotations
// in the aggregated AllocationProperties object
AggregatedMetadata bool `json:"-"` //@bingen:field[ignore]
}
// AllocationLabels is a schema-free mapping of key/value pairs that can be
// attributed to an Allocation
type AllocationLabels map[string]string
// AllocationAnnotations is a schema-free mapping of key/value pairs that can be
// attributed to an Allocation
type AllocationAnnotations map[string]string
func (p *AllocationProperties) Clone() *AllocationProperties {
if p == nil {
return nil
}
clone := &AllocationProperties{}
clone.Cluster = p.Cluster
clone.Node = p.Node
clone.Container = p.Container
clone.Controller = p.Controller
clone.ControllerKind = p.ControllerKind
clone.Namespace = p.Namespace
clone.Pod = p.Pod
clone.ProviderID = p.ProviderID
var services []string
services = append(services, p.Services...)
clone.Services = services
labels := make(map[string]string, len(p.Labels))
for k, v := range p.Labels {
labels[k] = v
}
clone.Labels = labels
nsLabels := make(map[string]string, len(p.NamespaceLabels))
for k, v := range p.NamespaceLabels {
nsLabels[k] = v
}
clone.NamespaceLabels = nsLabels
annotations := make(map[string]string, len(p.Annotations))
for k, v := range p.Annotations {
annotations[k] = v
}
clone.Annotations = annotations
nsAnnotations := make(map[string]string, len(p.NamespaceAnnotations))
for k, v := range p.NamespaceAnnotations {
nsAnnotations[k] = v
}
clone.NamespaceAnnotations = nsAnnotations
clone.AggregatedMetadata = p.AggregatedMetadata
return clone
}
func (p *AllocationProperties) Equal(that *AllocationProperties) bool {
if p == nil || that == nil {
return false
}
if p.Cluster != that.Cluster {
return false
}
if p.Node != that.Node {
return false
}
if p.Container != that.Container {
return false
}
if p.Controller != that.Controller {
return false
}
if p.ControllerKind != that.ControllerKind {
return false
}
if p.Namespace != that.Namespace {
return false
}
if p.Pod != that.Pod {
return false
}
if p.ProviderID != that.ProviderID {
return false
}
pLabels := p.Labels
thatLabels := that.Labels
if len(pLabels) == len(thatLabels) {
for k, pv := range pLabels {
tv, ok := thatLabels[k]
if !ok || tv != pv {
return false
}
}
} else {
return false
}
pNamespaceLabels := p.NamespaceLabels
thatNamespaceLabels := that.NamespaceLabels
if len(pNamespaceLabels) == len(thatNamespaceLabels) {
for k, pv := range pNamespaceLabels {
tv, ok := thatNamespaceLabels[k]
if !ok || tv != pv {
return false
}
}
} else {
return false
}
pAnnotations := p.Annotations
thatAnnotations := that.Annotations
if len(pAnnotations) == len(thatAnnotations) {
for k, pv := range pAnnotations {
tv, ok := thatAnnotations[k]
if !ok || tv != pv {
return false
}
}
} else {
return false
}
pNamespaceAnnotations := p.NamespaceAnnotations
thatNamespaceAnnotations := that.NamespaceAnnotations
if len(pNamespaceAnnotations) == len(thatNamespaceAnnotations) {
for k, pv := range pNamespaceAnnotations {
tv, ok := thatNamespaceAnnotations[k]
if !ok || tv != pv {
return false
}
}
} else {
return false
}
pServices := p.Services
thatServices := that.Services
if len(pServices) == len(thatServices) {
sort.Strings(pServices)
sort.Strings(thatServices)
for i, pv := range pServices {
tv := thatServices[i]
if tv != pv {
return false
}
}
} else {
return false
}
return true
}
// GenerateKey generates a string that represents the key by which the
// AllocationProperties should be aggregated, given the properties defined by
// the aggregateBy parameter and the given label configuration.
func (p *AllocationProperties) GenerateKey(aggregateBy []string, labelConfig *LabelConfig) string {
if p == nil {
return ""
}
if labelConfig == nil {
labelConfig = NewLabelConfig()
}
// Names will ultimately be joined into a single name, which uniquely
// identifies allocations.
names := []string{}
for _, agg := range aggregateBy {
switch true {
case agg == AllocationClusterProp:
names = append(names, p.Cluster)
case agg == AllocationNodeProp:
names = append(names, p.Node)
case agg == AllocationNamespaceProp:
names = append(names, p.Namespace)
case agg == AllocationControllerKindProp:
controllerKind := p.ControllerKind
if controllerKind == "" {
// Indicate that allocation has no controller
controllerKind = UnallocatedSuffix
}
names = append(names, controllerKind)
case agg == AllocationDaemonSetProp || agg == AllocationStatefulSetProp || agg == AllocationDeploymentProp || agg == AllocationJobProp:
controller := p.Controller
if agg != p.ControllerKind || controller == "" {
// The allocation does not have the specified controller kind
controller = UnallocatedSuffix
}
names = append(names, controller)
case agg == AllocationControllerProp:
controller := p.Controller
if controller == "" {
// Indicate that allocation has no controller
controller = UnallocatedSuffix
} else if p.ControllerKind != "" {
controller = fmt.Sprintf("%s:%s", p.ControllerKind, controller)
}
names = append(names, controller)
case agg == AllocationPodProp:
names = append(names, p.Pod)
case agg == AllocationContainerProp:
names = append(names, p.Container)
case agg == AllocationServiceProp:
services := p.Services
if len(services) == 0 {
// Indicate that allocation has no services
names = append(names, UnallocatedSuffix)
} else {
// Unmounted load balancers lead to __unmounted__ Allocations whose
// services field is populated. If we don't have a special case, the
// __unmounted__ Allocation will be transformed into a regular Allocation,
// causing issues with AggregateBy and drilldown
if p.Pod == UnmountedSuffix || p.Namespace == UnmountedSuffix || p.Container == UnmountedSuffix {
names = append(names, UnmountedSuffix)
} else {
// This just uses the first service
for _, service := range services {
names = append(names, service)
break
}
}
}
case strings.HasPrefix(agg, "label:"):
labels := p.Labels
if labels == nil {
names = append(names, UnallocatedSuffix)
} else {
labelName := labelConfig.Sanitize(strings.TrimPrefix(agg, "label:"))
if labelValue, ok := labels[labelName]; ok {
names = append(names, fmt.Sprintf("%s", labelValue))
} else {
names = append(names, UnallocatedSuffix)
}
}
case strings.HasPrefix(agg, "annotation:"):
annotations := p.Annotations
if annotations == nil {
names = append(names, UnallocatedSuffix)
} else {
annotationName := labelConfig.Sanitize(strings.TrimPrefix(agg, "annotation:"))
if annotationValue, ok := annotations[annotationName]; ok {
names = append(names, fmt.Sprintf("%s", annotationValue))
} else {
names = append(names, UnallocatedSuffix)
}
}
case agg == AllocationDepartmentProp:
labels := p.Labels
annotations := p.Annotations
if labels == nil && annotations == nil {
names = append(names, UnallocatedSuffix)
} else {
labelNames := strings.Split(labelConfig.DepartmentLabel, ",")
for _, labelName := range labelNames {
labelName = labelConfig.Sanitize(labelName)
if labelValue, ok := labels[labelName]; ok {
names = append(names, labelValue)
} else if annotationValue, ok := annotations[labelName]; ok {
names = append(names, annotationValue)
} else {
names = append(names, UnallocatedSuffix)
}
}
}
case agg == AllocationEnvironmentProp:
labels := p.Labels
annotations := p.Annotations
if labels == nil && annotations == nil {
names = append(names, UnallocatedSuffix)
} else {
labelNames := strings.Split(labelConfig.EnvironmentLabel, ",")
for _, labelName := range labelNames {
labelName = labelConfig.Sanitize(labelName)
if labelValue, ok := labels[labelName]; ok {
names = append(names, labelValue)
} else if annotationValue, ok := annotations[labelName]; ok {
names = append(names, annotationValue)
} else {
names = append(names, UnallocatedSuffix)
}
}
}
case agg == AllocationOwnerProp:
labels := p.Labels
annotations := p.Annotations
if labels == nil && annotations == nil {
names = append(names, UnallocatedSuffix)
} else {
labelNames := strings.Split(labelConfig.OwnerLabel, ",")
for _, labelName := range labelNames {
labelName = labelConfig.Sanitize(labelName)
if labelValue, ok := labels[labelName]; ok {
names = append(names, labelValue)
} else if annotationValue, ok := annotations[labelName]; ok {
names = append(names, annotationValue)
} else {
names = append(names, UnallocatedSuffix)
}
}
}
case agg == AllocationProductProp:
labels := p.Labels
annotations := p.Annotations
if labels == nil && annotations == nil {
names = append(names, UnallocatedSuffix)
} else {
labelNames := strings.Split(labelConfig.ProductLabel, ",")
for _, labelName := range labelNames {
labelName = labelConfig.Sanitize(labelName)
if labelValue, ok := labels[labelName]; ok {
names = append(names, labelValue)
} else if annotationValue, ok := annotations[labelName]; ok {
names = append(names, annotationValue)
} else {
names = append(names, UnallocatedSuffix)
}
}
}
case agg == AllocationTeamProp:
labels := p.Labels
annotations := p.Annotations
if labels == nil && annotations == nil {
names = append(names, UnallocatedSuffix)
} else {
labelNames := strings.Split(labelConfig.TeamLabel, ",")
for _, labelName := range labelNames {
labelName = labelConfig.Sanitize(labelName)
if labelValue, ok := labels[labelName]; ok {
names = append(names, labelValue)
} else if annotationValue, ok := annotations[labelName]; ok {
names = append(names, annotationValue)
} else {
names = append(names, UnallocatedSuffix)
}
}
}
default:
// This case should never be reached, as input up until this point
// should be checked and rejected if invalid. But if we do get a
// value we don't recognize, log a warning.
log.Warnf("generateKey: illegal aggregation parameter: %s", agg)
}
}
return strings.Join(names, "/")
}
// Intersection returns an *AllocationProperties which contains all matching fields between the calling and parameter AllocationProperties
// nillable slices and maps are left as nil
func (p *AllocationProperties) Intersection(that *AllocationProperties) *AllocationProperties {
if p == nil || that == nil {
return nil
}
intersectionProps := &AllocationProperties{}
if p.Cluster == that.Cluster {
intersectionProps.Cluster = p.Cluster
}
if p.Node == that.Node {
intersectionProps.Node = p.Node
}
if p.Container == that.Container {
intersectionProps.Container = p.Container
}
if p.Controller == that.Controller {
intersectionProps.Controller = p.Controller
}
if p.ControllerKind == that.ControllerKind {
intersectionProps.ControllerKind = p.ControllerKind
}
if p.Namespace == that.Namespace {
intersectionProps.Namespace = p.Namespace
// CORE-140: In the case that the namespace is the same, also copy over the namespaceLabels and annotations
// Note - assume that if the namespace is the same on both, then namespace label/annotation sets
// will be the same, so just carry one set over
if p.Container == UnmountedSuffix {
// This logic is designed to effectively ignore the unmounted/unallocated objects
// and just copy over the labels from the other, 'legitimate' allocation.
intersectionProps.NamespaceLabels = copyStringMap(that.NamespaceLabels)
intersectionProps.NamespaceAnnotations = copyStringMap(that.NamespaceAnnotations)
} else {
intersectionProps.NamespaceLabels = copyStringMap(p.NamespaceLabels)
intersectionProps.NamespaceAnnotations = copyStringMap(p.NamespaceAnnotations)
}
// ignore the incoming labels from unallocated or unmounted special case pods
if p.AggregatedMetadata || that.AggregatedMetadata {
intersectionProps.AggregatedMetadata = true
// When aggregating by metadata, we maintain the intersection of the labels/annotations
// of the two AllocationProperties objects being intersected here.
// Special case unallocated/unmounted Allocations never have any labels or annotations.
// As a result, they have the effect of always clearing out the intersection,
// regardless if all the other actual allocations/etc have them.
// This logic is designed to effectively ignore the unmounted/unallocated objects
// and just copy over the labels from the other object - we only take the intersection
// of 'legitimate' allocations.
if p.Container == UnmountedSuffix {
intersectionProps.Annotations = that.Annotations
intersectionProps.Labels = that.Labels
} else if that.Container == UnmountedSuffix {
intersectionProps.Annotations = p.Annotations
intersectionProps.Labels = p.Labels
} else {
intersectionProps.Annotations = mapIntersection(p.Annotations, that.Annotations)
intersectionProps.Labels = mapIntersection(p.Labels, that.Labels)
}
}
}
if p.Pod == that.Pod {
intersectionProps.Pod = p.Pod
}
if p.ProviderID == that.ProviderID {
intersectionProps.ProviderID = p.ProviderID
}
return intersectionProps
}
func copyStringMap(original map[string]string) map[string]string {
copy := make(map[string]string)
for key, value := range original {
copy[key] = value
}
return copy
}
func mapIntersection(map1, map2 map[string]string) map[string]string {
result := make(map[string]string)
for key, value := range map1 {
if value2, ok := map2[key]; ok {
if value2 == value {
result[key] = value
}
}
}
return result
}
func (p *AllocationProperties) String() string {
if p == nil {
return "<nil>"
}
strs := []string{}
if p.Cluster != "" {
strs = append(strs, "Cluster:"+p.Cluster)
}
if p.Node != "" {
strs = append(strs, "Node:"+p.Node)
}
if p.Container != "" {
strs = append(strs, "Container:"+p.Container)
}
if p.Controller != "" {
strs = append(strs, "Controller:"+p.Controller)
}
if p.ControllerKind != "" {
strs = append(strs, "ControllerKind:"+p.ControllerKind)
}
if p.Namespace != "" {
strs = append(strs, "Namespace:"+p.Namespace)
}
if p.Pod != "" {
strs = append(strs, "Pod:"+p.Pod)
}
if p.ProviderID != "" {
strs = append(strs, "ProviderID:"+p.ProviderID)
}
if len(p.Services) > 0 {
strs = append(strs, "Services:"+strings.Join(p.Services, ";"))
}
var labelStrs []string
for k, prop := range p.Labels {
labelStrs = append(labelStrs, fmt.Sprintf("%s:%s", k, prop))
}
strs = append(strs, fmt.Sprintf("Labels:{%s}", strings.Join(labelStrs, ",")))
var nsLabelStrs []string
for k, prop := range p.NamespaceLabels {
nsLabelStrs = append(nsLabelStrs, fmt.Sprintf("%s:%s", k, prop))
}
strs = append(strs, fmt.Sprintf("NamespaceLabels:{%s}", strings.Join(nsLabelStrs, ",")))
var annotationStrs []string
for k, prop := range p.Annotations {
annotationStrs = append(annotationStrs, fmt.Sprintf("%s:%s", k, prop))
}
strs = append(strs, fmt.Sprintf("Annotations:{%s}", strings.Join(annotationStrs, ",")))
var nsAnnotationStrs []string
for k, prop := range p.NamespaceAnnotations {
nsAnnotationStrs = append(nsAnnotationStrs, fmt.Sprintf("%s:%s", k, prop))
}
strs = append(strs, fmt.Sprintf("NamespaceAnnotations:{%s}", strings.Join(nsAnnotationStrs, ",")))
return fmt.Sprintf("{%s}", strings.Join(strs, "; "))
}