Skip to content

Commit abc7c87

Browse files
authored
Support Function references in Alias resource (#53)
Issue #, if available: Description of changes: Adding functionality to the lambda controller to be able to reference a `Function` from an `Alias` resource using resource references. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 8a92cf3 commit abc7c87

File tree

9 files changed

+202
-26
lines changed

9 files changed

+202
-26
lines changed

apis/v1alpha1/alias.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.

apis/v1alpha1/generator.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ resources:
6262
is_primary_key: true
6363
FunctionName:
6464
is_required: true
65+
references:
66+
resource: Function
67+
path: Spec.Name
6568
FunctionVersion:
6669
is_required: true
6770
tags:

config/crd/bases/lambda.services.k8s.aws_aliases.yaml

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
21
---
32
apiVersion: apiextensions.k8s.io/v1
43
kind: CustomResourceDefinition
54
metadata:
65
annotations:
7-
controller-gen.kubebuilder.io/version: v0.7.0
6+
controller-gen.kubebuilder.io/version: v0.9.2
87
creationTimestamp: null
98
name: aliases.lambda.services.k8s.aws
109
spec:
@@ -40,11 +39,24 @@ spec:
4039
type: string
4140
functionName:
4241
description: "The name of the Lambda function. \n Name formats \n
43-
\ * Function name - MyFunction. \n * Function ARN - arn:aws:lambda:us-west-2:123456789012:function:MyFunction.
44-
\n * Partial ARN - 123456789012:function:MyFunction. \n The length
42+
* Function name - MyFunction. \n * Function ARN - arn:aws:lambda:us-west-2:123456789012:function:MyFunction.
43+
\n * Partial ARN - 123456789012:function:MyFunction. \n The length
4544
constraint applies only to the full ARN. If you specify only the
4645
function name, it is limited to 64 characters in length."
4746
type: string
47+
functionRef:
48+
description: 'AWSResourceReferenceWrapper provides a wrapper around
49+
*AWSResourceReference type to provide more user friendly syntax
50+
for references using ''from'' field Ex: APIIDRef: from: name: my-api'
51+
properties:
52+
from:
53+
description: AWSResourceReference provides all the values necessary
54+
to reference another k8s resource for finding the identifier(Id/ARN/Name)
55+
properties:
56+
name:
57+
type: string
58+
type: object
59+
type: object
4860
functionVersion:
4961
description: The function version that the alias invokes.
5062
type: string
@@ -61,7 +73,6 @@ spec:
6173
type: object
6274
type: object
6375
required:
64-
- functionName
6576
- functionVersion
6677
- name
6778
type: object
@@ -139,9 +150,3 @@ spec:
139150
storage: true
140151
subresources:
141152
status: {}
142-
status:
143-
acceptedNames:
144-
kind: ""
145-
plural: ""
146-
conditions: []
147-
storedVersions: []

generator.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ resources:
6262
is_primary_key: true
6363
FunctionName:
6464
is_required: true
65+
references:
66+
resource: Function
67+
path: Spec.Name
6568
FunctionVersion:
6669
is_required: true
6770
tags:

helm/crds/lambda.services.k8s.aws_aliases.yaml

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
21
---
32
apiVersion: apiextensions.k8s.io/v1
43
kind: CustomResourceDefinition
54
metadata:
65
annotations:
7-
controller-gen.kubebuilder.io/version: v0.7.0
6+
controller-gen.kubebuilder.io/version: v0.9.2
87
creationTimestamp: null
98
name: aliases.lambda.services.k8s.aws
109
spec:
@@ -40,11 +39,24 @@ spec:
4039
type: string
4140
functionName:
4241
description: "The name of the Lambda function. \n Name formats \n
43-
\ * Function name - MyFunction. \n * Function ARN - arn:aws:lambda:us-west-2:123456789012:function:MyFunction.
44-
\n * Partial ARN - 123456789012:function:MyFunction. \n The length
42+
* Function name - MyFunction. \n * Function ARN - arn:aws:lambda:us-west-2:123456789012:function:MyFunction.
43+
\n * Partial ARN - 123456789012:function:MyFunction. \n The length
4544
constraint applies only to the full ARN. If you specify only the
4645
function name, it is limited to 64 characters in length."
4746
type: string
47+
functionRef:
48+
description: 'AWSResourceReferenceWrapper provides a wrapper around
49+
*AWSResourceReference type to provide more user friendly syntax
50+
for references using ''from'' field Ex: APIIDRef: from: name: my-api'
51+
properties:
52+
from:
53+
description: AWSResourceReference provides all the values necessary
54+
to reference another k8s resource for finding the identifier(Id/ARN/Name)
55+
properties:
56+
name:
57+
type: string
58+
type: object
59+
type: object
4860
functionVersion:
4961
description: The function version that the alias invokes.
5062
type: string
@@ -61,7 +73,6 @@ spec:
6173
type: object
6274
type: object
6375
required:
64-
- functionName
6576
- functionVersion
6677
- name
6778
type: object
@@ -139,9 +150,3 @@ spec:
139150
storage: true
140151
subresources:
141152
status: {}
142-
status:
143-
acceptedNames:
144-
kind: ""
145-
plural: ""
146-
conditions: []
147-
storedVersions: []

pkg/resource/alias/delta.go

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

pkg/resource/alias/references.go

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

test/e2e/resources/alias-ref.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: lambda.services.k8s.aws/v1alpha1
2+
kind: Alias
3+
metadata:
4+
name: $ALIAS_NAME
5+
annotations:
6+
services.k8s.aws/region: $AWS_REGION
7+
spec:
8+
name: $ALIAS_NAME
9+
functionRef:
10+
from:
11+
name: $FUNCTION_REF_NAME
12+
functionVersion: $FUNCTION_VERSION
13+
description: alias created by ACK lambda-controller e2e tests

test/e2e/tests/test_alias.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,62 @@ def test_smoke(self, lambda_client, lambda_function):
136136

137137
# Check alias doesn't exist
138138
assert not lambda_validator.alias_exists(resource_name, lambda_function_name)
139+
140+
def test_smoke_ref(self, lambda_client, lambda_function):
141+
(_, function_resource) = lambda_function
142+
function_resource_name = function_resource["metadata"]["name"]
143+
144+
resource_name = random_suffix_name("lambda-alias", 24)
145+
146+
replacements = REPLACEMENT_VALUES.copy()
147+
replacements["AWS_REGION"] = get_region()
148+
replacements["ALIAS_NAME"] = resource_name
149+
replacements["FUNCTION_REF_NAME"] = function_resource_name
150+
replacements["FUNCTION_VERSION"] = "$LATEST"
151+
152+
# Load alias CR
153+
resource_data = load_lambda_resource(
154+
"alias-ref",
155+
additional_replacements=replacements,
156+
)
157+
logging.debug(resource_data)
158+
159+
# Create k8s resource
160+
ref = k8s.CustomResourceReference(
161+
CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL,
162+
resource_name, namespace="default",
163+
)
164+
k8s.create_custom_resource(ref, resource_data)
165+
cr = k8s.wait_resource_consumed_by_controller(ref)
166+
167+
assert cr is not None
168+
assert k8s.get_resource_exists(ref)
169+
170+
time.sleep(CREATE_WAIT_AFTER_SECONDS)
171+
172+
lambda_validator = LambdaValidator(lambda_client)
173+
# Check alias exists
174+
assert lambda_validator.alias_exists(resource_name, function_resource_name)
175+
176+
cr = k8s.wait_resource_consumed_by_controller(ref)
177+
178+
# Update cr
179+
cr["spec"]["description"] = ""
180+
181+
# Patch k8s resource
182+
k8s.patch_custom_resource(ref, cr)
183+
time.sleep(UPDATE_WAIT_AFTER_SECONDS)
184+
185+
# Check alias description
186+
alias = lambda_validator.get_alias(resource_name, function_resource_name)
187+
assert alias is not None
188+
assert alias["Description"] == ""
189+
190+
# Delete k8s resource
191+
_, deleted = k8s.delete_custom_resource(ref)
192+
assert deleted
193+
194+
time.sleep(DELETE_WAIT_AFTER_SECONDS)
195+
196+
# Check alias doesn't exist
197+
assert not lambda_validator.alias_exists(resource_name, function_resource_name)

0 commit comments

Comments
 (0)