Skip to content

Commit 7e210d2

Browse files
committed
fixes to log delivery modification
1 parent b7f1724 commit 7e210d2

File tree

12 files changed

+204
-38
lines changed

12 files changed

+204
-38
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2021-10-12T16:29:01Z"
3-
build_hash: 4b30ff5578e2f570d1c5b1741f3098be0d78e246
4-
go_version: go1.16.5
2+
build_date: "2021-10-14T18:44:40Z"
3+
build_hash: 385779a205bea50e8762b76bc75cab957cf723b9
4+
go_version: go1.15.2
55
version: v0.15.1
6-
api_directory_checksum: 69c917d1e1ee2b196c23799a02417b920297bc74
6+
api_directory_checksum: 8e4808b17b3a814f0a624eee8d68a0b45ba5e2c4
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.38.52
99
generator_config_info:
10-
file_checksum: 261e03a6b9fb25805b045293947f13e5674d2924
10+
file_checksum: 28e3cd3119df8028a65eb0729a462b4354d27fa9
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/annotations.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package v1alpha1
15+
16+
const (
17+
// AnnotationPrefix is the prefix for all annotations specifically for
18+
// the elasticache service.
19+
AnnotationPrefix = "elasticache.services.k8s.aws/"
20+
)

apis/v1alpha1/generator.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ resources:
5656
AuthToken:
5757
is_secret: true
5858
LogDeliveryConfigurations:
59-
is_read_only: true
59+
is_read_only: true # creates an additional status field of the same name
6060
from:
6161
operation: CreateReplicationGroup
6262
path: ReplicationGroup.LogDeliveryConfigurations
63+
compare: # removes the spec field from automatic delta comparison
64+
is_ignored: true
6365
hooks:
6466
sdk_read_many_post_set_output:
6567
template_path: hooks/replication_group/sdk_read_many_post_set_output.go.tpl

generator.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ resources:
5656
AuthToken:
5757
is_secret: true
5858
LogDeliveryConfigurations:
59-
is_read_only: true
59+
is_read_only: true # creates an additional status field of the same name
6060
from:
6161
operation: CreateReplicationGroup
6262
path: ReplicationGroup.LogDeliveryConfigurations
63+
compare: # removes the spec field from automatic delta comparison
64+
is_ignored: true
6365
hooks:
6466
sdk_read_many_post_set_output:
6567
template_path: hooks/replication_group/sdk_read_many_post_set_output.go.tpl

helm/templates/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ spec:
6161
valueFrom:
6262
fieldRef:
6363
fieldPath: metadata.namespace
64+
- name: AWS_ACCOUNT_ID
65+
value: {{ .Values.aws.account_id | quote }}
6466
- name: AWS_REGION
6567
value: {{ .Values.aws.region }}
6668
- name: AWS_ENDPOINT_URL

helm/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ resources:
3838
aws:
3939
# If specified, use the AWS region for AWS API calls
4040
region: ""
41+
account_id: ""
4142
endpoint_url: ""
4243

4344
# log level for the controller
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package replication_group
15+
16+
import (
17+
svcapitypes "github.com/aws-controllers-k8s/elasticache-controller/apis/v1alpha1"
18+
)
19+
20+
const (
21+
// AnnotationLastRequestedLDCs is an annotation whose value is the marshaled list of pointers to
22+
// LogDeliveryConfigurationRequest structs passed in as input to either the create or modify API called most
23+
// recently
24+
AnnotationLastRequestedLDCs = svcapitypes.AnnotationPrefix + "last-requested-log-delivery-configurations"
25+
)

pkg/resource/replication_group/custom_set_output.go

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ package replication_group
1515

