-
Notifications
You must be signed in to change notification settings - Fork 59
/
private.go
1021 lines (922 loc) · 31.5 KB
/
private.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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package aws
// "Private" or non-interface conforming methods
import (
"context"
goError "errors"
"fmt"
"path"
"reflect"
"strings"
"time"
"gopkg.in/yaml.v2"
cloudingressv1alpha1 "github.com/openshift/cloud-ingress-operator/pkg/apis/cloudingress/v1alpha1"
machineapi "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
awsproviderapi "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsproviderconfig/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/openshift/cloud-ingress-operator/config"
"github.com/openshift/cloud-ingress-operator/pkg/errors"
baseutils "github.com/openshift/cloud-ingress-operator/pkg/utils"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/aws/aws-sdk-go/service/route53"
)
type awsLoadBalancer struct {
elbName string
dnsName string
dnsZoneID string
}
type loadBalancer struct {
endpointName string // from APIScheme
baseDomain string // cluster base domain
}
type loadBalancerV2 struct {
canonicalHostedZoneNameID string
dnsName string
loadBalancerArn string
loadBalancerName string
scheme string
vpcID string
}
// installConfig represents the bare minimum requirement to get the AWS cluster region from the install-config
// See https://bugzilla.redhat.com/show_bug.cgi?id=1814332
type installConfig struct {
Platform struct {
AWS struct {
Region string `json:"region"`
} `json:"aws"`
} `json:"platform"`
}
// removeAWSLBFromMasterMachines removes a Load Balancer (with name elbName) from
// the spec.providerSpec.value.loadBalancers list for each of the master machine
// objects in a cluster
func removeAWSLBFromMasterMachines(kclient client.Client, elbName string, masterNodes *machineapi.MachineList) error {
for _, machine := range masterNodes.Items {
providerSpecDecoded, err := getAWSDecodedProviderSpec(machine)
if err != nil {
log.Error(err, "Error retrieving decoded ProviderSpec for machine", "machine", machine.Name)
return err
}
lbList := providerSpecDecoded.LoadBalancers
newLBList := []awsproviderapi.LoadBalancerReference{}
for _, lb := range lbList {
if lb.Name != elbName {
log.Info("Machine's LB does not match LB to remove", "Machine LB", lb.Name, "LB to remove", elbName)
log.Info("Keeping machine's LB in machine object", "LB", lb.Name, "Machine", machine.Name)
newLBList = append(newLBList, lb)
}
}
err = updateAWSLBList(kclient, lbList, newLBList, machine, providerSpecDecoded)
if err != nil {
log.Error(err, "Error updating LB list for machine", "machine", machine.Name)
return err
}
}
return nil
}
// getAWSDecodedProviderSpec casts the spec.providerSpec of an OpenShift machine
// object to an AWSMachineProviderConfig object, which is required to read and
// interact with fields in a machine's providerSpec
func getAWSDecodedProviderSpec(machine machineapi.Machine) (*awsproviderapi.AWSMachineProviderConfig, error) {
awsCodec, err := awsproviderapi.NewCodec()
if err != nil {
log.Error(err, "Error creating AWSProviderConfigCodec")
return nil, err
}
providerSpecEncoded := machine.Spec.ProviderSpec
providerSpecDecoded := &awsproviderapi.AWSMachineProviderConfig{}
err = awsCodec.DecodeProviderSpec(&providerSpecEncoded, providerSpecDecoded)
if err != nil {
log.Error(err, "Error decoding provider spec for machine", "machine", machine.Name)
return nil, err
}
return providerSpecDecoded, nil
}
// updateAWSLBList compares an oldLBList to a newLBList and updates a machine
// object's spec.providerSpec.value.loadBalancers list with the newLBList if
// the old and new lists are not equal. this function requires the decoded
// ProviderSpec (as an AWSMachineProviderConfig object) that the
// getAWSDecodedProviderSpec function will provide
func updateAWSLBList(kclient client.Client, oldLBList []awsproviderapi.LoadBalancerReference, newLBList []awsproviderapi.LoadBalancerReference, machineToPatch machineapi.Machine, providerSpecDecoded *awsproviderapi.AWSMachineProviderConfig) error {
baseToPatch := client.MergeFrom(machineToPatch.DeepCopy())
awsCodec, err := awsproviderapi.NewCodec()
if err != nil {
log.Error(err, "Error creating AWSProviderConfigCodec")
return err
}
if !reflect.DeepEqual(oldLBList, newLBList) {
providerSpecDecoded.LoadBalancers = newLBList
newProviderSpecEncoded, err := awsCodec.EncodeProviderSpec(providerSpecDecoded)
if err != nil {
log.Error(err, "Error encoding provider spec for machine", "machine", machineToPatch.Name)
return err
}
machineToPatch.Spec.ProviderSpec = *newProviderSpecEncoded
if err := kclient.Patch(context.Background(), &machineToPatch, baseToPatch); err != nil {
log.Error(err, "Failed to update LBs in machine's providerSpec", "machine", machineToPatch.Name)
return err
}
log.Info("Updated master machine's LBs in providerSpec", "masterMachine", machineToPatch.Name)
return nil
}
log.Info("No need to update LBs for master machine", "masterMachine", machineToPatch.Name)
return nil
}
// ensureAdminAPIDNS ensure the DNS record for the rh-api "admin API" for
// APIScheme is present and mapped to the corresponding Service's AWS
// LoadBalancer
func (c *Client) ensureAdminAPIDNS(ctx context.Context, kclient client.Client, instance *cloudingressv1alpha1.APIScheme, svc *corev1.Service) error {
return c.ensureDNSForService(ctx, kclient, svc, instance.Spec.ManagementAPIServerIngress.DNSName, "RH API Endpoint")
}
// deleteAdminAPIDNS removes the DNS record for the rh-api "admin API" for
// APIScheme
func (c *Client) deleteAdminAPIDNS(ctx context.Context, kclient client.Client, instance *cloudingressv1alpha1.APIScheme, svc *corev1.Service) error {
return c.removeDNSForService(ctx, kclient, svc, instance.Spec.ManagementAPIServerIngress.DNSName, "RH API Endpoint")
}
// ensureSSHDNS ensures the DNS record for the SSH Service LoadBalancer is set
func (c *Client) ensureSSHDNS(ctx context.Context, kclient client.Client, instance *cloudingressv1alpha1.SSHD, svc *corev1.Service) error {
return c.ensureDNSForService(ctx, kclient, svc, instance.Spec.DNSName, "RH SSH Endpoint")
}
// deleteSSHDNS ensures the DNS record for the SSH Service AWS LoadBalancer is unset
func (c *Client) deleteSSHDNS(ctx context.Context, kclient client.Client, instance *cloudingressv1alpha1.SSHD, svc *corev1.Service) error {
return c.removeDNSForService(ctx, kclient, svc, instance.Spec.DNSName, "RH SSH Endpoint")
}
// setDefaultAPIPrivate sets the default api (api.<cluster-domain>) to private
// scope
func (c *Client) setDefaultAPIPrivate(ctx context.Context, kclient client.Client, _ *cloudingressv1alpha1.PublishingStrategy) error {
// Delete the NLB and remove the NLB from the master Machine objects in
// cluster. At the same time, get the name of the DNS zone and base domain for
// the internal load balancer
intDNSName, intHostedZoneID, err := c.removeLoadBalancerFromMasterNodes(ctx, kclient)
if err != nil {
return err
}
baseDomain, err := baseutils.GetClusterBaseDomain(kclient)
if err != nil {
return err
}
pubDomainName := baseDomain[strings.Index(baseDomain, ".")+1:]
apiDNSName := fmt.Sprintf("api.%s.", baseDomain)
comment := "Update api.<clusterName> alias to internal NLB"
err = c.upsertARecord(pubDomainName+".", intDNSName, intHostedZoneID, apiDNSName, comment, false)
if err != nil {
return err
}
return nil
}
// setDefaultAPIPublic sets the default API (api.<cluster-domain>) to public
// scope
func (c *Client) setDefaultAPIPublic(ctx context.Context, kclient client.Client, instance *cloudingressv1alpha1.PublishingStrategy) error {
nlbs, err := c.listOwnedNLBs(kclient)
if err != nil {
return err
}
// TODO: Check for the expected name?
for _, networkLoadBalancer := range nlbs {
if networkLoadBalancer.scheme == "internet-facing" {
// nothing to do
return nil
}
}
// create new ext nlb
infrastructureName, err := baseutils.GetClusterName(kclient)
if err != nil {
return err
}
extNLBName := infrastructureName + "-ext"
subnetIDs, err := c.getPublicSubnets(kclient)
if err != nil {
return err
}
if len(subnetIDs) == 0 {
err = goError.New("No public subnets, can't change API to public")
return err
}
newNLBs, err := c.createNetworkLoadBalancer(extNLBName, "internet-facing", subnetIDs[0])
if err != nil {
return err
}
if len(newNLBs) != 1 {
return fmt.Errorf("more than one NLB, or no new NLB detected (expected 1, got %d)", len(newNLBs))
}
err = c.addTagsForNLB(newNLBs[0].loadBalancerArn, infrastructureName)
if err != nil {
return err
}
// attempt to use existing TargetGroup
targetGroupName := fmt.Sprintf("%s-aext", infrastructureName)
targetGroupARN, err := c.getTargetGroupArn(targetGroupName)
if err != nil {
return err
}
err = c.createListenerForNLB(targetGroupARN, newNLBs[0].loadBalancerArn)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
if aerr.Code() == "TargetGroupAssociationLimit" {
// not possible to modify lb, we'd have to create a new targetGroup
return nil
}
return err
}
// TODO: log - cant create listener for new ext nlb
return err
}
// can't create listener for new ext nlb
baseDomain, err := baseutils.GetClusterBaseDomain(kclient)
if err != nil {
return err
}
pubDomainName := baseDomain[strings.Index(baseDomain, ".")+1:]
apiDNSName := fmt.Sprintf("api.%s.", baseDomain)
// not tested yet
comment := "Update api.<clusterName> alias to external NLB"
err = c.upsertARecord(pubDomainName+".",
newNLBs[0].dnsName,
newNLBs[0].canonicalHostedZoneNameID,
apiDNSName,
comment,
false)
if err != nil {
return err
}
// success
return nil
}
// getMasterNodeSubnets returns all the subnets for Machines with 'master' label.
// return structure:
// {
// public => subnetname,
// private => subnetname,
// }
//
func getMasterNodeSubnets(kclient client.Client) (map[string]string, error) {
subnets := make(map[string]string)
machineList, err := baseutils.GetMasterMachines(kclient)
if err != nil {
return subnets, err
}
if len(machineList.Items) == 0 {
return subnets, fmt.Errorf("Did not find any master Machine objects")
}
// get the AZ from a Master object's providerSpec.
codec, err := awsproviderapi.NewCodec()
if err != nil {
return subnets, err
}
// Obtain the availability zone
awsconfig := &awsproviderapi.AWSMachineProviderConfig{}
err = codec.DecodeProviderSpec(&machineList.Items[0].Spec.ProviderSpec, awsconfig)
if err != nil {
return subnets, err
}
// Infra object gives us the Infrastructure name, which is the combination of
// cluster name and identifier.
infra, err := baseutils.GetInfrastructureObject(kclient)
if err != nil {
return subnets, err
}
subnets["public"] = fmt.Sprintf("%s-public-%s", infra.Status.InfrastructureName, awsconfig.Placement.AvailabilityZone)
subnets["private"] = fmt.Sprintf("%s-private-%s", infra.Status.InfrastructureName, awsconfig.Placement.AvailabilityZone)
return subnets, nil
}
func (c *Client) getPublicSubnets(kclient client.Client) ([]string, error) {
var publicSubnets []string
machineList, err := baseutils.GetMasterMachines(kclient)
if err != nil {
log.Error(err, "No master machines found")
return nil, err
}
// Get the first master machine in the list
masterMachine := machineList.Items[0]
// Get the instance ID of the machine in the form of aws:///us-east-1a/i-<hash>
instanceIDLong := masterMachine.Spec.ProviderID
split := strings.Split(*instanceIDLong, "/")
// The instance ID should be the last element of the split
instanceID := split[len(split)-1]
// Ensure we acutally have an instnace ID by erroring if its missing
if instanceID == "" {
err = goError.New("Instance ID is blank")
return nil, err
}
// Get VPC the instance is in
describeInstanceOutput, err := c.ec2Client.DescribeInstances(
&ec2.DescribeInstancesInput{
InstanceIds: []*string{aws.String(instanceID)},
},
)
if err != nil {
return nil, err
}
// Extract the VPC ID from the subnet metadata
targetVPC := describeInstanceOutput.Reservations[0].Instances[0].VpcId
// List all subnets in the VPC
allSubnets, err := c.getAllSubnetsInVPC(*targetVPC)
if err != nil {
return nil, err
}
// List all route tables associated with the VPC
routeTables, err := c.getAllRouteTablesInVPC(*targetVPC)
if err != nil {
return nil, err
}
for _, subnet := range allSubnets {
isPublic, err := isSubnetPublic(routeTables, *subnet.SubnetId)
if err != nil {
log.Error(err, "Error while determining if subnet is public")
return nil, err
}
if isPublic {
publicSubnets = append(publicSubnets, *subnet.SubnetId)
}
}
return publicSubnets, nil
}
func (c *Client) getAllSubnetsInVPC(vpcID string) ([]*ec2.Subnet, error) {
var subnetIDs []*ec2.Subnet
token := aws.String("initString")
for token != nil {
describeSubnetOutput, err := c.ec2Client.DescribeSubnets(
&ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{aws.String(vpcID)},
},
},
})
if err != nil {
log.Error(err, "Error while describing subnets")
return nil, err
}
subnetIDs = append(subnetIDs, describeSubnetOutput.Subnets...)
token = describeSubnetOutput.NextToken
}
return subnetIDs, nil
}
func (c *Client) getAllRouteTablesInVPC(vpcID string) ([]*ec2.RouteTable, error) {
var routeTables []*ec2.RouteTable
token := aws.String("initString")
for token != nil {
describeRouteTablesOutput, err := c.ec2Client.DescribeRouteTables(&ec2.DescribeRouteTablesInput{Filters: []*ec2.Filter{{Name: aws.String("vpc-id"), Values: []*string{aws.String(vpcID)}}}})
if err != nil {
log.Error(err, "Error while describing route tables")
return nil, err
}
routeTables = append(routeTables, describeRouteTablesOutput.RouteTables...)
token = describeRouteTablesOutput.NextToken
}
return routeTables, nil
}
func isSubnetPublic(rt []*ec2.RouteTable, subnetID string) (bool, error) {
var subnetTable *ec2.RouteTable
for _, table := range rt {
for _, assoc := range table.Associations {
if aws.StringValue(assoc.SubnetId) == subnetID {
subnetTable = table
break
}
}
}
if subnetTable == nil {
// If there is no explicit association, the subnet will be implicitly
// associated with the VPC's main routing table.
for _, table := range rt {
for _, assoc := range table.Associations {
if aws.BoolValue(assoc.Main) {
log.Info(fmt.Sprintf(
"Assuming implicit use of main routing table %s for %s",
aws.StringValue(table.RouteTableId), subnetID))
subnetTable = table
break
}
}
}
}
if subnetTable == nil {
return false, fmt.Errorf("could not locate routing table for %s", subnetID)
}
for _, route := range subnetTable.Routes {
// There is no direct way in the AWS API to determine if a subnet is public or private.
// A public subnet is one which has an internet gateway route
// we look for the gatewayId and make sure it has the prefix of igw to differentiate
// from the default in-subnet route which is called "local"
// or other virtual gateway (starting with vgv)
// or vpc peering connections (starting with pcx).
if strings.HasPrefix(aws.StringValue(route.GatewayId), "igw") {
return true, nil
}
}
return false, nil
}
//getClusterRegion returns the installed cluster's AWS region
func getClusterRegion(kclient client.Client) (string, error) {
infra, err := baseutils.GetInfrastructureObject(kclient)
if err != nil {
return "", err
} else if infra.Status.PlatformStatus == nil {
// Try the deprecated configmap. See https://bugzilla.redhat.com/show_bug.cgi?id=1814332
return readClusterRegionFromConfigMap(kclient)
}
return infra.Status.PlatformStatus.AWS.Region, nil
}
func readClusterRegionFromConfigMap(kclient client.Client) (string, error) {
cm, err := getClusterConfigMap(kclient)
if err != nil {
return "", err
}
return parseClusterRegionFromConfigMap(cm)
}
func getClusterConfigMap(kclient client.Client) (*corev1.ConfigMap, error) {
cm := &corev1.ConfigMap{}
ns := types.NamespacedName{
Namespace: "kube-system",
Name: "cluster-config-v1",
}
err := kclient.Get(context.TODO(), ns, cm)
return cm, err
}
func parseClusterRegionFromConfigMap(cm *corev1.ConfigMap) (string, error) {
if cm == nil || cm.Data == nil {
return "", fmt.Errorf("unexpected nil configmap or nil configmap Data")
}
data, ok := cm.Data["install-config"]
if !ok {
return "", fmt.Errorf("Missing install-config in configmap")
}
var ic installConfig
if err := yaml.Unmarshal([]byte(data), &ic); err != nil {
return "", fmt.Errorf("Invalid install-config: %v\njson:%s", err, data)
}
return ic.Platform.AWS.Region, nil
}
/* Helper functions below, sorted by AWS API type */
// ELB (v1)
func (c *Client) doesELBExist(elbName string) (*awsLoadBalancer, error) {
input := &elb.DescribeLoadBalancersInput{
LoadBalancerNames: []*string{aws.String(elbName)},
}
output, err := c.elbClient.DescribeLoadBalancers(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case elb.ErrCodeAccessPointNotFoundException:
return &awsLoadBalancer{}, errors.NewLoadBalancerNotReadyError()
default:
return &awsLoadBalancer{}, err
}
}
}
return &awsLoadBalancer{
elbName: elbName,
dnsName: *output.LoadBalancerDescriptions[0].DNSName,
dnsZoneID: *output.LoadBalancerDescriptions[0].CanonicalHostedZoneNameID},
nil
}
// route53
func (c *Client) ensureDNSForService(ctx context.Context, kclient client.Client, svc *corev1.Service, dnsName, dnsComment string) error {
// Get the ELB name from the Service's UID. Truncate to 32 characters for AWS
elbName := strings.ReplaceAll("a"+string(svc.ObjectMeta.UID), "-", "")
if len(elbName) > 32 {
// Truncate to 32 characters
elbName = elbName[0:32]
}
awsELB, err := c.doesELBExist(elbName)
// Primarily checking to see if this exists. It is an error if it does not,
// likely because AWS is still creating it and the Reconcile should be retried
if err != nil {
return err
}
// ELB exists, now let's set the DNS
clusterBaseDomain, err := baseutils.GetClusterBaseDomain(kclient)
if err != nil {
return err
}
lb := &loadBalancer{
endpointName: dnsName,
baseDomain: clusterBaseDomain,
}
return c.ensureDNSRecord(lb, awsELB, dnsComment)
}
// removeDNSForService will remove a DNS entry for a particular Service
func (c *Client) removeDNSForService(ctx context.Context, kclient client.Client, svc *corev1.Service, dnsName, dnsComment string) error {
// Get the ELB name from the Service's UID. Truncate to 32 characters for AWS
elbName := strings.ReplaceAll("a"+string(svc.ObjectMeta.UID), "-", "")[0:32]
awsELB, err := c.doesELBExist(elbName)
// Primarily checking to see if this exists. It is an error if it does not,
// likely because AWS is still creating it and the Reconcile should be retried
if err != nil {
return err
}
// ELB exists, now let's set the DNS
clusterBaseDomain, err := baseutils.GetClusterBaseDomain(kclient)
if err != nil {
return err
}
return c.ensureDNSRecordsRemoved(
clusterBaseDomain,
awsELB.dnsName,
awsELB.dnsZoneID,
dnsName+"."+clusterBaseDomain,
dnsComment,
false)
}
func (c *Client) deleteARecord(clusterDomain, DNSName, aliasDNSZoneID, resourceRecordSetName string, targetHealth bool) error {
publicHostedZoneID, err := c.getPublicHostedZoneID(clusterDomain)
if err != nil {
return err
}
change := &route53.ChangeResourceRecordSetsInput{
ChangeBatch: &route53.ChangeBatch{
Changes: []*route53.Change{
{
Action: aws.String("DELETE"),
ResourceRecordSet: &route53.ResourceRecordSet{
AliasTarget: &route53.AliasTarget{
DNSName: aws.String(DNSName),
EvaluateTargetHealth: aws.Bool(targetHealth),
HostedZoneId: aws.String(aliasDNSZoneID),
},
Name: aws.String(resourceRecordSetName),
Type: aws.String("A"),
},
},
},
},
HostedZoneId: aws.String(publicHostedZoneID),
}
_, err = c.route53Client.ChangeResourceRecordSets(change)
if err != nil {
// If the DNS entry was not found, disregard the error.
//
// XXX The error code in this case is InvalidChangeBatch
// with no other errors in awserr.Error.OrigErr() or
// in awserr.BatchedErrors.OrigErrs().
//
// So there seems to be no way, short of parsing the
// message string, to verify the error was caused by
// a missing DNS entry and not something else.
//
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == route53.ErrCodeInvalidChangeBatch {
return nil
}
}
}
return err
}
func (c *Client) upsertARecord(clusterDomain, DNSName, aliasDNSZoneID, resourceRecordSetName, comment string, targetHealth bool) error {
publicHostedZoneID, err := c.getPublicHostedZoneID(clusterDomain)
if err != nil {
return err
}
change := &route53.ChangeResourceRecordSetsInput{
ChangeBatch: &route53.ChangeBatch{
Changes: []*route53.Change{
{
Action: aws.String("UPSERT"),
ResourceRecordSet: &route53.ResourceRecordSet{
AliasTarget: &route53.AliasTarget{
DNSName: aws.String(DNSName),
EvaluateTargetHealth: aws.Bool(targetHealth),
HostedZoneId: aws.String(aliasDNSZoneID),
},
Name: aws.String(resourceRecordSetName),
Type: aws.String("A"),
},
},
},
Comment: aws.String(comment),
},
HostedZoneId: aws.String(publicHostedZoneID),
}
_, err = c.route53Client.ChangeResourceRecordSets(change)
return err
}
func (c *Client) getPublicHostedZoneID(clusterDomain string) (string, error) {
input := &route53.ListHostedZonesByNameInput{
DNSName: aws.String(clusterDomain),
}
output, err := c.route53Client.ListHostedZonesByName(input)
if err != nil {
return "", err
}
for _, zone := range output.HostedZones {
if *zone.Name == clusterDomain {
return path.Base(aws.StringValue(zone.Id)), nil
}
}
return "", fmt.Errorf("Route53 Zone not found for %s", clusterDomain)
}
func (c *Client) ensureDNSRecord(lb *loadBalancer, awsObj *awsLoadBalancer, comment string) error {
// private zone
for i := 1; i <= config.MaxAPIRetries; i++ {
err := c.upsertARecord(
lb.baseDomain+".",
awsObj.dnsName,
awsObj.dnsZoneID,
lb.endpointName+"."+lb.baseDomain,
comment,
false)
if err != nil {
log.Error(err, "Couldn't upsert A record for private zone",
"retryAttempt", i,
"publicZone", lb.baseDomain+".",
"dnsName", awsObj.dnsName,
"dnsZoneID", awsObj.dnsZoneID,
"endpointName", lb.endpointName+".", lb.baseDomain)
if i == config.MaxAPIRetries {
log.Error(err, "Couldn't upsert A record for private zone: Retries Exhausted")
return err
}
// TODO: Logging - sleep
time.Sleep(time.Duration(i) * time.Second)
} else {
// success
break
}
}
// Public zone
// The public zone omits the cluster name. So an example:
// A cluster's base domain of alice-cluster.l4s7.s1.domain.com will need an
// entry made in l4s7.s1.domain.com. zone.
// Public zone
// The public zone omits the cluster name. So an example:
// A cluster's base domain of alice-cluster.l4s7.s1.domain.com will need an
// entry made in l4s7.s1.domain.com. zone.
publicZone := lb.baseDomain[strings.Index(lb.baseDomain, ".")+1:]
for i := 1; i <= config.MaxAPIRetries; i++ {
// Append a . to get the zone name
err := c.upsertARecord(
publicZone+".",
awsObj.dnsName,
awsObj.dnsZoneID,
lb.endpointName+"."+lb.baseDomain,
"RH API Endpoint",
false)
if err != nil {
log.Error(err, "Couldn't upsert A record for public zone",
"retryAttempt", i,
"publicZone", publicZone+".",
"dnsName", awsObj.dnsName,
"dnsZoneID", awsObj.dnsZoneID,
"endpointName", lb.endpointName+".", lb.baseDomain)
if i == config.MaxAPIRetries {
log.Error(err, "Couldn't upsert A record for public zone: Retries Exhausted")
return err
}
time.Sleep(time.Duration(i) * time.Second)
} else {
// success
break
}
}
return nil
}
// ensureDNSRecordsRemoved undoes ensureDNSRecord
func (c *Client) ensureDNSRecordsRemoved(clusterDomain, DNSName, aliasDNSZoneID, resourceRecordSetName, comment string, targetHealth bool) error {
for i := 1; i <= config.MaxAPIRetries; i++ {
err := c.deleteARecord(
clusterDomain+".",
DNSName,
aliasDNSZoneID,
resourceRecordSetName,
targetHealth)
if err != nil {
// retry
// TODO: logging
if i == config.MaxAPIRetries {
// TODO: logging
return err
}
// TODO: logging
time.Sleep(time.Duration(i) * time.Second)
} else {
break
}
}
for i := 1; i <= config.MaxAPIRetries; i++ {
err := c.deleteARecord(
// The public zone name omits the cluster name.
// e.g. mycluster.abcd.s1.openshift.com -> abcd.s1.openshift.com
clusterDomain[strings.Index(clusterDomain, ".")+1:]+".",
DNSName,
aliasDNSZoneID,
resourceRecordSetName,
targetHealth)
if err != nil {
// retry
// TODO: logging
if i == config.MaxAPIRetries {
// TODO: logging
return err
}
// TODO: logging
time.Sleep(time.Duration(i) * time.Second)
} else {
break
}
}
// public
return nil
}
// ELBv2
// removeLoadBalancerFromMasterNodes
func (c *Client) removeLoadBalancerFromMasterNodes(ctx context.Context, kclient client.Client) (string, string, error) {
nlbs, err := c.listOwnedNLBs(kclient)
if err != nil {
return "", "", err
}
masterList, err := baseutils.GetMasterMachines(kclient)
if err != nil {
return "", "", err
}
var intDNSName, intHostedZoneID, lbName string
for _, networkLoadBalancer := range nlbs {
if networkLoadBalancer.scheme == "internet-facing" {
lbName = networkLoadBalancer.loadBalancerName
err := c.deleteExternalLoadBalancer(networkLoadBalancer.loadBalancerArn)
if err != nil {
return "", "", err
}
err = removeAWSLBFromMasterMachines(kclient, lbName, masterList)
if err != nil {
return "", "", err
}
}
// we need this to update DNS
if networkLoadBalancer.scheme == "internal" {
intDNSName = networkLoadBalancer.dnsName
intHostedZoneID = networkLoadBalancer.canonicalHostedZoneNameID
}
}
return intDNSName, intHostedZoneID, nil
}
// listOwnedNLBs uses the DescribeLoadBalancersV2 to get back a list of all
// Network Load Balancers, then filters the list to those owned by the cluster
func (c *Client) listOwnedNLBs(kclient client.Client) ([]loadBalancerV2, error) {
// Build the load balancer tag to look for.
clusterName, err := baseutils.GetClusterName(kclient)
if err != nil {
return []loadBalancerV2{}, err
}
ownedTag := &elbv2.Tag{
Key: aws.String("kubernetes.io/cluster/" + clusterName),
Value: aws.String("owned"),
}
// Collect all load balancers into a map, indexed by ARN.
// Simultaneously, collect all load balancer ARNs into a slice.
// The slice is used to request load balancer tags in batches.
resourceArns := make([]string, 0, 20)
loadBalancerMap := make(map[string]*elbv2.LoadBalancer)
err = c.elbv2Client.DescribeLoadBalancersPages(
&elbv2.DescribeLoadBalancersInput{},
func(page *elbv2.DescribeLoadBalancersOutput, lastPage bool) bool {
for _, loadBalancer := range page.LoadBalancers {
arn := aws.StringValue(loadBalancer.LoadBalancerArn)
resourceArns = append(resourceArns, arn)
loadBalancerMap[arn] = loadBalancer
}
return true
},
)
if err != nil {
return []loadBalancerV2{}, err
}
// Request tags for up to 20 load balancers at a time.
for i := 0; i < len(resourceArns); i += 20 {
end := i + 20
if end > len(resourceArns) {
end = len(resourceArns)
}
tagsOutput, err := c.elbv2Client.DescribeTags(
&elbv2.DescribeTagsInput{
ResourceArns: aws.StringSlice(resourceArns[i:end]),
},
)
if err != nil {
return []loadBalancerV2{}, err
}
// Keep only load balancers owned by the cluster.
for _, tagDescription := range tagsOutput.TagDescriptions {
var foundTag bool
for _, tag := range tagDescription.Tags {
if reflect.DeepEqual(tag, ownedTag) {
foundTag = true
break
}
}
if !foundTag {
arn := aws.StringValue(tagDescription.ResourceArn)
delete(loadBalancerMap, arn)
}
}
}
loadBalancers := make([]loadBalancerV2, 0, len(loadBalancerMap))
for _, loadBalancer := range loadBalancerMap {
loadBalancers = append(loadBalancers, loadBalancerV2{
canonicalHostedZoneNameID: aws.StringValue(loadBalancer.CanonicalHostedZoneId),
dnsName: aws.StringValue(loadBalancer.DNSName),
loadBalancerArn: aws.StringValue(loadBalancer.LoadBalancerArn),
loadBalancerName: aws.StringValue(loadBalancer.LoadBalancerName),
scheme: aws.StringValue(loadBalancer.Scheme),
vpcID: aws.StringValue(loadBalancer.VpcId),
})
}
return loadBalancers, nil
}
// deleteExternalLoadBalancer takes in the external LB arn and deletes the entire LB
func (c *Client) deleteExternalLoadBalancer(extLoadBalancerArn string) error {
i := elbv2.DeleteLoadBalancerInput{
LoadBalancerArn: aws.String(extLoadBalancerArn),
}
_, err := c.elbv2Client.DeleteLoadBalancer(&i)
return err
}
// createNetworkLoadBalancer should only return one new NLB at a time
func (c *Client) createNetworkLoadBalancer(lbName, scheme, subnet string) ([]loadBalancerV2, error) {
i := &elbv2.CreateLoadBalancerInput{
Name: aws.String(lbName),
Scheme: aws.String(scheme),
Subnets: []*string{
aws.String(subnet),
},
Type: aws.String("network"),
}
result, err := c.elbv2Client.CreateLoadBalancer(i)
if err != nil {
return []loadBalancerV2{}, err
}
// there should only be 1 NLB made, but since CreateLoadBalancerOutput takes in slice
// we return it as slice
loadBalancers := make([]loadBalancerV2, 0)
for _, loadBalancer := range result.LoadBalancers {
loadBalancers = append(loadBalancers, loadBalancerV2{
canonicalHostedZoneNameID: aws.StringValue(loadBalancer.CanonicalHostedZoneId),
dnsName: aws.StringValue(loadBalancer.DNSName),
loadBalancerArn: aws.StringValue(loadBalancer.LoadBalancerArn),
loadBalancerName: aws.StringValue(loadBalancer.LoadBalancerName),
scheme: aws.StringValue(loadBalancer.Scheme),
vpcID: aws.StringValue(loadBalancer.VpcId),
})
}
return loadBalancers, nil
}
// createListenerForNLB creates a listener between target group and nlb given their arn
func (c *Client) createListenerForNLB(targetGroupArn, loadBalancerArn string) error {
i := &elbv2.CreateListenerInput{
DefaultActions: []*elbv2.Action{
{
TargetGroupArn: aws.String(targetGroupArn),
Type: aws.String("forward"),
},
},
LoadBalancerArn: aws.String(loadBalancerArn),
Port: aws.Int64(6443),
Protocol: aws.String("TCP"),
}
_, err := c.elbv2Client.CreateListener(i)
if err != nil {
return err
}
return nil
}
// addTagsForNLB creates needed tags for an NLB
func (c *Client) addTagsForNLB(resourceARN string, clusterName string) error {
i := &elbv2.AddTagsInput{
ResourceArns: []*string{
aws.String(resourceARN), // ext nlb resources arn
},
Tags: []*elbv2.Tag{
{
Key: aws.String("kubernetes.io/cluster/" + clusterName),
Value: aws.String("owned"),
},
{
Key: aws.String("Name"),
Value: aws.String(clusterName + "-ext"), //in form of samn-test-qb58m-ext
},
},
}