-
Notifications
You must be signed in to change notification settings - Fork 523
Expand file tree
/
Copy pathutil.go
More file actions
361 lines (304 loc) · 8.29 KB
/
util.go
File metadata and controls
361 lines (304 loc) · 8.29 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package ovs
import (
"context"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"sync/atomic"
"time"
"k8s.io/klog/v2"
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 "localnet." + subnet
}
func trimCommandOutput(raw []byte) string {
return strings.Trim(strings.TrimSpace(string(raw)), `"`)
}
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 {
if sgName == "" {
return ""
}
return strings.ReplaceAll("ovn.sg."+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.SplitSeq(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
}
// buildDHCPv4Options constructs the DHCP options string for ipv4
func buildDHCPv4Options(options, gateway, mac string, mtu int, necessaryOptions []string) map[string]string {
if len(options) == 0 {
return map[string]string{
"lease_time": "3600",
"router": gateway,
"server_id": "169.254.0.254",
"server_mac": mac,
"mtu": strconv.Itoa(mtu),
}
}
parsedOptions := parseDHCPOptions(options)
for _, opt := range necessaryOptions {
if _, ok := parsedOptions[opt]; !ok {
switch opt {
case "lease_time":
parsedOptions[opt] = "3600"
case "router":
parsedOptions[opt] = gateway
case "server_id":
parsedOptions[opt] = "169.254.0.254"
case "server_mac":
parsedOptions[opt] = mac
case "mtu":
parsedOptions[opt] = strconv.Itoa(mtu)
}
}
}
return parsedOptions
}
// buildDHCPv6Options constructs the DHCP options string for ipv6
func buildDHCPv6Options(options, mac string, necessaryOptions []string) map[string]string {
if len(options) == 0 {
return map[string]string{
"server_id": mac,
}
}
parsedOptions := parseDHCPOptions(options)
for _, opt := range necessaryOptions {
if _, ok := parsedOptions[opt]; !ok {
if opt == "server_id" {
parsedOptions[opt] = mac
}
}
}
return parsedOptions
}
// formatDHCPOptions converts the parsed options map into a string format
// e.g. dns_server="{8.8.8.8,8.8.4.4}", lease_time="3600", mtu="1500", router="192.168.80.1", server_id="169.254.0.254", server_mac="5e:4e:e7:48:3d:7d"
func formatDHCPOptions(options map[string]string) string {
var sb strings.Builder
for k, v := range options {
if sb.Len() > 0 {
sb.WriteString(",")
}
if k == "dns_server" {
v = strings.ReplaceAll(v, ",", ";")
}
fmt.Fprintf(&sb, "%s=%s", k, v)
}
return sb.String()
}
// 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.SplitSeq(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
}
if kv[0] == "dns_server" {
kv[1] = strings.ReplaceAll(kv[1], ";", ",")
}
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 {
klog.Error(err)
return "", fmt.Errorf("generate match %s: %w", 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 {
klog.Error(err)
return "", fmt.Errorf("generate match %s: %w", match, err)
}
// has more than 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 "", errors.New("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 errors.New("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)
}