Skip to content

fix: Support to update architectures field #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions pkg/resource/function/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ func (rm *resourceManager) customUpdateFunction(
}
}
}
if delta.DifferentAt("Spec.Architectures") {
err = rm.updateFunctionArchitectures(ctx, desired, latest)
if err != nil {
return nil, err
}
}

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

// updateFunctionArchitectures calls UpdateFunctionCode to update architecture for lambda
// function code.
func (rm *resourceManager) updateFunctionArchitectures(
ctx context.Context,
desired *resource,
latest *resource,
) error {
var err error
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.updateFunctionArchitectures")
defer exit(err)

dspec := desired.ko.Spec
input := &svcsdk.UpdateFunctionCodeInput{
FunctionName: aws.String(*dspec.Name),
}

if dspec.Architectures != nil {
input.Architectures = dspec.Architectures
} else {
input.Architectures = nil
}

if latest.ko.Spec.Code != nil {
if latest.ko.Spec.PackageType != nil && *latest.ko.Spec.PackageType == "Image" {
input.ImageUri = latest.ko.Spec.Code.ImageURI
} else if latest.ko.Spec.PackageType != nil && *latest.ko.Spec.PackageType == "Zip" {
input.S3Bucket = latest.ko.Spec.Code.S3Bucket
input.S3Key = latest.ko.Spec.Code.S3Key
}
}

_, err = rm.sdkapi.UpdateFunctionCodeWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "UpdateFunctionArchitectures", err)
if err != nil {
return err
}

return nil
}

// updateFunctionsCode calls UpdateFunctionCode to update a specific lambda
// function code.
func (rm *resourceManager) updateFunctionCode(
Expand Down
11 changes: 11 additions & 0 deletions pkg/resource/function/sdk.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions templates/hooks/function/sdk_read_one_post_set_output.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
ko.Spec.Code.ImageURI = resp.Code.ImageUri
}
}
if resp.Configuration.Architectures != nil {
f1 := []*string{}
for _, f1iter := range resp.Configuration.Architectures {
var f1elem string
f1elem = *f1iter
f1 = append(f1, &f1elem)
}
ko.Spec.Architectures = f1
} else {
ko.Spec.Architectures = nil
}
if resp.Configuration.CodeSha256 != nil {
ko.Status.CodeSHA256 = resp.Configuration.CodeSha256
} else {
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/resources/function_architectures.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: lambda.services.k8s.aws/v1alpha1
kind: Function
metadata:
name: $FUNCTION_NAME
annotations:
services.k8s.aws/region: $AWS_REGION
spec:
name: $FUNCTION_NAME
code:
s3Bucket: $BUCKET_NAME
s3Key: $LAMBDA_FILE_NAME
role: $LAMBDA_ROLE
architectures: [$ARCHITECTURES]
runtime: python3.9
handler: main
description: function created by ACK lambda-controller e2e tests
reservedConcurrentExecutions: $RESERVED_CONCURRENT_EXECUTIONS
codeSigningConfigARN: "$CODE_SIGNING_CONFIG_ARN"
65 changes: 65 additions & 0 deletions test/e2e/tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,71 @@ def test_function_snapstart(self, lambda_client):
# Check Lambda function doesn't exist
assert not lambda_validator.function_exists(resource_name)

def test_function_architecture(self, lambda_client):
resource_name = random_suffix_name("functionsarchitecture", 24)

resources = get_bootstrap_resources()
logging.debug(resources)

replacements = REPLACEMENT_VALUES.copy()
replacements["FUNCTION_NAME"] = resource_name
replacements["BUCKET_NAME"] = resources.FunctionsBucket.name
replacements["LAMBDA_ROLE"] = resources.BasicRole.arn
replacements["LAMBDA_FILE_NAME"] = LAMBDA_FUNCTION_FILE_ZIP
replacements["RESERVED_CONCURRENT_EXECUTIONS"] = "0"
replacements["CODE_SIGNING_CONFIG_ARN"] = ""
replacements["AWS_REGION"] = get_region()
replacements["ARCHITECTURES"] = 'x86_64'

# Load Lambda CR
resource_data = load_lambda_resource(
"function_architectures",
additional_replacements=replacements,
)
logging.debug(resource_data)

# Create k8s resource
ref = k8s.CustomResourceReference(
CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL,
resource_name, namespace="default",
)
k8s.create_custom_resource(ref, resource_data)
cr = k8s.wait_resource_consumed_by_controller(ref)

assert cr is not None
assert k8s.get_resource_exists(ref)

time.sleep(CREATE_WAIT_AFTER_SECONDS)

cr = k8s.wait_resource_consumed_by_controller(ref)

lambda_validator = LambdaValidator(lambda_client)

# Check Lambda function exists
assert lambda_validator.function_exists(resource_name)

# Update cr
cr["spec"]["architectures"] = ['arm64']
cr["spec"]["code"]["s3Bucket"] = resources.FunctionsBucket.name
cr["spec"]["code"]["s3Key"] = LAMBDA_FUNCTION_FILE_ZIP

#Patch k8s resource
k8s.patch_custom_resource(ref, cr)
time.sleep(UPDATE_WAIT_AFTER_SECONDS)

#Check function_snapstart update fields
function = lambda_validator.get_function(resource_name)
assert function["Configuration"]["Architectures"] == ['arm64']

# Delete k8s resource
_, deleted = k8s.delete_custom_resource(ref)
assert deleted is True

time.sleep(DELETE_WAIT_AFTER_SECONDS)

# Check Lambda function doesn't exist
assert not lambda_validator.function_exists(resource_name)

def test_function_event_invoke_config(self, lambda_client):
resource_name = random_suffix_name("lambda-function", 24)

Expand Down