Skip to content

Commit f6301ef

Browse files
authored
Add reservedConcurrentExecutions field for Function Resource (#12)
The AWS Lambda API support setting up reserved concurrent executions for a lambda functions. This patch adds a new field to allow users to set the concurrency configuration for functions. Description of changes: - Add `reservedConcurrentExecutions` fields for `Function` Resource - Add a new e2e test case to specifically test update and create operations on function resources with a non-nil `reservedConcurrentExecutions` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent b605c58 commit f6301ef

File tree

15 files changed

+180
-12
lines changed

15 files changed

+180
-12
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2021-11-19T13:19:34Z"
2+
build_date: "2021-11-23T23:35:17Z"
33
build_hash: dca757058c55b80b4eb20f62f55829981a70f7de
44
go_version: go1.16.4
55
version: v0.15.2
6-
api_directory_checksum: 84d93710fb0b89a7ed99b0923611297242a55a99
6+
api_directory_checksum: a3c5e80eca3fc5591e8a0a2763d048f2ed4a6ddd
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.40.28
99
generator_config_info:
10-
file_checksum: 4f4df395ccad50e23ed16cffcf1d4d24a909f9cc
10+
file_checksum: 47d63e3b8348d4f33111e6a3cbd6eca2412e9b46
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/function.go

Lines changed: 2 additions & 0 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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ resources:
1212
Name:
1313
is_primary_key: true
1414
is_required: true
15-
# ReservedConcurrentExecutions:
16-
# from:
17-
# operation: PutFunctionConcurrency
18-
# path: ReservedConcurrentExecutions
15+
ReservedConcurrentExecutions:
16+
from:
17+
operation: PutFunctionConcurrency
18+
path: ReservedConcurrentExecutions
1919
Code:
2020
compare:
2121
is_ignored: true

apis/v1alpha1/zz_generated.deepcopy.go

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ spec:
137137
description: Set to true to publish the first version of the function
138138
during creation.
139139
type: boolean
140+
reservedConcurrentExecutions:
141+
description: The number of simultaneous executions to reserve for
142+
the function.
143+
format: int64
144+
type: integer
140145
role:
141146
description: The Amazon Resource Name (ARN) of the function's execution
142147
role.

generator.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ resources:
1212
Name:
1313
is_primary_key: true
1414
is_required: true
15-
# ReservedConcurrentExecutions:
16-
# from:
17-
# operation: PutFunctionConcurrency
18-
# path: ReservedConcurrentExecutions
15+
ReservedConcurrentExecutions:
16+
from:
17+
operation: PutFunctionConcurrency
18+
path: ReservedConcurrentExecutions
1919
Code:
2020
compare:
2121
is_ignored: true

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ spec:
137137
description: Set to true to publish the first version of the function
138138
during creation.
139139
type: boolean
140+
reservedConcurrentExecutions:
141+
description: The number of simultaneous executions to reserve for
142+
the function.
143+
format: int64
144+
type: integer
140145
role:
141146
description: The Amazon Resource Name (ARN) of the function's execution
142147
role.

pkg/resource/function/delta.go

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

pkg/resource/function/hooks.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package function
1616
import (
1717
"context"
1818

19+
svcapitypes "github.com/aws-controllers-k8s/lambda-controller/apis/v1alpha1"
1920
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
2021
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
2122
"github.com/aws/aws-sdk-go/aws"
@@ -63,6 +64,12 @@ func (rm *resourceManager) customUpdateFunction(
6364
return nil, err
6465
}
6566
}
67+
if delta.DifferentAt("Spec.ReservedConcurrentExecutions") {
68+
err = rm.updateFunctionConcurrency(ctx, desired)
69+
if err != nil {
70+
return nil, err
71+
}
72+
}
6673

6774
readOneLatest, err := rm.ReadOne(ctx, desired)
6875
if err != nil {
@@ -346,3 +353,58 @@ func customPreCompare(
346353
}
347354
}
348355
}
356+
357+
// updateFunctionConcurrency calls UpdateFunctionConcurrency to update a specific
358+
// lambda function reserved concurrent executions.
359+
func (rm *resourceManager) updateFunctionConcurrency(
360+
ctx context.Context,
361+
desired *resource,
362+
) error {
363+
var err error
364+
rlog := ackrtlog.FromContext(ctx)
365+
exit := rlog.Trace("rm.updateFunctionConcurrency")
366+
defer exit(err)
367+
368+
dspec := desired.ko.Spec
369+
input := &svcsdk.PutFunctionConcurrencyInput{
370+
FunctionName: aws.String(*dspec.Name),
371+
}
372+
373+
if desired.ko.Spec.ReservedConcurrentExecutions != nil {
374+
input.ReservedConcurrentExecutions = aws.Int64(*desired.ko.Spec.ReservedConcurrentExecutions)
375+
} else {
376+
input.ReservedConcurrentExecutions = aws.Int64(0)
377+
}
378+
379+
_, err = rm.sdkapi.PutFunctionConcurrencyWithContext(ctx, input)
380+
rm.metrics.RecordAPICall("UPDATE", "PutFunctionConcurrency", err)
381+
if err != nil {
382+
return err
383+
}
384+
return nil
385+
}
386+
387+
// setResourceAdditionalFields will describe the fields that are not return by
388+
// GetFunctionConcurrency calls
389+
func (rm *resourceManager) setResourceAdditionalFields(
390+
ctx context.Context,
391+
ko *svcapitypes.Function,
392+
) (err error) {
393+
rlog := ackrtlog.FromContext(ctx)
394+
exit := rlog.Trace("rm.setResourceAdditionalFields")
395+
defer exit(err)
396+
397+
var getFunctionConcurrencyOutput *svcsdk.GetFunctionConcurrencyOutput
398+
getFunctionConcurrencyOutput, err = rm.sdkapi.GetFunctionConcurrencyWithContext(
399+
ctx,
400+
&svcsdk.GetFunctionConcurrencyInput{
401+
FunctionName: ko.Spec.Name,
402+
},
403+
)
404+
rm.metrics.RecordAPICall("GET", "GetFunctionConcurrency", err)
405+
if err != nil {
406+
return err
407+
}
408+
ko.Spec.ReservedConcurrentExecutions = getFunctionConcurrencyOutput.ReservedConcurrentExecutions
409+
return nil
410+
}

pkg/resource/function/sdk.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.

0 commit comments

Comments
 (0)