-
Notifications
You must be signed in to change notification settings - Fork 333
/
multihoming_utils.go
514 lines (462 loc) · 13.4 KB
/
multihoming_utils.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package e2e
import (
"context"
"encoding/json"
"fmt"
"net"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/rand"
clientset "k8s.io/client-go/kubernetes"
e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
mnpapi "github.com/k8snetworkplumbingwg/multi-networkpolicy/pkg/apis/k8s.cni.cncf.io/v1beta1"
nadapi "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
)
func netCIDR(netCIDR string, netPrefixLengthPerNode int) string {
return fmt.Sprintf("%s/%d", netCIDR, netPrefixLengthPerNode)
}
func getNetCIDRSubnet(netCIDR string) (string, error) {
subStrings := strings.Split(netCIDR, "/")
if len(subStrings) == 3 {
return subStrings[0] + "/" + subStrings[1], nil
} else if len(subStrings) == 2 {
return netCIDR, nil
}
return "", fmt.Errorf("invalid network cidr: %q", netCIDR)
}
type networkAttachmentConfigParams struct {
cidr string
excludeCIDRs []string
namespace string
name string
topology string
networkName string
vlanID int
}
type networkAttachmentConfig struct {
networkAttachmentConfigParams
}
func newNetworkAttachmentConfig(params networkAttachmentConfigParams) networkAttachmentConfig {
networkAttachmentConfig := networkAttachmentConfig{
networkAttachmentConfigParams: params,
}
if networkAttachmentConfig.networkName == "" {
networkAttachmentConfig.networkName = uniqueNadName(networkAttachmentConfig.name)
}
return networkAttachmentConfig
}
func uniqueNadName(originalNetName string) string {
const randomStringLength = 5
return fmt.Sprintf("%s_%s", rand.String(randomStringLength), originalNetName)
}
func generateNAD(config networkAttachmentConfig) *nadapi.NetworkAttachmentDefinition {
nadSpec := fmt.Sprintf(
`
{
"cniVersion": "0.3.0",
"name": %q,
"type": "ovn-k8s-cni-overlay",
"topology":%q,
"subnets": %q,
"excludeSubnets": %q,
"mtu": 1300,
"netAttachDefName": %q,
"vlanID": %d
}
`,
config.networkName,
config.topology,
config.cidr,
strings.Join(config.excludeCIDRs, ","),
namespacedName(config.namespace, config.name),
config.vlanID,
)
return generateNetAttachDef(config.namespace, config.name, nadSpec)
}
func generateNetAttachDef(namespace, nadName, nadSpec string) *nadapi.NetworkAttachmentDefinition {
return &nadapi.NetworkAttachmentDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: nadName,
Namespace: namespace,
},
Spec: nadapi.NetworkAttachmentDefinitionSpec{Config: nadSpec},
}
}
type podConfiguration struct {
attachments []nadapi.NetworkSelectionElement
containerCmd []string
name string
namespace string
nodeSelector map[string]string
isPrivileged bool
labels map[string]string
requiresExtraNamespace bool
}
func generatePodSpec(config podConfiguration) *v1.Pod {
podSpec := e2epod.NewAgnhostPod(config.namespace, config.name, nil, nil, nil, config.containerCmd...)
podSpec.Annotations = networkSelectionElements(config.attachments...)
podSpec.Spec.NodeSelector = config.nodeSelector
podSpec.Labels = config.labels
if config.isPrivileged {
privileged := true
podSpec.Spec.Containers[0].SecurityContext.Privileged = &privileged
}
return podSpec
}
func networkSelectionElements(elements ...nadapi.NetworkSelectionElement) map[string]string {
marshalledElements, err := json.Marshal(elements)
if err != nil {
panic(fmt.Errorf("programmer error: you've provided wrong input to the test data: %v", err))
}
return map[string]string{
nadapi.NetworkAttachmentAnnot: string(marshalledElements),
}
}
func httpServerContainerCmd(port uint16) []string {
return []string{"netexec", "--http-port", fmt.Sprintf("%d", port)}
}
func podNetworkStatus(pod *v1.Pod, predicates ...func(nadapi.NetworkStatus) bool) ([]nadapi.NetworkStatus, error) {
podNetStatus, found := pod.Annotations[nadapi.NetworkStatusAnnot]
if !found {
return nil, fmt.Errorf("the pod must feature the `networks-status` annotation")
}
var netStatus []nadapi.NetworkStatus
if err := json.Unmarshal([]byte(podNetStatus), &netStatus); err != nil {
return nil, err
}
var netStatusMeetingPredicates []nadapi.NetworkStatus
for i := range netStatus {
for _, predicate := range predicates {
if predicate(netStatus[i]) {
netStatusMeetingPredicates = append(netStatusMeetingPredicates, netStatus[i])
continue
}
}
}
return netStatusMeetingPredicates, nil
}
func namespacedName(namespace, name string) string {
return fmt.Sprintf("%s/%s", namespace, name)
}
func inRange(cidr string, ip string) error {
_, cidrRange, err := net.ParseCIDR(cidr)
if err != nil {
return err
}
if cidrRange.Contains(net.ParseIP(ip)) {
return nil
}
return fmt.Errorf("ip [%s] is NOT in range %s", ip, cidr)
}
func connectToServer(clientPodConfig podConfiguration, serverIP string, port int) error {
_, err := e2ekubectl.RunKubectl(
clientPodConfig.namespace,
"exec",
clientPodConfig.name,
"--",
"curl",
"--connect-timeout",
"2",
net.JoinHostPort(serverIP, fmt.Sprintf("%d", port)),
)
return err
}
func newAttachmentConfigWithOverriddenName(name, namespace, networkName, topology, cidr string) networkAttachmentConfig {
return newNetworkAttachmentConfig(
networkAttachmentConfigParams{
cidr: cidr,
name: name,
namespace: namespace,
networkName: networkName,
topology: topology,
},
)
}
func configurePodStaticIP(podNamespace string, podName string, staticIP string) error {
_, err := e2ekubectl.RunKubectl(
podNamespace, "exec", podName, "--",
"ip", "addr", "add", staticIP, "dev", "net1",
)
return err
}
func areStaticIPsConfiguredViaCNI(podConfig podConfiguration) bool {
for _, attachment := range podConfig.attachments {
if len(attachment.IPRequest) > 0 {
return true
}
}
return false
}
func podIPForAttachment(k8sClient clientset.Interface, podNamespace string, podName string, attachmentName string, ipIndex int) (string, error) {
pod, err := k8sClient.CoreV1().Pods(podNamespace).Get(context.Background(), podName, metav1.GetOptions{})
if err != nil {
return "", err
}
netStatus, err := podNetworkStatus(pod, func(status nadapi.NetworkStatus) bool {
return status.Name == namespacedName(podNamespace, attachmentName)
})
if err != nil {
return "", err
}
if len(netStatus) != 1 {
return "", fmt.Errorf("more than one status entry for attachment %s on pod %s", attachmentName, namespacedName(podNamespace, podName))
}
if len(netStatus[0].IPs) == 0 {
return "", fmt.Errorf("no IPs for attachment %s on pod %s", attachmentName, namespacedName(podNamespace, podName))
}
return netStatus[0].IPs[ipIndex], nil
}
func allowedClient(podName string) string {
return "allowed-" + podName
}
func blockedClient(podName string) string {
return "blocked-" + podName
}
func multiNetIngressLimitingPolicy(policyFor string, appliesFor metav1.LabelSelector, allowForSelector metav1.LabelSelector, allowPorts ...int) *mnpapi.MultiNetworkPolicy {
var (
portAllowlist []mnpapi.MultiNetworkPolicyPort
)
tcp := v1.ProtocolTCP
for _, port := range allowPorts {
p := intstr.FromInt(port)
portAllowlist = append(portAllowlist, mnpapi.MultiNetworkPolicyPort{
Protocol: &tcp,
Port: &p,
})
}
return &mnpapi.MultiNetworkPolicy{
ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{
PolicyForAnnotation: policyFor,
},
Name: "allow-ports-via-pod-selector",
},
Spec: mnpapi.MultiNetworkPolicySpec{
PodSelector: appliesFor,
Ingress: []mnpapi.MultiNetworkPolicyIngressRule{
{
Ports: portAllowlist,
From: []mnpapi.MultiNetworkPolicyPeer{
{
PodSelector: &allowForSelector,
},
},
},
},
PolicyTypes: []mnpapi.MultiPolicyType{mnpapi.PolicyTypeIngress},
},
}
}
func multiNetIngressLimitingIPBlockPolicy(
policyFor string,
appliesFor metav1.LabelSelector,
allowForIPBlock mnpapi.IPBlock,
allowPorts ...int,
) *mnpapi.MultiNetworkPolicy {
var (
portAllowlist []mnpapi.MultiNetworkPolicyPort
)
tcp := v1.ProtocolTCP
for _, port := range allowPorts {
p := intstr.FromInt(port)
portAllowlist = append(portAllowlist, mnpapi.MultiNetworkPolicyPort{
Protocol: &tcp,
Port: &p,
})
}
return &mnpapi.MultiNetworkPolicy{
ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{
PolicyForAnnotation: policyFor,
},
Name: "allow-ports-ip-block",
},
Spec: mnpapi.MultiNetworkPolicySpec{
PodSelector: appliesFor,
Ingress: []mnpapi.MultiNetworkPolicyIngressRule{
{
Ports: portAllowlist,
From: []mnpapi.MultiNetworkPolicyPeer{
{
IPBlock: &allowForIPBlock,
},
},
},
},
PolicyTypes: []mnpapi.MultiPolicyType{mnpapi.PolicyTypeIngress},
},
}
}
func multiNetEgressLimitingIPBlockPolicy(
policyName string,
policyFor string,
appliesFor metav1.LabelSelector,
allowForIPBlock mnpapi.IPBlock,
) *mnpapi.MultiNetworkPolicy {
return &mnpapi.MultiNetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: policyName,
Annotations: map[string]string{
PolicyForAnnotation: policyFor,
},
},
Spec: mnpapi.MultiNetworkPolicySpec{
PodSelector: appliesFor,
Egress: []mnpapi.MultiNetworkPolicyEgressRule{
{
To: []mnpapi.MultiNetworkPolicyPeer{
{
IPBlock: &allowForIPBlock,
},
},
},
},
PolicyTypes: []mnpapi.MultiPolicyType{mnpapi.PolicyTypeEgress},
},
}
}
func multiNetPolicy(
policyName string,
policyFor string,
appliesFor metav1.LabelSelector,
policyTypes []mnpapi.MultiPolicyType,
ingress []mnpapi.MultiNetworkPolicyIngressRule,
egress []mnpapi.MultiNetworkPolicyEgressRule,
) *mnpapi.MultiNetworkPolicy {
return &mnpapi.MultiNetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: policyName,
Annotations: map[string]string{
PolicyForAnnotation: policyFor,
},
},
Spec: mnpapi.MultiNetworkPolicySpec{
PodSelector: appliesFor,
PolicyTypes: policyTypes,
Ingress: ingress,
Egress: egress,
},
}
}
func doesPolicyFeatAnIPBlock(policy *mnpapi.MultiNetworkPolicy) bool {
for _, rule := range policy.Spec.Ingress {
for _, peer := range rule.From {
if peer.IPBlock != nil {
return true
}
}
}
for _, rule := range policy.Spec.Egress {
for _, peer := range rule.To {
if peer.IPBlock != nil {
return true
}
}
}
return false
}
func setBlockedClientIPInPolicyIPBlockExcludedRanges(policy *mnpapi.MultiNetworkPolicy, blockedIP string) {
if policy.Spec.Ingress != nil {
for _, rule := range policy.Spec.Ingress {
for _, peer := range rule.From {
if peer.IPBlock != nil {
peer.IPBlock.Except = []string{blockedIP}
}
}
}
}
if policy.Spec.Egress != nil {
for _, rule := range policy.Spec.Egress {
for _, peer := range rule.To {
if peer.IPBlock != nil {
peer.IPBlock.Except = []string{blockedIP}
}
}
}
}
}
func multiNetIngressLimitingPolicyAllowFromNamespace(
policyFor string, appliesFor metav1.LabelSelector, allowForSelector metav1.LabelSelector, allowPorts ...int,
) *mnpapi.MultiNetworkPolicy {
return &mnpapi.MultiNetworkPolicy{
ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{
PolicyForAnnotation: policyFor,
},
Name: "allow-ports-same-ns",
},
Spec: mnpapi.MultiNetworkPolicySpec{
PodSelector: appliesFor,
Ingress: []mnpapi.MultiNetworkPolicyIngressRule{
{
Ports: allowedTCPPortsForPolicy(allowPorts...),
From: []mnpapi.MultiNetworkPolicyPeer{
{
NamespaceSelector: &allowForSelector,
},
},
},
},
PolicyTypes: []mnpapi.MultiPolicyType{mnpapi.PolicyTypeIngress},
},
}
}
func allowedTCPPortsForPolicy(allowPorts ...int) []mnpapi.MultiNetworkPolicyPort {
var (
portAllowlist []mnpapi.MultiNetworkPolicyPort
)
tcp := v1.ProtocolTCP
for _, port := range allowPorts {
p := intstr.FromInt(port)
portAllowlist = append(portAllowlist, mnpapi.MultiNetworkPolicyPort{
Protocol: &tcp,
Port: &p,
})
}
return portAllowlist
}
func reachToServerPodFromClient(cs clientset.Interface, serverConfig podConfiguration, clientConfig podConfiguration, serverIP string, serverPort int) error {
updatedPod, err := cs.CoreV1().Pods(serverConfig.namespace).Get(context.Background(), serverConfig.name, metav1.GetOptions{})
if err != nil {
return err
}
if updatedPod.Status.Phase == v1.PodRunning {
return connectToServer(clientConfig, serverIP, serverPort)
}
return fmt.Errorf("pod not running. /me is sad")
}
func findInterfaceByIP(targetIP string) (string, error) {
interfaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range interfaces {
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
if err != nil {
return "", err
}
if ip.String() == targetIP {
return iface.Name, nil
}
}
}
return "", fmt.Errorf("Interface with IP %s not found", targetIP)
}
func getNetworkGateway(cli *client.Client, networkName string) (string, error) {
network, err := cli.NetworkInspect(context.Background(), networkName, types.NetworkInspectOptions{})
if err != nil {
return "", err
}
if network.IPAM.Config != nil && len(network.IPAM.Config) > 0 {
gatewayIP := network.IPAM.Config[0].Gateway
return gatewayIP, nil
}
return "", fmt.Errorf("Gateway not found for network %q", networkName)
}