Skip to content

Commit 0848c57

Browse files
committed
Enable AdoptedResource and add required custom logic for ScalableTarget
1 parent 79ca2cc commit 0848c57

File tree

13 files changed

+354
-11
lines changed

13 files changed

+354
-11
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-08T23:45:08Z"
2+
build_date: "2021-09-10T17:51:36Z"
33
build_hash: 394e8294aabf0b221c35f7a5c7bc7e4e43bc6f10
4-
go_version: go1.16.4 linux/amd64
4+
go_version: go1.14.14 darwin/amd64
55
version: v0.13.2
66
api_directory_checksum: 155104e002d9c69b75fd9e85789bf7ee292b866c
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.37.10
99
generator_config_info:
10-
file_checksum: 4bf1e52543400e3d52cc745da4c1e2e518e9e1e1
10+
file_checksum: ef8cd242243fc5a9d7b2f0e8f689798458b85d43
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation
14-
timestamp: 2021-09-08 23:45:13.439536495 +0000 UTC
14+
timestamp: 2021-09-10 17:51:47.055507 +0000 UTC

apis/v1alpha1/generator.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ operations:
1515
resource_name: ScalableTarget
1616
DescribeScalableTargets:
1717
primary_identifier_field_name: ResourceId
18+
custom_check_required_fields_missing_method: customCheckRequiredFieldsMissingMethod
1819
DescribeScalingPolicies:
1920
primary_identifier_field_name: ResourceId
2021
resources:
2122
ScalableTarget:
22-
is_adoptable: false
2323
hooks:
2424
sdk_read_many_post_build_request:
2525
code: rm.customDescribeScalableTarget(ctx, r, input)
2626
delta_pre_compare:
2727
code: customSetDefaults(a, b)
28+
set_identifiers_pre_read_one:
29+
code: r.customSetIdentifierCode(identifier)
2830
fields:
2931
ResourceID:
3032
is_name: true
@@ -33,8 +35,12 @@ resources:
3335
from:
3436
operation: DescribeScalableTargets
3537
path: ScalableTargets..CreationTime
38+
# ResourceIds:
39+
# is_read_only: true
40+
# from:
41+
# operation: DescribeScalableTargets
42+
# path: ScalableTargets..ResourceId
3643
ScalingPolicy:
37-
is_adoptable: false
3844
fields:
3945
ResourceID:
4046
is_name: true

generator.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ operations:
1515
resource_name: ScalableTarget
1616
DescribeScalableTargets:
1717
primary_identifier_field_name: ResourceId
18+
custom_check_required_fields_missing_method: customCheckRequiredFieldsMissingMethod
1819
DescribeScalingPolicies:
1920
primary_identifier_field_name: ResourceId
2021
resources:
2122
ScalableTarget:
22-
is_adoptable: false
2323
hooks:
2424
sdk_read_many_post_build_request:
2525
code: rm.customDescribeScalableTarget(ctx, r, input)
2626
delta_pre_compare:
2727
code: customSetDefaults(a, b)
28+
set_identifiers_pre_read_one:
29+
code: r.customSetIdentifierCode(identifier)
2830
fields:
2931
ResourceID:
3032
is_name: true
@@ -34,7 +36,6 @@ resources:
3436
operation: DescribeScalableTargets
3537
path: ScalableTargets..CreationTime
3638
ScalingPolicy:
37-
is_adoptable: false
3839
fields:
3940
ResourceID:
4041
is_name: true

pkg/resource/scalable_target/custom_api.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package scalable_target
1515

1616
import (
1717
"context"
18+
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
1819
svcsdk "github.com/aws/aws-sdk-go/service/applicationautoscaling"
1920
)
2021

