From 682bfe2d2cba793a9e6a792635f9e41d55688a36 Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Thu, 22 Sep 2022 00:57:30 +0200 Subject: [PATCH] Initialise `spec.Code` before setting the imageURI Fixes https://github.com/aws-controllers-k8s/community/issues/1480 The runtime panics observed in issue 1480 were mainly caused by the `sdk_read_one` hook we use for `Function` resource. The hook didn't correctly initialise `spec.Code`. This patch fixes the initalisation to avoid nil pointer panics when adopting function resources --- pkg/resource/function/sdk.go | 6 ++++++ .../hooks/function/sdk_read_one_post_set_output.go.tpl | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/resource/function/sdk.go b/pkg/resource/function/sdk.go index 866ae82e..a4cb369e 100644 --- a/pkg/resource/function/sdk.go +++ b/pkg/resource/function/sdk.go @@ -104,6 +104,12 @@ func (rm *resourceManager) sdkFind( // 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{} + } if resp.Code.ImageUri != nil { ko.Spec.Code.ImageURI = resp.Code.ImageUri } diff --git a/templates/hooks/function/sdk_read_one_post_set_output.go.tpl b/templates/hooks/function/sdk_read_one_post_set_output.go.tpl index fabdbf33..e779c14d 100644 --- a/templates/hooks/function/sdk_read_one_post_set_output.go.tpl +++ b/templates/hooks/function/sdk_read_one_post_set_output.go.tpl @@ -1,7 +1,13 @@ if resp.Code != nil { - // We need to keep the desired .Code s3Bucket s3Key and s3ObjectVersion + // 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{} + } if resp.Code.ImageUri != nil { ko.Spec.Code.ImageURI = resp.Code.ImageUri }