Skip to content

Commit 0f9204d

Browse files
committed
Support for update architecture field
1 parent ea36496 commit 0f9204d

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

pkg/resource/function/hooks.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ func (rm *resourceManager) customUpdateFunction(
102102
}
103103
}
104104
}
105+
if delta.DifferentAt("Spec.Architectures") {
106+
err = rm.updateFunctionArchitectures(ctx, desired, latest)
107+
if err != nil {
108+
return nil, err
109+
}
110+
}
105111

106112
// Only try to update Spec.Code or Spec.Configuration at once. It is
107113
// not correct to sequentially call UpdateFunctionConfiguration and
@@ -343,6 +349,47 @@ func (rm *resourceManager) updateFunctionTags(
343349
return nil
344350
}
345351

352+
// updateFunctionArchitectures calls UpdateFunctionCode to update architecture for lambda
353+
// function code.
354+
func (rm *resourceManager) updateFunctionArchitectures(
355+
ctx context.Context,
356+
desired *resource,
357+
latest *resource,
358+
) error {
359+
var err error
360+
rlog := ackrtlog.FromContext(ctx)
361+
exit := rlog.Trace("rm.updateFunctionArchitectures")
362+
defer exit(err)
363+
364+
dspec := desired.ko.Spec
365+
input := &svcsdk.UpdateFunctionCodeInput{
366+
FunctionName: aws.String(*dspec.Name),
367+
}
368+
369+
if dspec.Architectures != nil {
370+
input.Architectures = dspec.Architectures
371+
} else {
372+
input.Architectures = nil
373+
}
374+
375+
if latest.ko.Spec.Code != nil {
376+
if latest.ko.Spec.PackageType != nil && *latest.ko.Spec.PackageType == "Image" {
377+
input.ImageUri = latest.ko.Spec.Code.ImageURI
378+
} else if latest.ko.Spec.PackageType != nil && *latest.ko.Spec.PackageType == "Zip" {
379+
input.S3Bucket = latest.ko.Spec.Code.S3Bucket
380+
input.S3Key = latest.ko.Spec.Code.S3Key
381+
}
382+
}
383+
384+
_, err = rm.sdkapi.UpdateFunctionCodeWithContext(ctx, input)
385+
rm.metrics.RecordAPICall("UPDATE", "UpdateFunctionArchitectures", err)
386+
if err != nil {
387+
return err
388+
}
389+
390+
return nil
391+
}
392+
346393
// updateFunctionsCode calls UpdateFunctionCode to update a specific lambda
347394
// function code.
348395
func (rm *resourceManager) updateFunctionCode(

pkg/resource/function/sdk.go

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

templates/hooks/function/sdk_read_one_post_set_output.go.tpl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@
1212
ko.Spec.Code.ImageURI = resp.Code.ImageUri
1313
}
1414
}
15+
if resp.Configuration.Architectures != nil {
16+
f1 := []*string{}
17+
for _, f1iter := range resp.Configuration.Architectures {
18+
var f1elem string
19+
f1elem = *f1iter
20+
f1 = append(f1, &f1elem)
21+
}
22+
ko.Spec.Architectures = f1
23+
} else {
24+
ko.Spec.Architectures = nil
25+
}
1526
if resp.Configuration.CodeSha256 != nil {
1627
ko.Status.CodeSHA256 = resp.Configuration.CodeSha256
1728
} else {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: lambda.services.k8s.aws/v1alpha1
2+
kind: Function
3+
metadata:
4+
name: $FUNCTION_NAME
5+
annotations:
6+
services.k8s.aws/region: $AWS_REGION
7+
spec:
8+
name: $FUNCTION_NAME
9+
code:
10+
s3Bucket: $BUCKET_NAME
11+
s3Key: $LAMBDA_FILE_NAME
12+
role: $LAMBDA_ROLE
13+
architectures: [$ARCHITECTURES]
14+
runtime: python3.9
15+
handler: main
16+
description: function created by ACK lambda-controller e2e tests
17+
reservedConcurrentExecutions: $RESERVED_CONCURRENT_EXECUTIONS
18+
codeSigningConfigARN: "$CODE_SIGNING_CONFIG_ARN"

test/e2e/tests/test_function.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,71 @@ def test_function_snapstart(self, lambda_client):
539539
# Check Lambda function doesn't exist
540540
assert not lambda_validator.function_exists(resource_name)
541541

542+
def test_function_architecture(self, lambda_client):
543+
resource_name = random_suffix_name("functionsarchitecture", 24)
544+
545+
resources = get_bootstrap_resources()
546+
logging.debug(resources)
547+
548+
replacements = REPLACEMENT_VALUES.copy()
549+
replacements["FUNCTION_NAME"] = resource_name
550+
replacements["BUCKET_NAME"] = resources.FunctionsBucket.name
551+
replacements["LAMBDA_ROLE"] = resources.BasicRole.arn
552+
replacements["LAMBDA_FILE_NAME"] = LAMBDA_FUNCTION_FILE_ZIP
553+
replacements["RESERVED_CONCURRENT_EXECUTIONS"] = "0"
554+
replacements["CODE_SIGNING_CONFIG_ARN"] = ""
555+
replacements["AWS_REGION"] = get_region()
556+
replacements["ARCHITECTURES"] = 'x86_64'
557+
558+
# Load Lambda CR
559+
resource_data = load_lambda_resource(
560+
"function_architectures",
561+
additional_replacements=replacements,
562+
)
563+
logging.debug(resource_data)
564+
565+
# Create k8s resource
566+
ref = k8s.CustomResourceReference(
567+
CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL,
568+
resource_name, namespace="default",
569+
)
570+
k8s.create_custom_resource(ref, resource_data)
571+
cr = k8s.wait_resource_consumed_by_controller(ref)
572+
573+
assert cr is not None
574+
assert k8s.get_resource_exists(ref)
575+
576+
time.sleep(CREATE_WAIT_AFTER_SECONDS)
577+
578+
cr = k8s.wait_resource_consumed_by_controller(ref)
579+
580+
lambda_validator = LambdaValidator(lambda_client)
581+
582+
# Check Lambda function exists
583+
assert lambda_validator.function_exists(resource_name)
584+
585+
# Update cr
586+
cr["spec"]["architectures"] = ['arm64']
587+
cr["spec"]["code"]["s3Bucket"] = resources.FunctionsBucket.name
588+
cr["spec"]["code"]["s3Key"] = LAMBDA_FUNCTION_FILE_ZIP
589+
590+
#Patch k8s resource
591+
k8s.patch_custom_resource(ref, cr)
592+
time.sleep(UPDATE_WAIT_AFTER_SECONDS)
593+
594+
#Check function_snapstart update fields
595+
function = lambda_validator.get_function(resource_name)
596+
assert function["Configuration"]["Architectures"] == ['arm64']
597+
598+
# Delete k8s resource
599+
_, deleted = k8s.delete_custom_resource(ref)
600+
assert deleted is True
601+
602+
time.sleep(DELETE_WAIT_AFTER_SECONDS)
603+
604+
# Check Lambda function doesn't exist
605+
assert not lambda_validator.function_exists(resource_name)
606+
542607
def test_function_event_invoke_config(self, lambda_client):
543608
resource_name = random_suffix_name("lambda-function", 24)
544609

0 commit comments

Comments
 (0)