forked from cookiesowns/edgecontroller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraffic_policy_kube_ovn.go
390 lines (331 loc) · 7.99 KB
/
traffic_policy_kube_ovn.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2019 Intel Corporation
package cce
import (
"errors"
"fmt"
"net"
"strings"
"github.com/open-ness/edgecontroller/uuid"
coreV1 "k8s.io/api/core/v1"
networkingV1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
// TrafficPolicyKubeOVN is an application or interface traffic policy.
type TrafficPolicyKubeOVN struct {
ID string `json:"id"`
Name string `json:"name"`
Ingress []*IngressRule `json:"ingress_rules"`
Egress []*EgressRule `json:"egress_rules"`
}
// GetTableName returns the name of the persistence table.
func (*TrafficPolicyKubeOVN) GetTableName() string {
return "traffic_policies"
}
// GetID gets the ID.
func (tp *TrafficPolicyKubeOVN) GetID() string {
return tp.ID
}
// SetID sets the ID.
func (tp *TrafficPolicyKubeOVN) SetID(id string) {
tp.ID = id
}
// Validate validates the model.
func (tp *TrafficPolicyKubeOVN) Validate() error {
if !uuid.IsValid(tp.ID) {
return errors.New("id not a valid UUID")
}
if tp.Name == "" {
return errors.New("name cannot be empty")
}
if len(tp.Ingress) == 0 && len(tp.Egress) == 0 {
return errors.New("ingress and egress cannot be empty")
}
for i, rule := range tp.Ingress {
if err := rule.Validate(); err != nil {
return fmt.Errorf("Ingress[%d].%s", i, err.Error())
}
}
for i, rule := range tp.Egress {
if err := rule.Validate(); err != nil {
return fmt.Errorf("Egress[%d].%s", i, err.Error())
}
}
return nil
}
// FilterFields returns the filterable fields for this model.
func (*TrafficPolicyKubeOVN) FilterFields() []string {
return []string{}
}
func (tp *TrafficPolicyKubeOVN) String() string {
var ingress, egress string
for i, rule := range tp.Ingress {
ingress += rule.String()
if i < len(tp.Ingress)-1 {
ingress += "\n "
}
}
for i, rule := range tp.Egress {
egress += rule.String()
if i < len(tp.Egress)-1 {
egress += "\n "
}
}
return fmt.Sprintf(strings.TrimSpace(`
TrafficPolicyKubeOVN[
ID: %s,
Name: %s,
Ingress: [
%s
],
Egress: [
%s
]
]`),
tp.ID,
tp.Name,
ingress,
egress)
}
// IngressRule is the model for a ingress rule.
type IngressRule struct {
Description string `json:"description"`
From []*IPBlock `json:"from"`
Ports []*Port `json:"ports"`
}
// Validate validates the model.
func (ir *IngressRule) Validate() error {
for i, block := range ir.From {
if err := block.Validate(); err != nil {
return fmt.Errorf("From[%d].%s", i, err.Error())
}
}
for i, port := range ir.Ports {
if err := port.Validate(); err != nil {
return fmt.Errorf("Ports[%d].%s", i, err.Error())
}
}
return nil
}
func (ir *IngressRule) String() string {
var from, ports string
for i, block := range ir.From {
from += block.String()
if i < len(ir.From)-1 {
from += "\n "
}
}
for i, port := range ir.Ports {
ports += port.String()
if i < len(ir.Ports)-1 {
ports += "\n "
}
}
return fmt.Sprintf(strings.TrimSpace(`
IngressRule[
Description: %s,
From: [
%s
],
Ports: [
%s
]
]`),
ir.Description,
from,
ports)
}
// EgressRule is the model for a egress rule.
type EgressRule struct {
Description string `json:"description"`
To []*IPBlock `json:"to"`
Ports []*Port `json:"ports"`
}
// Validate validates the model.
func (er *EgressRule) Validate() error {
for i, block := range er.To {
if err := block.Validate(); err != nil {
return fmt.Errorf("To[%d].%s", i, err.Error())
}
}
for i, port := range er.Ports {
if err := port.Validate(); err != nil {
return fmt.Errorf("Ports[%d].%s", i, err.Error())
}
}
return nil
}
func (er *EgressRule) String() string {
var to, ports string
for i, block := range er.To {
to += block.String()
if i < len(er.To)-1 {
to += "\n "
}
}
for i, port := range er.Ports {
ports += port.String()
if i < len(er.Ports)-1 {
ports += "\n "
}
}
return fmt.Sprintf(strings.TrimSpace(`
EgressRule[
Description: %s,
To: [
%s
],
Ports: [
%s
]
]`),
er.Description,
to,
ports)
}
// IPBlock is the model for a ip block.
type IPBlock struct {
CIDR string `json:"cidr"`
Except []string `json:"except"`
}
// Validate validates the model.
func (ipb *IPBlock) Validate() error {
_, n, err := net.ParseCIDR(ipb.CIDR)
if err != nil {
return fmt.Errorf("Invalid CIDR: %s", err.Error())
}
for i, exceptCIDR := range ipb.Except {
exceptIP, exceptNet, err := net.ParseCIDR(exceptCIDR)
if err != nil {
return fmt.Errorf("Except[%d].Invalid CIDR: %s", i, err.Error())
}
if exceptNet.String() == n.String() {
return fmt.Errorf("Except[%d].CIDR(%s) is the same as CIDR(%s)", i, exceptCIDR, ipb.CIDR)
}
if !n.IP.Equal(exceptIP.Mask(n.Mask)) {
return fmt.Errorf("Except[%d].CIDR(%s) is not in CIDR(%s)", i, exceptCIDR, ipb.CIDR)
}
eS, _ := exceptNet.Mask.Size()
nS, _ := n.Mask.Size()
if eS <= nS {
return fmt.Errorf("Except[%d].CIDR(%s) mask is invalid", i, exceptCIDR)
}
}
return nil
}
func (ipb *IPBlock) String() string {
var except string
for i, e := range ipb.Except {
except += e
if i < len(ipb.Except)-1 {
except += "\n "
}
}
return fmt.Sprintf(strings.TrimSpace(`
IPBlock[
CIDR: %s,
Except: [
%s
]
]`),
ipb.CIDR,
except)
}
// Port is the model for a port.
type Port struct {
Port uint16 `json:"port"`
Protocol string `json:"protocol"`
}
// Validate validates the model.
func (p *Port) Validate() error {
if p.Protocol != "tcp" && p.Protocol != "udp" && p.Protocol != "sctp" {
return fmt.Errorf("Not supported protocol: %s", p.Protocol)
}
return nil
}
func (p *Port) String() string {
return fmt.Sprintf(strings.TrimSpace(`
Port[
Port: %d,
Protocol: %s
]`),
p.Port,
p.Protocol)
}
// ToK8s converts traffic policy into Kubernetes' NetworkPolicy
func (tp *TrafficPolicyKubeOVN) ToK8s() *networkingV1.NetworkPolicy {
np := &networkingV1.NetworkPolicy{
Spec: networkingV1.NetworkPolicySpec{},
}
is := []networkingV1.NetworkPolicyIngressRule{}
for _, ingress := range tp.Ingress {
is = append(is, ingress.ToK8s())
}
es := []networkingV1.NetworkPolicyEgressRule{}
for _, egress := range tp.Egress {
es = append(es, egress.ToK8s())
}
policyTypes := []networkingV1.PolicyType{}
if len(es) > 0 {
np.Spec.Egress = es
policyTypes = append(policyTypes, "Egress")
}
if len(is) > 0 {
np.Spec.Ingress = is
policyTypes = append(policyTypes, "Ingress")
}
np.Spec.PolicyTypes = policyTypes
return np
}
// ToK8s converts port into Kubernetes' port
func (p *Port) ToK8s() networkingV1.NetworkPolicyPort {
k8sProtocol := func(protocol string) coreV1.Protocol {
switch protocol {
case "udp", "UDP":
return coreV1.ProtocolUDP
case "sctp", "SCTP":
return coreV1.ProtocolSCTP
default:
return coreV1.ProtocolTCP
}
}
protocol := k8sProtocol(p.Protocol)
intstrPort := intstr.FromInt(int(p.Port))
return networkingV1.NetworkPolicyPort{
Protocol: &protocol,
Port: &intstrPort,
}
}
// ToK8s converts IPBlock into Kubernetes' Peer
func (ipb *IPBlock) ToK8s() networkingV1.NetworkPolicyPeer {
excepts := []string{}
excepts = append(excepts, ipb.Except...)
return networkingV1.NetworkPolicyPeer{
IPBlock: &networkingV1.IPBlock{
CIDR: ipb.CIDR,
Except: excepts,
},
}
}
// ToK8s converts ingress rule into Kubernetes' ingress rule
func (ir *IngressRule) ToK8s() networkingV1.NetworkPolicyIngressRule {
ingress := networkingV1.NetworkPolicyIngressRule{}
for _, port := range ir.Ports {
ingress.Ports = append(ingress.Ports, port.ToK8s())
}
for _, from := range ir.From {
ingress.From = append(ingress.From, from.ToK8s())
}
return ingress
}
// ToK8s converts egress rule into Kubernetes' egress rule
func (er *EgressRule) ToK8s() networkingV1.NetworkPolicyEgressRule {
egress := networkingV1.NetworkPolicyEgressRule{}
for _, port := range er.Ports {
egress.Ports = append(egress.Ports, port.ToK8s())
}
for _, to := range er.To {
egress.To = append(egress.To, to.ToK8s())
}
return egress
}