1616
import (
1717
"context"
18+
"encoding/json"
19+
1820
svcapitypes "github.com/aws-controllers-k8s/elasticache-controller/apis/v1alpha1"
1921
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
2022
"github.com/aws/aws-sdk-go/service/elasticache"
@@ -54,6 +56,7 @@ func (rm *resourceManager) CustomCreateReplicationGroupSetOutput(
5456
ko *svcapitypes.ReplicationGroup,
5557
) (*svcapitypes.ReplicationGroup, error) {
5658
rm.customSetOutput(resp.ReplicationGroup, ko)
59+
rm.setAnnotationsFields(r, ko)
5760
return ko, nil
5861
}
5962

@@ -64,6 +67,16 @@ func (rm *resourceManager) CustomModifyReplicationGroupSetOutput(
6467
ko *svcapitypes.ReplicationGroup,
6568
) (*svcapitypes.ReplicationGroup, error) {
6669
rm.customSetOutput(resp.ReplicationGroup, ko)
70+
71+
// reset latest.spec.LDC to original value in desired to prevent stale data
72+
// from the modify API being merged back into desired upon spec patching
73+
var logDeliveryConfig []*svcapitypes.LogDeliveryConfigurationRequest
74+
for _, ldc := range r.ko.Spec.LogDeliveryConfigurations {
75+
logDeliveryConfig = append(logDeliveryConfig, ldc.DeepCopy())
76+
}
77+
ko.Spec.LogDeliveryConfigurations = logDeliveryConfig
78+
79+
rm.setAnnotationsFields(r, ko)
6780
return ko, nil
6881
}
6982

@@ -142,31 +155,8 @@ func (rm *resourceManager) customSetOutput(
142155
ko.Status.AllowedScaleDownModifications = nil
143156
ko.Status.AllowedScaleUpModifications = nil
144157
}
145-
}
146-
147-
// newListAllowedNodeTypeModificationsPayLoad returns an SDK-specific struct for the HTTP request
148-
// payload of the ListAllowedNodeTypeModifications API call.
149-
func (rm *resourceManager) newListAllowedNodeTypeModificationsPayLoad(respRG *elasticache.ReplicationGroup) (
150-
*svcsdk.ListAllowedNodeTypeModificationsInput, error) {
151-
res := &svcsdk.ListAllowedNodeTypeModificationsInput{}
152-
153-
if respRG.ReplicationGroupId != nil {
154-
res.SetReplicationGroupId(*respRG.ReplicationGroupId)
155-
}
156-
157-
return res, nil
158-
}
159158

