-
Notifications
You must be signed in to change notification settings - Fork 3
/
entity_config.go
66 lines (58 loc) · 1.81 KB
/
entity_config.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
package v3
import (
"strconv"
"strings"
corev2 "github.com/sensu/core/v2"
stringutil "github.com/sensu/core/v3/internal/strings"
)
var entityConfigRBACName = (&corev2.Entity{}).RBACName()
func (e *EntityConfig) rbacName() string {
return entityConfigRBACName
}
func (e *EntityConfig) Fields() map[string]string {
fields := map[string]string{
"entity_config.name": e.Metadata.Name,
"entity_config.namespace": e.Metadata.Namespace,
"entity_config.deregister": strconv.FormatBool(e.Deregister),
"entity_config.entity_class": e.EntityClass,
"entity_config.subscriptions": strings.Join(e.Subscriptions, ","),
}
MergeMapWithPrefix(fields, e.Metadata.Labels, "entity_config.labels.")
return fields
}
// MergeMapWithPrefix merges contents of one map into another using a prefix.
func MergeMapWithPrefix(a map[string]string, b map[string]string, prefix string) {
for k, v := range b {
a[prefix+k] = v
}
}
func redactMap(m map[string]string, redact []string) map[string]string {
if len(redact) == 0 {
redact = corev2.DefaultRedactFields
}
result := make(map[string]string, len(m))
for k, v := range m {
if stringutil.FoundInArray(k, redact) {
result[k] = corev2.Redacted
} else {
result[k] = v
}
}
return result
}
// ProduceRedacted redacts the entity according to the entity's Redact fields.
// A redacted copy is returned. The copy contains pointers to the original's
// memory, with different Labels and Annotations.
func (e *EntityConfig) ProduceRedacted() Resource {
if e == nil {
return nil
}
if e.Metadata == nil || (e.Metadata.Labels == nil && e.Metadata.Annotations == nil) {
return e
}
copy := &EntityConfig{}
*copy = *e
copy.Metadata.Annotations = redactMap(e.Metadata.Annotations, e.Redact)
copy.Metadata.Labels = redactMap(e.Metadata.Labels, e.Redact)
return copy
}