Skip to content

Commit 00df311

Browse files
authored
Adding ProvisionedConcurrency Support for Alias (#97)
Issue #, if available: **Summary** Adding support to configure `ProvisionedConcurrency` for alias in ACK. **Description** This PR adds support to configure `ProvisionedConcurrency` for Lambda function's alias using ACK. To implement this functionality I added `ProvisionedConcurrencyConfig` as an inline property to alias resource. Thus the user can directly set configurations for Provisioned Concurrency while creating alias. The user can edit the following code to set Provisioned Concurrency along with alias. ``` apiVersion: lambda.services.k8s.aws/v1alpha1 kind: Alias metadata: name: $ALIAS_NAME annotations: services.k8s.aws/region: $AWS_REGION spec: name: $ALIAS_NAME functionName: $FUNCTION_NAME functionVersion: $FUNCTION_VERSION description: alias created by ACK lambda-controller e2e tests provisionedConcurrencyConfig: provisionedConcurrentExecutions: $PROVISIONED_CONCURRENT_EXECUTIONS ``` This PR includes both implementation code and e2e tests. **Acknowledgement** By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 34cf9ff commit 00df311

18 files changed

+345
-17
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2023-07-17T23:54:05Z"
2+
build_date: "2023-07-24T18:19:28Z"
33
build_hash: e9b68590da73ce9143ba1e4361cebdc1d876c81e
4-
go_version: go1.20.5
4+
go_version: go1.19
55
version: v0.26.1-7-ge9b6859
6-
api_directory_checksum: 5d5c7aea8863c47e7303cc870aad4250267d93d2
6+
api_directory_checksum: 8f80588f678cbcff979aa67cf82e547315fdb2b8
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.181
99
generator_config_info:
10-
file_checksum: a3a0fb7f87067244de99c0b37aaaf917b026f03d
10+
file_checksum: 8b0c7bd625c74c9a471db45bbf8697069d35b218
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/alias.go

Lines changed: 6 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ resources:
108108
from:
109109
operation: PutFunctionEventInvokeConfig
110110
path: .
111+
ProvisionedConcurrencyConfig:
112+
from:
113+
operation: PutProvisionedConcurrencyConfig
114+
path: .
111115
hooks:
112116
sdk_update_pre_build_request:
113117
template_path: hooks/alias/sdk_update_pre_build_request.go.tpl

apis/v1alpha1/types.go

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

apis/v1alpha1/zz_generated.deepcopy.go

Lines changed: 40 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_aliases.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ spec:
104104
name:
105105
description: The name of the alias.
106106
type: string
107+
provisionedConcurrencyConfig:
108+
description: "Configures provisioned concurrency to a function's alias
109+
\n - ProvisionedConcurrentExecutions The amount of provisioned concurrency
110+
to allocate for the version or alias. Minimum value of 1 is required"
111+
properties:
112+
functionName:
113+
type: string
114+
provisionedConcurrentExecutions:
115+
format: int64
116+
type: integer
117+
qualifier:
118+
type: string
119+
type: object
107120
routingConfig:
108121
description: The routing configuration (https://docs.aws.amazon.com/lambda/latest/dg/configuration-aliases.html#configuring-alias-routing)
109122
of the alias.

documentation.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,11 @@ resources:
3939
4040
- MaximumRetryAttempts
4141
The maximum number of times to retry when the function returns an error.
42+
43+
ProvisionedConcurrencyConfig:
44+
prepend: |
45+
Configures provisioned concurrency to a function's alias
46+
47+
- ProvisionedConcurrentExecutions
48+
The amount of provisioned concurrency to allocate for the version or alias.
49+
Minimum value of 1 is required

generator.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ resources:
108108
from:
109109
operation: PutFunctionEventInvokeConfig
110110
path: .
111+
ProvisionedConcurrencyConfig:
112+
from:
113+
operation: PutProvisionedConcurrencyConfig
114+
path: .
111115
hooks:
112116
sdk_update_pre_build_request:
113117
template_path: hooks/alias/sdk_update_pre_build_request.go.tpl

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ spec:
104104
name:
105105
description: The name of the alias.
106106
type: string
107+
provisionedConcurrencyConfig:
108+
description: "Configures provisioned concurrency to a function's alias
109+
\n - ProvisionedConcurrentExecutions The amount of provisioned concurrency
110+
to allocate for the version or alias. Minimum value of 1 is required"
111+
properties:
112+
functionName:
113+
type: string
114+
provisionedConcurrentExecutions:
115+
format: int64
116+
type: integer
117+
qualifier:
118+
type: string
119+
type: object
107120
routingConfig:
108121
description: The routing configuration (https://docs.aws.amazon.com/lambda/latest/dg/configuration-aliases.html#configuring-alias-routing)
109122
of the alias.

pkg/resource/alias/delta.go

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

pkg/resource/alias/hooks.go

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

19+
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
1920
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
2021
"github.com/aws/aws-sdk-go/aws"
2122
svcsdk "github.com/aws/aws-sdk-go/service/lambda"
@@ -70,14 +71,71 @@ func (rm *resourceManager) syncEventInvokeConfig(
7071
return r, nil
7172
}
7273

73-
func (rm *resourceManager) setResourceAdditionalFields(
74+
func (rm *resourceManager) updateProvisionedConcurrency(
7475
ctx context.Context,
75-
ko *svcapitypes.Alias,
76-
) (err error) {
76+
desired *resource,
77+
) error {
78+
var err error
7779
rlog := ackrtlog.FromContext(ctx)
78-
exit := rlog.Trace("rm.setResourceAdditionalFields")
80+
exit := rlog.Trace("rm.updateProvisionedConcurrency")
7981
defer exit(err)
8082

83+
dspec := desired.ko.Spec
84+
input := &svcsdk.PutProvisionedConcurrencyConfigInput{
85+
FunctionName: aws.String(*dspec.FunctionName),
86+
Qualifier: aws.String(*dspec.Name),
87+
}
88+
89+
if desired.ko.Spec.ProvisionedConcurrencyConfig != nil {
90+
if desired.ko.Spec.ProvisionedConcurrencyConfig.ProvisionedConcurrentExecutions != nil {
91+
input.ProvisionedConcurrentExecutions = aws.Int64(*desired.ko.Spec.ProvisionedConcurrencyConfig.ProvisionedConcurrentExecutions)
92+
} else {
93+
input.ProvisionedConcurrentExecutions = aws.Int64(0)
94+
}
95+
} else {
96+
input.ProvisionedConcurrentExecutions = aws.Int64(0)
97+
}
98+
99+
_, err = rm.sdkapi.PutProvisionedConcurrencyConfigWithContext(ctx, input)
100+
rm.metrics.RecordAPICall("UPDATE", "UpdateProvisionedConcurrency", err)
101+
if err != nil {
102+
return err
103+
}
104+
return nil
105+
}
106+
107+
func (rm *resourceManager) getProvisionedConcurrencyConfig(
108+
ctx context.Context,
109+
ko *svcapitypes.Alias,
110+
) (err error) {
111+
112+
var getProvisionedConcurrencyConfigOutput *svcsdk.GetProvisionedConcurrencyConfigOutput
113+
getProvisionedConcurrencyConfigOutput, err = rm.sdkapi.GetProvisionedConcurrencyConfigWithContext(
114+
ctx,
115+
&svcsdk.GetProvisionedConcurrencyConfigInput{
116+
FunctionName: ko.Spec.FunctionName,
117+
Qualifier: ko.Spec.Name,
118+
},
119+
)
120+
rm.metrics.RecordAPICall("GET", "GetProvisionedConcurrencyConfig", err)
121+
122+
if err != nil {
123+
if awserr, ok := ackerr.AWSError(err); ok && (awserr.Code() == "ProvisionedConcurrencyConfigNotFoundException" || awserr.Code() == "ResourceNotFoundException") {
124+
ko.Spec.ProvisionedConcurrencyConfig = nil
125+
} else {
126+
return err
127+
}
128+
} else {
129+
ko.Spec.ProvisionedConcurrencyConfig.ProvisionedConcurrentExecutions = getProvisionedConcurrencyConfigOutput.RequestedProvisionedConcurrentExecutions
130+
}
131+
132+
return nil
133+
}
134+
135+
func (rm *resourceManager) getFunctionEventInvokeConfig(
136+
ctx context.Context,
137+
ko *svcapitypes.Alias,
138+
) (err error) {
81139
var getFunctionEventInvokeConfigOutput *svcsdk.GetFunctionEventInvokeConfigOutput
82140
getFunctionEventInvokeConfigOutput, err = rm.sdkapi.GetFunctionEventInvokeConfigWithContext(
83141
ctx,
@@ -88,8 +146,13 @@ func (rm *resourceManager) setResourceAdditionalFields(
88146
)
89147

90148
rm.metrics.RecordAPICall("GET", "GetFunctionEventInvokeConfig", err)
149+
91150
if err != nil {
92-
ko.Spec.FunctionEventInvokeConfig = nil
151+
if awserr, ok := ackerr.AWSError(err); ok && (awserr.Code() == "EventInvokeConfigNotFoundException" || awserr.Code() == "ResourceNotFoundException") {
152+
ko.Spec.FunctionEventInvokeConfig = nil
153+
} else {
154+
return err
155+
}
93156
} else {
94157
if getFunctionEventInvokeConfigOutput.DestinationConfig != nil {
95158
if getFunctionEventInvokeConfigOutput.DestinationConfig.OnFailure != nil {
@@ -116,5 +179,27 @@ func (rm *resourceManager) setResourceAdditionalFields(
116179
ko.Spec.FunctionEventInvokeConfig.MaximumRetryAttempts = nil
117180
}
118181
}
182+
183+
return nil
184+
}
185+
186+
func (rm *resourceManager) setResourceAdditionalFields(
187+
ctx context.Context,
188+
ko *svcapitypes.Alias,
189+
) (err error) {
190+
rlog := ackrtlog.FromContext(ctx)
191+
exit := rlog.Trace("rm.setResourceAdditionalFields")
192+
defer exit(err)
193+
194+
eic_err := rm.getFunctionEventInvokeConfig(ctx, ko)
195+
if eic_err != nil {
196+
return eic_err
197+
}
198+
199+
pc_err := rm.getProvisionedConcurrencyConfig(ctx, ko)
200+
if pc_err != nil {
201+
return pc_err
202+
}
203+
119204
return nil
120205
}

pkg/resource/alias/sdk.go

Lines changed: 14 additions & 1 deletion
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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,11 @@ func (rm *resourceManager) setResourceAdditionalFields(
623623
)
624624
rm.metrics.RecordAPICall("GET", "GetFunctionEventInvokeConfig", err)
625625
if err != nil {
626-
ko.Spec.FunctionEventInvokeConfig = nil
626+
if awserr, ok := ackerr.AWSError(err); ok && (awserr.Code() == "EventInvokeConfigNotFoundException" || awserr.Code() == "ResourceNotFoundException") {
627+
ko.Spec.FunctionEventInvokeConfig = nil
628+
} else {
629+
return err
630+
}
627631
} else {
628632
if getFunctionEventInvokeConfigOutput.DestinationConfig != nil {
629633
if getFunctionEventInvokeConfigOutput.DestinationConfig.OnFailure != nil {

0 commit comments

Comments
 (0)