Skip to content

Commit e661ce9

Browse files
authored
ensure attribute fields have a ShapeRef (#383)
When attempting to generate the SNS controller, I was getting the following failure: ``` [jaypipes@thelio code-generator]$ make build-controller SERVICE=sns building ack-generate ... ok. ==== building sns-controller ==== Copying common custom resource definitions into sns Building Kubernetes API objects for sns Generating deepcopy code for sns Generating custom resource definitions for sns Building service controller for sns Error: template: /home/jaypipes/go/src/github.com/aws-controllers-k8s/code-generator/templates/pkg/resource/delta.go.tpl:34:3: executing "/home/jaypipes/go/src/github.com/aws-controllers-k8s/code-generator/templates/pkg/resource/delta.go.tpl" at <GoCodeCompare .CRD "delta" "a.ko" "b.ko" 1>: error calling GoCodeCompare: runtime error: invalid memory address or nil pointer dereference make: *** [Makefile:41: build-controller] Error 1 ``` This failure turned out to be due to attribute-based API fields that do not have a `type: XXX` generator.yaml configuration option specified (as they do in the sqs-controller's generator.yaml) would end up getting a `pkg/model.Field` object created that had a `nil` ShapeRef. This caused problems in certain Go code generator functions (such as `pkg/generate/code.CompareResource`) that rely on `Field.ShapeRef` being non-nil. Signed-off-by: Jay Pipes <[email protected]> By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 16f0e20 commit e661ce9

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

pkg/model/field.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ import (
2525
awssdkmodel "github.com/aws/aws-sdk-go/private/model/api"
2626
)
2727

28+
// simpleStringShapeRef is used for attribute fields and fields where there was
29+
// no found ShapeRef
30+
var simpleStringShapeRef *awssdkmodel.ShapeRef = &awssdkmodel.ShapeRef{
31+
Shape: &awssdkmodel.Shape{
32+
Type: "string",
33+
},
34+
}
35+
2836
// Field represents a single field in the CRD's Spec or Status objects. The
2937
// field may be a direct field of the Spec or Status object or may be a field
3038
// of a list or struct-type field of the Spec or Status object. We call these
@@ -323,6 +331,7 @@ func newFieldRecurse(
323331
gte = "string"
324332
gt = "*string"
325333
gtwp = "*string"
334+
shapeRef = simpleStringShapeRef
326335
}
327336

328337
return &Field{

templates/apis/crd.go.tpl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ import (
1515

1616
{{ .CRD.Documentation }}
1717
type {{ .CRD.Kind }}Spec struct {
18-
{{- range $fieldName, $field := .CRD.SpecFields }}
19-
{{- if $field.ShapeRef }}
20-
{{ $field.ShapeRef.Documentation }}
21-
{{- end }}
22-
{{ if and ($field.IsRequired) (not $field.HasReference) }} // +kubebuilder:validation:Required
23-
{{ $field.Names.Camel }} {{ $field.GoType }} `json:"{{ $field.Names.CamelLower }}"`
24-
{{- else }} {{ $field.Names.Camel }} {{ $field.GoType }} `json:"{{ $field.Names.CamelLower }},omitempty"` {{ end }}
18+
{{ range $fieldName, $field := .CRD.SpecFields }}
19+
{{ if $field.ShapeRef.Documentation -}}
20+
{{ $field.ShapeRef.Documentation }}
21+
{{ end -}}
22+
{{- if and ($field.IsRequired) (not $field.HasReference) -}}
23+
// +kubebuilder:validation:Required
24+
{{ end -}}
25+
{{ $field.Names.Camel }} {{ $field.GoType }} `json:"{{ $field.Names.CamelLower }}{{- if or (not $field.IsRequired) ($field.HasReference) }},omitempty{{- end -}}"`
2526
{{- end }}
2627
}
2728

@@ -39,7 +40,7 @@ type {{ .CRD.Kind }}Status struct {
3940
// +kubebuilder:validation:Optional
4041
Conditions []*ackv1alpha1.Condition `json:"conditions"`
4142
{{- range $fieldName, $field := .CRD.StatusFields }}
42-
{{- if $field.ShapeRef }}
43+
{{- if $field.ShapeRef.Documentation }}
4344
{{ $field.ShapeRef.Documentation }}
4445
{{- end }}
4546
// +kubebuilder:validation:Optional

0 commit comments

Comments
 (0)