/
setup.go
204 lines (182 loc) · 8.11 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
package dbclusterparametergroup
import (
"context"
"github.com/pkg/errors"
svcsdk "github.com/aws/aws-sdk-go/service/rds"
svcsdkapi "github.com/aws/aws-sdk-go/service/rds/rdsiface"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
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"
svcapitypes "github.com/crossplane/provider-aws/apis/rds/v1alpha1"
"github.com/crossplane/provider-aws/apis/v1alpha1"
awsclients "github.com/crossplane/provider-aws/pkg/clients"
"github.com/crossplane/provider-aws/pkg/features"
)
const (
errRequireDBParameterGroupFamilyOrFromEngine = "either spec.forProvider.dbParameterGroupFamily or spec.forProvider.dbParameterGroupFamilyFromEngine is required"
errDetermineDBParameterGroupFamily = "cannot determine DB parametergroup family"
errGetDBEngineVersion = "cannot decsribe DB engine versions"
errNoDBEngineVersions = "no DB engine versions returned by AWS"
)
// SetupDBClusterParameterGroup adds a controller that reconciles DBClusterParameterGroup.
func SetupDBClusterParameterGroup(mgr ctrl.Manager, o controller.Options) error {
name := managed.ControllerName(svcapitypes.DBClusterParameterGroupGroupKind)
opts := []option{
func(e *external) {
c := &custom{client: e.client, kube: e.kube}
e.preCreate = c.preCreate
e.preObserve = preObserve
e.preUpdate = preUpdate
e.preDelete = preDelete
e.postObserve = postObserve
e.lateInitialize = lateInitialize
e.isUpToDate = c.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.DBClusterParameterGroup{}).
Complete(managed.NewReconciler(mgr,
resource.ManagedKind(svcapitypes.DBClusterParameterGroupGroupVersionKind),
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...)))
}
type custom struct {
kube client.Client
client svcsdkapi.RDSAPI
}
func preObserve(_ context.Context, cr *svcapitypes.DBClusterParameterGroup, obj *svcsdk.DescribeDBClusterParameterGroupsInput) error {
obj.DBClusterParameterGroupName = awsclients.String(meta.GetExternalName(cr))
return nil
}
func postObserve(_ context.Context, cr *svcapitypes.DBClusterParameterGroup, _ *svcsdk.DescribeDBClusterParameterGroupsOutput, obs managed.ExternalObservation, err error) (managed.ExternalObservation, error) {
if err != nil {
return managed.ExternalObservation{}, err
}
cr.SetConditions(xpv1.Available())
return obs, err
}
func (c *custom) preCreate(ctx context.Context, cr *svcapitypes.DBClusterParameterGroup, obj *svcsdk.CreateDBClusterParameterGroupInput) error {
if err := c.ensureParameterGroupFamily(ctx, cr); err != nil {
return errors.Wrap(err, errDetermineDBParameterGroupFamily)
}
obj.DBClusterParameterGroupName = awsclients.String(meta.GetExternalName(cr))
obj.DBParameterGroupFamily = cr.Spec.ForProvider.DBParameterGroupFamily
return nil
}
func preUpdate(_ context.Context, cr *svcapitypes.DBClusterParameterGroup, obj *svcsdk.ModifyDBClusterParameterGroupInput) error {
obj.DBClusterParameterGroupName = awsclients.String(meta.GetExternalName(cr))
obj.Parameters = make([]*svcsdk.Parameter, len(cr.Spec.ForProvider.Parameters))
for i, v := range cr.Spec.ForProvider.Parameters {
// check if mandatory parameters are set (ApplyMethod, ParameterName, ParameterValue)
if (v.ApplyMethod == nil) || (v.ParameterName == nil) || (v.ParameterValue == nil) {
return errors.New("ApplyMethod, ParameterName and ParameterValue are mandatory fields and can not be nil")
}
obj.Parameters[i] = &svcsdk.Parameter{
AllowedValues: v.AllowedValues,
ApplyMethod: v.ApplyMethod,
ApplyType: v.ApplyType,
DataType: v.DataType,
Description: v.Description,
IsModifiable: v.IsModifiable,
MinimumEngineVersion: v.MinimumEngineVersion,
ParameterName: v.ParameterName,
ParameterValue: v.ParameterValue,
Source: v.Source,
SupportedEngineModes: v.SupportedEngineModes,
}
}
return nil
}
func preDelete(_ context.Context, cr *svcapitypes.DBClusterParameterGroup, obj *svcsdk.DeleteDBClusterParameterGroupInput) (bool, error) {
obj.DBClusterParameterGroupName = awsclients.String(meta.GetExternalName(cr))
return false, nil
}
func lateInitialize(spec *svcapitypes.DBClusterParameterGroupParameters, current *svcsdk.DescribeDBClusterParameterGroupsOutput) error {
// Len > 0 is ensured by the generated controller.
obj := current.DBClusterParameterGroups[0]
spec.DBParameterGroupFamily = obj.DBParameterGroupFamily
return nil
}
func (c *custom) isUpToDate(cr *svcapitypes.DBClusterParameterGroup, _ *svcsdk.DescribeDBClusterParameterGroupsOutput) (bool, error) {
// TODO(armsnyder): We need isUpToDate to have context.
ctx := context.TODO()
results, err := c.getCurrentDBClusterParameters(ctx, cr)
if err != nil {
return false, err
}
observed := make(map[string]svcsdk.Parameter, len(results))
for _, p := range results {
observed[awsclients.StringValue(p.ParameterName)] = *p
}
// compare CR with currently set Parameters
for _, v := range cr.Spec.ForProvider.Parameters {
existing, ok := observed[awsclients.StringValue(v.ParameterName)]
if !ok {
return false, nil
}
switch {
case awsclients.StringValue(existing.ParameterValue) != awsclients.StringValue(v.ParameterValue):
return false, nil
case awsclients.StringValue(existing.ApplyMethod) != awsclients.StringValue(v.ApplyMethod):
return false, nil
}
}
return true, err
}
func (c *custom) getCurrentDBClusterParameters(ctx context.Context, cr *svcapitypes.DBClusterParameterGroup) ([]*svcsdk.Parameter, error) {
input := &svcsdk.DescribeDBClusterParametersInput{
DBClusterParameterGroupName: awsclients.String(meta.GetExternalName(cr)),
MaxRecords: awsclients.Int64(100),
}
var results []*svcsdk.Parameter
err := c.client.DescribeDBClusterParametersPagesWithContext(ctx, input, func(page *svcsdk.DescribeDBClusterParametersOutput, lastPage bool) bool {
results = append(results, page.Parameters...)
return !lastPage
})
if err != nil {
return results, err
}
return results, nil
}
func (c *custom) ensureParameterGroupFamily(ctx context.Context, cr *svcapitypes.DBClusterParameterGroup) error {
if cr.Spec.ForProvider.DBParameterGroupFamily == nil {
engineVersion, err := c.getDBEngineVersion(ctx, cr.Spec.ForProvider.DBParameterGroupFamilySelector)
if err != nil {
return errors.Wrap(err, errGetDBEngineVersion)
}
cr.Spec.ForProvider.DBParameterGroupFamily = engineVersion.DBParameterGroupFamily
}
return nil
}
func (c *custom) getDBEngineVersion(ctx context.Context, selector *svcapitypes.DBParameterGroupFamilyNameSelector) (*svcsdk.DBEngineVersion, error) {
if selector == nil {
return nil, errors.New(errRequireDBParameterGroupFamilyOrFromEngine)
}
resp, err := c.client.DescribeDBEngineVersionsWithContext(ctx, &svcsdk.DescribeDBEngineVersionsInput{
Engine: &selector.Engine,
EngineVersion: selector.EngineVersion,
DefaultOnly: awsclients.Bool(selector.EngineVersion == nil),
})
if err != nil {
return nil, err
}
if resp.DBEngineVersions == nil || len(resp.DBEngineVersions) == 0 || resp.DBEngineVersions[0] == nil {
return nil, errors.New(errNoDBEngineVersions)
}
return resp.DBEngineVersions[0], nil
}