-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
152 lines (132 loc) · 4.26 KB
/
filter.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
package main
import (
"fmt"
"regexp"
"strings"
"github.com/gobwas/glob"
"github.com/rancher/go-rancher/v2"
)
func filterEnvironment(rancher RancherGenClient, env client.Project, filter string) bool {
return filterSomething(rancher, env, filter1Environment, filter)
}
func filterHost(rancher RancherGenClient, host client.Host, filter string) bool {
return filterSomething(rancher, host, filter1Host, filter)
}
func filterStack(rancher RancherGenClient, stack client.Stack, filter string) bool {
return filterSomething(rancher, stack, filter1Stack, filter)
}
func filterService(rancher RancherGenClient, service client.Service, filter string) bool {
return filterSomething(rancher, service, filter1Service, filter)
}
func filterSomething(rancher RancherGenClient,
obj interface{},
filterFunc func(RancherGenClient, interface{}, string) bool,
filter string) (match bool) {
match = false
re := regexp.MustCompile("^([+-]?)([a-zA-Z0-9\\.=\\_*%\\(\\)-]*)(!L)?$")
for _, r := range strings.Split(filter, ",") {
ruleParts := re.FindStringSubmatch(r)
if ruleParts == nil {
panic("failed to match " + r)
}
ie, rule, mod := ruleParts[1], ruleParts[2], ruleParts[3]
if filterFunc(rancher, obj, rule) {
if ie == "-" {
match = false
} else {
match = true
}
if mod == "!L" {
return
}
}
}
return
}
func filter1Environment(rancher RancherGenClient, obj interface{}, rule string) bool {
env := obj.(client.Project)
if rule == "" {
return true
} else if glob.MustCompile(rule).Match(env.Name) {
return true
}
return false
}
func filter1Host(rancher RancherGenClient, obj interface{}, rule string) bool {
host := obj.(client.Host)
if rule == "" {
return true
} else if m := regexp.MustCompile("^%ENV=([a-zA-Z\\.\\*]+)$").FindStringSubmatch(rule); m != nil {
if glob.MustCompile(m[1]).Match(rancher.GetEnvironment(host.AccountId).Name) {
return true
}
} else if m := regexp.MustCompile("^([a-zA-Z\\.\\*]+)=([a-zA-Z\\.\\*]+)$").FindStringSubmatch(rule); m != nil {
for l, v := range host.Labels {
if glob.MustCompile(rule).Match(fmt.Sprintf("%s=%s", l, v)) {
return true
}
}
} else if glob.MustCompile(rule).Match(host.Hostname) {
return true
}
return false
}
func filter1Stack(rancher RancherGenClient, obj interface{}, rule string) bool {
stack := obj.(client.Stack)
if rule == "" {
return true
} else if m := regexp.MustCompile("^%ENV=([a-zA-Z\\.\\*]+)$").FindStringSubmatch(rule); m != nil {
if glob.MustCompile(m[1]).Match(rancher.GetEnvironment(stack.AccountId).Name) {
return true
}
} else if rule == "%SYSTEM" {
if stack.System {
return true
}
} else if m := regexp.MustCompile("^%HAS_SERVICE\\(([a-zA-Z0-9\\.\\*_-]+)\\)$").FindStringSubmatch(rule); m != nil {
for _, s := range stack.ServiceIds {
if glob.MustCompile(m[1]).Match(rancher.GetService(s).Name) {
return true
}
}
} else if m := regexp.MustCompile("^%HAS_SERVICE\\(([a-zA-Z0-9\\.\\*_-]+)=([a-zA-Z0-9\\.\\*_-]+)\\)$").FindStringSubmatch(rule); m != nil {
for _, s := range stack.ServiceIds {
service := rancher.GetService(s)
for l, v := range service.LaunchConfig.Labels {
if glob.MustCompile(fmt.Sprintf("%s=%s", m[1], m[2])).Match(fmt.Sprintf("%s=%s", l, v)) {
return true
}
}
}
} else if glob.MustCompile(rule).Match(stack.Name) {
return true
}
return false
}
func filter1Service(rancher RancherGenClient, obj interface{}, rule string) bool {
service := obj.(client.Service)
if rule == "" {
return true
} else if m := regexp.MustCompile("^%ENV=([a-zA-Z\\.\\*]+)$").FindStringSubmatch(rule); m != nil {
if glob.MustCompile(m[1]).Match(rancher.GetEnvironment(service.AccountId).Name) {
return true
}
} else if rule == "%SYSTEM" {
if service.System {
return true
}
} else if m := regexp.MustCompile("^%STACK=([a-zA-Z\\.\\*]+)$").FindStringSubmatch(rule); m != nil {
if glob.MustCompile(m[1]).Match(rancher.GetStack(service.StackId).Name) {
return true
}
} else if m := regexp.MustCompile("^([a-zA-Z\\.\\*]+)=([a-zA-Z\\.\\*]+)$").FindStringSubmatch(rule); m != nil {
for l, v := range service.LaunchConfig.Labels {
if glob.MustCompile(rule).Match(fmt.Sprintf("%s=%s", l, v)) {
return true
}
}
} else if glob.MustCompile(rule).Match(service.Name) {
return true
}
return false
}