Skip to content

Commit d2499ab

Browse files
authored
requeue db instance while not available (#28)
After creating a new DBInstance resource, we were not setting ConditionTypeResourceSynced on the resource nor were we asking to requeue the resource after some time. This meant that the resource's Status was not being updated on the Kubernetes side, even after the DB instance's status transitioned to 'available' on the RDS side. In this patch, we're adding two generic hook implementations for sdk_read_many_post_set_output and sdk_create_post_set_output that will create a ConditionTypeResourceSynced on the resource with a value of ConditionFalse (because the resource isn't actually synced yet). This causes the controller to queue the resource up for a new reconciliation loop, during which the resource's Status will be updated to the latest observed values. Fixes Issue aws-controllers-k8s/community#923 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 3060fa3 commit d2499ab

File tree

8 files changed

+76
-4
lines changed

8 files changed

+76
-4
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
ack_generate_info:
2-
build_date: "2021-09-01T12:27:55Z"
2+
build_date: "2021-09-01T14:48:37Z"
33
build_hash: 6f22b7b568e25b4ee007bb1ab5f9338c16daf172
44
go_version: go1.17 linux/amd64
55
version: v0.13.0
6-
api_directory_checksum: 387f19255122f7e68716af4d6dbce0e7498cfe37
6+
api_directory_checksum: 183f450ec5188cdb1cf76da50f21933f6b4cd7e3
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.37.10
99
generator_config_info:
10-
file_checksum: b592c20bd6f321ab727cc08d77bcae382b5c1bd8
10+
file_checksum: f62cefeb4c379bb1cc428d6685a4e5e4e182eb92
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation
14-
timestamp: 2021-09-01 12:27:59.692202323 +0000 UTC
14+
timestamp: 2021-09-01 14:48:42.205143913 +0000 UTC

apis/v1alpha1/generator.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ resources:
122122
path: Parameters
123123
DBInstance:
124124
hooks:
125+
sdk_create_post_set_output:
126+
template_path: hooks/db_instance/sdk_create_post_set_output.go.tpl
127+
sdk_read_many_post_set_output:
128+
template_path: hooks/db_instance/sdk_read_many_post_set_output.go.tpl
125129
sdk_update_pre_build_request:
126130
template_path: hooks/db_instance/sdk_update_pre_build_request.go.tpl
127131
sdk_delete_pre_build_request:

generator.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ resources:
122122
path: Parameters
123123
DBInstance:
124124
hooks:
125+
sdk_create_post_set_output:
126+
template_path: hooks/db_instance/sdk_create_post_set_output.go.tpl
127+
sdk_read_many_post_set_output:
128+
template_path: hooks/db_instance/sdk_read_many_post_set_output.go.tpl
125129
sdk_update_pre_build_request:
126130
template_path: hooks/db_instance/sdk_update_pre_build_request.go.tpl
127131
sdk_delete_pre_build_request:

pkg/resource/db_instance/hooks.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ func instanceHasTerminalStatus(r *resource) bool {
125125
return false
126126
}
127127

128+
// instanceAvailable returns true if the supplied DB instance is in an
129+
// available status
130+
func instanceAvailable(r *resource) bool {
131+
if r.ko.Status.DBInstanceStatus == nil {
132+
return false
133+
}
134+
dbis := *r.ko.Status.DBInstanceStatus
135+
return dbis == StatusAvailable
136+
}
137+
128138
// instanceCreating returns true if the supplied DB instance is in the process
129139
// of being created
130140
func instanceCreating(r *resource) bool {

pkg/resource/db_instance/sdk.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// We expect the DB instance to be in 'creating' status since we just
2+
// issued the call to create it, but I suppose it doesn't hurt to check
3+
// here.
4+
if instanceCreating(&resource{ko}) {
5+
// Setting resource synced condition to false will trigger a requeue of
6+
// the resource. No need to return a requeue error here.
7+
setSyncedCondition(&resource{ko}, corev1.ConditionFalse, nil, nil)
8+
return &resource{ko}, nil
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if !instanceAvailable(&resource{ko}) {
2+
// Setting resource synced condition to false will trigger a requeue of
3+
// the resource. No need to return a requeue error here.
4+
setSyncedCondition(&resource{ko}, corev1.ConditionFalse, nil, nil)
5+
return &resource{ko}, nil
6+
}

test/e2e/tests/test_db_instance.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
CREATE_INTERVAL_SLEEP_SECONDS = 15
4040
# Time to wait before we get to an expected `available` state.
4141
CREATE_TIMEOUT_SECONDS = 600
42+
# Time we wait after resource becoming available in RDS and checking the CR's
43+
# Status has been updated
44+
CHECK_STATUS_WAIT_SECONDS = 10
4245

4346
MODIFY_WAIT_AFTER_SECONDS = 20
4447

@@ -91,6 +94,9 @@ def test_crud_postgres13_t3_micro(
9194
cr = k8s.wait_resource_consumed_by_controller(ref)
9295

9396
assert cr is not None
97+
assert 'status' in cr
98+
assert 'dbInstanceStatus' in cr['status']
99+
assert cr['status']['dbInstanceStatus'] == 'creating'
94100

95101
# Let's check that the DB instance appears in RDS
96102
aws_res = rds_client.describe_db_instances(DBInstanceIdentifier=db_id)
@@ -110,6 +116,22 @@ def test_crud_postgres13_t3_micro(
110116
assert len(aws_res['DBInstances']) == 1
111117
dbi_rec = aws_res['DBInstances'][0]
112118

119+
time.sleep(CHECK_STATUS_WAIT_SECONDS)
120+
121+
# Before we update the DBInstance CR below, let's check to see that the
122+
# DbInstanceStatus field in the CR has been updated to something other
123+
# than 'creating', which is what is set after the initial creation.
124+
# The CR's `Status.DBInstanceStatus` should be updated because the CR
125+
# is requeued on successful reconciliation loops and subsequent
126+
# reconciliation loops call ReadOne and should update the CR's Status
127+
# with the latest observed information.
128+
# https://github.com/aws-controllers-k8s/community/issues/923
129+
cr = k8s.get_resource(ref)
130+
assert cr is not None
131+
assert 'status' in cr
132+
assert 'dbInstanceStatus' in cr['status']
133+
assert cr['status']['dbInstanceStatus'] != 'creating'
134+
113135
# We're now going to modify the CopyTagsToSnapshot field of the DB
114136
# instance, wait some time and verify that the RDS server-side resource
115137
# shows the new value of the field.

0 commit comments

Comments
 (0)