Skip to content

Commit 8eb6934

Browse files
authored
Use the CreationTime to update LastModifiedTime (#49)
Issue #, if available: Fixing [comment](#40 (review)) on previous PR #40 Description of changes: - Use the CreationTime to update LastModifiedTime - Add associated test to check LastModifiedTime By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent a7caf16 commit 8eb6934

File tree

9 files changed

+107
-41
lines changed

9 files changed

+107
-41
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2021-09-16T00:00:15Z"
2+
build_date: "2021-09-16T00:51:44Z"
33
build_hash: 29a44ff03ae7d871faa8603c6ee9885cab5abd92
44
go_version: go1.14.14
55
version: v0.14.0
66
api_directory_checksum: a13caf20935ebb6193efdee1ab377cae33311ad7
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.37.10
99
generator_config_info:
10-
file_checksum: b9cb2cfa6c027466a00a98d2031047d9edaa648a
10+
file_checksum: baa788a6de90973eba7fbee7ee1f146c803186f8
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ operations:
1818
resources:
1919
ScalableTarget:
2020
hooks:
21-
sdk_create_post_set_output:
22-
code: rm.customSetLastModifiedTime(ko)
21+
sdk_read_many_post_set_output:
22+
code: rm.customSetLastModifiedTimeToCreationTime(ko)
2323
sdk_read_many_post_build_request:
2424
code: rm.customDescribeScalableTarget(ctx, r, input)
2525
delta_pre_compare:
2626
code: customSetDefaults(a, b)
2727
sdk_update_post_set_output:
28-
code: rm.customSetLastModifiedTime(ko)
28+
code: rm.customSetLastModifiedTimeToCurrentTime(ko)
2929
fields:
3030
ResourceID:
3131
is_primary_key: true
@@ -41,10 +41,10 @@ resources:
4141
path: ScalableTargets..CreationTime
4242
ScalingPolicy:
4343
hooks:
44-
sdk_create_post_set_output:
45-
code: rm.customSetLastModifiedTime(ko)
44+
sdk_read_many_post_set_output:
45+
code: rm.customSetLastModifiedTimeToCreationTime(ko)
4646
sdk_update_post_set_output:
47-
code: rm.customSetLastModifiedTime(ko)
47+
code: rm.customSetLastModifiedTimeToCurrentTime(ko)
4848
fields:
4949
ResourceID:
5050
is_primary_key: true

generator.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ operations:
1818
resources:
1919
ScalableTarget:
2020
hooks:
21-
sdk_create_post_set_output:
22-
code: rm.customSetLastModifiedTime(ko)
21+
sdk_read_many_post_set_output:
22+
code: rm.customSetLastModifiedTimeToCreationTime(ko)
2323
sdk_read_many_post_build_request:
2424
code: rm.customDescribeScalableTarget(ctx, r, input)
2525
delta_pre_compare:
2626
code: customSetDefaults(a, b)
2727
sdk_update_post_set_output:
28-
code: rm.customSetLastModifiedTime(ko)
28+
code: rm.customSetLastModifiedTimeToCurrentTime(ko)
2929
fields:
3030
ResourceID:
3131
is_primary_key: true
@@ -41,10 +41,10 @@ resources:
4141
path: ScalableTargets..CreationTime
4242
ScalingPolicy:
4343
hooks:
44-
sdk_create_post_set_output:
45-
code: rm.customSetLastModifiedTime(ko)
44+
sdk_read_many_post_set_output:
45+
code: rm.customSetLastModifiedTimeToCreationTime(ko)
4646
sdk_update_post_set_output:
47-
code: rm.customSetLastModifiedTime(ko)
47+
code: rm.customSetLastModifiedTimeToCurrentTime(ko)
4848
fields:
4949
ResourceID:
5050
is_primary_key: true

pkg/resource/scalable_target/custom_api.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ func (rm *resourceManager) customDescribeScalableTarget(
3535
}
3636
}
3737

