Skip to content
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
7 changes: 6 additions & 1 deletion cmd/ack-generate/command/crossplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ func generateCrossplane(_ *cobra.Command, args []string) error {
return err
}
svcAlias := strings.ToLower(args[0])
optGeneratorConfigPath = filepath.Join(optOutputPath, "apis", svcAlias, optGenVersion, "generator-config.yaml")
if optGeneratorConfigPath == "" {
// default generator configuration file path is now: apis/<service>/<generator-config.yaml>
// as this configuration is per API group (per service), and
// resources can exist in multiple versions.
optGeneratorConfigPath = filepath.Join(optOutputPath, "apis", svcAlias, "generator-config.yaml")
}
m, err := loadModel(svcAlias, optGenVersion, "aws.crossplane.io", cpgenerate.DefaultConfig)
if err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions pkg/generate/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ type ResourceConfig struct {
// All ShortNames must be distinct from any other ShortNames installed into the cluster,
// otherwise the CRD will fail to install.
ShortNames []string `json:"shortNames,omitempty"`
// APIVersions represents the API versions defined for the generated CRD.
// Default version to be used is the one specified via the "--version"
// command-line argument, if none is specified here.
// For the Crossplane pipeline, the generation target is the version
// marked as the storage version.
APIVersions []APIVersion `json:"api_versions,omitempty"`
// IsAdoptable determines whether the CRD should be accepted by the adoption reconciler.
// If set to false, the user will be given an error if they attempt to adopt a resource
// with this type.
Expand Down
25 changes: 25 additions & 0 deletions pkg/generate/config/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package config

// APIVersion represents an API version of the generated CRD.
type APIVersion struct {
// Name of the API version, e.g. v1beta1
Name string `json:"name"`
// Served whether this version is enabled or not
Served *bool `json:"served,omitempty"`
// Storage whether this version is the storage version.
// One and only one version can be set as the storage version.
Storage *bool `json:"storage,omitempty"`
}
52 changes: 37 additions & 15 deletions pkg/generate/crossplane/crossplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,33 +135,49 @@ func Crossplane(
)

metaVars := m.MetaVars()
detectedCRDVersions := make(map[string]bool)
for _, crd := range crds {
v, err := crd.GetStorageVersion(metaVars.APIVersion)
if err != nil {
return nil, err
}
detectedCRDVersions[v] = true
}

// First add all the CRDs and API types
apiVars := &templateAPIVars{
metaVars,
enumDefs,
typeDefs,
}
for _, path := range apisGenericTemplatesPaths {
outPath := filepath.Join(
"apis",
metaVars.ServicePackageName,
metaVars.APIVersion,
"zz_"+strings.TrimSuffix(filepath.Base(path), ".tpl"),
)
if err = ts.Add(outPath, path, apiVars); err != nil {
return nil, err
for apiVersion := range detectedCRDVersions {
for _, path := range apisGenericTemplatesPaths {
apiVars := &templateAPIVars{
metaVars,
enumDefs,
typeDefs,
}
apiVars.APIVersion = apiVersion
outPath := filepath.Join(
"apis",
metaVars.ServicePackageName,
apiVersion,
"zz_"+strings.TrimSuffix(filepath.Base(path), ".tpl"),
)
if err = ts.Add(outPath, path, apiVars); err != nil {
return nil, err
}
}
}
for _, crd := range crds {
v, err := crd.GetStorageVersion(metaVars.APIVersion)
if err != nil {
return nil, err
}
crdFileName := filepath.Join(
"apis", metaVars.ServicePackageName, metaVars.APIVersion,
"apis", metaVars.ServicePackageName, v,
"zz_"+strcase.ToSnake(crd.Kind)+".go",
)
crdVars := &templateCRDVars{
metaVars,
crd,
}
crdVars.APIVersion = v
if err = ts.Add(crdFileName, crdTemplatePath, crdVars); err != nil {
return nil, err
}
Expand All @@ -177,6 +193,9 @@ func Crossplane(
metaVars,
crd,
}
if crdVars.APIVersion, err = crd.GetStorageVersion(metaVars.APIVersion); err != nil {
return nil, err
}
if err = ts.Add(outPath, controllerTmplPath, crdVars); err != nil {
return nil, err
}
Expand All @@ -188,6 +207,9 @@ func Crossplane(
metaVars,
crd,
}
if crdVars.APIVersion, err = crd.GetStorageVersion(metaVars.APIVersion); err != nil {
return nil, err
}
if err = ts.Add(outPath, conversionsTmplPath, crdVars); err != nil {
return nil, err
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/model/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ func (r *CRD) Config() *ackgenconfig.Config {
return r.cfg
}

// GetStorageVersion returns the configured storage API version for the CRD, or
// the specified default version.
func (r *CRD) GetStorageVersion(defaultVersion string) (string, error) {
// if not configured
if r.cfg == nil || len(r.cfg.Resources[r.Names.Original].APIVersions) == 0 {
return defaultVersion, nil
}

for _, v := range r.cfg.Resources[r.Names.Original].APIVersions {
if v.Storage != nil && *v.Storage {
return v.Name, nil
}
}
return "", fmt.Errorf("exactly one configured version must be marked as the storage version for the %q CRD",
r.Names.Original)
}

// SDKAPIPackageName returns the aws-sdk-go package name used for this
// resource's API
func (r *CRD) SDKAPIPackageName() string {
Expand Down