Skip to content
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

Additional nil checks during reference resolution #326

Merged
merged 2 commits into from
Apr 21, 2022
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
3 changes: 3 additions & 0 deletions pkg/generate/ack/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ var (
"CheckNilFieldPath": func(f *ackmodel.Field, sourceVarName string) string {
return code.CheckNilFieldPath(f, sourceVarName)
},
"CheckNilReferencesPath": func(f *ackmodel.Field, sourceVarName string) string {
return code.CheckNilReferencesPath(f, sourceVarName)
},
}
)

Expand Down
26 changes: 26 additions & 0 deletions pkg/generate/code/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,29 @@ func CheckNilFieldPath(field *model.Field, sourceVarName string) string {
}
return strings.TrimPrefix(out, " || ")
}

// CheckNilReferencesPath returns the condition statement for Nil check
// on the path in ReferencesConfig. This nil check on the reference path is
// useful to avoid nil pointer panics when accessing the referenced value.
//
// This function only outputs the logical condition and not the "if" block
// so that the output can be reused in many templates, where
Copy link
Contributor

Choose a reason for hiding this comment

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

🚀

// logic inside "if" block can be different.
//
// Example Output for ReferencesConfig path "Status.ACKResourceMetadata.ARN",
// and sourceVarName "obj" is
// "obj.Status.ACKResourceMetadata == nil || obj.Status.ACKResourceMetadata.ARN == nil"
func CheckNilReferencesPath(field *model.Field, sourceVarName string) string {
out := ""
if field.HasReference() {
refPath := fieldpath.FromString(field.FieldConfig.References.Path)
// Remove the front from reference path because "Spec" or "Status" being
// an struct cannot be added in nil check
fieldNamePrefix := "." + refPath.PopFront()
for refPath.Size() > 0 {
fieldNamePrefix = fmt.Sprintf("%s.%s", fieldNamePrefix, refPath.PopFront())
out += fmt.Sprintf(" || %s%s == nil", sourceVarName, fieldNamePrefix)
}
}
vijtrip2 marked this conversation as resolved.
Show resolved Hide resolved
return strings.TrimPrefix(out, " || ")
}
24 changes: 24 additions & 0 deletions pkg/generate/code/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"strings"
"testing"

"github.com/aws-controllers-k8s/code-generator/pkg/config"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -155,3 +157,25 @@ func TestCheckNilFieldPath(t *testing.T) {
"ko.Spec.JWTConfiguration == nil || ko.Spec.JWTConfiguration.Issuer == nil",
code.CheckNilFieldPath(&field, "ko.Spec"))
}

func TestCheckNilReferencesPath(t *testing.T) {
field := model.Field{}
// Empty ReferencesPath
referenceFieldConfig := config.ReferencesConfig{Path: ""}
fieldConfig := config.FieldConfig{References: &referenceFieldConfig}
field.FieldConfig = &fieldConfig
assert.Equal(t, "", code.CheckNilReferencesPath(&field, "obj"))
// Non nested ReferencesPath
referenceFieldConfig.Path = "Status"
assert.Equal(t, "", code.CheckNilReferencesPath(&field, "obj"))
// Nested ReferencesPath
referenceFieldConfig.Path = "Status.ACKResourceMetadata"
assert.Equal(t,
"obj.Status.ACKResourceMetadata == nil",
code.CheckNilReferencesPath(&field, "obj"))
// Multi Level Nested ReferencesPath
referenceFieldConfig.Path = "Status.ACKResourceMetadata.ARN"
assert.Equal(t,
"obj.Status.ACKResourceMetadata == nil || obj.Status.ACKResourceMetadata.ARN == nil",
code.CheckNilReferencesPath(&field, "obj"))
}
6 changes: 3 additions & 3 deletions templates/cmd/controller/main.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import (
ctrlrt "sigs.k8s.io/controller-runtime"
ctrlrtmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
svcsdk "github.com/aws/aws-sdk-go/service/{{ .ServicePackageName }}"

svcresource "github.com/aws-controllers-k8s/{{ .ServicePackageName }}-controller/pkg/resource"
svctypes "github.com/aws-controllers-k8s/{{ .ServicePackageName }}-controller/apis/{{ .APIVersion }}"
{{- /* Import the go types from service controllers whose resources are referenced in this service controller.
If these referenced types are not added to scheme, this service controller will not be able to read
resources across service controller. */ -}}
Expand All @@ -29,6 +26,9 @@ resources across service controller. */ -}}
{{ $referencedServiceName }}apitypes "github.com/aws-controllers-k8s/{{ $referencedServiceName }}-controller/apis/{{ $apiVersion }}"
{{- end }}
{{- end }}

svcresource "github.com/aws-controllers-k8s/{{ .ServicePackageName }}-controller/pkg/resource"
svctypes "github.com/aws-controllers-k8s/{{ .ServicePackageName }}-controller/apis/{{ .APIVersion }}"
vijtrip2 marked this conversation as resolved.
Show resolved Hide resolved
{{/* TODO(a-hilaly): import apis/* packages to register webhooks */}}
{{range $crdName := .SnakeCasedCRDNames }}_ "github.com/aws-controllers-k8s/{{ $servicePackageName }}-controller/pkg/resource/{{ $crdName }}"
{{end}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ Where field is of type 'Field' from aws-controllers-k8s/code-generator/pkg/model
"{{ .FieldConfig.References.Resource }}",
namespace, *arr.Name)
}
if obj.{{ .FieldConfig.References.Path }} == nil {
{{ $nilCheck := CheckNilReferencesPath . "obj" -}}
{{ if not (eq $nilCheck "") -}}
if {{ $nilCheck }} {
return ackerr.ResourceReferenceMissingTargetFieldFor(
"{{ .FieldConfig.References.Resource }}",
namespace, *arr.Name,
"{{ .FieldConfig.References.Path }}")
}
{{- end -}}
{{- end -}}