/
client_dns.go
372 lines (338 loc) · 12.4 KB
/
client_dns.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package client
import (
"context"
"fmt"
"strings"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
"golang.org/x/time/rate"
"k8s.io/utils/strings/slices"
"sigs.k8s.io/controller-runtime/pkg/log"
"github.com/gardener/gardener-extension-provider-alicloud/pkg/alicloud"
)
const (
domainsCacheTTL = 1 * time.Hour
rateLimiterCacheTTL = 1 * time.Hour
)
// RateLimiterWaitError is an error to be reported if waiting for a aliyun dns rate limiter fails.
// This can only happen if the wait time would exceed the configured wait timeout.
type RateLimiterWaitError struct {
Cause error
}
func (e *RateLimiterWaitError) Error() string {
return fmt.Sprintf("could not wait for client-side aliyun dns rate limiter: %+v", e.Cause)
}
// NewDNSClient creates a new DNS client with given region, accessKeyID, and accessKeySecret.
func (f *clientFactory) NewDNSClient(region, accessKeyID, accessKeySecret string) (DNS, error) {
client, err := alidns.NewClientWithAccessKey(region, accessKeyID, accessKeySecret)
if err != nil {
return nil, err
}
return &dnsClient{
Client: *client,
accessKeyID: accessKeyID,
domainsCache: f.domainsCache,
domainsCacheMutex: &f.domainsCacheMutex,
RateLimiter: f.getRateLimiter(accessKeyID),
RateLimiterWaitTimeout: f.waitTimeout,
Logger: log.Log.WithName("ali-dnsclient"),
}, nil
}
func (f *clientFactory) getRateLimiter(accessKeyID string) *rate.Limiter {
// cache.Expiring Get and Set methods are concurrency-safe
// However, if f rate limiter is not present in the cache, it may happen that multiple rate limiters are created
// at the same time for the same access key id, and the desired QPS is exceeded, so use f mutex to guard against this
f.rateLimitersMutex.Lock()
defer f.rateLimitersMutex.Unlock()
// Get f rate limiter from the cache, or create f new one if not present
var rateLimiter *rate.Limiter
if v, ok := f.rateLimiters.Get(accessKeyID); ok {
rateLimiter = v.(*rate.Limiter)
} else {
rateLimiter = rate.NewLimiter(f.limit, f.burst)
}
// Set should be called on every Get with cache.Expiring to refresh the TTL
f.rateLimiters.Set(accessKeyID, rateLimiter, rateLimiterCacheTTL)
return rateLimiter
}
// GetDomainNames returns a map of all domain names mapped to their composite domain names.
func (d *dnsClient) GetDomainNames(ctx context.Context) (map[string]string, error) {
domains, err := d.getDomainsWithCache(ctx)
if err != nil {
return nil, err
}
domainNames := make(map[string]string)
for _, domain := range domains {
domainNames[domain.DomainName] = CompositeDomainName(domain.DomainName, domain.DomainId)
}
return domainNames, nil
}
// GetDomainName returns the composite domain name of the domain with the given domain id.
func (d *dnsClient) GetDomainName(ctx context.Context, domainId string) (string, error) {
domains, err := d.getDomainsWithCache(ctx)
if err != nil {
return "", err
}
domain, ok := domains[domainId]
if !ok {
return "", fmt.Errorf("DNS domain with id %s not found", domainId)
}
return CompositeDomainName(domain.DomainName, domain.DomainId), nil
}
// CreateOrUpdateDomainRecords creates or updates the domain records with the given domain name, name, record type,
// values, and ttl.
// * For each element in values that has an existing domain record, the existing record is updated if needed.
// * For each element in values that doesn't have an existing domain record, a new domain record is created.
// * For each existing domain record that doesn't have a corresponding element in values, the existing record is deleted.
func (d *dnsClient) CreateOrUpdateDomainRecords(ctx context.Context, domainName, name, recordType string, values []string, ttl int64) error {
domainName, _ = DomainNameAndId(domainName)
rr, err := getRR(name, domainName)
if err != nil {
return err
}
records, err := d.getDomainRecords(ctx, domainName, rr, recordType)
if err != nil {
return err
}
for _, value := range values {
if record, ok := records[value]; ok {
// Only update the existing domain record if the current TTL value is different from the given one
// At this point we know that rr, recordType, and value are the same
if record.TTL != ttl {
if err := d.updateDomainRecord(ctx, record.RecordId, rr, recordType, value, ttl); err != nil {
return err
}
}
} else {
if err := d.createDomainRecord(ctx, domainName, rr, recordType, value, ttl); err != nil {
return err
}
}
}
for value, record := range records {
if !slices.Contains(values, value) {
if err := d.deleteDomainRecord(ctx, record.RecordId); err != nil {
return err
}
}
}
return nil
}
// DeleteDomainRecords deletes the domain records with the given domain name, name and record type.
func (d *dnsClient) DeleteDomainRecords(ctx context.Context, domainName, name, recordType string) error {
domainName, _ = DomainNameAndId(domainName)
rr, err := getRR(name, domainName)
if err != nil {
return err
}
records, err := d.getDomainRecords(ctx, domainName, rr, recordType)
if err != nil {
return err
}
for _, record := range records {
if err := d.deleteDomainRecord(ctx, record.RecordId); err != nil {
return err
}
}
return nil
}
func (d *dnsClient) getDomainsWithCache(ctx context.Context) (map[string]alidns.Domain, error) {
// cache.Expiring Get and Set methods are concurrency-safe.
// However, if an accessKeyID is not present in the cache and multiple DNSRecords are reconciled at the same time,
// it may happen that getDomains is called multiple times instead of just one, so use a mutex to guard against this.
// It is ok to use a shared mutex here as far as the number of accessKeyIDs using custom domains is low.
// This may need to be revisited with a larger number of such accessKeyIDs to avoid them blocking each other
// during the (potentially long-running) call to getDomains.
d.domainsCacheMutex.Lock()
defer d.domainsCacheMutex.Unlock()
if v, ok := d.domainsCache.Get(d.accessKeyID); ok {
return v.(map[string]alidns.Domain), nil
}
domains, err := d.getDomains(ctx)
if err != nil {
return nil, err
}
d.domainsCache.Set(d.accessKeyID, domains, domainsCacheTTL)
return domains, nil
}
// getDomains returns all domains.
func (d *dnsClient) getDomains(ctx context.Context) (map[string]alidns.Domain, error) {
if err := d.waitForAliDNSRateLimiter(ctx); err != nil {
return nil, err
}
domains := make(map[string]alidns.Domain)
pageSize, pageNumber := 20, 1
req := alidns.CreateDescribeDomainsRequest()
req.PageSize = requests.NewInteger(pageSize)
for {
req.PageNumber = requests.NewInteger(pageNumber)
resp, err := d.Client.DescribeDomains(req)
if err != nil {
return nil, err
}
for _, domain := range resp.Domains.Domain {
domains[domain.DomainId] = getDomainFromResponse(domain)
}
if resp.PageNumber*int64(pageSize) >= resp.TotalCount {
break
}
pageNumber++
}
return domains, nil
}
func getDomainFromResponse(domainFromResp alidns.DomainInDescribeDomains) alidns.Domain {
return alidns.Domain{
DomainId: domainFromResp.DomainId,
DomainName: domainFromResp.DomainName,
AliDomain: domainFromResp.AliDomain,
CreateTimestamp: domainFromResp.CreateTimestamp,
//ExpireTimestamp: ,
InstanceEndTime: domainFromResp.InstanceEndTime,
CreateTime: domainFromResp.CreateTime,
// SourceProtocol: domainFromResp.SourceProtocol,
GroupName: domainFromResp.GroupName,
VersionCode: domainFromResp.VersionCode,
// UpdateTimestamp: domainFromResp.UpdateTimestamp,
RecordCount: domainFromResp.RecordCount,
InstanceExpired: domainFromResp.InstanceExpired,
ResourceGroupId: domainFromResp.ResourceGroupId,
// CacheTtlMin: domainFromResp.CacheTtlMin,
InstanceId: domainFromResp.InstanceId,
//ExpireTime:,
GroupId: domainFromResp.GroupId,
// SourceEdns: domainFromResp.SourceEdns,
RegistrantEmail: domainFromResp.RegistrantEmail,
VersionName: domainFromResp.VersionName,
// UpdateTime: domainFromResp.UpdateTime,
Remark: domainFromResp.Remark,
// CacheTtlMax: domainFromResp.CacheTtlMax,
PunyCode: domainFromResp.PunyCode,
Starmark: domainFromResp.Starmark,
// DnsServers: domainFromResp.DnsServers,
Tags: domainFromResp.Tags,
// SourceDnsServers: domainFromResp.SourceDnsServers,
}
}
// getDomainRecords returns the domain records with the given domain name, rr, and record type.
func (d *dnsClient) getDomainRecords(ctx context.Context, domainName, rr, recordType string) (map[string]alidns.Record, error) {
if err := d.waitForAliDNSRateLimiter(ctx); err != nil {
return nil, err
}
records := make(map[string]alidns.Record)
pageSize, pageNumber := 20, 1
req := alidns.CreateDescribeDomainRecordsRequest()
req.PageSize = requests.NewInteger(pageSize)
for {
req.PageNumber = requests.NewInteger(pageNumber)
req.DomainName = domainName
req.RRKeyWord = rr
req.TypeKeyWord = recordType
resp, err := d.Client.DescribeDomainRecords(req)
if err != nil {
return nil, err
}
for _, record := range resp.DomainRecords.Record {
records[record.Value] = record
}
if resp.PageNumber*int64(pageSize) >= resp.TotalCount {
break
}
pageNumber++
}
return records, nil
}
func (d *dnsClient) createDomainRecord(ctx context.Context, domainName, rr, recordType, value string, ttl int64) error {
if err := d.waitForAliDNSRateLimiter(ctx); err != nil {
return err
}
req := alidns.CreateAddDomainRecordRequest()
req.DomainName = domainName
req.RR = rr
req.Type = recordType
req.Value = value
req.TTL = requests.NewInteger(int(ttl))
_, err := d.Client.AddDomainRecord(req)
return err
}
func (d *dnsClient) updateDomainRecord(ctx context.Context, id, rr, recordType, value string, ttl int64) error {
if err := d.waitForAliDNSRateLimiter(ctx); err != nil {
return err
}
req := alidns.CreateUpdateDomainRecordRequest()
req.RecordId = id
req.RR = rr
req.Type = recordType
req.Value = value
req.TTL = requests.NewInteger(int(ttl))
_, err := d.Client.UpdateDomainRecord(req)
return err
}
func (d *dnsClient) deleteDomainRecord(ctx context.Context, id string) error {
if err := d.waitForAliDNSRateLimiter(ctx); err != nil {
return err
}
req := alidns.CreateDeleteDomainRecordRequest()
req.RecordId = id
if _, err := d.Client.DeleteDomainRecord(req); err != nil && !isDomainRecordDoesNotExistError(err) {
return err
}
return nil
}
func (d *dnsClient) waitForAliDNSRateLimiter(ctx context.Context) error {
timeoutCtx, cancel := context.WithTimeout(ctx, d.RateLimiterWaitTimeout)
defer cancel()
t := time.Now()
if err := d.RateLimiter.Wait(timeoutCtx); err != nil {
return &RateLimiterWaitError{Cause: err}
}
if waitDuration := time.Since(t); waitDuration.Seconds() > 1/float64(d.RateLimiter.Limit()) {
d.Logger.Info("Waited for client-side aliyun DNS rate limiter", "waitDuration", waitDuration.String())
}
return nil
}
func getRR(name, domainName string) (string, error) {
if name == domainName {
return "@", nil
}
suffix := "." + domainName
if !strings.HasSuffix(name, suffix) {
return "", fmt.Errorf("name %s does not match domain name %s", name, domainName)
}
return strings.TrimSuffix(name, suffix), nil
}
func isDomainRecordDoesNotExistError(err error) bool {
if serverError, ok := err.(*errors.ServerError); ok {
if serverError.ErrorCode() == alicloud.ErrorCodeDomainRecordNotBelongToUser {
return true
}
}
return false
}
// CompositeDomainName composes and returns a composite domain name from the given domain name and id,
// in the format <domainName>:<domainId>
func CompositeDomainName(domainName, domainId string) string {
if domainId != "" {
return domainName + ":" + domainId
}
return domainName
}
// DomainNameAndId decomposes the given composite domain name in the format <domainName>:<domainId>
// into its constituent domain name and id.
func DomainNameAndId(compositeDomainName string) (string, string) {
if parts := strings.Split(compositeDomainName, ":"); len(parts) == 2 {
return parts[0], parts[1]
}
return compositeDomainName, ""
}
// IsThrottlingError returns true if the error is a throttling error.
func IsThrottlingError(err error) bool {
if alierr, ok := err.(errors.Error); ok && strings.Contains(alierr.Message(), "Throttling") {
return true
}
return false
}