-
Notifications
You must be signed in to change notification settings - Fork 20
/
transformers.go
81 lines (73 loc) · 2.29 KB
/
transformers.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
package secrets
import (
"fmt"
v1 "k8s.io/api/core/v1"
"strings"
)
// SecretTransformer is a function that modifies the given secret.
type SecretTransformer = func(*v1.Secret)
// UniqueNameTransformer returns a secret transformer function that sets
// `metadata.generateName` to the original `metadata.name` with '-' appended as
// separator and removes `metadata.name`.
func UniqueNameTransformer() SecretTransformer {
return func(secret *v1.Secret) {
secret.SetGenerateName(fmt.Sprintf("%s-", secret.GetName()))
secret.SetName("")
}
}
// SetAnnotationTransformer returns a secret transformer function that sets the
// annotation with the given key to the given value.
func SetAnnotationTransformer(key string, value string) SecretTransformer {
return func(secret *v1.Secret) {
annotations := secret.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
annotations[key] = value
secret.SetAnnotations(annotations)
}
}
// StripAnnotationsTransformer returns a secret transformer function that
// removes all annotations where the key starts with the given 'keyPrefix'.
func StripAnnotationsTransformer(keyPrefix string) SecretTransformer {
return func(secret *v1.Secret) {
annotations := secret.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
for key := range annotations {
if strings.HasPrefix(key, keyPrefix) {
delete(annotations, key)
}
}
secret.SetAnnotations(annotations)
}
}
// SetLabelTransformer returns a secret transformer function that sets the
// label with the given key to the given value.
func SetLabelTransformer(key string, value string) SecretTransformer {
return func(secret *v1.Secret) {
labels := secret.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[key] = value
secret.SetLabels(labels)
}
}
// StripLabelsTransformer returns a secret transformer function that
// removes all labels where the key starts with the given 'keyPrefix'.
func StripLabelsTransformer(keyPrefix string) SecretTransformer {
return func(secret *v1.Secret) {
labels := secret.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
for key := range labels {
if strings.HasPrefix(key, keyPrefix) {
delete(labels, key)
}
}
secret.SetLabels(labels)
}
}