/
lb_mgmt_network.go
870 lines (803 loc) · 25.2 KB
/
lb_mgmt_network.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/*
Licensed under the Apache License, Version 2.0 (the "License");
@you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package octavia
import (
"context"
"fmt"
"github.com/go-logr/logr"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/external"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/provider"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
octaviav1 "github.com/openstack-k8s-operators/octavia-operator/api/v1beta1"
)
type NetworkProvisioningSummary struct {
TenantNetworkID string
TenantSubnetID string
TenantRouterPortID string
ProviderNetworkID string
RouterID string
SecurityGroupID string
}
//
// TODO(beagles) we need to decide what, if any of the results of these methods we want to expose in the controller's
// status.
//
func findPort(client *gophercloud.ServiceClient, networkID string, ipAddress string, log *logr.Logger) (*ports.Port, error) {
listOpts := ports.ListOpts{
NetworkID: networkID,
}
allPages, err := ports.List(client, listOpts).AllPages()
if err != nil {
log.Error(err, fmt.Sprintf("Unable to list ports for %s", networkID))
return nil, err
}
allPorts, err := ports.ExtractPorts(allPages)
if err != nil {
log.Error(err, "Unable to extract port information from list")
return nil, err
}
if len(allPorts) > 0 {
for _, port := range allPorts {
if len(port.FixedIPs) > 0 && port.FixedIPs[0].IPAddress == ipAddress {
return &port, nil
}
}
}
return nil, nil
}
func ensurePort(client *gophercloud.ServiceClient, tenantNetwork *networks.Network, tenantSubnet *subnets.Subnet,
securityGroups *[]string, log *logr.Logger) (*ports.Port, error) {
ipAddress := LbMgmtRouterPortIPv4
if tenantSubnet.IPVersion == 6 {
ipAddress = LbMgmtRouterPortIPv6
}
p, err := findPort(client, tenantNetwork.ID, ipAddress, log)
if err != nil {
return nil, err
}
if p != nil {
//
// TODO(beagles): reconcile port properties? Is there anything to do? Security groups possibly.
//
return p, nil
}
log.Info("Unable to locate port, creating new one")
asu := true
createOpts := ports.CreateOpts{
Name: LbMgmtRouterPortName,
AdminStateUp: &asu,
NetworkID: tenantNetwork.ID,
FixedIPs: []ports.IP{
{
SubnetID: tenantSubnet.ID,
IPAddress: ipAddress,
},
},
SecurityGroups: securityGroups,
}
p, err = ports.Create(client, createOpts).Extract()
if err != nil {
log.Error(err, "Error creating port")
return nil, err
}
return p, nil
}
func ensureSubnet(client *gophercloud.ServiceClient, ipVersion int, createOpts subnets.CreateOpts, log *logr.Logger) (*subnets.Subnet, error) {
listOpts := subnets.ListOpts{
Name: createOpts.Name,
NetworkID: createOpts.NetworkID,
TenantID: createOpts.TenantID,
IPVersion: ipVersion,
}
allPages, err := subnets.List(client, listOpts).AllPages()
if err != nil {
return nil, err
}
allSubnets, err := subnets.ExtractSubnets(allPages)
if err != nil {
return nil, err
}
var lbMgmtSubnet *subnets.Subnet
if len(allSubnets) == 0 {
log.Info(fmt.Sprintf("Creating Octavia management subnet \"%s\"", createOpts.Name))
lbMgmtSubnet, err = subnets.Create(client, createOpts).Extract()
if err != nil {
return nil, err
}
} else {
lbMgmtSubnet = &allSubnets[0]
delta := subnets.UpdateOpts{}
updateNeeded := false
if lbMgmtSubnet.Description != createOpts.Description {
delta.Description = &createOpts.Description
updateNeeded = true
}
if lbMgmtSubnet.AllocationPools[0].Start != createOpts.AllocationPools[0].Start ||
lbMgmtSubnet.AllocationPools[0].End != createOpts.AllocationPools[0].End {
delta.AllocationPools = []subnets.AllocationPool{
{
Start: createOpts.AllocationPools[0].Start,
End: createOpts.AllocationPools[0].End,
},
}
updateNeeded = true
}
if lbMgmtSubnet.GatewayIP != *createOpts.GatewayIP {
delta.GatewayIP = createOpts.GatewayIP
updateNeeded = true
}
if updateNeeded {
log.Info(fmt.Sprintf("Updating Octavia management subnet \"%s\"", createOpts.Name))
lbMgmtSubnet, err = subnets.Update(client, lbMgmtSubnet.ID, delta).Extract()
if err != nil {
return nil, err
}
}
}
return lbMgmtSubnet, nil
}
func getNetwork(client *gophercloud.ServiceClient, networkName string, serviceTenantID string) (*networks.Network, error) {
listOpts := networks.ListOpts{
Name: networkName,
TenantID: serviceTenantID,
}
allPages, err := networks.List(client, listOpts).AllPages()
if err != nil {
return nil, err
}
allNetworks, err := networks.ExtractNetworks(allPages)
if err != nil {
return nil, err
}
if len(allNetworks) > 0 {
return &allNetworks[0], nil
}
return nil, nil
}
func getNetworkExt(client *gophercloud.ServiceClient, networkName string, serviceTenantID string) (*networks.Network, error) {
extTrue := true
listOpts := external.ListOptsExt{
ListOptsBuilder: networks.ListOpts{
Name: networkName,
TenantID: serviceTenantID,
},
External: &extTrue,
}
allPages, err := networks.List(client, listOpts).AllPages()
if err != nil {
return nil, err
}
allNetworks, err := networks.ExtractNetworks(allPages)
if err != nil {
return nil, err
}
if len(allNetworks) > 0 {
return &allNetworks[0], nil
}
return nil, nil
}
func ensureNetwork(client *gophercloud.ServiceClient, createOpts networks.CreateOpts, log *logr.Logger,
serviceTenantID string) (*networks.Network, error) {
foundNetwork, err := getNetwork(client, createOpts.Name, serviceTenantID)
if err != nil {
return nil, err
}
if foundNetwork == nil {
log.Info(fmt.Sprintf("Creating Octavia network \"%s\"", createOpts.Name))
foundNetwork, err = networks.Create(client, createOpts).Extract()
if err != nil {
return nil, err
}
} else {
emptyOpts := networks.UpdateOpts{}
delta := networks.UpdateOpts{}
if foundNetwork.Description != createOpts.Description {
delta.Description = &createOpts.Description
}
if foundNetwork.AdminStateUp != *createOpts.AdminStateUp {
delta.AdminStateUp = createOpts.AdminStateUp
}
if delta != emptyOpts {
log.Info(fmt.Sprintf("Updating Octavia management network \"%s\"", createOpts.Name))
foundNetwork, err = networks.Update(client, foundNetwork.ID, delta).Extract()
if err != nil {
return nil, err
}
}
}
return foundNetwork, nil
}
func ensureNetworkExt(client *gophercloud.ServiceClient, createOpts networks.CreateOpts, log *logr.Logger, serviceTenantID string) (*networks.Network, error) {
foundNetwork, err := getNetworkExt(client, createOpts.Name, serviceTenantID)
if err != nil {
return nil, err
}
extTrue := true
if foundNetwork == nil {
segment := []provider.Segment{
{
NetworkType: "flat",
PhysicalNetwork: LbProvPhysicalNet,
},
}
providerOpts := provider.CreateOptsExt{
CreateOptsBuilder: createOpts,
Segments: segment,
}
extCreateOpts := external.CreateOptsExt{
CreateOptsBuilder: providerOpts,
External: &extTrue,
}
log.Info(fmt.Sprintf("Creating Octavia network \"%s\"", createOpts.Name))
foundNetwork, err = networks.Create(client, extCreateOpts).Extract()
if err != nil {
return nil, err
}
} else {
emptyOpts := networks.UpdateOpts{}
delta := networks.UpdateOpts{}
if foundNetwork.Description != createOpts.Description {
delta.Description = &createOpts.Description
}
if foundNetwork.AdminStateUp != *createOpts.AdminStateUp {
delta.AdminStateUp = createOpts.AdminStateUp
}
if delta != emptyOpts {
log.Info(fmt.Sprintf("Updating Octavia management network \"%s\"", createOpts.Name))
foundNetwork, err = networks.Update(client, foundNetwork.ID, delta).Extract()
if err != nil {
return nil, err
}
}
}
return foundNetwork, nil
}
func ensureProvSubnet(client *gophercloud.ServiceClient, providerNetwork *networks.Network, log *logr.Logger) (
*subnets.Subnet, error) {
gatewayIP := LbProvSubnetGatewayIP
createOpts := subnets.CreateOpts{
Name: LbProvSubnetName,
Description: LbProvSubnetDescription,
NetworkID: providerNetwork.ID,
TenantID: providerNetwork.TenantID,
CIDR: LbProvSubnetCIDR,
IPVersion: gophercloud.IPVersion(4),
AllocationPools: []subnets.AllocationPool{
{
Start: LbProvSubnetAllocationPoolStart,
End: LbProvSubnetAllocationPoolEnd,
},
},
GatewayIP: &gatewayIP,
}
return ensureSubnet(client, 4, createOpts, log)
}
func ensureProvNetwork(client *gophercloud.ServiceClient, serviceTenantID string, log *logr.Logger) (
*networks.Network, error) {
_, err := getNetwork(client, LbProvNetName, serviceTenantID)
if err != nil {
return nil, err
}
asu := true
createOpts := networks.CreateOpts{
Name: LbProvNetName,
Description: LbProvNetDescription,
AdminStateUp: &asu,
TenantID: serviceTenantID,
}
provNet, err := ensureNetworkExt(client, createOpts, log, serviceTenantID)
if err != nil {
return nil, err
}
return provNet, nil
}
func ensureLbMgmtSubnet(
client *gophercloud.ServiceClient,
networkDetails *octaviav1.OctaviaLbMgmtNetworks,
tenantNetwork *networks.Network,
log *logr.Logger,
) (*subnets.Subnet, error) {
ipVersion := networkDetails.SubnetIPVersion
var createOpts subnets.CreateOpts
if ipVersion == 6 {
gatewayIP := LbMgmtSubnetIPv6GatewayIP
createOpts = subnets.CreateOpts{
Name: LbMgmtSubnetName,
Description: LbMgmtSubnetDescription,
NetworkID: tenantNetwork.ID,
TenantID: tenantNetwork.TenantID,
CIDR: LbMgmtSubnetIPv6CIDR,
IPVersion: gophercloud.IPVersion(ipVersion),
IPv6AddressMode: LbMgmtSubnetIPv6AddressMode,
IPv6RAMode: LbMgmtSubnetIPv6RAMode,
AllocationPools: []subnets.AllocationPool{
{
Start: LbMgmtSubnetIPv6AllocationPoolStart,
End: LbMgmtSubnetIPv6AllocationPoolEnd,
},
},
GatewayIP: &gatewayIP,
// TODO(beagles): ipv6 host routes
}
} else {
gatewayIP := LbMgmtSubnetGatewayIP
createOpts = subnets.CreateOpts{
Name: LbMgmtSubnetName,
Description: LbMgmtSubnetDescription,
NetworkID: tenantNetwork.ID,
TenantID: tenantNetwork.TenantID,
CIDR: LbMgmtSubnetCIDR,
IPVersion: gophercloud.IPVersion(ipVersion),
AllocationPools: []subnets.AllocationPool{
{
Start: LbMgmtSubnetAllocationPoolStart,
End: LbMgmtSubnetAllocationPoolEnd,
},
},
HostRoutes: []subnets.HostRoute{
{
DestinationCIDR: LbProvSubnetCIDR,
NextHop: LbMgmtRouterPortIPv4,
},
},
GatewayIP: &gatewayIP,
}
}
return ensureSubnet(client, ipVersion, createOpts, log)
}
func getLbMgmtNetwork(client *gophercloud.ServiceClient, serviceTenantID string) (*networks.Network, error) {
return getNetwork(client, LbMgmtNetName, serviceTenantID)
}
func ensureLbMgmtNetwork(client *gophercloud.ServiceClient, networkDetails *octaviav1.OctaviaLbMgmtNetworks,
serviceTenantID string, log *logr.Logger) (*networks.Network, error) {
mgmtNetwork, err := getLbMgmtNetwork(client, serviceTenantID)
if err != nil {
return nil, err
}
if networkDetails == nil && mgmtNetwork == nil {
return nil, fmt.Errorf("Cannot find network \"%s\"", LbMgmtNetName)
}
asu := true
createOpts := networks.CreateOpts{
Name: LbMgmtNetName,
Description: LbMgmtNetDescription,
AdminStateUp: &asu,
TenantID: serviceTenantID,
}
mgmtNetwork, err = ensureNetwork(client, createOpts, log, serviceTenantID)
if err != nil {
return nil, err
}
return mgmtNetwork, nil
}
func externalFixedIPs(subnetID string) []routers.ExternalFixedIP {
ips := []routers.ExternalFixedIP{
{
IPAddress: LbRouterFixedIPAddress,
SubnetID: subnetID,
},
}
return ips
}
func compareExternalFixedIPs(a []routers.ExternalFixedIP, b []routers.ExternalFixedIP) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i].IPAddress != b[i].IPAddress {
return false
}
if a[i].SubnetID != b[i].SubnetID {
return false
}
}
return true
}
// reconcileRouter compares existing router properties against what is expected/desired and updates the router if
// necessary!
func reconcileRouter(client *gophercloud.ServiceClient, router *routers.Router,
gatewayNetwork *networks.Network,
gatewaySubnet *subnets.Subnet,
log *logr.Logger) (*routers.Router, error) {
if !router.AdminStateUp {
return router, fmt.Errorf("Router %s is not up", router.Name)
}
// TODO(beagles) check the status string.
// if router.Status == ?
needsUpdate := false
updateInfo := routers.UpdateOpts{}
enableSNAT := false
fixedIPs := externalFixedIPs(gatewaySubnet.ID)
//
// TODO(beagles) we don't care about the other fields right now because we
// are just going with neutron defaults but in the future we may care about
// Distributed (oddly HA doesn't seem to be an option)
//
gatewayInfo := router.GatewayInfo
if gatewayNetwork.ID != gatewayInfo.NetworkID || *gatewayInfo.EnableSNAT ||
!compareExternalFixedIPs(gatewayInfo.ExternalFixedIPs, fixedIPs) {
gwInfo := routers.GatewayInfo{
NetworkID: gatewayNetwork.ID,
EnableSNAT: &enableSNAT,
ExternalFixedIPs: fixedIPs,
}
updateInfo.GatewayInfo = &gwInfo
needsUpdate = true
}
if needsUpdate {
updatedRouter, err := routers.Update(client, router.ID, updateInfo).Extract()
if err != nil {
return nil, err
}
log.Info(fmt.Sprintf("Updated octavia management router %s", router.ID))
return updatedRouter, nil
}
return router, nil
}
// findRouter is a simple helper method...
func findRouter(client *gophercloud.ServiceClient, log *logr.Logger) (*routers.Router, error) {
listOpts := routers.ListOpts{
Name: LbRouterName,
}
allPages, err := routers.List(client, listOpts).AllPages()
if err != nil {
log.Error(err, "Unable to list routers")
return nil, err
}
allRouters, err := routers.ExtractRouters(allPages)
if err != nil {
log.Error(err, "Unable to extract router results")
return nil, err
}
if len(allRouters) > 0 {
for _, router := range allRouters {
if router.Name == LbRouterName {
return &router, nil
}
}
}
return nil, nil
}
//
// IMPORTANT NOTE:
// Take care to specify the project/tenant IDs when querying AND creating resources.
// Otherwise, the project IDs on the security groups and rules may not match and
// errors will occur as the code attempts to create duplicate rules. Ask me how I
// know -- beagles
//
// findSecurityGroupRule is different than the other findX helper functions because of the wide variety of
// potential values
func findSecurityGroupRule(client *gophercloud.ServiceClient, criteria *rules.ListOpts, log *logr.Logger) (*rules.SecGroupRule, error) {
//
// Strip description out of search. While informative, we are not concerned with that field.
//
listOpts := *criteria
listOpts.Description = ""
allPages, err := rules.List(client, listOpts).AllPages()
if err != nil {
log.Error(err, "findSecurityGroupRule: Unable to find security group rule")
return nil, err
}
allRules, err := rules.ExtractRules(allPages)
if err != nil {
log.Error(err, "findSecurityGroupRule: error extracting security group rule")
return nil, err
}
if len(allRules) != 0 {
return &allRules[0], nil
}
return nil, nil
}
func strToRuleEtherType(v string) rules.RuleEtherType {
if v == "IPv6" {
return rules.EtherType6
}
return rules.EtherType4
}
func strToRuleProtocol(p string) rules.RuleProtocol {
switch p {
case "tcp":
return rules.ProtocolTCP
case "udp":
return rules.ProtocolUDP
}
return ""
}
func ensureSecurityGroupRules(client *gophercloud.ServiceClient, securityGroup *groups.SecGroup, rulesDefinitions []rules.ListOpts, log *logr.Logger) error {
for _, r := range rulesDefinitions {
r.TenantID = securityGroup.TenantID
r.SecGroupID = securityGroup.ID
r.Direction = "ingress"
rule, err := findSecurityGroupRule(client, &r, log)
// Don't break on error if not found, but create the rest.
if err != nil {
log.Error(err, fmt.Sprintf("ensureSecurityGroupRules: error searching for %s", r.Description))
continue
}
if rule != nil {
// There is a rule that matches this description, carry on.
continue
}
//
// Rule not found. Create a new one.
createOpts := rules.CreateOpts{
Description: r.Description,
EtherType: strToRuleEtherType(r.EtherType),
PortRangeMax: r.PortRangeMax,
PortRangeMin: r.PortRangeMin,
Protocol: strToRuleProtocol(r.Protocol),
SecGroupID: securityGroup.ID,
Direction: rules.DirIngress,
ProjectID: securityGroup.TenantID,
}
_, err = rules.Create(client, createOpts).Extract()
if err != nil {
log.Error(err, fmt.Sprintf("ensureSecurityGroupRules: error creating rule %s", r.Description))
}
}
return nil
}
type ensureRules func(client *gophercloud.ServiceClient, securityGroup *groups.SecGroup, log *logr.Logger) error
func ensureMgmtRules(client *gophercloud.ServiceClient, securityGroup *groups.SecGroup, log *logr.Logger) error {
rulesDefinitions := []rules.ListOpts{
{
Description: "ssh port IPv4 rule",
PortRangeMax: 22,
PortRangeMin: 22,
EtherType: "IPv4",
Protocol: "tcp",
},
{
Description: "ssh port IPv6 rule",
PortRangeMax: 22,
PortRangeMin: 22,
EtherType: "IPv6",
Protocol: "tcp",
},
{
Description: "amphora agent port IPv4 rule",
PortRangeMax: 9443,
PortRangeMin: 9443,
EtherType: "IPv4",
Protocol: "tcp",
},
{
Description: "amphora agent port IPv6 rule",
PortRangeMax: 9443,
PortRangeMin: 9443,
EtherType: "IPv6",
Protocol: "tcp",
},
}
return ensureSecurityGroupRules(client, securityGroup, rulesDefinitions, log)
}
func ensureHealthMgrRules(client *gophercloud.ServiceClient, securityGroup *groups.SecGroup, log *logr.Logger) error {
healthManagerRules := []rules.ListOpts{
{
Description: "health manager status port IPv4 rule",
PortRangeMax: 5555,
PortRangeMin: 5555,
EtherType: "IPv4",
Protocol: "udp",
},
{
Description: "health manager status port IPv6 rule",
PortRangeMax: 5555,
PortRangeMin: 5555,
EtherType: "IPv6",
Protocol: "udp",
},
{
Description: "log offloading udp IPv4 rule",
PortRangeMax: 514,
PortRangeMin: 514,
EtherType: "IPv4",
Protocol: "udp",
},
{
Description: "log offloading udp IPv6 rule",
PortRangeMax: 514,
PortRangeMin: 514,
EtherType: "IPv6",
Protocol: "udp",
},
{
Description: "log offloading udp IPv4 rule",
PortRangeMax: 514,
PortRangeMin: 514,
EtherType: "IPv4",
Protocol: "tcp",
},
{
Description: "log offloading udp IPv6 rule",
PortRangeMax: 514,
PortRangeMin: 514,
EtherType: "IPv6",
Protocol: "tcp",
},
}
return ensureSecurityGroupRules(client, securityGroup, healthManagerRules, log)
}
func findSecurityGroup(client *gophercloud.ServiceClient, tenantID string, groupName string, log *logr.Logger) (*groups.SecGroup, error) {
listOpts := groups.ListOpts{
TenantID: tenantID,
}
allPages, err := groups.List(client, listOpts).AllPages()
if err != nil {
log.Error(err, "findSecurityGroup: Unable to find security groups")
return nil, err
}
allGroups, err := groups.ExtractGroups(allPages)
if err != nil {
log.Error(err, "findSecurityGroup: error extracting security groups")
return nil, err
}
for _, group := range allGroups {
if group.Name == groupName {
return &group, nil
}
}
return nil, nil
}
func ensureSecurityGroup(
client *gophercloud.ServiceClient,
tenantID string,
groupName string,
ruleFn ensureRules,
log *logr.Logger) (
string, error) {
secGroup, err := findSecurityGroup(client, tenantID, groupName, log)
if err != nil {
return "", err
}
if secGroup == nil {
log.Info(fmt.Sprintf("ensureSecurityGroup: security group %s not found, creating...", groupName))
createOpts := groups.CreateOpts{
Name: groupName,
TenantID: tenantID,
}
secGroup, err = groups.Create(client, createOpts).Extract()
if err != nil {
log.Error(err, fmt.Sprintf("ensureLbMgmtSecurityGroup: unable to create security group %s",
groupName))
return "", err
}
}
err = ruleFn(client, secGroup, log)
if err != nil {
return "", err
}
return secGroup.ID, nil
}
// EnsureAmphoraManagementNetwork - retrieve, create and reconcile the Octavia management network for the in cluster link to the
// management tenant network.
func EnsureAmphoraManagementNetwork(
ctx context.Context,
ns string,
tenantName string,
netDetails *octaviav1.OctaviaLbMgmtNetworks,
log *logr.Logger,
helper *helper.Helper,
) (NetworkProvisioningSummary, error) {
o, err := GetOpenstackClient(ctx, ns, helper)
if err != nil {
return NetworkProvisioningSummary{}, err
}
client, err := GetNetworkClient(o)
if err != nil {
return NetworkProvisioningSummary{}, err
}
serviceTenant, err := GetProject(o, tenantName)
if err != nil {
return NetworkProvisioningSummary{}, err
}
tenantNetwork, err := ensureLbMgmtNetwork(client, netDetails, serviceTenant.ID, log)
if err != nil {
return NetworkProvisioningSummary{}, err
}
tenantSubnet, err := ensureLbMgmtSubnet(client, netDetails, tenantNetwork, log)
if err != nil {
return NetworkProvisioningSummary{}, err
}
lbMgmtSecurityGroupID, err := ensureSecurityGroup(client, tenantNetwork.TenantID, LbMgmtNetworkSecurityGroupName, ensureMgmtRules, log)
if err != nil {
log.Error(err, "Unable to complete configuration of management network security groups, continuing...")
}
lbHealthSecurityGroupID, err := ensureSecurityGroup(client, tenantNetwork.TenantID, LbMgmtHealthManagerSecurityGroupName, ensureHealthMgrRules, log)
if err != nil {
log.Error(err, "Unable to complete configuration of management network security groups, continuing...")
}
securityGroups := []string{lbMgmtSecurityGroupID, lbHealthSecurityGroupID}
tenantRouterPort, err := ensurePort(client, tenantNetwork, tenantSubnet, &securityGroups, log)
if err != nil {
return NetworkProvisioningSummary{}, err
}
adminTenant, err := GetProject(o, AdminTenant)
if err != nil {
return NetworkProvisioningSummary{}, err
}
_, err = ensureSecurityGroup(client, adminTenant.ID, LbProvNetworkSecurityGroupName, ensureMgmtRules, log)
if err != nil {
log.Error(err, "Unable to complete configuration of octavia provider network security groups, continuing...")
}
_, err = ensureSecurityGroup(client, adminTenant.ID, LbProvHealthManagerSecurityGroupName, ensureHealthMgrRules, log)
if err != nil {
log.Error(err, "Unable to complete configuration of octavia provider network security groups, continuing...")
}
providerNetwork, err := ensureProvNetwork(client, adminTenant.ID, log)
if err != nil {
return NetworkProvisioningSummary{}, err
}
providerSubnet, err := ensureProvSubnet(client, providerNetwork, log)
if err != nil {
return NetworkProvisioningSummary{}, err
}
router, err := findRouter(client, log)
if err != nil {
return NetworkProvisioningSummary{}, err
}
if router != nil {
log.Info("Router object found, reconciling")
router, err = reconcileRouter(client, router, providerNetwork, providerSubnet, log)
if err != nil {
return NetworkProvisioningSummary{}, err
}
log.Info(fmt.Sprintf("Reconciled router %s (%s)", router.Name, router.ID))
} else {
log.Info("Creating octavia provider router")
enableSNAT := false
gatewayInfo := routers.GatewayInfo{
NetworkID: providerNetwork.ID,
EnableSNAT: &enableSNAT,
ExternalFixedIPs: externalFixedIPs(providerSubnet.ID),
}
adminStateUp := true
createOpts := routers.CreateOpts{
Name: LbRouterName,
AdminStateUp: &adminStateUp,
GatewayInfo: &gatewayInfo,
}
router, err = routers.Create(client, createOpts).Extract()
if err != nil {
log.Error(err, "Unable to create router object")
return NetworkProvisioningSummary{}, err
}
}
if tenantRouterPort.DeviceID == "" {
interfaceOpts := routers.AddInterfaceOpts{
PortID: tenantRouterPort.ID,
}
_, err := routers.AddInterface(client, router.ID, interfaceOpts).Extract()
if err != nil {
log.Error(err, fmt.Sprintf("Unable to add interface port %s to router %s", tenantRouterPort.ID, router.ID))
}
} else if tenantRouterPort.DeviceID != router.ID {
return NetworkProvisioningSummary{},
fmt.Errorf("Port %s has unexpected device ID %s and cannot be added to router %s", tenantRouterPort.ID,
tenantRouterPort.DeviceID, router.ID)
}
return NetworkProvisioningSummary{
TenantNetworkID: tenantNetwork.ID,
TenantSubnetID: tenantSubnet.ID,
TenantRouterPortID: tenantRouterPort.ID,
ProviderNetworkID: providerNetwork.ID,
RouterID: router.ID,
SecurityGroupID: lbMgmtSecurityGroupID,
}, nil
}