-
Notifications
You must be signed in to change notification settings - Fork 67
/
interface.go
255 lines (213 loc) · 6.58 KB
/
interface.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
/*
* 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 provider
import (
"context"
"fmt"
"time"
"github.com/gardener/controller-manager-library/pkg/config"
"github.com/gardener/controller-manager-library/pkg/controllermanager/controller"
"github.com/gardener/controller-manager-library/pkg/logger"
"github.com/gardener/controller-manager-library/pkg/resources"
"github.com/gardener/controller-manager-library/pkg/utils"
"github.com/gardener/external-dns-management/pkg/dns"
dnsutils "github.com/gardener/external-dns-management/pkg/dns/utils"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/util/flowcontrol"
)
type Config struct {
TTL int64
CacheTTL time.Duration
CacheDir string
RescheduleDelay time.Duration
Ident string
Dryrun bool
ZoneStateCaching bool
Delay time.Duration
Enabled utils.StringSet
Options *FactoryOptions
Factory DNSHandlerFactory
}
func NewConfigForController(c controller.Interface, factory DNSHandlerFactory) (*Config, error) {
ident, err := c.GetStringOption(OPT_IDENTIFIER)
if err != nil {
ident = "identifier-not-configured"
}
ttl, err := c.GetIntOption(OPT_TTL)
if err != nil {
ttl = 300
}
cttl, err := c.GetIntOption(OPT_CACHE_TTL)
if err != nil {
cttl = 60
}
cdir, _ := c.GetStringOption(OPT_CACHE_DIR)
dryrun, _ := c.GetBoolOption(OPT_DRYRUN)
delay, err := c.GetDurationOption(OPT_DNSDELAY)
if err != nil {
delay = 10 * time.Second
}
rescheduleDelay, err := c.GetDurationOption(OPT_RESCHEDULEDELAY)
if err != nil {
delay = 120 * time.Second
}
disableZoneStateCaching, _ := c.GetBoolOption(OPT_DISABLE_ZONE_STATE_CACHING)
enabled := utils.StringSet{}
types, err := c.GetStringOption(OPT_PROVIDERTYPES)
if err != nil || types == "" {
enabled.AddSet(factory.TypeCodes())
} else {
enabled.AddAllSplitted(types)
if enabled.Contains("all") {
enabled.Remove("all")
set := factory.TypeCodes()
set.Remove("mock-inmemory" /* mock.TYPE_CODE */)
enabled.AddSet(set)
}
_, del := enabled.DiffFrom(factory.TypeCodes())
if len(del) != 0 {
return nil, fmt.Errorf("unknown providers types %s", del)
}
}
osrc, _ := c.GetOptionSource(FACTORY_OPTIONS)
fopts := GetFactoryOptions(osrc)
return &Config{
Ident: ident,
TTL: int64(ttl),
CacheTTL: time.Duration(cttl) * time.Second,
CacheDir: cdir,
RescheduleDelay: rescheduleDelay,
Dryrun: dryrun,
ZoneStateCaching: !disableZoneStateCaching,
Delay: delay,
Enabled: enabled,
Options: fopts,
Factory: factory,
}, nil
}
type DNSHostedZone interface {
ProviderType() string
Key() string
Id() string
Domain() string
ForwardedDomains() []string
Match(dnsname string) int
IsPrivate() bool
}
type DNSHostedZones []DNSHostedZone
func (this DNSHostedZones) equivalentTo(infos DNSHostedZones) bool {
if len(this) != len(infos) {
return false
}
outer:
for _, i := range infos {
for _, t := range this {
if i.Key() == t.Key() && i.Domain() == t.Domain() {
continue outer
}
return false
}
}
return true
}
type DNSHandlerConfig struct {
Logger logger.LogContext
Properties utils.Properties
Config *runtime.RawExtension
DryRun bool
Context context.Context
CacheConfig ZoneCacheConfig
Options *FactoryOptions
Metrics Metrics
RateLimiter flowcontrol.RateLimiter
}
type DNSZoneState interface {
GetDNSSets() dns.DNSSets
}
const (
M_LISTZONES = "list_zones"
M_PLISTZONES = "list_zones_pages"
M_LISTRECORDS = "list_records"
M_PLISTRECORDS = "list_records_pages"
M_UPDATERECORDS = "update_records"
M_PUPDATEREORDS = "update_records_pages"
M_CREATERECORDS = "create_records"
M_DELETERECORDS = "delete_records"
M_CACHED_GETZONES = "cached_getzones"
M_CACHED_GETZONESTATE = "cached_getzonestate"
)
type Metrics interface {
AddRequests(request_type string, n int)
}
type Finalizers interface {
Finalizers() utils.StringSet
}
type DNSHandler interface {
ProviderType() string
GetZones() (DNSHostedZones, error)
GetZoneState(DNSHostedZone) (DNSZoneState, error)
ReportZoneStateConflict(zone DNSHostedZone, err error) bool
ExecuteRequests(logger logger.LogContext, zone DNSHostedZone, state DNSZoneState, reqs []*ChangeRequest) error
MapTarget(t Target) Target
Release()
}
type DefaultDNSHandler struct {
providerType string
}
func NewDefaultDNSHandler(providerType string) DefaultDNSHandler {
return DefaultDNSHandler{providerType}
}
func (this *DefaultDNSHandler) ProviderType() string {
return this.providerType
}
func (this *DefaultDNSHandler) MapTarget(t Target) Target {
return t
}
////////////////////////////////////////////////////////////////////////////////
type DNSHandlerOptionSource interface {
CreateOptionSource() (local config.OptionSource, defaults *GenericFactoryOptions)
}
type DNSHandlerFactory interface {
Name() string
TypeCodes() utils.StringSet
Create(typecode string, config *DNSHandlerConfig) (DNSHandler, error)
IsResponsibleFor(object *dnsutils.DNSProviderObject) bool
SupportZoneStateCache(typecode string) (bool, error)
}
type DNSProviders map[resources.ObjectName]DNSProvider
type DNSProvider interface {
ObjectName() resources.ObjectName
Object() resources.Object
TypeCode() string
DefaultTTL() int64
GetZones() DNSHostedZones
IncludesZone(zoneID string) bool
GetZoneState(zone DNSHostedZone) (DNSZoneState, error)
ExecuteRequests(logger logger.LogContext, zone DNSHostedZone, state DNSZoneState, requests []*ChangeRequest) error
Match(dns string) int
MatchZone(dns string) int
IsValid() bool
AccountHash() string
MapTarget(t Target) Target
// ReportZoneStateConflict is used to report a conflict because of stale data.
// It returns true if zone data will be updated and a retry may resolve the conflict
ReportZoneStateConflict(zone DNSHostedZone, err error) bool
}
type DoneHandler interface {
SetInvalid(err error)
Failed(err error)
Succeeded()
}