Skip to content

Commit

Permalink
Add a config.Resource configuration option to be able to mark
Browse files Browse the repository at this point in the history
the generated CRD API version as the storage version.

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed Apr 18, 2024
1 parent 767b88f commit 7f036c3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func DefaultResource(name string, terraformSchema *schema.Resource, terraformPlu
UseAsync: true,
SchemaElementOptions: make(SchemaElementOptions),
ServerSideApplyMergeStrategies: make(ServerSideApplyMergeStrategies),
MarkStorageVersion: true,
}
for _, f := range opts {
f(r)
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestDefaultResource(t *testing.T) {
UseAsync: true,
SchemaElementOptions: SchemaElementOptions{},
ServerSideApplyMergeStrategies: ServerSideApplyMergeStrategies{},
MarkStorageVersion: true,
},
},
"TwoSectionsName": {
Expand All @@ -63,6 +64,7 @@ func TestDefaultResource(t *testing.T) {
UseAsync: true,
SchemaElementOptions: SchemaElementOptions{},
ServerSideApplyMergeStrategies: ServerSideApplyMergeStrategies{},
MarkStorageVersion: true,
},
},
"NameWithPrefixAcronym": {
Expand All @@ -81,6 +83,7 @@ func TestDefaultResource(t *testing.T) {
UseAsync: true,
SchemaElementOptions: SchemaElementOptions{},
ServerSideApplyMergeStrategies: ServerSideApplyMergeStrategies{},
MarkStorageVersion: true,
},
},
"NameWithSuffixAcronym": {
Expand All @@ -99,6 +102,7 @@ func TestDefaultResource(t *testing.T) {
UseAsync: true,
SchemaElementOptions: SchemaElementOptions{},
ServerSideApplyMergeStrategies: ServerSideApplyMergeStrategies{},
MarkStorageVersion: true,
},
},
"NameWithMultipleAcronyms": {
Expand All @@ -117,6 +121,7 @@ func TestDefaultResource(t *testing.T) {
UseAsync: true,
SchemaElementOptions: SchemaElementOptions{},
ServerSideApplyMergeStrategies: ServerSideApplyMergeStrategies{},
MarkStorageVersion: true,
},
},
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,11 @@ type Resource struct {
// SchemaElementOption for configuring options for schema elements.
SchemaElementOptions SchemaElementOptions

// MarkStorageVersion sets the generated CRD API version as
// the CRD storage version. The default value is true, i.e., the generated
// version is by default the storage version.
MarkStorageVersion bool

// listConversionPaths is the Terraform field paths of embedded objects that
// need to be converted into singleton lists (lists of at most one element)
// at runtime.
Expand Down
18 changes: 10 additions & 8 deletions pkg/pipeline/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go/types"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -85,14 +86,15 @@ func (cg *CRDGenerator) Generate(cfg *config.Resource) (string, error) {
vars := map[string]any{
"Types": typesStr,
"CRD": map[string]string{
"APIVersion": cfg.Version,
"Group": cg.Group,
"Kind": cfg.Kind,
"ForProviderType": gen.ForProviderType.Obj().Name(),
"InitProviderType": gen.InitProviderType.Obj().Name(),
"AtProviderType": gen.AtProviderType.Obj().Name(),
"ValidationRules": gen.ValidationRules,
"Path": cfg.Path,
"APIVersion": cfg.Version,
"Group": cg.Group,
"Kind": cfg.Kind,
"MarkStorageVersion": strconv.FormatBool(cfg.MarkStorageVersion),
"ForProviderType": gen.ForProviderType.Obj().Name(),
"InitProviderType": gen.InitProviderType.Obj().Name(),
"AtProviderType": gen.AtProviderType.Obj().Name(),
"ValidationRules": gen.ValidationRules,
"Path": cfg.Path,
},
"Provider": map[string]string{
"ShortName": cg.ProviderShortName,
Expand Down
2 changes: 2 additions & 0 deletions pkg/pipeline/templates/crd_types.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ type {{ .CRD.Kind }}Status struct {

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
{{ if eq .CRD.MarkStorageVersion "true" -}}
// +kubebuilder:storageversion
{{ end }}

// {{ .CRD.Kind }} is the Schema for the {{ .CRD.Kind }}s API. {{ .CRD.Description }}
// +kubebuilder:printcolumn:name="SYNCED",type="string",JSONPath=".status.conditions[?(@.type=='Synced')].status"
Expand Down

0 comments on commit 7f036c3

Please sign in to comment.