38-
// customSetOutputUpdate sets the LastModifiedTime field to the current time post an update
39-
func (rm *resourceManager) customSetLastModifiedTime(ko *svcapitypes.ScalableTarget) {
38+
// customSetLastModifiedTimeToCreationTime sets the LastModifiedTime field to the creationTime
39+
func (rm *resourceManager) customSetLastModifiedTimeToCreationTime(ko *svcapitypes.ScalableTarget) {
40+
if ko.Status.CreationTime != nil && ko.Status.LastModifiedTime == nil {
41+
ko.Status.LastModifiedTime = ko.Status.CreationTime
42+
}
43+
}
44+
45+
// customSetLastModifiedTimeToCurrentTime sets the LastModifiedTime field to the current time post an update
46+
func (rm *resourceManager) customSetLastModifiedTimeToCurrentTime(ko *svcapitypes.ScalableTarget) {
4047
currentTime := metav1.Time{Time: time.Now().UTC()}
4148
ko.Status.LastModifiedTime = &currentTime
4249
}

pkg/resource/scalable_target/sdk.go

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

pkg/resource/scaling_policy/custom_api.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@ import (
1919
"time"
2020
)
2121

22-
// customSetOutputUpdate sets the LastModifiedTime field to the current time post an update
23-
func (rm *resourceManager) customSetLastModifiedTime(ko *svcapitypes.ScalingPolicy) {
22+
// customSetLastModifiedTimeToCreationTime sets the LastModifiedTime field to the creationTime
23+
func (rm *resourceManager) customSetLastModifiedTimeToCreationTime(ko *svcapitypes.ScalingPolicy) {
24+
if ko.Status.CreationTime != nil && ko.Status.LastModifiedTime == nil {
25+
ko.Status.LastModifiedTime = ko.Status.CreationTime
26+
}
27+
}
28+
29+
// customSetLastModifiedTimeToCurrentTime sets the LastModifiedTime field to the current time post an update
30+
func (rm *resourceManager) customSetLastModifiedTimeToCurrentTime(ko *svcapitypes.ScalingPolicy) {
2431
currentTime := metav1.Time{Time: time.Now().UTC()}
2532
ko.Status.LastModifiedTime = &currentTime
2633
}

pkg/resource/scaling_policy/sdk.go

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

test/e2e/tests/test_adopted_resource.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ def adopt_scaling_policy(adopt_scalable_target):
143143
@service_marker
144144
@pytest.mark.canary
145145
class TestAdopted:
146-
def test_sagemaker_endpoint_autoscaling(self, put_scaling_policy, adopt_scaling_policy):
146+
def test_sagemaker_endpoint_autoscaling(
147+
self, put_scaling_policy, adopt_scaling_policy
148+
):
147149
sdk_resource_id = put_scaling_policy
148150

149151
(

test/e2e/tests/test_sagemaker_endpoint_autoscaling.py

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import boto3
1717
import botocore
1818
import pytest
19+
import datetime
1920
import logging
2021
from typing import Dict, Tuple
2122

@@ -83,7 +84,11 @@ def generate_sagemaker_target(sagemaker_endpoint):
8384
replacements["SCALABLETARGET_NAME"] = target_resource_name
8485
replacements["RESOURCE_ID"] = resource_id
8586

86-
target_reference, target_spec, target_resource = create_applicationautoscaling_resource(
87+
(
88+
target_reference,
89+
target_spec,
90+
target_resource,
91+
) = create_applicationautoscaling_resource(
8792
resource_plural=TARGET_RESOURCE_PLURAL,
8893
resource_name=target_resource_name,
8994
spec_file="sagemaker_endpoint_autoscaling_target",
@@ -92,7 +97,7 @@ def generate_sagemaker_target(sagemaker_endpoint):
9297

9398
assert target_resource is not None
9499

95-
yield (resource_id, target_spec, target_reference)
100+
yield (resource_id, target_reference, target_spec, target_resource)
96101

97102
if k8s.get_resource_exists(target_reference):
98103
_, deleted = k8s.delete_custom_resource(target_reference)
@@ -101,14 +106,23 @@ def generate_sagemaker_target(sagemaker_endpoint):
101106

102107
@pytest.fixture(scope="module")
103108
def generate_sagemaker_policy(generate_sagemaker_target):
104-
resource_id, target_spec, target_reference = generate_sagemaker_target
109+
(
110+
resource_id,
111+
target_reference,
112+
target_spec,
113+
target_resource,
114+
) = generate_sagemaker_target
105115
policy_resource_name = random_suffix_name("sagemaker-scaling-policy", 32)
106116

107117
replacements = REPLACEMENT_VALUES.copy()
108118
replacements["SCALINGPOLICY_NAME"] = policy_resource_name
109119
replacements["RESOURCE_ID"] = resource_id
110120

111-
policy_reference, policy_spec, policy_resource = create_applicationautoscaling_resource(
121+
(
122+
policy_reference,
123+
policy_spec,
124+
policy_resource,
125+
) = create_applicationautoscaling_resource(
112126
resource_plural=POLICY_RESOURCE_PLURAL,
113127
resource_name=policy_resource_name,
114128
spec_file="sagemaker_endpoint_autoscaling_policy",
@@ -117,7 +131,15 @@ def generate_sagemaker_policy(generate_sagemaker_target):
117131

118132
assert policy_resource is not None
119133

120-
yield (resource_id, target_spec, target_reference, policy_resource, policy_spec, policy_reference)
134+
yield (
135+
resource_id,
136+
target_reference,
137+
target_spec,
138+
target_resource,
139+
policy_resource,
140+
policy_spec,
141+
policy_reference,
142+
)
121143

122144
if k8s.get_resource_exists(policy_reference):
123145
_, deleted = k8s.delete_custom_resource(policy_reference)
@@ -127,6 +149,23 @@ def generate_sagemaker_policy(generate_sagemaker_target):
127149
@service_marker
128150
@pytest.mark.canary
129151
class TestSageMakerEndpointAutoscaling:
152+
def wait_until_update(
153+
self, reference, previous_modified_time, wait_period=2, wait_time=30
154+
):
155+
for i in range(wait_period):
156+
resource = k8s.get_resource(reference)
157+
assert resource is not None
158+
assert "lastModifiedTime" in resource["status"]
159+
last_modified_time = resource["status"]["lastModifiedTime"]
160+
d1 = datetime.datetime.strptime(last_modified_time, "%Y-%m-%dT%H:%M:%SZ")
161+
d2 = datetime.datetime.strptime(
162+
previous_modified_time, "%Y-%m-%dT%H:%M:%SZ"
163+
)
164+
if d1 > d2:
165+
return True
166+
sleep(wait_time)
167+
return False
168+
130169
def get_sagemaker_scalable_target_description(
131170
self, applicationautoscaling_client, resource_id: str, expectedTargets: int
132171
):
@@ -163,8 +202,9 @@ def get_sagemaker_scaling_policy_description(
163202
def test_create(self, applicationautoscaling_client, generate_sagemaker_policy):
164203
(
165204
resource_id,
166-
target_spec,
167205
target_reference,
206+
target_spec,
207+
target_resource,
168208
policy_resource,
169209
policy_spec,
170210
policy_reference,
@@ -189,46 +229,56 @@ def test_create(self, applicationautoscaling_client, generate_sagemaker_policy):
189229
def test_update(self, applicationautoscaling_client, generate_sagemaker_policy):
190230
(
191231
resource_id,
192-
target_spec,
193232
target_reference,
233+
target_spec,
234+
target_resource,
194235
policy_resource,
195236
policy_spec,
196237
policy_reference,
197238
) = generate_sagemaker_policy
198239

199240
updatedMaxCapacity = 4
200241
updatedTargetValue = 120
201-
242+
202243
# Update the ScalableTarget
203244
target_spec["spec"]["maxCapacity"] = updatedMaxCapacity
245+
assert "lastModifiedTime" in target_resource["status"]
246+
last_modified_time = target_resource["status"]["lastModifiedTime"]
204247
k8s.patch_custom_resource(target_reference, target_spec)
205-
sleep(5)
206-
248+
assert self.wait_until_update(target_reference, last_modified_time) == True
249+
207250
updated_target_description = self.get_sagemaker_scalable_target_description(
208251
applicationautoscaling_client, resource_id, 1
209252
)
210253
assert updated_target_description is not None
211-
assert (
212-
updated_target_description[0]["MaxCapacity"] == updatedMaxCapacity
213-
)
254+
assert updated_target_description[0]["MaxCapacity"] == updatedMaxCapacity
214255

215256
# Update the ScalingPolicy
216-
policy_spec["spec"]["targetTrackingScalingPolicyConfiguration"]["targetValue"] = updatedTargetValue
257+
policy_spec["spec"]["targetTrackingScalingPolicyConfiguration"][
258+
"targetValue"
259+
] = updatedTargetValue
260+
assert "lastModifiedTime" in policy_resource["status"]
261+
last_modified_time = policy_resource["status"]["lastModifiedTime"]
217262
k8s.patch_custom_resource(policy_reference, policy_spec)
218-
sleep(5)
263+
assert self.wait_until_update(policy_reference, last_modified_time) == True
264+
219265
updated_policy_description = self.get_sagemaker_scaling_policy_description(
220266
applicationautoscaling_client, resource_id, 1
221267
)
222268
assert updated_policy_description is not None
223269
assert (
224-
updated_policy_description[0]["TargetTrackingScalingPolicyConfiguration"]["TargetValue"] == updatedTargetValue
270+
updated_policy_description[0]["TargetTrackingScalingPolicyConfiguration"][
271+
"TargetValue"
272+
]
273+
== updatedTargetValue
225274
)
226275

227276
def test_delete(self, applicationautoscaling_client, generate_sagemaker_policy):
228277
(
229278
resource_id,
230-
target_spec,
231279
target_reference,
280+
target_spec,
281+
target_resource,
232282
policy_resource,
233283
policy_spec,
234284
policy_reference,
@@ -249,4 +299,4 @@ def test_delete(self, applicationautoscaling_client, generate_sagemaker_policy):
249299
# TODO: Ideally this check should pass after line 188 itself; but it requires the scalabletarget to be deleted too.
250300
policy_description = self.get_sagemaker_scaling_policy_description(
251301
applicationautoscaling_client, resource_id, 0
252-
)
302+
)

0 commit comments

Comments
 (0)