@@ -31,3 +32,19 @@ func (rm *resourceManager) customDescribeScalableTarget(
3132
input.SetResourceIds(resourceIDList)
3233
}
3334
}
35+
36+
func (r *resource) customSetIdentifierCode(
37+
identifier *ackv1alpha1.AWSIdentifiers,
38+
) {
39+
r.ko.Spec.ResourceID = &identifier.NameOrID
40+
}
41+
42+
// customCheckRequiredFieldsMissingMethod returns true if there are any fields
43+
// for the ReadOne Input shape that are required but not present in the
44+
// resource's Spec or Status
45+
func (rm *resourceManager) customCheckRequiredFieldsMissingMethod(
46+
r *resource,
47+
) bool {
48+
return r.ko.Spec.ResourceID == nil
49+
50+
}

pkg/resource/scalable_target/manager_factory.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/resource/scalable_target/resource.go

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

pkg/resource/scalable_target/sdk.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/resource/scaling_policy/manager_factory.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
SERVICE_NAME = "applicationautoscaling"
2222
CRD_GROUP = "applicationautoscaling.services.k8s.aws"
23+
ADOPTED_RESOURCE_CRD_GROUP = "services.k8s.aws"
2324
CRD_VERSION = "v1alpha1"
2425

2526
# PyTest marker for the current service
@@ -61,3 +62,21 @@ def create_applicationautoscaling_resource(
6162
)
6263

6364
return reference, spec, resource
65+
66+
def create_adopted_resource(resource_name, spec_file, replacements, namespace="default"):
67+
"""
68+
Wrapper around k8s.load_and_create_resource to create a Adopoted resource
69+
"""
70+
71+
reference, spec, resource = k8s.load_and_create_resource(
72+
resource_directory,
73+
ADOPTED_RESOURCE_CRD_GROUP,
74+
CRD_VERSION,
75+
"adoptedresources",
76+
resource_name,
77+
spec_file,
78+
replacements,
79+
namespace,
80+
)
81+
82+
return reference, spec, resource
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
15+
import boto3
16+
17+
def application_autoscaling_client():
18+
return boto3.client("application-autoscaling")
19+
20+
def sagemaker_endpoint_register_scalable_target(resource_id):
21+
target_input = {
22+
"ServiceNamespace": "sagemaker",
23+
"ResourceId": resource_id,
24+
"ScalableDimension": "sagemaker:variant:DesiredInstanceCount",
25+
"MinCapacity": 1,
26+
"MaxCapacity": 2,
27+
}
28+
29+
target_response = application_autoscaling_client().register_scalable_target(**target_input)
30+
return target_response
31+
32+
def sagemaker_endpoint_put_scaling_policy(resource_id, policy_name):
33+
policy_input = {
34+
"PolicyName": policy_name,
35+
"ServiceNamespace": "sagemaker",
36+
"ResourceId": resource_id,
37+
"ScalableDimension": "sagemaker:variant:DesiredInstanceCount",
38+
"PolicyType": "TargetTrackingScaling",
39+
"TargetTrackingScalingPolicyConfiguration": {
40+
"TargetValue": 70.0,
41+
"ScaleInCooldown": 700,
42+
"ScaleOutCooldown": 300,
43+
"PredefinedMetricSpecification": {
44+
"PredefinedMetricType": "SageMakerVariantInvocationsPerInstance",
45+
},
46+
}
47+
}
48+
49+
policy_response = application_autoscaling_client().put_scaling_policy(**policy_input)
50+
return policy_response
51+
52+
def sagemaker_endpoint_deregister_scalable_target(resource_id):
53+
target_input = {
54+
"ServiceNamespace": "sagemaker",
55+
"ResourceId": resource_id,
56+
"ScalableDimension": "sagemaker:variant:DesiredInstanceCount",
57+
}
58+
59+
target_response = application_autoscaling_client().deregister_scalable_target(**target_input)
60+
return target_response
61+
62+
def sagemaker_endpoint_delete_scaling_policy(resource_id, policy_name):
63+
policy_input = {
64+
"ServiceNamespace": "sagemaker",
65+
"ResourceId": resource_id,
66+
"ScalableDimension": "sagemaker:variant:DesiredInstanceCount",
67+
"PolicyName": policy_name
68+
}
69+
70+
policy_response = application_autoscaling_client().delete_scaling_policy(**policy_input)
71+
return policy_response

0 commit comments

Comments
 (0)