Skip to content

Commit 753891a

Browse files
authored
Support Function references in FunctionUrlConfig resource (#54)
Issue #, if available: Description of changes: Adding functionality to the lambda controller to be able to reference a Function from a FunctionUrlConfig 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 abc7c87 commit 753891a

File tree

9 files changed

+215
-31
lines changed

9 files changed

+215
-31
lines changed

apis/v1alpha1/function_url_config.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
@@ -96,4 +96,7 @@ resources:
9696
ignore: true
9797
fields:
9898
FunctionName:
99+
references:
100+
resource: Function
101+
path: Spec.Name
99102
is_primary_key: true

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

Lines changed: 18 additions & 13 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: functionurlconfigs.lambda.services.k8s.aws
109
spec:
@@ -72,17 +71,29 @@ spec:
7271
type: object
7372
functionName:
7473
description: "The name of the Lambda function. \n Name formats \n
75-
\ * Function name - my-function. \n * Function ARN - arn:aws:lambda:us-west-2:123456789012:function:my-function.
76-
\n * Partial ARN - 123456789012:function:my-function. \n The
77-
length constraint applies only to the full ARN. If you specify only
78-
the function name, it is limited to 64 characters in length."
74+
* Function name - my-function. \n * Function ARN - arn:aws:lambda:us-west-2:123456789012:function:my-function.
75+
\n * Partial ARN - 123456789012:function:my-function. \n The length
76+
constraint applies only to the full ARN. If you specify only the
77+
function name, it is limited to 64 characters in length."
7978
type: string
79+
functionRef:
80+
description: 'AWSResourceReferenceWrapper provides a wrapper around
81+
*AWSResourceReference type to provide more user friendly syntax
82+
for references using ''from'' field Ex: APIIDRef: from: name: my-api'
83+
properties:
84+
from:
85+
description: AWSResourceReference provides all the values necessary
86+
to reference another k8s resource for finding the identifier(Id/ARN/Name)
87+
properties:
88+
name:
89+
type: string
90+
type: object
91+
type: object
8092
qualifier:
8193
description: The alias name.
8294
type: string
8395
required:
8496
- authType
85-
- functionName
8697
type: object
8798
status:
8899
description: FunctionURLConfigStatus defines the observed state of FunctionURLConfig
@@ -164,9 +175,3 @@ spec:
164175
storage: true
165176
subresources:
166177
status: {}
167-
status:
168-
acceptedNames:
169-
kind: ""
170-
plural: ""
171-
conditions: []
172-
storedVersions: []

generator.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,7 @@ resources:
9696
ignore: true
9797
fields:
9898
FunctionName:
99+
references:
100+
resource: Function
101+
path: Spec.Name
99102
is_primary_key: true

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

Lines changed: 18 additions & 13 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: functionurlconfigs.lambda.services.k8s.aws
109
spec:
@@ -72,17 +71,29 @@ spec:
7271
type: object
7372
functionName:
7473
description: "The name of the Lambda function. \n Name formats \n
75-
\ * Function name - my-function. \n * Function ARN - arn:aws:lambda:us-west-2:123456789012:function:my-function.
76-
\n * Partial ARN - 123456789012:function:my-function. \n The
77-
length constraint applies only to the full ARN. If you specify only
78-
the function name, it is limited to 64 characters in length."
74+
* Function name - my-function. \n * Function ARN - arn:aws:lambda:us-west-2:123456789012:function:my-function.
75+
\n * Partial ARN - 123456789012:function:my-function. \n The length
76+
constraint applies only to the full ARN. If you specify only the
77+
function name, it is limited to 64 characters in length."
7978
type: string
79+
functionRef:
80+
description: 'AWSResourceReferenceWrapper provides a wrapper around
81+
*AWSResourceReference type to provide more user friendly syntax
82+
for references using ''from'' field Ex: APIIDRef: from: name: my-api'
83+
properties:
84+
from:
85+
description: AWSResourceReference provides all the values necessary
86+
to reference another k8s resource for finding the identifier(Id/ARN/Name)
87+
properties:
88+
name:
89+
type: string
90+
type: object
91+
type: object
8092
qualifier:
8193
description: The alias name.
8294
type: string
8395
required:
8496
- authType
85-
- functionName
8697
type: object
8798
status:
8899
description: FunctionURLConfigStatus defines the observed state of FunctionURLConfig
@@ -164,9 +175,3 @@ spec:
164175
storage: true
165176
subresources:
166177
status: {}
167-
status:
168-
acceptedNames:
169-
kind: ""
170-
plural: ""
171-
conditions: []
172-
storedVersions: []

pkg/resource/function_url_config/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/function_url_config/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.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: lambda.services.k8s.aws/v1alpha1
2+
kind: FunctionURLConfig
3+
metadata:
4+
name: $FUNCTION_URL_CONFIG_NAME
5+
annotations:
6+
services.k8s.aws/region: $AWS_REGION
7+
spec:
8+
name: $FUNCTION_URL_CONFIG_NAME
9+
functionRef:
10+
from:
11+
name: $FUNCTION_REF_NAME
12+
authType: $AUTH_TYPE

test/e2e/tests/test_function_url_config.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,72 @@ def test_smoke(self, lambda_client, lambda_function):
153153
time.sleep(DELETE_WAIT_AFTER_SECONDS)
154154

155155
# Check FunctionURLConfig doesn't exist
156-
assert not lambda_validator.function_url_config_exists(lambda_function_name)
156+
assert not lambda_validator.function_url_config_exists(lambda_function_name)
157+
158+
def test_smoke_ref(self, lambda_client, lambda_function):
159+
(_, function_resource) = lambda_function
160+
function_resource_name = function_resource["metadata"]["name"]
161+
162+
resource_name = random_suffix_name("functionurlconfig", 24)
163+
164+
replacements = REPLACEMENT_VALUES.copy()
165+
replacements["AWS_REGION"] = get_region()
166+
replacements["FUNCTION_URL_CONFIG_NAME"] = resource_name
167+
replacements["FUNCTION_REF_NAME"] = function_resource_name
168+
replacements["AUTH_TYPE"] = "NONE"
169+
170+
# Load FunctionURLConfig CR
171+
resource_data = load_lambda_resource(
172+
"function_url_config_ref",
173+
additional_replacements=replacements,
174+
)
175+
logging.debug(resource_data)
176+
177+
# Create k8s resource
178+
ref = k8s.CustomResourceReference(
179+
CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL,
180+
resource_name, namespace="default",
181+
)
182+
k8s.create_custom_resource(ref, resource_data)
183+
cr = k8s.wait_resource_consumed_by_controller(ref)
184+
185+
assert cr is not None
186+
assert k8s.get_resource_exists(ref)
187+
188+
time.sleep(CREATE_WAIT_AFTER_SECONDS)
189+
190+
# Check FunctionURLConfig exists
191+
lambda_validator = LambdaValidator(lambda_client)
192+
193+
# Check function url config exists
194+
function_url_config = lambda_validator.get_function_url_config(function_resource_name)
195+
assert function_url_config is not None
196+
assert function_url_config["AuthType"] == "NONE"
197+
198+
cr = k8s.wait_resource_consumed_by_controller(ref)
199+
200+
# Update cr
201+
cr["spec"]["cors"] = {
202+
"maxAge": 10,
203+
"allowOrigins": ["https://*"],
204+
}
205+
206+
# Patch k8s resource
207+
k8s.patch_custom_resource(ref, cr)
208+
time.sleep(UPDATE_WAIT_AFTER_SECONDS)
209+
210+
# Check FunctionURLConfig MaxAge and AllowOrigins array
211+
function_url_config = lambda_validator.get_function_url_config(function_resource_name)
212+
assert function_url_config is not None
213+
assert function_url_config["Cors"] is not None
214+
assert function_url_config["Cors"]["MaxAge"] == 10
215+
assert function_url_config["Cors"]["AllowOrigins"] == ["https://*"]
216+
217+
# Delete k8s resource
218+
_, deleted = k8s.delete_custom_resource(ref)
219+
assert deleted
220+
221+
time.sleep(DELETE_WAIT_AFTER_SECONDS)
222+
223+
# Check FunctionURLConfig doesn't exist
224+
assert not lambda_validator.function_url_config_exists(function_resource_name)

0 commit comments

Comments
 (0)