/
setup.go
351 lines (293 loc) · 13.9 KB
/
setup.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
/*
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 connection
import (
"context"
"errors"
"time"
awsarn "github.com/aws/aws-sdk-go/aws/arn"
svcsdk "github.com/aws/aws-sdk-go/service/glue"
"github.com/aws/aws-sdk-go/service/glue/glueiface"
svcsdksts "github.com/aws/aws-sdk-go/service/sts"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/connection"
"github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
svcapitypes "github.com/crossplane-contrib/provider-aws/apis/glue/v1alpha1"
"github.com/crossplane-contrib/provider-aws/apis/v1alpha1"
svcutils "github.com/crossplane-contrib/provider-aws/pkg/controller/glue/utils"
"github.com/crossplane-contrib/provider-aws/pkg/features"
connectaws "github.com/crossplane-contrib/provider-aws/pkg/utils/connect/aws"
errorutils "github.com/crossplane-contrib/provider-aws/pkg/utils/errors"
"github.com/crossplane-contrib/provider-aws/pkg/utils/pointer"
custommanaged "github.com/crossplane-contrib/provider-aws/pkg/utils/reconciler/managed"
)
const (
errBuildARN = "cannot built the ARN for Connection" // to be able to update Tags, add the correct ARN to the annotation
errGetARN = "cannot get a correct ARN for Connection"
errMissingARNAnno = "cannot find the annotation for the Connection ARN"
annotationARN = "crossplane.io/external-aws-glue-connection-arn"
)
// SetupConnection adds a controller that reconciles Connection.
func SetupConnection(mgr ctrl.Manager, o controller.Options) error {
name := managed.ControllerName(svcapitypes.ConnectionGroupKind)
opts := []option{
func(e *external) {
h := &hooks{kube: e.kube, client: e.client}
e.preObserve = preObserve
e.postObserve = postObserve
e.preDelete = preDelete
e.postCreate = h.postCreate
e.preCreate = preCreate
e.isUpToDate = h.isUpToDate
e.preUpdate = preUpdate
e.postUpdate = h.postUpdate
},
}
cps := []managed.ConnectionPublisher{managed.NewAPISecretPublisher(mgr.GetClient(), mgr.GetScheme())}
if o.Features.Enabled(features.EnableAlphaExternalSecretStores) {
cps = append(cps, connection.NewDetailsManager(mgr.GetClient(), v1alpha1.StoreConfigGroupVersionKind))
}
reconcilerOpts := []managed.ReconcilerOption{
managed.WithCriticalAnnotationUpdater(custommanaged.NewRetryingCriticalAnnotationUpdater(mgr.GetClient())),
managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}),
managed.WithPollInterval(o.PollInterval),
managed.WithLogger(o.Logger.WithValues("controller", name)),
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))),
managed.WithConnectionPublishers(cps...),
}
if o.Features.Enabled(features.EnableAlphaManagementPolicies) {
reconcilerOpts = append(reconcilerOpts, managed.WithManagementPolicies())
}
r := managed.NewReconciler(mgr,
resource.ManagedKind(svcapitypes.ConnectionGroupVersionKind),
reconcilerOpts...)
return ctrl.NewControllerManagedBy(mgr).
Named(name).
WithOptions(o.ForControllerRuntime()).
WithEventFilter(resource.DesiredStateChanged()).
For(&svcapitypes.Connection{}).
Complete(r)
}
type hooks struct {
kube client.Client
client glueiface.GlueAPI
}
func preDelete(_ context.Context, cr *svcapitypes.Connection, obj *svcsdk.DeleteConnectionInput) (bool, error) {
obj.ConnectionName = pointer.ToOrNilIfZeroValue(meta.GetExternalName(cr))
return false, nil
}
func preObserve(_ context.Context, cr *svcapitypes.Connection, obj *svcsdk.GetConnectionInput) error {
obj.Name = pointer.ToOrNilIfZeroValue(meta.GetExternalName(cr))
return nil
}
func postObserve(ctx context.Context, cr *svcapitypes.Connection, obj *svcsdk.GetConnectionOutput, obs managed.ExternalObservation, err error) (managed.ExternalObservation, error) {
if err != nil {
return managed.ExternalObservation{}, err
}
// not needed if we get the fields properly set in GenerateConnection() (metav1 import issue)
cr.Status.AtProvider.CreationTime = fromTimePtr(obj.Connection.CreationTime)
cr.Status.AtProvider.LastUpdatedTime = fromTimePtr(obj.Connection.LastUpdatedTime)
cr.SetConditions(xpv1.Available())
return obs, nil
}
func (h *hooks) isUpToDate(_ context.Context, cr *svcapitypes.Connection, resp *svcsdk.GetConnectionOutput) (bool, string, error) {
currentParams := customGenerateConnection(resp).Spec.ForProvider
if diff := cmp.Diff(cr.Spec.ForProvider, currentParams, cmpopts.EquateEmpty(),
cmpopts.IgnoreTypes(&xpv1.Reference{}, &xpv1.Selector{}, []xpv1.Reference{}),
cmpopts.IgnoreFields(svcapitypes.ConnectionParameters{}, "Region", "Tags", "CatalogID")); diff != "" {
return false, diff, nil
}
// CatalogID is updatable (and is given to UpdateConnectionInput),
// however the field seems not to be readable through the API for an isUpToDate-check
// retrieve ARN and check if Tags need update
arn, err := h.getARN(cr)
if err != nil {
return true, "", err
}
areTagsUpToDate, err := svcutils.AreTagsUpToDate(h.client, cr.Spec.ForProvider.Tags, arn)
return areTagsUpToDate, "", err
}
func preUpdate(_ context.Context, cr *svcapitypes.Connection, obj *svcsdk.UpdateConnectionInput) error {
obj.Name = pointer.ToOrNilIfZeroValue(meta.GetExternalName(cr))
if cr.Spec.ForProvider.CustomConnectionInput != nil {
obj.ConnectionInput = &svcsdk.ConnectionInput{
Name: pointer.ToOrNilIfZeroValue(meta.GetExternalName(cr)),
ConnectionProperties: cr.Spec.ForProvider.CustomConnectionInput.ConnectionProperties,
ConnectionType: pointer.ToOrNilIfZeroValue(cr.Spec.ForProvider.CustomConnectionInput.ConnectionType),
Description: cr.Spec.ForProvider.CustomConnectionInput.Description,
MatchCriteria: cr.Spec.ForProvider.CustomConnectionInput.MatchCriteria,
}
if cr.Spec.ForProvider.CustomConnectionInput.CustomPhysicalConnectionRequirements != nil {
obj.ConnectionInput.PhysicalConnectionRequirements = &svcsdk.PhysicalConnectionRequirements{
AvailabilityZone: cr.Spec.ForProvider.CustomConnectionInput.CustomPhysicalConnectionRequirements.AvailabilityZone,
SubnetId: cr.Spec.ForProvider.CustomConnectionInput.CustomPhysicalConnectionRequirements.SubnetID,
}
for i := range cr.Spec.ForProvider.CustomConnectionInput.CustomPhysicalConnectionRequirements.SecurityGroupIDList {
obj.ConnectionInput.PhysicalConnectionRequirements.SecurityGroupIdList = append(obj.ConnectionInput.PhysicalConnectionRequirements.SecurityGroupIdList, &cr.Spec.ForProvider.CustomConnectionInput.CustomPhysicalConnectionRequirements.SecurityGroupIDList[i])
}
}
}
return nil
}
func (h *hooks) postUpdate(ctx context.Context, cr *svcapitypes.Connection, obj *svcsdk.UpdateConnectionOutput, upd managed.ExternalUpdate, err error) (managed.ExternalUpdate, error) {
if err != nil {
return managed.ExternalUpdate{}, err
}
// update Tags if ARN available
connectionARN, err := h.getARN(cr)
if err != nil {
return upd, err
}
return upd, svcutils.UpdateTagsForResource(ctx, h.client, cr.Spec.ForProvider.Tags, connectionARN)
}
func preCreate(_ context.Context, cr *svcapitypes.Connection, obj *svcsdk.CreateConnectionInput) error {
if cr.Spec.ForProvider.CustomConnectionInput != nil {
obj.ConnectionInput = &svcsdk.ConnectionInput{
Name: pointer.ToOrNilIfZeroValue(meta.GetExternalName(cr)),
ConnectionProperties: cr.Spec.ForProvider.CustomConnectionInput.ConnectionProperties,
ConnectionType: pointer.ToOrNilIfZeroValue(cr.Spec.ForProvider.CustomConnectionInput.ConnectionType),
Description: cr.Spec.ForProvider.CustomConnectionInput.Description,
MatchCriteria: cr.Spec.ForProvider.CustomConnectionInput.MatchCriteria,
}
if cr.Spec.ForProvider.CustomConnectionInput.CustomPhysicalConnectionRequirements != nil {
obj.ConnectionInput.PhysicalConnectionRequirements = &svcsdk.PhysicalConnectionRequirements{
AvailabilityZone: cr.Spec.ForProvider.CustomConnectionInput.CustomPhysicalConnectionRequirements.AvailabilityZone,
SubnetId: cr.Spec.ForProvider.CustomConnectionInput.CustomPhysicalConnectionRequirements.SubnetID,
}
for i := range cr.Spec.ForProvider.CustomConnectionInput.CustomPhysicalConnectionRequirements.SecurityGroupIDList {
obj.ConnectionInput.PhysicalConnectionRequirements.SecurityGroupIdList = append(obj.ConnectionInput.PhysicalConnectionRequirements.SecurityGroupIdList, &cr.Spec.ForProvider.CustomConnectionInput.CustomPhysicalConnectionRequirements.SecurityGroupIDList[i])
}
}
}
return nil
}
func (h *hooks) postCreate(ctx context.Context, cr *svcapitypes.Connection, obj *svcsdk.CreateConnectionOutput, _ managed.ExternalCreation, err error) (managed.ExternalCreation, error) {
if err != nil {
return managed.ExternalCreation{}, err
}
// the AWS API does not provide ARNs for Glue Resources
// however an ARN is needed to read and update Tags
// therefore we built the ARN of the Glue Resource from
// the Caller's AccountID directly after creation and
// save it as an annotation for later retrieval
// this also allows the user to manually add or
// change the ARN if we get it wrong
// build ARN and save/add it to the annotations
connectionARN, err := h.buildARN(ctx, cr)
if err != nil {
return managed.ExternalCreation{}, err
}
annotation := map[string]string{
annotationARN: pointer.StringValue(connectionARN),
}
meta.AddAnnotations(cr, annotation)
return managed.ExternalCreation{}, nil
}
// Custom GenerateConnection for isuptodate (the generated one in zz_conversion.go is missing too much)
func customGenerateConnection(resp *svcsdk.GetConnectionOutput) *svcapitypes.Connection {
cr := &svcapitypes.Connection{}
cr.Spec.ForProvider.CustomConnectionInput = &svcapitypes.CustomConnectionInput{}
if resp.Connection.ConnectionProperties != nil {
cr.Spec.ForProvider.CustomConnectionInput.ConnectionProperties = resp.Connection.ConnectionProperties
} else {
cr.Spec.ForProvider.CustomConnectionInput.ConnectionProperties = nil
}
if resp.Connection.ConnectionType != nil {
cr.Spec.ForProvider.CustomConnectionInput.ConnectionType = pointer.StringValue(resp.Connection.ConnectionType)
}
if resp.Connection.Description != nil {
cr.Spec.ForProvider.CustomConnectionInput.Description = resp.Connection.Description
} else {
cr.Spec.ForProvider.CustomConnectionInput.Description = nil
}
if resp.Connection.MatchCriteria != nil {
cr.Spec.ForProvider.CustomConnectionInput.MatchCriteria = resp.Connection.MatchCriteria
} else {
cr.Spec.ForProvider.CustomConnectionInput.MatchCriteria = nil
}
if resp.Connection.PhysicalConnectionRequirements != nil {
cr.Spec.ForProvider.CustomConnectionInput.CustomPhysicalConnectionRequirements = &svcapitypes.CustomPhysicalConnectionRequirements{
AvailabilityZone: resp.Connection.PhysicalConnectionRequirements.AvailabilityZone,
SubnetID: resp.Connection.PhysicalConnectionRequirements.SubnetId,
}
for i := range resp.Connection.PhysicalConnectionRequirements.SecurityGroupIdList {
cr.Spec.ForProvider.CustomConnectionInput.CustomPhysicalConnectionRequirements.SecurityGroupIDList = append(cr.Spec.ForProvider.CustomConnectionInput.CustomPhysicalConnectionRequirements.SecurityGroupIDList, *resp.Connection.PhysicalConnectionRequirements.SecurityGroupIdList[i])
}
} else {
cr.Spec.ForProvider.CustomConnectionInput.CustomPhysicalConnectionRequirements = nil
}
return cr
}
// fromTimePtr probably not needed if metav1 import issue in zz_conversions.go is fixed
// see https://github.com/aws-controllers-k8s/community/issues/1372
// fromTimePtr is a helper for converting a *time.Time to a *metav1.Time
func fromTimePtr(t *time.Time) *metav1.Time {
if t != nil {
m := metav1.NewTime(*t)
return &m
}
return nil
}
// getARN is a helper to retrieve the saved ARN from the annotation
func (h *hooks) getARN(cr *svcapitypes.Connection) (*string, error) {
var arn string
// retrieve
for anno, content := range cr.GetAnnotations() {
if anno == annotationARN {
arn = content
}
}
if !awsarn.IsARN(arn) {
if arn == "" {
return nil, errors.New(errMissingARNAnno)
}
return nil, errors.New(errGetARN)
}
return &arn, nil
}
// buildARN builds the Resource ARN from the Caller AccountID
func (h *hooks) buildARN(ctx context.Context, cr *svcapitypes.Connection) (*string, error) {
var accountID string
// when CatalogID is provided, fetching the CallerID is unneeded
if cr.Spec.ForProvider.CatalogID != nil {
accountID = pointer.StringValue(cr.Spec.ForProvider.CatalogID)
} else {
sess, err := connectaws.GetConfigV1(ctx, h.kube, cr, cr.Spec.ForProvider.Region)
if err != nil {
return nil, errorutils.Wrap(err, errBuildARN)
}
stsclient := svcsdksts.New(sess)
callerID, err := stsclient.GetCallerIdentityWithContext(ctx, &svcsdksts.GetCallerIdentityInput{})
if err != nil {
return nil, err
}
accountID = pointer.StringValue(callerID.Account)
}
connectionARN := ("arn:aws:glue:" +
cr.Spec.ForProvider.Region + ":" +
accountID + ":connection/" +
meta.GetExternalName(cr))
if !awsarn.IsARN(connectionARN) {
return nil, errors.New(errBuildARN)
}
return &connectionARN, nil
}