Skip to content

Commit d0a1dd3

Browse files
committed
Bug fix for EventInvokeConfig in Function
1 parent c4e68a1 commit d0a1dd3

File tree

2 files changed

+158
-63
lines changed

2 files changed

+158
-63
lines changed

pkg/resource/function/hooks.go

Lines changed: 147 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -497,41 +497,53 @@ func (rm *resourceManager) updateFunctionEventInvokeConfig(
497497
) error {
498498
var err error
499499
rlog := ackrtlog.FromContext(ctx)
500-
exit := rlog.Trace("rm.updateFunctionEventInvokeConfig")
500+
exit := rlog.Trace("rm.syncEventInvokeConfig")
501501
defer exit(err)
502502

503+
// Check if the user deleted the 'FunctionEventInvokeConfig' configuration
504+
// If yes, delete FunctionEventInvokeConfig
505+
if desired.ko.Spec.FunctionEventInvokeConfig == nil {
506+
input_delete := &svcsdk.DeleteFunctionEventInvokeConfigInput{
507+
FunctionName: aws.String(*desired.ko.Spec.Name),
508+
}
509+
_, err = rm.sdkapi.DeleteFunctionEventInvokeConfigWithContext(ctx, input_delete)
510+
rm.metrics.RecordAPICall("DELETE", "DeleteFunctionEventInvokeConfig", err)
511+
if err != nil {
512+
return err
513+
}
514+
return nil
515+
}
516+
503517
dspec := desired.ko.Spec
504518
input := &svcsdk.PutFunctionEventInvokeConfigInput{
505519
FunctionName: aws.String(*dspec.Name),
506520
}
507521

508-
if desired.ko.Spec.FunctionEventInvokeConfig != nil {
509-
if desired.ko.Spec.FunctionEventInvokeConfig.MaximumEventAgeInSeconds != nil {
510-
input.MaximumEventAgeInSeconds = aws.Int64(*desired.ko.Spec.FunctionEventInvokeConfig.MaximumEventAgeInSeconds)
511-
}
512-
if desired.ko.Spec.FunctionEventInvokeConfig.MaximumRetryAttempts != nil {
513-
input.MaximumRetryAttempts = aws.Int64(*desired.ko.Spec.FunctionEventInvokeConfig.MaximumRetryAttempts)
514-
}
515-
if desired.ko.Spec.FunctionEventInvokeConfig.DestinationConfig != nil {
516-
destinations := &svcsdk.DestinationConfig{}
517-
if desired.ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnFailure != nil {
518-
destinations.OnFailure = &svcsdk.OnFailure{}
519-
if desired.ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnFailure.Destination != nil {
520-
destinations.OnFailure.Destination = aws.String(*desired.ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnFailure.Destination)
521-
}
522+
if dspec.FunctionEventInvokeConfig.DestinationConfig != nil {
523+
destinations := &svcsdk.DestinationConfig{}
524+
if dspec.FunctionEventInvokeConfig.DestinationConfig.OnFailure != nil {
525+
destinations.OnFailure = &svcsdk.OnFailure{}
526+
if dspec.FunctionEventInvokeConfig.DestinationConfig.OnFailure.Destination != nil {
527+
destinations.OnFailure.Destination = aws.String(*dspec.FunctionEventInvokeConfig.DestinationConfig.OnFailure.Destination)
522528
}
523-
if desired.ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnSuccess != nil {
524-
destinations.OnSuccess = &svcsdk.OnSuccess{}
525-
if desired.ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnSuccess.Destination != nil {
526-
destinations.OnSuccess.Destination = aws.String(*desired.ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnSuccess.Destination)
527-
}
529+
}
530+
if dspec.FunctionEventInvokeConfig.DestinationConfig.OnSuccess != nil {
531+
destinations.OnSuccess = &svcsdk.OnSuccess{}
532+
if dspec.FunctionEventInvokeConfig.DestinationConfig.OnSuccess.Destination != nil {
533+
destinations.OnSuccess.Destination = aws.String(*dspec.FunctionEventInvokeConfig.DestinationConfig.OnSuccess.Destination)
528534
}
529-
input.DestinationConfig = destinations
530535
}
536+
input.DestinationConfig = destinations
537+
}
538+
if dspec.FunctionEventInvokeConfig.MaximumEventAgeInSeconds != nil {
539+
input.MaximumEventAgeInSeconds = aws.Int64(*dspec.FunctionEventInvokeConfig.MaximumEventAgeInSeconds)
540+
}
541+
if dspec.FunctionEventInvokeConfig.MaximumRetryAttempts != nil {
542+
input.MaximumRetryAttempts = aws.Int64(*dspec.FunctionEventInvokeConfig.MaximumRetryAttempts)
531543
}
532544

533545
_, err = rm.sdkapi.PutFunctionEventInvokeConfigWithContext(ctx, input)
534-
rm.metrics.RecordAPICall("UPDATE", "PutFunctionEventInvokeConfig", err)
546+
rm.metrics.RecordAPICall("UPDATE", "SyncEventInvokeConfig", err)
535547
if err != nil {
536548
return err
537549
}
@@ -591,16 +603,11 @@ func (rm *resourceManager) deleteFunctionCodeSigningConfig(
591603
return nil
592604
}
593605

594-
// setResourceAdditionalFields will describe the fields that are not return by
595-
// GetFunctionConcurrency calls
596-
func (rm *resourceManager) setResourceAdditionalFields(
606+
// getFunctionConcurrency will describe the fields that are not return by GetFunctionConcurrency calls
607+
func (rm *resourceManager) getFunctionConcurrency(
597608
ctx context.Context,
598609
ko *svcapitypes.Function,
599610
) (err error) {
600-
rlog := ackrtlog.FromContext(ctx)
601-
exit := rlog.Trace("rm.setResourceAdditionalFields")
602-
defer exit(err)
603-
604611
var getFunctionConcurrencyOutput *svcsdk.GetFunctionConcurrencyOutput
605612
getFunctionConcurrencyOutput, err = rm.sdkapi.GetFunctionConcurrencyWithContext(
606613
ctx,
@@ -614,63 +621,140 @@ func (rm *resourceManager) setResourceAdditionalFields(
614621
}
615622
ko.Spec.ReservedConcurrentExecutions = getFunctionConcurrencyOutput.ReservedConcurrentExecutions
616623

617-
var getFunctionEventInvokeConfigOutput *svcsdk.GetFunctionEventInvokeConfigOutput
618-
getFunctionEventInvokeConfigOutput, err = rm.sdkapi.GetFunctionEventInvokeConfigWithContext(
624+
return nil
625+
}
626+
627+
// getFunctionCodeSigningConfig will describe the code signing
628+
// fields for the Function resource
629+
func (rm *resourceManager) getFunctionCodeSigningConfig(
630+
ctx context.Context,
631+
ko *svcapitypes.Function,
632+
) (err error) {
633+
var getFunctionCodeSigningConfigOutput *svcsdk.GetFunctionCodeSigningConfigOutput
634+
getFunctionCodeSigningConfigOutput, err = rm.sdkapi.GetFunctionCodeSigningConfigWithContext(
619635
ctx,
620-
&svcsdk.GetFunctionEventInvokeConfigInput{
636+
&svcsdk.GetFunctionCodeSigningConfigInput{
621637
FunctionName: ko.Spec.Name,
622638
},
623639
)
624-
rm.metrics.RecordAPICall("GET", "GetFunctionEventInvokeConfig", err)
640+
rm.metrics.RecordAPICall("GET", "GetFunctionCodeSigningConfig", err)
625641
if err != nil {
626-
if awserr, ok := ackerr.AWSError(err); ok && (awserr.Code() == "EventInvokeConfigNotFoundException" || awserr.Code() == "ResourceNotFoundException") {
642+
return err
643+
}
644+
ko.Spec.CodeSigningConfigARN = getFunctionCodeSigningConfigOutput.CodeSigningConfigArn
645+
646+
return nil
647+
}
648+
649+
func (rm *resourceManager) setFunctionEventInvokeConfigFromResponse(
650+
ko *svcapitypes.Function,
651+
getFunctionEventInvokeConfigOutput *svcsdk.GetFunctionEventInvokeConfigOutput,
652+
apiError error,
653+
) (err error) {
654+
655+
if apiError != nil {
656+
if awserr, ok := ackerr.AWSError(apiError); ok && (awserr.Code() == "EventInvokeConfigNotFoundException" || awserr.Code() == "ResourceNotFoundException") {
627657
ko.Spec.FunctionEventInvokeConfig = nil
628658
} else {
629-
return err
659+
return apiError
630660
}
631661
} else {
632-
if getFunctionEventInvokeConfigOutput.DestinationConfig != nil {
633-
if getFunctionEventInvokeConfigOutput.DestinationConfig.OnFailure != nil {
634-
if getFunctionEventInvokeConfigOutput.DestinationConfig.OnFailure.Destination != nil {
635-
ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnFailure.Destination = getFunctionEventInvokeConfigOutput.DestinationConfig.OnFailure.Destination
662+
if ko.Spec.FunctionEventInvokeConfig != nil {
663+
if getFunctionEventInvokeConfigOutput.DestinationConfig != nil {
664+
if getFunctionEventInvokeConfigOutput.DestinationConfig.OnFailure != nil {
665+
if getFunctionEventInvokeConfigOutput.DestinationConfig.OnFailure.Destination != nil {
666+
ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnFailure.Destination = getFunctionEventInvokeConfigOutput.DestinationConfig.OnFailure.Destination
667+
}
636668
}
637-
}
638-
if getFunctionEventInvokeConfigOutput.DestinationConfig.OnSuccess != nil {
639-
if getFunctionEventInvokeConfigOutput.DestinationConfig.OnSuccess.Destination != nil {
640-
ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnSuccess.Destination = getFunctionEventInvokeConfigOutput.DestinationConfig.OnSuccess.Destination
669+
if getFunctionEventInvokeConfigOutput.DestinationConfig.OnSuccess != nil {
670+
if getFunctionEventInvokeConfigOutput.DestinationConfig.OnSuccess.Destination != nil {
671+
ko.Spec.FunctionEventInvokeConfig.DestinationConfig.OnSuccess.Destination = getFunctionEventInvokeConfigOutput.DestinationConfig.OnSuccess.Destination
672+
}
641673
}
674+
} else {
675+
ko.Spec.FunctionEventInvokeConfig.DestinationConfig = nil
676+
}
677+
if getFunctionEventInvokeConfigOutput.MaximumEventAgeInSeconds != nil {
678+
ko.Spec.FunctionEventInvokeConfig.MaximumEventAgeInSeconds = getFunctionEventInvokeConfigOutput.MaximumEventAgeInSeconds
679+
} else {
680+
ko.Spec.FunctionEventInvokeConfig.MaximumEventAgeInSeconds = nil
681+
}
682+
if getFunctionEventInvokeConfigOutput.DestinationConfig != nil {
683+
ko.Spec.FunctionEventInvokeConfig.MaximumRetryAttempts = getFunctionEventInvokeConfigOutput.MaximumRetryAttempts
684+
} else {
685+
ko.Spec.FunctionEventInvokeConfig.MaximumRetryAttempts = nil
642686
}
643687
} else {
644-
ko.Spec.FunctionEventInvokeConfig.DestinationConfig = nil
645-
}
646-
if getFunctionEventInvokeConfigOutput.MaximumEventAgeInSeconds != nil {
647-
ko.Spec.FunctionEventInvokeConfig.MaximumEventAgeInSeconds = getFunctionEventInvokeConfigOutput.MaximumEventAgeInSeconds
648-
} else {
649-
ko.Spec.FunctionEventInvokeConfig.MaximumEventAgeInSeconds = nil
650-
}
651-
if getFunctionEventInvokeConfigOutput.DestinationConfig != nil {
652-
ko.Spec.FunctionEventInvokeConfig.MaximumRetryAttempts = getFunctionEventInvokeConfigOutput.MaximumRetryAttempts
653-
} else {
654-
ko.Spec.FunctionEventInvokeConfig.MaximumRetryAttempts = nil
688+
cloudFunctionEventInvokeConfig := &svcapitypes.PutFunctionEventInvokeConfigInput{}
689+
cloudFunctionEventInvokeConfig.DestinationConfig = &svcapitypes.DestinationConfig{}
690+
cloudFunctionEventInvokeConfig.DestinationConfig.OnFailure = &svcapitypes.OnFailure{}
691+
cloudFunctionEventInvokeConfig.DestinationConfig.OnSuccess = &svcapitypes.OnSuccess{}
692+
cloudFunctionEventInvokeConfig.DestinationConfig.OnFailure.Destination = getFunctionEventInvokeConfigOutput.DestinationConfig.OnFailure.Destination
693+
cloudFunctionEventInvokeConfig.DestinationConfig.OnSuccess.Destination = getFunctionEventInvokeConfigOutput.DestinationConfig.OnSuccess.Destination
694+
cloudFunctionEventInvokeConfig.MaximumEventAgeInSeconds = getFunctionEventInvokeConfigOutput.MaximumEventAgeInSeconds
695+
cloudFunctionEventInvokeConfig.MaximumRetryAttempts = getFunctionEventInvokeConfigOutput.MaximumRetryAttempts
696+
ko.Spec.FunctionEventInvokeConfig = cloudFunctionEventInvokeConfig
655697
}
656698
}
699+
return nil
700+
}
701+
702+
// getFunctionEventInvokeConfig will describe the fields that are
703+
// custom to the Function resource
704+
func (rm *resourceManager) getFunctionEventInvokeConfig(
705+
ctx context.Context,
706+
ko *svcapitypes.Function,
707+
) (err error) {
708+
var getFunctionEventInvokeConfigOutput *svcsdk.GetFunctionEventInvokeConfigOutput
709+
getFunctionEventInvokeConfigOutput, err = rm.sdkapi.GetFunctionEventInvokeConfigWithContext(
710+
ctx,
711+
&svcsdk.GetFunctionEventInvokeConfigInput{
712+
FunctionName: ko.Spec.Name,
713+
},
714+
)
715+
rm.metrics.RecordAPICall("GET", "GetFunctionEventInvokeConfig", err)
716+
717+
err = rm.setFunctionEventInvokeConfigFromResponse(ko, getFunctionEventInvokeConfigOutput, err)
718+
if err != nil {
719+
return err
720+
}
721+
722+
return nil
723+
}
724+
725+
// setResourceAdditionalFields will describe the fields that are not return by
726+
// API calls
727+
func (rm *resourceManager) setResourceAdditionalFields(
728+
ctx context.Context,
729+
ko *svcapitypes.Function,
730+
) (err error) {
731+
rlog := ackrtlog.FromContext(ctx)
732+
exit := rlog.Trace("rm.setResourceAdditionalFields")
733+
defer exit(err)
734+
735+
// To set Function Concurrency for the function
736+
err = rm.getFunctionConcurrency(ctx, ko)
737+
if err != nil {
738+
return err
739+
}
740+
741+
// To set Asynchronous Invocations for the function
742+
err = rm.getFunctionEventInvokeConfig(ctx, ko)
743+
if err != nil {
744+
return err
745+
}
746+
747+
// To set Code Signing Config based on the PackageType for the function
657748
if ko.Spec.PackageType != nil && *ko.Spec.PackageType == "Zip" {
658-
var getFunctionCodeSigningConfigOutput *svcsdk.GetFunctionCodeSigningConfigOutput
659-
getFunctionCodeSigningConfigOutput, err = rm.sdkapi.GetFunctionCodeSigningConfigWithContext(
660-
ctx,
661-
&svcsdk.GetFunctionCodeSigningConfigInput{
662-
FunctionName: ko.Spec.Name,
663-
},
664-
)
665-
rm.metrics.RecordAPICall("GET", "GetFunctionCodeSigningConfig", err)
749+
err = rm.getFunctionCodeSigningConfig(ctx, ko)
666750
if err != nil {
667751
return err
668752
}
669-
ko.Spec.CodeSigningConfigARN = getFunctionCodeSigningConfigOutput.CodeSigningConfigArn
670753
}
671754
if ko.Spec.PackageType != nil && *ko.Spec.PackageType == "Image" &&
672755
ko.Spec.CodeSigningConfigARN != nil && *ko.Spec.CodeSigningConfigARN != "" {
673756
return ackerr.NewTerminalError(ErrCannotSetFunctionCSC)
674757
}
758+
675759
return nil
676760
}

test/e2e/tests/test_function.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,17 @@ def test_function_event_invoke_config(self, lambda_client):
596596
assert function_event_invoke_config["MaximumEventAgeInSeconds"] == 200
597597
assert function_event_invoke_config["MaximumRetryAttempts"] == 2
598598

599+
# Delete FunctionEventInvokeConfig
600+
cr = k8s.wait_resource_consumed_by_controller(ref)
601+
cr["spec"]["functionEventInvokeConfig"] = None
602+
603+
# Patch k8s resource
604+
k8s.patch_custom_resource(ref, cr)
605+
time.sleep(UPDATE_WAIT_AFTER_SECONDS)
606+
607+
# Check if FunctionEventInvokeConfig is deleted
608+
assert not lambda_validator.get_function_event_invoke_config(resource_name)
609+
599610
# Delete k8s resource
600611
_, deleted = k8s.delete_custom_resource(ref)
601612
assert deleted is True

0 commit comments

Comments
 (0)