-
Notifications
You must be signed in to change notification settings - Fork 363
/
setup.go
120 lines (94 loc) · 4.24 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
package functionurlconfig
import (
"context"
svcsdk "github.com/aws/aws-sdk-go/service/lambda"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
ctrl "sigs.k8s.io/controller-runtime"
"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/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
svcapitypes "github.com/crossplane-contrib/provider-aws/apis/lambda/v1alpha1"
"github.com/crossplane-contrib/provider-aws/apis/v1alpha1"
"github.com/crossplane-contrib/provider-aws/pkg/features"
aws "github.com/crossplane-contrib/provider-aws/pkg/clients"
)
// SetupFunctionURL adds a controller that reconciles FunctionURLConfig.
func SetupFunctionURL(mgr ctrl.Manager, o controller.Options) error {
name := managed.ControllerName(svcapitypes.FunctionURLConfigGroupKind)
opts := []option{
func(e *external) {
e.preObserve = preObserve
e.preCreate = preCreate
e.preUpdate = preUpdate
e.preDelete = preDelete
e.postObserve = postObserve
e.isUpToDate = isUpToDate
},
}
cps := []managed.ConnectionPublisher{managed.NewAPISecretPublisher(mgr.GetClient(), mgr.GetScheme())}
if o.Features.Enabled(features.EnableAlphaExternalSecretStores) {
cps = append(cps, connection.NewDetailsManager(mgr.GetClient(), v1alpha1.StoreConfigGroupVersionKind))
}
return ctrl.NewControllerManagedBy(mgr).
Named(name).
WithOptions(o.ForControllerRuntime()).
For(&svcapitypes.FunctionURLConfig{}).
Complete(managed.NewReconciler(mgr,
resource.ManagedKind(svcapitypes.FunctionURLConfigGroupVersionKind),
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...)))
}
func preObserve(_ context.Context, cr *svcapitypes.FunctionURLConfig, obj *svcsdk.GetFunctionUrlConfigInput) error {
obj.FunctionName = cr.Spec.ForProvider.FunctionName
return nil
}
func preCreate(_ context.Context, cr *svcapitypes.FunctionURLConfig, obj *svcsdk.CreateFunctionUrlConfigInput) error {
obj.FunctionName = cr.Spec.ForProvider.FunctionName
return nil
}
func preUpdate(_ context.Context, cr *svcapitypes.FunctionURLConfig, obj *svcsdk.UpdateFunctionUrlConfigInput) error {
obj.FunctionName = cr.Spec.ForProvider.FunctionName
return nil
}
func preDelete(_ context.Context, cr *svcapitypes.FunctionURLConfig, obj *svcsdk.DeleteFunctionUrlConfigInput) (bool, error) {
obj.FunctionName = cr.Spec.ForProvider.FunctionName
return false, nil
}
func postObserve(_ context.Context, cr *svcapitypes.FunctionURLConfig, _ *svcsdk.GetFunctionUrlConfigOutput, obs managed.ExternalObservation, err error) (managed.ExternalObservation, error) {
if err != nil {
return managed.ExternalObservation{}, err
}
cr.SetConditions(xpv1.Available())
return obs, nil
}
func isUpToDate(cr *svcapitypes.FunctionURLConfig, obj *svcsdk.GetFunctionUrlConfigOutput) (bool, error) {
if aws.StringValue(cr.Spec.ForProvider.AuthType) != aws.StringValue(obj.AuthType) {
return false, nil
}
return isUpToDateCors(cr, obj), nil
}
func isUpToDateCors(cr *svcapitypes.FunctionURLConfig, obj *svcsdk.GetFunctionUrlConfigOutput) bool {
sortCmp := cmpopts.SortSlices(func(x, y *string) bool {
return *x < *y
})
switch {
case cr.Spec.ForProvider.CORS == nil && obj.Cors == nil, cr.Spec.ForProvider.CORS == nil:
return true
case obj.Cors == nil,
aws.BoolValue(cr.Spec.ForProvider.CORS.AllowCredentials) != aws.BoolValue(obj.Cors.AllowCredentials),
!cmp.Equal(&cr.Spec.ForProvider.CORS.AllowHeaders, &obj.Cors.AllowHeaders, sortCmp),
!cmp.Equal(&cr.Spec.ForProvider.CORS.AllowMethods, &obj.Cors.AllowMethods, sortCmp),
!cmp.Equal(&cr.Spec.ForProvider.CORS.AllowOrigins, &obj.Cors.AllowOrigins, sortCmp),
!cmp.Equal(&cr.Spec.ForProvider.CORS.ExposeHeaders, &obj.Cors.ExposeHeaders, sortCmp),
aws.Int64Value(cr.Spec.ForProvider.CORS.MaxAge) != aws.Int64Value(obj.Cors.MaxAge):
return false
}
return true
}