forked from openshift/external-dns-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
187 lines (165 loc) · 5.69 KB
/
aws.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
//go:build e2e
// +build e2e
package e2e
import (
"fmt"
"time"
"github.com/openshift/external-dns-operator/test/common"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/route53"
configv1 "github.com/openshift/api/config/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
operatorv1alpha1 "github.com/openshift/external-dns-operator/api/v1alpha1"
operatorv1beta1 "github.com/openshift/external-dns-operator/api/v1beta1"
)
type awsTestHelper struct {
r53Client *route53.Route53
keyID string
secretKey string
}
func newAWSHelper(isOpenShiftCI bool) (providerTestHelper, error) {
provider := &awsTestHelper{}
if err := provider.prepareConfigurations(isOpenShiftCI); err != nil {
return nil, err
}
awsSession := session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(provider.keyID, provider.secretKey, ""),
}))
provider.r53Client = route53.New(awsSession)
return provider, nil
}
func (a *awsTestHelper) makeCredentialsSecret(namespace string) *corev1.Secret {
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("aws-access-key-%s", randomString(16)),
Namespace: namespace,
},
Data: map[string][]byte{
"aws_access_key_id": []byte(a.keyID),
"aws_secret_access_key": []byte(a.secretKey),
},
}
}
func (a *awsTestHelper) buildExternalDNS(name, zoneID, zoneDomain string, credsSecret *corev1.Secret) operatorv1beta1.ExternalDNS {
resource := defaultExternalDNS(name, zoneID, zoneDomain)
resource.Spec.Provider = operatorv1beta1.ExternalDNSProvider{
Type: operatorv1beta1.ProviderTypeAWS,
AWS: &operatorv1beta1.ExternalDNSAWSProviderOptions{
Credentials: operatorv1beta1.SecretReference{
Name: credsSecret.Name,
},
},
}
return resource
}
func (a *awsTestHelper) buildOpenShiftExternalDNS(name, zoneID, zoneDomain, routerName string, _ *corev1.Secret) operatorv1beta1.ExternalDNS {
resource := routeExternalDNS(name, zoneID, zoneDomain, routerName)
resource.Spec.Provider = operatorv1beta1.ExternalDNSProvider{
Type: operatorv1beta1.ProviderTypeAWS,
}
return resource
}
func (a *awsTestHelper) buildOpenShiftExternalDNSV1Alpha1(name, zoneID, zoneDomain, routerName string, _ *corev1.Secret) operatorv1alpha1.ExternalDNS {
resource := routeExternalDNSV1Alpha1(name, zoneID, zoneDomain, routerName)
resource.Spec.Provider = operatorv1alpha1.ExternalDNSProvider{
Type: operatorv1alpha1.ProviderTypeAWS,
}
return resource
}
func (a *awsTestHelper) platform() string {
return string(configv1.AWSPlatformType)
}
func (a *awsTestHelper) ensureHostedZone(zoneDomain string) (string, []string, error) {
zones, err := a.r53Client.ListHostedZones(&route53.ListHostedZonesInput{})
if err != nil {
return "", nil, fmt.Errorf("failed to list hosted zones: %w", err)
}
// if hosted zone exists then return its id and nameservers
for _, zone := range zones.HostedZones {
if aws.StringValue(zone.Name) == zoneDomain {
hostedZone, err := a.r53Client.GetHostedZone(&route53.GetHostedZoneInput{Id: zone.Id})
if err != nil {
return "", nil, fmt.Errorf("failed to get hosted zone %s: %v", aws.StringValue(zone.Id), err)
}
return aws.StringValue(zone.Id), aws.StringValueSlice(hostedZone.DelegationSet.NameServers), nil
}
}
// create hosted zone and return its id and nameservers
zone, err := a.r53Client.CreateHostedZone(&route53.CreateHostedZoneInput{
Name: aws.String(zoneDomain),
CallerReference: aws.String(time.Now().Format(time.RFC3339)),
})
if err != nil {
return "", nil, fmt.Errorf("failed to get create hosted zone: %w", err)
}
return aws.StringValue(zone.HostedZone.Id), aws.StringValueSlice(zone.DelegationSet.NameServers), nil
}
// AWS sdk expect to zone ID to delete the xone, where as Azure expect Domain Name
func (a *awsTestHelper) deleteHostedZone(zoneID, zoneDomain string) error {
input := route53.ListResourceRecordSetsInput{
HostedZoneId: &zoneID,
}
output, err := a.r53Client.ListResourceRecordSets(&input)
if err != nil {
return err
}
var recordChanges []*route53.Change
// create change set deleting all DNS records which are not of NS and SOA types in hosted zone
for len(output.ResourceRecordSets) != 0 {
for _, recordset := range output.ResourceRecordSets {
if *recordset.Type == "SOA" || *recordset.Type == "NS" {
continue
} else {
recordDelete := &route53.Change{
Action: aws.String("DELETE"),
ResourceRecordSet: recordset,
}
recordChanges = append(recordChanges, recordDelete)
}
}
if !(*output.IsTruncated) {
break
} else {
input.StartRecordName = output.NextRecordName
output, err = a.r53Client.ListResourceRecordSets(&input)
if err != nil {
return err
}
}
}
if len(recordChanges) != 0 {
changeRecordsInput := route53.ChangeResourceRecordSetsInput{
HostedZoneId: &zoneID,
ChangeBatch: &route53.ChangeBatch{
Changes: recordChanges,
},
}
if _, err := a.r53Client.ChangeResourceRecordSets(&changeRecordsInput); err != nil {
return err
}
}
zoneInput := route53.DeleteHostedZoneInput{
Id: &zoneID,
}
if _, err := a.r53Client.DeleteHostedZone(&zoneInput); err != nil {
return err
}
return nil
}
func (a *awsTestHelper) prepareConfigurations(isOpenShiftCI bool) error {
if isOpenShiftCI {
data, err := common.RootCredentials("aws-creds")
if err != nil {
return fmt.Errorf("failed to get AWS credentials: %w", err)
}
a.keyID = string(data["aws_access_key_id"])
a.secretKey = string(data["aws_secret_access_key"])
} else {
a.keyID = common.MustGetEnv("AWS_ACCESS_KEY_ID")
a.secretKey = common.MustGetEnv("AWS_SECRET_ACCESS_KEY")
}
return nil
}