/
custom_set_output.go
361 lines (330 loc) · 11.7 KB
/
custom_set_output.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
352
353
354
355
356
357
358
359
360
361
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 replication_group
import (
"context"
"encoding/json"
"strconv"
svcapitypes "github.com/aws-controllers-k8s/elasticache-controller/apis/v1alpha1"
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
"github.com/aws/aws-sdk-go/service/elasticache"
svcsdk "github.com/aws/aws-sdk-go/service/elasticache"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
// The number of minutes worth of events to retrieve.
// 14 days in minutes
eventsDuration = 20160
)
func (rm *resourceManager) CustomDescribeReplicationGroupsSetOutput(
ctx context.Context,
r *resource,
resp *elasticache.DescribeReplicationGroupsOutput,
ko *svcapitypes.ReplicationGroup,
) (*svcapitypes.ReplicationGroup, error) {
if len(resp.ReplicationGroups) == 0 {
return ko, nil
}
elem := resp.ReplicationGroups[0]
rm.customSetOutput(elem, ko)
err := rm.customSetOutputSupplementAPIs(ctx, r, elem, ko)
if err != nil {
return nil, err
}
return ko, nil
}
func (rm *resourceManager) CustomCreateReplicationGroupSetOutput(
ctx context.Context,
r *resource,
resp *elasticache.CreateReplicationGroupOutput,
ko *svcapitypes.ReplicationGroup,
) (*svcapitypes.ReplicationGroup, error) {
rm.customSetOutput(resp.ReplicationGroup, ko)
rm.setAnnotationsFields(r, ko)
rm.setLastRequestedNodeGroupConfiguration(r, ko)
rm.setLastRequestedNumNodeGroups(r, ko)
return ko, nil
}
func (rm *resourceManager) CustomModifyReplicationGroupSetOutput(
ctx context.Context,
r *resource,
resp *elasticache.ModifyReplicationGroupOutput,
ko *svcapitypes.ReplicationGroup,
) (*svcapitypes.ReplicationGroup, error) {
rm.customSetOutput(resp.ReplicationGroup, ko)
// reset latest.spec.LDC to original value in desired to prevent stale data
// from the modify API being merged back into desired upon spec patching
var logDeliveryConfig []*svcapitypes.LogDeliveryConfigurationRequest
for _, ldc := range r.ko.Spec.LogDeliveryConfigurations {
logDeliveryConfig = append(logDeliveryConfig, ldc.DeepCopy())
}
ko.Spec.LogDeliveryConfigurations = logDeliveryConfig
// Keep the value of desired for CacheNodeType.
ko.Spec.CacheNodeType = r.ko.Spec.CacheNodeType
rm.setAnnotationsFields(r, ko)
return ko, nil
}
func (rm *resourceManager) customSetOutput(
respRG *elasticache.ReplicationGroup,
ko *svcapitypes.ReplicationGroup,
) {
if ko.Status.Conditions == nil {
ko.Status.Conditions = []*ackv1alpha1.Condition{}
}
allNodeGroupsAvailable := true
nodeGroupMembersCount := 0
memberClustersCount := 0
if respRG.NodeGroups != nil {
for _, nodeGroup := range respRG.NodeGroups {
if nodeGroup.Status == nil || *nodeGroup.Status != "available" {
allNodeGroupsAvailable = false
break
}
}
for _, nodeGroup := range respRG.NodeGroups {
if nodeGroup.NodeGroupMembers == nil {
continue
}
nodeGroupMembersCount = nodeGroupMembersCount + len(nodeGroup.NodeGroupMembers)
}
}
if respRG.MemberClusters != nil {
memberClustersCount = len(respRG.MemberClusters)
}
rgStatus := respRG.Status
syncConditionStatus := corev1.ConditionUnknown
if rgStatus != nil {
if (*rgStatus == "available" && allNodeGroupsAvailable && memberClustersCount == nodeGroupMembersCount) ||
*rgStatus == "create-failed" {
syncConditionStatus = corev1.ConditionTrue
} else {
// resource in "creating", "modifying" , "deleting", "snapshotting"
// states is being modified at server end
// thus current status is considered out of sync.
syncConditionStatus = corev1.ConditionFalse
}
}
var resourceSyncedCondition *ackv1alpha1.Condition = nil
for _, condition := range ko.Status.Conditions {
if condition.Type == ackv1alpha1.ConditionTypeResourceSynced {
resourceSyncedCondition = condition
break
}
}
if resourceSyncedCondition == nil {
resourceSyncedCondition = &ackv1alpha1.Condition{
Type: ackv1alpha1.ConditionTypeResourceSynced,
Status: syncConditionStatus,
}
ko.Status.Conditions = append(ko.Status.Conditions, resourceSyncedCondition)
} else {
resourceSyncedCondition.Status = syncConditionStatus
}
if rgStatus != nil && (*rgStatus == "available" || *rgStatus == "snapshotting") {
input, err := rm.newListAllowedNodeTypeModificationsPayLoad(respRG)
if err == nil {
resp, apiErr := rm.sdkapi.ListAllowedNodeTypeModifications(input)
rm.metrics.RecordAPICall("READ_MANY", "ListAllowedNodeTypeModifications", apiErr)
// Overwrite the values for ScaleUp and ScaleDown
if apiErr == nil {
ko.Status.AllowedScaleDownModifications = resp.ScaleDownModifications
ko.Status.AllowedScaleUpModifications = resp.ScaleUpModifications
}
}
} else {
ko.Status.AllowedScaleDownModifications = nil
ko.Status.AllowedScaleUpModifications = nil
}
// populate status logDeliveryConfigurations struct
if respRG.LogDeliveryConfigurations != nil {
var f11 []*svcapitypes.LogDeliveryConfiguration
for _, f11iter := range respRG.LogDeliveryConfigurations {
f11elem := &svcapitypes.LogDeliveryConfiguration{}
if f11iter.DestinationDetails != nil {
f11elemf0 := &svcapitypes.DestinationDetails{}
if f11iter.DestinationDetails.CloudWatchLogsDetails != nil {
f11elemf0f0 := &svcapitypes.CloudWatchLogsDestinationDetails{}
if f11iter.DestinationDetails.CloudWatchLogsDetails.LogGroup != nil {
f11elemf0f0.LogGroup = f11iter.DestinationDetails.CloudWatchLogsDetails.LogGroup
}
f11elemf0.CloudWatchLogsDetails = f11elemf0f0
}
if f11iter.DestinationDetails.KinesisFirehoseDetails != nil {
f11elemf0f1 := &svcapitypes.KinesisFirehoseDestinationDetails{}
if f11iter.DestinationDetails.KinesisFirehoseDetails.DeliveryStream != nil {
f11elemf0f1.DeliveryStream = f11iter.DestinationDetails.KinesisFirehoseDetails.DeliveryStream
}
f11elemf0.KinesisFirehoseDetails = f11elemf0f1
}
f11elem.DestinationDetails = f11elemf0
}
if f11iter.DestinationType != nil {
f11elem.DestinationType = f11iter.DestinationType
}
if f11iter.LogFormat != nil {
f11elem.LogFormat = f11iter.LogFormat
}
if f11iter.LogType != nil {
f11elem.LogType = f11iter.LogType
}
if f11iter.Status != nil {
f11elem.Status = f11iter.Status
}
if f11iter.Message != nil {
f11elem.Message = f11iter.Message
}
f11 = append(f11, f11elem)
}
ko.Status.LogDeliveryConfigurations = f11
} else {
ko.Status.LogDeliveryConfigurations = nil
}
}
// newListAllowedNodeTypeModificationsPayLoad returns an SDK-specific struct for the HTTP request
// payload of the ListAllowedNodeTypeModifications API call.
func (rm *resourceManager) newListAllowedNodeTypeModificationsPayLoad(respRG *elasticache.ReplicationGroup) (
*svcsdk.ListAllowedNodeTypeModificationsInput, error) {
res := &svcsdk.ListAllowedNodeTypeModificationsInput{}
if respRG.ReplicationGroupId != nil {
res.SetReplicationGroupId(*respRG.ReplicationGroupId)
}
return res, nil
}
func (rm *resourceManager) customSetOutputSupplementAPIs(
ctx context.Context,
r *resource,
respRG *elasticache.ReplicationGroup,
ko *svcapitypes.ReplicationGroup,
) error {
events, err := rm.provideEvents(ctx, r.ko.Spec.ReplicationGroupID, 20)
if err != nil {
return err
}
ko.Status.Events = events
return nil
}
func (rm *resourceManager) provideEvents(
ctx context.Context,
replicationGroupId *string,
maxRecords int64,
) ([]*svcapitypes.Event, error) {
input := &elasticache.DescribeEventsInput{}
input.SetSourceType("replication-group")
input.SetSourceIdentifier(*replicationGroupId)
input.SetMaxRecords(maxRecords)
input.SetDuration(eventsDuration)
resp, err := rm.sdkapi.DescribeEventsWithContext(ctx, input)
rm.metrics.RecordAPICall("READ_MANY", "DescribeEvents-ReplicationGroup", err)
if err != nil {
rm.log.V(1).Info("Error during DescribeEvents-ReplicationGroup", "error", err)
return nil, err
}
events := []*svcapitypes.Event{}
if resp.Events != nil {
for _, respEvent := range resp.Events {
event := &svcapitypes.Event{}
if respEvent.Message != nil {
event.Message = respEvent.Message
}
if respEvent.Date != nil {
eventDate := metav1.NewTime(*respEvent.Date)
event.Date = &eventDate
}
// Not copying redundant source id (replication id)
// and source type (replication group)
// into each event object
events = append(events, event)
}
}
return events, nil
}
// setAnnotationsFields copies the desired object's annotations, populates any
// relevant fields, and sets the latest object's annotations to this newly populated map.
// Fields that are handled by custom modify implementation are not set here.
// This should only be called upon a successful create or modify call.
func (rm *resourceManager) setAnnotationsFields(
r *resource,
ko *svcapitypes.ReplicationGroup,
) {
annotations := getAnnotationsFields(r, ko)
rm.setLastRequestedLogDeliveryConfigurations(r, annotations)
rm.setLastRequestedCacheNodeType(r, annotations)
ko.ObjectMeta.Annotations = annotations
}
// getAnnotationsFields return the annotations map that would be used to set the fields
func getAnnotationsFields(
r *resource,
ko *svcapitypes.ReplicationGroup) map[string]string {
if ko.ObjectMeta.Annotations != nil {
return ko.ObjectMeta.Annotations
}
desiredAnnotations := r.ko.ObjectMeta.GetAnnotations()
annotations := make(map[string]string)
for k, v := range desiredAnnotations {
annotations[k] = v
}
ko.ObjectMeta.Annotations = annotations
return annotations
}
// setLastRequestedLogDeliveryConfigurations copies desired.Spec.LogDeliveryConfigurations
// into the annotations of the object.
// r is the desired resource, and annotations is the annotations map modified by this method
func (rm *resourceManager) setLastRequestedLogDeliveryConfigurations(
r *resource,
annotations map[string]string,
) {
lastRequestedConfigs, err := json.Marshal(r.ko.Spec.LogDeliveryConfigurations)
if err != nil {
annotations[AnnotationLastRequestedLDCs] = "null"
} else {
annotations[AnnotationLastRequestedLDCs] = string(lastRequestedConfigs)
}
}
// setLastRequestedCacheNodeType copies desired.Spec.CacheNodeType into the annotation
// of the object.
func (rm *resourceManager) setLastRequestedCacheNodeType(
r *resource,
annotations map[string]string,
) {
if r.ko.Spec.CacheNodeType != nil {
annotations[AnnotationLastRequestedCNT] = *r.ko.Spec.CacheNodeType
}
}
// setLastRequestedNodeGroupConfiguration copies desired.spec.NodeGroupConfiguration into the
// annotation of the object
func (rm *resourceManager) setLastRequestedNodeGroupConfiguration(
r *resource,
ko *svcapitypes.ReplicationGroup,
) {
annotations := getAnnotationsFields(r, ko)
lastRequestedConfigs, err := json.Marshal(r.ko.Spec.NodeGroupConfiguration)
if err != nil {
annotations[AnnotationLastRequestedNGC] = "null"
} else {
annotations[AnnotationLastRequestedNGC] = string(lastRequestedConfigs)
}
}
// setLastRequestedNumNodeGroups copies desired.spec.NumNodeGroups into the
// annotation of the object
func (rm *resourceManager) setLastRequestedNumNodeGroups(
r *resource,
ko *svcapitypes.ReplicationGroup,
) {
annotations := getAnnotationsFields(r, ko)
if r.ko.Spec.NumNodeGroups != nil {
annotations[AnnotationLastRequestedNNG] = strconv.Itoa(int(*r.ko.Spec.NumNodeGroups))
} else {
annotations[AnnotationLastRequestedNNG] = "null"
}
}