-
Notifications
You must be signed in to change notification settings - Fork 66
/
access.go
202 lines (180 loc) · 6.02 KB
/
access.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
/*
* Copyright 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
*
* 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
*
*/
package alicloud
import (
"fmt"
"strings"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
"k8s.io/client-go/util/flowcontrol"
"github.com/gardener/external-dns-management/pkg/dns/provider"
"github.com/gardener/external-dns-management/pkg/dns/provider/raw"
)
var nullHost = "@"
var defaultPageSize = 20
func GetDNSName(r alidns.Record) string {
if r.RR == nullHost {
return r.DomainName
}
return r.RR + "." + r.DomainName
}
func GetRR(dnsname, domain string) string {
if dnsname == domain {
return nullHost
}
suffix := "." + domain
if !strings.HasSuffix(dnsname, suffix) {
panic(fmt.Sprintf("OOPS: dnsname %q does not match zone %q", dnsname, domain))
}
return dnsname[:len(dnsname)-len(suffix)]
}
type Access interface {
ListDomains(consume func(domain alidns.Domain) (bool, error)) error
ListRecords(zoneID, domain string, consume func(record alidns.Record) (bool, error)) error
raw.Executor
}
type access struct {
client *alidns.Client
metrics provider.Metrics
rateLimiter flowcontrol.RateLimiter
}
func NewAccess(accessKeyId, accessKeySecret string, metrics provider.Metrics, rateLimiter flowcontrol.RateLimiter) (Access, error) {
client, err := alidns.NewClientWithAccessKey("cn-shanghai", accessKeyId, accessKeySecret)
if err != nil {
return nil, err
}
return &access{client, metrics, rateLimiter}, nil
}
func (this *access) nextPageNumber(pageNumber, pageSize, totalCount int) int {
if pageNumber*pageSize >= totalCount {
return 0
}
return pageNumber + 1
}
func (this *access) ListDomains(consume func(domain alidns.Domain) (bool, error)) error {
request := alidns.CreateDescribeDomainsRequest()
request.PageSize = requests.NewInteger(defaultPageSize)
nextPage := 1
rt := provider.M_LISTZONES
for {
this.metrics.AddGenericRequests(rt, 1)
rt = provider.M_PLISTZONES
request.PageNumber = requests.NewInteger(nextPage)
this.rateLimiter.Accept()
resp, err := this.client.DescribeDomains(request)
if err != nil {
return err
}
for _, d := range resp.Domains.Domain {
if cont, err := consume(d); !cont || err != nil {
return err
}
}
if nextPage = this.nextPageNumber(resp.PageNumber, defaultPageSize, resp.TotalCount); nextPage == 0 {
return nil
}
}
}
func (this *access) ListRecords(zoneID, domain string, consume func(record alidns.Record) (bool, error)) error {
return this.listRecords(zoneID, domain, consume, nil)
}
func (this *access) listRecords(zoneID, domain string, consume func(record alidns.Record) (bool, error),
requestModifier func(request *alidns.DescribeDomainRecordsRequest)) error {
request := alidns.CreateDescribeDomainRecordsRequest()
request.DomainName = domain
if requestModifier != nil {
requestModifier(request)
}
request.PageSize = requests.NewInteger(defaultPageSize)
nextPage := 1
rt := provider.M_LISTRECORDS
for {
this.metrics.AddZoneRequests(zoneID, rt, 1)
rt = provider.M_PLISTRECORDS
request.PageNumber = requests.NewInteger(nextPage)
this.rateLimiter.Accept()
resp, err := this.client.DescribeDomainRecords(request)
if err != nil {
return err
}
for _, r := range resp.DomainRecords.Record {
if cont, err := consume(r); !cont || err != nil {
return err
}
}
if nextPage = this.nextPageNumber(resp.PageNumber, defaultPageSize, resp.TotalCount); nextPage == 0 {
return nil
}
}
}
func (this *access) CreateRecord(r raw.Record, zone provider.DNSHostedZone) error {
a := r.(*Record)
req := alidns.CreateAddDomainRecordRequest()
req.DomainName = a.DomainName
req.RR = a.RR
req.Type = a.Type
req.TTL = requests.NewInteger(a.TTL)
req.Value = a.Value
this.metrics.AddZoneRequests(zone.Id(), provider.M_UPDATERECORDS, 1)
this.rateLimiter.Accept()
_, err := this.client.AddDomainRecord(req)
return err
}
func (this *access) UpdateRecord(r raw.Record, zone provider.DNSHostedZone) error {
a := r.(*Record)
req := alidns.CreateUpdateDomainRecordRequest()
req.RecordId = a.RecordId
req.RR = a.RR
req.Type = a.Type
req.TTL = requests.NewInteger(a.TTL)
req.Value = a.Value
this.metrics.AddZoneRequests(zone.Id(), provider.M_UPDATERECORDS, 1)
this.rateLimiter.Accept()
_, err := this.client.UpdateDomainRecord(req)
return err
}
func (this *access) DeleteRecord(r raw.Record, zone provider.DNSHostedZone) error {
req := alidns.CreateDeleteDomainRecordRequest()
req.RecordId = r.GetId()
this.metrics.AddZoneRequests(zone.Id(), provider.M_UPDATERECORDS, 1)
this.rateLimiter.Accept()
_, err := this.client.DeleteDomainRecord(req)
return err
}
func (this *access) GetRecordSet(dnsName, rtype string, zone provider.DNSHostedZone) (raw.RecordSet, error) {
rr := GetRR(dnsName, zone.Domain())
requestModifier := func(request *alidns.DescribeDomainRecordsRequest) {
request.RRKeyWord = rr
request.TypeKeyWord = rtype
}
rs := raw.RecordSet{}
consume := func(record alidns.Record) (bool, error) {
a := (*Record)(&record)
if a.RR == rr {
rs = append(rs, a)
}
return true, nil
}
err := this.listRecords(zone.Id(), zone.Domain(), consume, requestModifier)
if err != nil {
return nil, err
}
return rs, nil
}
func (this *access) NewRecord(fqdn, rtype, value string, zone provider.DNSHostedZone, ttl int64) raw.Record {
rr := GetRR(fqdn, zone.Domain())
return (*Record)(&alidns.Record{RR: rr, Type: rtype, Value: value, DomainName: zone.Domain(), TTL: int(ttl)})
}