-
Notifications
You must be signed in to change notification settings - Fork 66
/
handler.go
208 lines (182 loc) · 6.04 KB
/
handler.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
/*
* 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 (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
"github.com/gardener/controller-manager-library/pkg/logger"
"github.com/gardener/external-dns-management/pkg/dns"
"github.com/gardener/external-dns-management/pkg/dns/provider"
perrs "github.com/gardener/external-dns-management/pkg/dns/provider/errors"
"github.com/gardener/external-dns-management/pkg/dns/provider/raw"
)
type Handler struct {
provider.DefaultDNSHandler
config provider.DNSHandlerConfig
cache provider.ZoneCache
access Access
}
var _ provider.DNSHandler = &Handler{}
func NewHandler(c *provider.DNSHandlerConfig) (provider.DNSHandler, error) {
var err error
h := &Handler{
DefaultDNSHandler: provider.NewDefaultDNSHandler(TYPE_CODE),
config: *c,
}
accessKeyID, err := c.GetRequiredProperty("ACCESS_KEY_ID", "accessKeyID")
if err != nil {
return nil, err
}
accessKeySecret, err := c.GetRequiredProperty("ACCESS_KEY_SECRET", "accessKeySecret")
if err != nil {
return nil, err
}
access, err := NewAccess(accessKeyID, accessKeySecret, c.Metrics, c.RateLimiter)
if err != nil {
return nil, perrs.WrapAsHandlerError(err, "Creating alicloud access with client credentials failed")
}
h.access = access
h.cache, err = provider.NewZoneCache(*c.CacheConfig.CopyWithDisabledZoneStateCache(), c.Metrics, nil, h.getZones, h.getZoneState)
if err != nil {
return nil, err
}
return h, nil
}
func (h *Handler) Release() {
h.cache.Release()
}
func (h *Handler) GetZones() (provider.DNSHostedZones, error) {
return h.cache.GetZones()
}
func (h *Handler) getZones(cache provider.ZoneCache) (provider.DNSHostedZones, error) {
blockedZones := h.config.Options.AdvancedOptions.GetBlockedZones()
raw := []alidns.Domain{}
{
f := func(zone alidns.Domain) (bool, error) {
if blockedZones.Contains(zone.DomainId) {
h.config.Logger.Infof("ignoring blocked zone id: %s", zone.DomainId)
} else {
raw = append(raw, zone)
}
return true, nil
}
err := h.access.ListDomains(f)
if err != nil {
return nil, perrs.WrapAsHandlerError(err, "list domains failed")
}
}
zones := provider.DNSHostedZones{}
{
for _, z := range raw {
forwarded := []string{}
f := func(r alidns.Record) (bool, error) {
if r.Type == dns.RS_NS {
name := GetDNSName(r)
if name != z.DomainName {
forwarded = append(forwarded, name)
}
}
return true, nil
}
err := h.access.ListRecords(z.DomainId, z.DomainName, f)
if err != nil {
if checkAccessForbidden(err) {
// It is reasonable for some RAM user, it is only allowed to access certain domain's records detail
// As a result, h domain should not be appended to the hosted zones
continue
}
return nil, perrs.WrapAsHandlerError(err, "list records failed")
}
hostedZone := provider.NewDNSHostedZone(h.ProviderType(), z.DomainId, z.DomainName, z.DomainName, forwarded, false)
zones = append(zones, hostedZone)
}
}
return zones, nil
}
func (h *Handler) GetZoneState(zone provider.DNSHostedZone) (provider.DNSZoneState, error) {
return h.cache.GetZoneState(zone)
}
func (h *Handler) getZoneState(zone provider.DNSHostedZone, cache provider.ZoneCache) (provider.DNSZoneState, error) {
state := raw.NewState()
f := func(r alidns.Record) (bool, error) {
a := (*Record)(&r)
state.AddRecord(a)
//fmt.Printf("**** found %s %s: %s\n", a.GetType(), a.GetDNSName(), a.GetValue() )
return true, nil
}
err := h.access.ListRecords(zone.Id(), zone.Key(), f)
if err != nil {
return nil, perrs.WrapAsHandlerError(err, "list records failed")
}
state.CalculateDNSSets()
return state, nil
}
func (h *Handler) ReportZoneStateConflict(zone provider.DNSHostedZone, err error) bool {
return h.cache.ReportZoneStateConflict(zone, err)
}
func (h *Handler) ExecuteRequests(logger logger.LogContext, zone provider.DNSHostedZone, state provider.DNSZoneState, reqs []*provider.ChangeRequest) error {
err := raw.ExecuteRequests(logger, &h.config, h.access, zone, state, reqs)
h.cache.ApplyRequests(logger, err, zone, reqs)
return err
}
func checkAccessForbidden(err error) bool {
if err != nil {
switch err.(type) {
case *errors.ServerError:
serverErr := err.(*errors.ServerError)
if serverErr.ErrorCode() == "Forbidden.RAM" {
return true
}
}
}
return false
}
func (h *Handler) GetRecordSet(zone provider.DNSHostedZone, dnsName, recordType string) (provider.DedicatedRecordSet, error) {
rs, err := h.access.GetRecordSet(dnsName, recordType, zone)
if err != nil {
return nil, err
}
d := provider.DedicatedRecordSet{}
for _, r := range rs {
d = append(d, r)
}
return d, nil
}
func (h *Handler) CreateOrUpdateRecordSet(logger logger.LogContext, zone provider.DNSHostedZone, old, new provider.DedicatedRecordSet) error {
err := h.DeleteRecordSet(logger, zone, old)
if err != nil {
return err
}
for _, r := range new {
r0 := h.access.NewRecord(r.GetDNSName(), r.GetType(), r.GetValue(), zone, int64(r.GetTTL()))
err = h.access.CreateRecord(r0, zone)
if err != nil {
return err
}
}
return err
}
func (h *Handler) DeleteRecordSet(logger logger.LogContext, zone provider.DNSHostedZone, rs provider.DedicatedRecordSet) error {
for _, r := range rs {
if r.(*Record).GetId() != "" {
err := h.access.DeleteRecord(r.(*Record), zone)
if err != nil {
return err
}
}
}
return nil
}