-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
58 lines (47 loc) · 1.41 KB
/
util.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
package resrc
import (
"regexp"
"strings"
"github.com/samber/lo"
"k8s.io/apimachinery/pkg/runtime/schema"
)
func IsCRDFromGK(groupKind schema.GroupKind) bool {
return groupKind == schema.GroupKind{
Group: "apiextensions.k8s.io",
Kind: "CustomResourceDefinition",
}
}
func IsCRDFromGR(groupKind schema.GroupResource) bool {
return groupKind == schema.GroupResource{
Group: "apiextensions.k8s.io",
Resource: "customresourcedefinitions",
}
}
func IsSecret(groupKind schema.GroupKind) bool {
return groupKind == schema.GroupKind{
Group: "",
Kind: "Secret",
}
}
func IsHook(annotations map[string]string) bool {
_, _, found := FindAnnotationOrLabelByKeyPattern(annotations, annotationKeyPatternHook)
return found
}
func FindAnnotationOrLabelByKeyPattern(annotationsOrLabels map[string]string, pattern *regexp.Regexp) (key string, value string, found bool) {
key, found = lo.FindKeyBy(annotationsOrLabels, func(k string, _ string) bool {
return pattern.MatchString(k)
})
if found {
value = strings.TrimSpace(annotationsOrLabels[key])
}
return key, value, found
}
func FindAnnotationsOrLabelsByKeyPattern(annotationsOrLabels map[string]string, pattern *regexp.Regexp) (result map[string]string, found bool) {
result = map[string]string{}
for key, value := range annotationsOrLabels {
if pattern.MatchString(key) {
result[key] = strings.TrimSpace(value)
}
}
return result, len(result) > 0
}