-
Notifications
You must be signed in to change notification settings - Fork 66
/
namespaces.go
55 lines (42 loc) · 1.25 KB
/
namespaces.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
package utils
import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"strings"
)
func GetNamespaceSets(nsInclude string, nsExclude string) (sets.String, sets.String) {
NamespaceInclude := sets.NewString()
NamespaceExclude := sets.NewString()
if nsInclude == "" {
NamespaceInclude.Insert("*")
} else {
inclusionList := strings.TrimSpace(nsInclude)
inclusion := strings.Split(inclusionList, ",")
for _, inc := range inclusion {
NamespaceInclude.Insert(strings.ToLower(inc))
}
}
if nsExclude != "" {
exclusionList := strings.TrimSpace(nsExclude)
exclusion := strings.Split(exclusionList, ",")
for _, exc := range exclusion {
NamespaceExclude.Insert(strings.ToLower(exc))
}
}
return NamespaceInclude, NamespaceExclude
}
func IsNamespaceIncluded(namespace string, nsInclude sets.String, nsExclude sets.String) bool {
if nsExclude.Len() > 0 && isInNamespace(namespace, nsExclude) {
return false
}
if nsInclude.Len() > 0 && isInNamespace(namespace, nsInclude) {
return true
}
return false
}
func isInNamespace(namespace string, namespaceSet sets.String) (valid bool) {
if namespaceSet.Has(v1.NamespaceAll) || namespaceSet.Has("*") || namespaceSet.Has(strings.ToLower(namespace)) {
return true
}
return false
}