160-
func (rm *resourceManager) customSetOutputSupplementAPIs(
161-
ctx context.Context,
162-
r *resource,
163-
respRG *elasticache.ReplicationGroup,
164-
ko *svcapitypes.ReplicationGroup,
165-
) error {
166-
events, err := rm.provideEvents(ctx, r.ko.Spec.ReplicationGroupID, 20)
167-
if err != nil {
168-
return err
169-
}
159+
// populate status logDeliveryConfigurations struct
170160
if respRG.LogDeliveryConfigurations != nil {
171161
var f11 []*svcapitypes.LogDeliveryConfiguration
172162
for _, f11iter := range respRG.LogDeliveryConfigurations {
@@ -210,6 +200,31 @@ func (rm *resourceManager) customSetOutputSupplementAPIs(
210200
} else {
211201
ko.Status.LogDeliveryConfigurations = nil
212202
}
203+
}
204+
205+
// newListAllowedNodeTypeModificationsPayLoad returns an SDK-specific struct for the HTTP request
206+
// payload of the ListAllowedNodeTypeModifications API call.
207+
func (rm *resourceManager) newListAllowedNodeTypeModificationsPayLoad(respRG *elasticache.ReplicationGroup) (
208+
*svcsdk.ListAllowedNodeTypeModificationsInput, error) {
209+
res := &svcsdk.ListAllowedNodeTypeModificationsInput{}
210+
211+
if respRG.ReplicationGroupId != nil {
212+
res.SetReplicationGroupId(*respRG.ReplicationGroupId)
213+
}
214+
215+
return res, nil
216+
}
217+
218+
func (rm *resourceManager) customSetOutputSupplementAPIs(
219+
ctx context.Context,
220+
r *resource,
221+
respRG *elasticache.ReplicationGroup,
222+
ko *svcapitypes.ReplicationGroup,
223+
) error {
224+
events, err := rm.provideEvents(ctx, r.ko.Spec.ReplicationGroupID, 20)
225+
if err != nil {
226+
return err
227+
}
213228
ko.Status.Events = events
214229
return nil
215230
}
@@ -249,3 +264,36 @@ func (rm *resourceManager) provideEvents(
249264
}
250265
return events, nil
251266
}
267+
268+
// setAnnotationsFields copies the desired object's annotations, populates any
269+
// relevant fields, and sets the latest object's annotations to this newly populated map.
270+
// This should only be called upon a successful create or modify call.
271+
func (rm *resourceManager) setAnnotationsFields(
272+
r *resource,
273+
ko *svcapitypes.ReplicationGroup,
274+
) {
275+
desiredAnnotations := r.ko.ObjectMeta.GetAnnotations()
276+
annotations := make(map[string]string)
277+
for k, v := range desiredAnnotations {
278+
annotations[k] = v
279+
}
280+
281+
rm.setLastRequestedLogDeliveryConfigurations(r, annotations)
282+
283+
ko.ObjectMeta.Annotations = annotations
284+
}
285+
286+
// setLastRequestedLogDeliveryConfigurations copies desired.Spec.LogDeliveryConfigurations
287+
// into the annotations of the object.
288+
// r is the desired resource, and annotations is the annotations map modified by this method
289+
func (rm *resourceManager) setLastRequestedLogDeliveryConfigurations(
290+
r *resource,
291+
annotations map[string]string,
292+
) {
293+
lastRequestedConfigs, err := json.Marshal(r.ko.Spec.LogDeliveryConfigurations)
294+
if err != nil {
295+
annotations[AnnotationLastRequestedLDCs] = "null"
296+
} else {
297+
annotations[AnnotationLastRequestedLDCs] = string(lastRequestedConfigs)
298+
}
299+
}

pkg/resource/replication_group/delta.go

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/resource/replication_group/delta_util.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
package replication_group
1515

1616
import (
17+
"encoding/json"
18+
"reflect"
1719
"regexp"
1820
"strings"
1921

2022
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
2123

24+
svcapitypes "github.com/aws-controllers-k8s/elasticache-controller/apis/v1alpha1"
2225
"github.com/aws-controllers-k8s/elasticache-controller/pkg/common"
2326
)
2427

25-
// remove non-meaningful differences from delta
28+
// filterDelta removes non-meaningful differences from the delta and adds additional differences if necessary
2629
func filterDelta(
2730
delta *ackcompare.Delta,
2831
desired *resource,
@@ -37,6 +40,20 @@ func filterDelta(
3740
}
3841
// TODO: handle the case of a nil difference (especially when desired EV is nil)
3942
}
43+
44+
// if server has given PreferredMaintenanceWindow a default value, no action needs to be taken
45+
if delta.DifferentAt("Spec.PreferredMaintenanceWindow") {
46+
if desired.ko.Spec.PreferredMaintenanceWindow == nil && latest.ko.Spec.PreferredMaintenanceWindow != nil {
47+
common.RemoveFromDelta(delta, "Spec.PreferredMaintenanceWindow")
48+
}
49+
}
50+
51+
// note that the comparison is actually done between desired.Spec.LogDeliveryConfigurations and
52+
// the last requested configurations saved in annotations (as opposed to latest.Spec.LogDeliveryConfigurations)
53+
if logDeliveryRequiresUpdate(desired) {
54+
delta.Add("Spec.LogDeliveryConfigurations", desired.ko.Spec.LogDeliveryConfigurations,
55+
unmarshalLastRequestedLDCs(desired))
56+
}
4057
}
4158

4259
// returns true if desired and latest engine versions match and false otherwise
@@ -62,3 +79,24 @@ func engineVersionsMatch(
6279

6380
return false
6481
}
82+
83+
// logDeliveryRequiresUpdate retrieves the last requested configurations saved in annotations and compares them
84+
// to the current desired configurations
85+
func logDeliveryRequiresUpdate(desired *resource) bool {
86+
desiredConfigs := desired.ko.Spec.LogDeliveryConfigurations
87+
lastRequestedConfigs := unmarshalLastRequestedLDCs(desired)
88+
return !reflect.DeepEqual(desiredConfigs, lastRequestedConfigs)
89+
}
90+
91+
// unmarshal the value found in annotations for the LogDeliveryConfigurations field requested in the last
92+
// successful create or modify call
93+
func unmarshalLastRequestedLDCs(desired *resource) []*svcapitypes.LogDeliveryConfigurationRequest {
94+
var lastRequestedConfigs []*svcapitypes.LogDeliveryConfigurationRequest
95+
96+
annotations := desired.ko.ObjectMeta.GetAnnotations()
97+
if val, ok := annotations[AnnotationLastRequestedLDCs]; ok {
98+
_ = json.Unmarshal([]byte(val), &lastRequestedConfigs)
99+
}
100+
101+
return lastRequestedConfigs
102+
}

0 commit comments

Comments
 (0)