Skip to content

fix: Update support for Layers #136

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 2 commits into from
Apr 23, 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
2 changes: 1 addition & 1 deletion apis/v1alpha1/ack-generate-metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ack_generate_info:
build_date: "2024-04-16T23:40:41Z"
build_date: "2024-04-18T22:18:38Z"
build_hash: 37f4ba2b5a121a8786bc516e9ec4aa0b8b590c7d
go_version: go1.21.6
version: v0.33.0-1-g37f4ba2
Expand Down
125 changes: 68 additions & 57 deletions pkg/resource/function/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,38 @@ func (rm *resourceManager) updateFunctionConfiguration(
}
}

if delta.DifferentAt("Spec.Environment") {
environment := &svcsdk.Environment{}
if dspec.Environment != nil {
environment.Variables = dspec.Environment.DeepCopy().Variables
}
input.Environment = environment
}

if delta.DifferentAt("Spec.EphemeralStorage") {
ephemeralStorage := &svcsdk.EphemeralStorage{}
if dspec.EphemeralStorage != nil {
ephemeralStorageCopy := dspec.EphemeralStorage.DeepCopy()
ephemeralStorage.Size = ephemeralStorageCopy.Size
}
input.EphemeralStorage = ephemeralStorage
}

if delta.DifferentAt("Spec.FileSystemConfigs") {
fileSystemConfigs := []*svcsdk.FileSystemConfig{}
if len(dspec.FileSystemConfigs) > 0 {
for _, elem := range dspec.FileSystemConfigs {
elemCopy := elem.DeepCopy()
fscElem := &svcsdk.FileSystemConfig{
Arn: elemCopy.ARN,
LocalMountPath: elemCopy.LocalMountPath,
}
fileSystemConfigs = append(fileSystemConfigs, fscElem)
}
input.FileSystemConfigs = fileSystemConfigs
}
}
Comment on lines +182 to +212
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Vandita2020 ! How are environement/ephemeralStorage/FileSystemConfigs/Timeouts affecting to layers?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I have just rearranged them according to the order in the API. Please ignore them, no changes for them.


