-
Notifications
You must be signed in to change notification settings - Fork 433
/
util.go
281 lines (232 loc) · 6.39 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package ovs
import (
"context"
"fmt"
"regexp"
"strings"
"sync/atomic"
"time"
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/kubeovn/kube-ovn/pkg/util"
)
var addressSetNameRegex = regexp.MustCompile(`^[a-zA-Z_.][a-zA-Z_.0-9]*$`)
// PodNameToPortName return the ovn port name for a given pod
func PodNameToPortName(pod, namespace, provider string) string {
if provider == util.OvnProvider {
return fmt.Sprintf("%s.%s", pod, namespace)
}
return fmt.Sprintf("%s.%s.%s", pod, namespace, provider)
}
func GetLocalnetName(subnet string) string {
return fmt.Sprintf("localnet.%s", subnet)
}
func trimCommandOutput(raw []byte) string {
output := strings.TrimSpace(string(raw))
return strings.Trim(output, "\"")
}
func LogicalRouterPortName(lr, ls string) string {
return fmt.Sprintf("%s-%s", lr, ls)
}
func LogicalSwitchPortName(lr, ls string) string {
return fmt.Sprintf("%s-%s", ls, lr)
}
func GetSgPortGroupName(sgName string) string {
return strings.ReplaceAll(fmt.Sprintf("ovn.sg.%s", sgName), "-", ".")
}
func GetSgV4AssociatedName(sgName string) string {
return strings.ReplaceAll(fmt.Sprintf("ovn.sg.%s.associated.v4", sgName), "-", ".")
}
func GetSgV6AssociatedName(sgName string) string {
return strings.ReplaceAll(fmt.Sprintf("ovn.sg.%s.associated.v6", sgName), "-", ".")
}
// parseIpv6RaConfigs parses the ipv6 ra config,
// return default Ipv6RaConfigs when raw="",
// the raw config's format is: address_mode=dhcpv6_stateful,max_interval=30,min_interval=5,send_periodic=true
func parseIpv6RaConfigs(raw string) map[string]string {
// return default Ipv6RaConfigs
if len(raw) == 0 {
return map[string]string{
"address_mode": "dhcpv6_stateful",
"max_interval": "30",
"min_interval": "5",
"send_periodic": "true",
}
}
Ipv6RaConfigs := make(map[string]string)
// trim blank
raw = strings.ReplaceAll(raw, " ", "")
options := strings.Split(raw, ",")
for _, option := range options {
kv := strings.Split(option, "=")
// TODO: ignore invalidate option, maybe need further validation
if len(kv) != 2 || len(kv[0]) == 0 || len(kv[1]) == 0 {
continue
}
Ipv6RaConfigs[kv[0]] = kv[1]
}
return Ipv6RaConfigs
}
// getIpv6Prefix get ipv6 prefix from networks
func getIpv6Prefix(networks []string) []string {
ipv6Prefix := make([]string, 0, len(networks))
for _, network := range networks {
if kubeovnv1.ProtocolIPv6 == util.CheckProtocol(network) {
ipv6Prefix = append(ipv6Prefix, strings.Split(network, "/")[1])
}
}
return ipv6Prefix
}
// parseDHCPOptions parses dhcp options,
// the raw option's format is: server_id=192.168.123.50,server_mac=00:00:00:08:0a:11
func parseDHCPOptions(raw string) map[string]string {
// return default Ipv6RaConfigs
if len(raw) == 0 {
return nil
}
dhcpOpt := make(map[string]string)
// trim blank
raw = strings.ReplaceAll(raw, " ", "")
options := strings.Split(raw, ",")
for _, option := range options {
kv := strings.Split(option, "=")
// TODO: ignore invalidate option, maybe need further validation
if len(kv) != 2 || len(kv[0]) == 0 || len(kv[1]) == 0 {
continue
}
dhcpOpt[kv[0]] = kv[1]
}
return dhcpOpt
}
func matchAddressSetName(asName string) bool {
return addressSetNameRegex.MatchString(asName)
}
type ACLMatch interface {
Match() (string, error)
String() string
}
type AndACLMatch struct {
matches []ACLMatch
}
func NewAndACLMatch(matches ...ACLMatch) ACLMatch {
return AndACLMatch{
matches: matches,
}
}
// Rule generate acl match like 'ip4.src == $test.allow.as && ip4.src != $test.except.as && 12345 <= tcp.dst <= 12500 && outport == @ovn.sg.test_sg && ip'
func (m AndACLMatch) Match() (string, error) {
var matches []string
for _, r := range m.matches {
match, err := r.Match()
if err != nil {
return "", fmt.Errorf("generate match %s: %v", match, err)
}
matches = append(matches, match)
}
return strings.Join(matches, " && "), nil
}
func (m AndACLMatch) String() string {
match, _ := m.Match()
return match
}
type OrACLMatch struct {
matches []ACLMatch
}
func NewOrACLMatch(matches ...ACLMatch) ACLMatch {
return OrACLMatch{
matches: matches,
}
}
// Match generate acl match like '(ip4.src==10.250.0.0/16 && ip4.dst==10.244.0.0/16) || (ip4.src==10.244.0.0/16 && ip4.dst==10.250.0.0/16)'
func (m OrACLMatch) Match() (string, error) {
var matches []string
for _, specification := range m.matches {
match, err := specification.Match()
if err != nil {
return "", fmt.Errorf("generate match %s: %v", match, err)
}
// has more then one rule
if strings.Contains(match, "&&") {
match = "(" + match + ")"
}
matches = append(matches, match)
}
return strings.Join(matches, " || "), nil
}
func (m OrACLMatch) String() string {
match, _ := m.Match()
return match
}
type aclMatch struct {
key string
value string
maxValue string
effect string
}
func NewACLMatch(key, effect, value, maxValue string) ACLMatch {
return aclMatch{
key: key,
effect: effect,
value: value,
maxValue: maxValue,
}
}
// Match generate acl match like
// 'ip4.src == $test.allow.as'
// or 'ip4.src != $test.except.as'
// or '12345 <= tcp.dst <= 12500'
// or 'tcp.dst == 13500'
// or 'outport == @ovn.sg.test_sg && ip'
func (m aclMatch) Match() (string, error) {
// key must exist at least
if len(m.key) == 0 {
return "", fmt.Errorf("acl rule key is required")
}
// like 'ip'
if len(m.effect) == 0 || len(m.value) == 0 {
return m.key, nil
}
// like 'tcp.dst == 13500' or 'ip4.src == $test.allow.as'
if len(m.maxValue) == 0 {
return fmt.Sprintf("%s %s %s", m.key, m.effect, m.value), nil
}
// like '12345 <= tcp.dst <= 12500'
return fmt.Sprintf("%s %s %s %s %s", m.value, m.effect, m.key, m.effect, m.maxValue), nil
}
func (m aclMatch) String() string {
rule, _ := m.Match()
return rule
}
type Limiter struct {
limit int32
current int32
}
func (l *Limiter) Limit() int32 {
return l.limit
}
func (l *Limiter) Current() int32 {
return atomic.LoadInt32(&l.current)
}
func (l *Limiter) Update(limit int32) {
l.limit = limit
}
func (l *Limiter) Wait(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return fmt.Errorf("context canceled by timeout")
default:
if l.limit == 0 {
atomic.AddInt32(&l.current, 1)
return nil
}
if atomic.LoadInt32(&l.current) < l.limit {
atomic.AddInt32(&l.current, 1)
return nil
}
time.Sleep(10 * time.Millisecond)
}
}
}
func (l *Limiter) Done() {
atomic.AddInt32(&l.current, -1)
}