forked from crossplane-contrib/provider-cloudflare
/
record.go
267 lines (213 loc) · 7.79 KB
/
record.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
/*
Copyright 2021 The Crossplane Authors.
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
limitations under the License.
*/
package record
import (
"context"
"time"
"github.com/pkg/errors"
"k8s.io/client-go/util/workqueue"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
rtv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/ratelimiter"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/cloudflare/cloudflare-go"
"github.com/benagricola/provider-cloudflare/apis/dns/v1alpha1"
clients "github.com/benagricola/provider-cloudflare/internal/clients"
records "github.com/benagricola/provider-cloudflare/internal/clients/records"
metrics "github.com/benagricola/provider-cloudflare/internal/metrics"
)
const (
errNotRecord = "managed resource is not a Record custom resource"
errClientConfig = "error getting client config"
errRecordLookup = "cannot lookup record"
errRecordCreation = "cannot create record"
errRecordUpdate = "cannot update record"
errRecordDeletion = "cannot delete record"
errRecordNoZone = "no zone found"
maxConcurrency = 5
// recordStatusActive = "active"
)
// Setup adds a controller that reconciles Record managed resources.
func Setup(mgr ctrl.Manager, l logging.Logger, rl workqueue.RateLimiter) error {
name := managed.ControllerName(v1alpha1.RecordGroupKind)
o := controller.Options{
RateLimiter: ratelimiter.NewDefaultManagedRateLimiter(rl),
MaxConcurrentReconciles: maxConcurrency,
}
hc := metrics.NewInstrumentedHTTPClient(name)
r := managed.NewReconciler(mgr,
resource.ManagedKind(v1alpha1.RecordGroupVersionKind),
managed.WithExternalConnecter(&connector{
kube: mgr.GetClient(),
newCloudflareClientFn: func(cfg clients.Config) (records.Client, error) {
return records.NewClient(cfg, hc)
},
}),
managed.WithLogger(l.WithValues("controller", name)),
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))),
managed.WithPollInterval(5*time.Minute),
// Do not initialize external-name field.
managed.WithInitializers(),
)
return ctrl.NewControllerManagedBy(mgr).
Named(name).
WithOptions(o).
For(&v1alpha1.Record{}).
Complete(r)
}
// A connector is expected to produce an ExternalClient when its Connect method
// is called.
type connector struct {
kube client.Client
newCloudflareClientFn func(cfg clients.Config) (records.Client, error)
}
// Connect produces a valid configuration for a Cloudflare API
// instance, and returns it as an external client.
func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
_, ok := mg.(*v1alpha1.Record)
if !ok {
return nil, errors.New(errNotRecord)
}
// Get client configuration
config, err := clients.GetConfig(ctx, c.kube, mg)
if err != nil {
return nil, errors.Wrap(err, errClientConfig)
}
client, err := c.newCloudflareClientFn(*config)
if err != nil {
return nil, err
}
return &external{client: client}, nil
}
// An ExternalClient observes, then either creates, updates, or deletes an
// external resource to ensure it reflects the managed resource's desired state.
type external struct {
client records.Client
}
func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.ExternalObservation, error) {
cr, ok := mg.(*v1alpha1.Record)
if !ok {
return managed.ExternalObservation{}, errors.New(errNotRecord)
}
// Record does not exist if we dont have an ID stored in external-name
rid := meta.GetExternalName(cr)
if rid == "" {
return managed.ExternalObservation{ResourceExists: false}, nil
}
if cr.Spec.ForProvider.Zone == nil {
return managed.ExternalObservation{}, errors.New(errRecordNoZone)
}
record, err := e.client.DNSRecord(ctx, *cr.Spec.ForProvider.Zone, rid)
if err != nil {
return managed.ExternalObservation{},
errors.Wrap(resource.Ignore(records.IsRecordNotFound, err), errRecordLookup)
}
cr.Status.AtProvider = records.GenerateObservation(record)
cr.SetConditions(rtv1.Available())
return managed.ExternalObservation{
ResourceExists: true,
ResourceLateInitialized: records.LateInitialize(&cr.Spec.ForProvider, record),
ResourceUpToDate: records.UpToDate(&cr.Spec.ForProvider, record),
}, nil
}
func (e *external) Create(ctx context.Context, mg resource.Managed) (managed.ExternalCreation, error) {
cr, ok := mg.(*v1alpha1.Record)
if !ok {
return managed.ExternalCreation{}, errors.New(errNotRecord)
}
if cr.Spec.ForProvider.Zone == nil {
return managed.ExternalCreation{},
errors.Wrap(errors.New(errRecordNoZone), errRecordCreation)
}
if cr.Spec.ForProvider.TTL == nil {
return managed.ExternalCreation{}, errors.New(errRecordCreation)
}
if cr.Spec.ForProvider.Type == nil {
return managed.ExternalCreation{}, errors.New(errRecordCreation)
}
// Required for MX, SRV and URI records; unused by other record types.
if cr.Spec.ForProvider.Priority == nil {
switch *cr.Spec.ForProvider.Type {
case "MX", "SRV", "URI":
return managed.ExternalCreation{}, errors.New(errRecordCreation)
}
}
cr.SetConditions(rtv1.Creating())
ttl := int(*cr.Spec.ForProvider.TTL)
var pri *uint16
if cr.Spec.ForProvider.Priority != nil {
val := uint16(*cr.Spec.ForProvider.Priority)
pri = &val
}
res, err := e.client.CreateDNSRecord(
ctx,
*cr.Spec.ForProvider.Zone,
cloudflare.DNSRecord{
Type: *cr.Spec.ForProvider.Type,
Name: cr.Spec.ForProvider.Name,
TTL: ttl,
Content: cr.Spec.ForProvider.Content,
Proxied: cr.Spec.ForProvider.Proxied,
Priority: pri,
},
)
if err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, errRecordCreation)
}
cr.Status.AtProvider = records.GenerateObservation(res.Result)
// Update the external name with the ID of the new DNS Record
meta.SetExternalName(cr, res.Result.ID)
return managed.ExternalCreation{ExternalNameAssigned: true}, nil
}
func (e *external) Update(ctx context.Context, mg resource.Managed) (managed.ExternalUpdate, error) {
cr, ok := mg.(*v1alpha1.Record)
if !ok {
return managed.ExternalUpdate{}, errors.New(errNotRecord)
}
if cr.Spec.ForProvider.Zone == nil {
return managed.ExternalUpdate{}, errors.Wrap(errors.New(errRecordNoZone), errRecordUpdate)
}
rid := meta.GetExternalName(cr)
// Update should never be called on a nonexistent resource
if rid == "" {
return managed.ExternalUpdate{}, errors.New(errRecordUpdate)
}
return managed.ExternalUpdate{},
errors.Wrap(
records.UpdateRecord(ctx, e.client, rid, &cr.Spec.ForProvider),
errRecordUpdate,
)
}
func (e *external) Delete(ctx context.Context, mg resource.Managed) error {
cr, ok := mg.(*v1alpha1.Record)
if !ok {
return errors.New(errNotRecord)
}
if cr.Spec.ForProvider.Zone == nil {
return errors.Wrap(errors.New(errRecordNoZone), errRecordDeletion)
}
rid := meta.GetExternalName(cr)
// Delete should never be called on a nonexistent resource
if rid == "" {
return errors.New(errRecordDeletion)
}
return errors.Wrap(
e.client.DeleteDNSRecord(ctx, *cr.Spec.ForProvider.Zone, meta.GetExternalName(cr)),
errRecordDeletion)
}