if delta.DifferentAt("Spec.Handler") {
if dspec.Handler != nil {
input.Handler = aws.String(*dspec.Handler)
Expand All @@ -187,6 +219,19 @@ func (rm *resourceManager) updateFunctionConfiguration(
}
}

if delta.DifferentAt("Spec.ImageConfig") {
if dspec.ImageConfig != nil && dspec.Code.ImageURI != nil && *dspec.Code.ImageURI != "" {
imageConfig := &svcsdk.ImageConfig{}
if dspec.ImageConfig != nil {
imageConfigCopy := dspec.ImageConfig.DeepCopy()
imageConfig.Command = imageConfigCopy.Command
imageConfig.EntryPoint = imageConfigCopy.EntryPoint
imageConfig.WorkingDirectory = imageConfigCopy.WorkingDirectory
}
input.ImageConfig = imageConfig
}
}

if delta.DifferentAt("Spec.KMSKeyARN") {
if dspec.KMSKeyARN != nil {
input.KMSKeyArn = aws.String(*dspec.KMSKeyARN)
Expand All @@ -195,11 +240,14 @@ func (rm *resourceManager) updateFunctionConfiguration(
}
}

if delta.DifferentAt("Spec.Role") {
if dspec.Role != nil {
input.Role = aws.String(*dspec.Role)
} else {
input.Role = aws.String("")
if delta.DifferentAt("Spec.Layers") {
layers := []*string{}
if len(dspec.Layers) > 0 {
for _, iter := range dspec.Layers {
var elem string = *iter
layers = append(layers, &elem)
}
input.Layers = layers
}
}

Expand All @@ -211,47 +259,28 @@ func (rm *resourceManager) updateFunctionConfiguration(
}
}

if delta.DifferentAt("Spec.Timeout") {
if dspec.Timeout != nil {
input.Timeout = aws.Int64(*dspec.Timeout)
if delta.DifferentAt("Spec.Role") {
if dspec.Role != nil {
input.Role = aws.String(*dspec.Role)
} else {
input.Timeout = aws.Int64(0)
}
}

if delta.DifferentAt("Spec.Environment") {
environment := &svcsdk.Environment{}
if dspec.Environment != nil {
environment.Variables = dspec.Environment.DeepCopy().Variables
input.Role = aws.String("")
}
input.Environment = environment
}

if delta.DifferentAt("Spec.FileSystemConfigs") {
fileSystemConfigs := []*svcsdk.FileSystemConfig{}
if len(dspec.FileSystemConfigs) > 0 {
for _, elem := range dspec.FileSystemConfigs {
elemCopy := elem.DeepCopy()
fscElem := &svcsdk.FileSystemConfig{
Arn: elemCopy.ARN,
LocalMountPath: elemCopy.LocalMountPath,
}
fileSystemConfigs = append(fileSystemConfigs, fscElem)
}
input.FileSystemConfigs = fileSystemConfigs
if delta.DifferentAt(("Spec.SnapStart")) {
snapStart := &svcsdk.SnapStart{}
if dspec.SnapStart != nil {
snapStartCopy := dspec.SnapStart.DeepCopy()
snapStart.ApplyOn = snapStartCopy.ApplyOn
}
input.SnapStart = snapStart
}

if delta.DifferentAt("Spec.ImageConfig") {
if dspec.ImageConfig != nil && dspec.Code.ImageURI != nil && *dspec.Code.ImageURI != "" {
imageConfig := &svcsdk.ImageConfig{}
if dspec.ImageConfig != nil {
imageConfigCopy := dspec.ImageConfig.DeepCopy()
imageConfig.Command = imageConfigCopy.Command
imageConfig.EntryPoint = imageConfigCopy.EntryPoint
imageConfig.WorkingDirectory = imageConfigCopy.WorkingDirectory
}
input.ImageConfig = imageConfig
if delta.DifferentAt("Spec.Timeout") {
if dspec.Timeout != nil {
input.Timeout = aws.Int64(*dspec.Timeout)
} else {
input.Timeout = aws.Int64(0)
}
}

Expand All @@ -273,24 +302,6 @@ func (rm *resourceManager) updateFunctionConfiguration(
input.VpcConfig = VPCConfig
}

if delta.DifferentAt("Spec.EphemeralStorage") {
ephemeralStorage := &svcsdk.EphemeralStorage{}
if dspec.EphemeralStorage != nil {
ephemeralStorageCopy := dspec.EphemeralStorage.DeepCopy()
ephemeralStorage.Size = ephemeralStorageCopy.Size
}
input.EphemeralStorage = ephemeralStorage
}

if delta.DifferentAt(("Spec.SnapStart")) {
snapStart := &svcsdk.SnapStart{}
if dspec.SnapStart != nil {
snapStartCopy := dspec.SnapStart.DeepCopy()
snapStart.ApplyOn = snapStartCopy.ApplyOn
}
input.SnapStart = snapStart
}

_, err = rm.sdkapi.UpdateFunctionConfigurationWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "UpdateFunctionConfiguration", err)
if err != nil {
Expand Down
3 changes: 3 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: 4 additions & 7 deletions templates/hooks/function/sdk_read_one_post_set_output.go.tpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
if resp.Code != nil {
// We need to keep the desired .Code s3Bucket s3Key and s3ObjectVersion
// part of the function's spec. So instead of setting Spec.Code to nil
// we only set ImageURI
//
// When adopting a Function resource, Spec.Code field should be manually
// initialised before injecting ImageURI.
if ko.Spec.Code == nil {
ko.Spec.Code = &svcapitypes.FunctionCode{}
}
Expand All @@ -13,7 +7,8 @@
}
}
if resp.Configuration.Layers != nil {
f16 := []*svcapitypes.Layer{}
f17 := []*svcapitypes.Layer{}
layer := []*string{}
for _, f16iter := range resp.Configuration.Layers {
f16elem := &svcapitypes.Layer{}
if f16iter.Arn != nil {
Expand All @@ -29,7 +24,9 @@
f16elem.SigningProfileVersionARN = f16iter.SigningProfileVersionArn
}
f16 = append(f16, f16elem)
layer = append(layer, f16iter.Arn)
}
ko.Spec.Layers = layer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have some smoke tests around layers updates?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

ko.Status.LayerStatuses = f16
} else {
ko.Status.LayerStatuses = nil
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/resources/function_layers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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
runtime: python3.9
handler: main
layers: [$LAYERS]
description: function created by ACK lambda-controller e2e tests
63 changes: 63 additions & 0 deletions test/e2e/tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,69 @@ def test_function_features(self, lambda_client):

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

def test_function_layers(self, lambda_client):
resource_name = random_suffix_name("functionlayers", 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.EICRole.arn
replacements["LAMBDA_FILE_NAME"] = LAMBDA_FUNCTION_FILE_ZIP
replacements["AWS_REGION"] = get_region()
replacements["LAYERS"] = "arn:aws:lambda:us-west-2:336392948345:layer:AWSSDKPandas-Python310:14"

# Load Lambda CR
resource_data = load_lambda_resource(
"function_layers",
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
layers_list = ["arn:aws:lambda:us-west-2:017000801446:layer:AWSLambdaPowertoolsPythonV2:68", "arn:aws:lambda:us-west-2:580247275435:layer:LambdaInsightsExtension:52"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qq: I guess those are official layers that anybody can reference?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, those are from the list of layers provided by AWS.

cr["spec"]["layers"] = layers_list

#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)
for i in range(len(function["Configuration"]["Layers"])) :
assert function["Configuration"]["Layers"][i]["Arn"] == layers_list[i]

# 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