Skip to content

Commit 2d95a1f

Browse files
authored
Use loadModelWithLatestAPIVersion to load models for crossplane (#237)
This fixes an issue where the crossplane command would fail to find API models for resources whose name differed between the model and API in the SDK. Previous behavior was to continue using the svcAlias instead of correctly using model_name from the configuration. The changes here also have the added benefit of making the generateCrossplane function code more similar to other generation functions and utilize more of the common codebase. Issue #, if available: N/A Description of changes: Replace duplicated code in the generateCrossplane function with implementations in common.go and change variables to match the naming scheme used elsewhere. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 31204bc commit 2d95a1f

File tree

3 files changed

+9
-44
lines changed

3 files changed

+9
-44
lines changed

cmd/ack-generate/command/crossplane.go

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@ import (
1717
"context"
1818
"fmt"
1919
"io/ioutil"
20-
"os"
2120
"os/exec"
2221
"path/filepath"
2322
"strings"
2423

2524
"github.com/pkg/errors"
2625
"github.com/spf13/cobra"
2726

28-
ackgenerate "github.com/aws-controllers-k8s/code-generator/pkg/generate/ack"
29-
ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/generate/config"
3027
cpgenerate "github.com/aws-controllers-k8s/code-generator/pkg/generate/crossplane"
31-
ackmodel "github.com/aws-controllers-k8s/code-generator/pkg/model"
32-
acksdk "github.com/aws-controllers-k8s/code-generator/pkg/sdk"
3328
)
3429

3530
// crossplaneCmd is the command that generates Crossplane API types
@@ -39,12 +34,7 @@ var crossplaneCmd = &cobra.Command{
3934
RunE: generateCrossplane,
4035
}
4136

42-
var providerDir string
43-
4437
func init() {
45-
crossplaneCmd.PersistentFlags().StringVar(
46-
&providerDir, "provider-dir", ".", "the directory of the Crossplane provider",
47-
)
4838
rootCmd.AddCommand(crossplaneCmd)
4939
}
5040

@@ -58,34 +48,9 @@ func generateCrossplane(_ *cobra.Command, args []string) error {
5848
return err
5949
}
6050
svcAlias := strings.ToLower(args[0])
61-
cfgPath := filepath.Join(providerDir, "apis", svcAlias, optGenVersion, "generator-config.yaml")
62-
_, err := os.Stat(cfgPath)
63-
if err != nil && !os.IsNotExist(err) {
64-
return err
65-
}
66-
if os.IsNotExist(err) {
67-
cfgPath = ""
68-
}
69-
cfg, err := ackgenconfig.New(cfgPath, ackgenerate.DefaultConfig)
70-
if err != nil {
71-
return err
72-
}
73-
sdkHelper := acksdk.NewHelper(sdkDir, cfg)
74-
sdkHelper.APIGroupSuffix = "aws.crossplane.io"
75-
sdkAPI, err := sdkHelper.API(svcAlias)
76-
if err != nil {
77-
retryModelName, err := FallBackFindServiceID(sdkDir, svcAlias)
78-
if err != nil {
79-
return err
80-
}
81-
// Retry using path found by querying service ID
82-
sdkAPI, err = sdkHelper.API(retryModelName)
83-
if err != nil {
84-
return fmt.Errorf("cannot get the API model for service %s", svcAlias)
85-
}
86-
}
87-
m, err := ackmodel.New(
88-
sdkAPI, svcAlias, optGenVersion, cfg,
51+
optGeneratorConfigPath = filepath.Join(optOutputPath, "apis", svcAlias, optGenVersion, "generator-config.yaml")
52+
m, err := loadModelWithLatestAPIVersion(
53+
svcAlias,
8954
)
9055
if err != nil {
9156
return err
@@ -106,7 +71,7 @@ func generateCrossplane(_ *cobra.Command, args []string) error {
10671
fmt.Println(strings.TrimSpace(contents.String()))
10772
continue
10873
}
109-
outPath := filepath.Join(providerDir, path)
74+
outPath := filepath.Join(optOutputPath, path)
11075
outDir := filepath.Dir(outPath)
11176
if _, err := ensureDir(outDir); err != nil {
11277
return err
@@ -115,8 +80,8 @@ func generateCrossplane(_ *cobra.Command, args []string) error {
11580
return err
11681
}
11782
}
118-
apiPath := filepath.Join(providerDir, "apis", svcAlias, optGenVersion)
119-
controllerPath := filepath.Join(providerDir, "pkg", "controller", svcAlias)
83+
apiPath := filepath.Join(optOutputPath, "apis", svcAlias, optGenVersion)
84+
controllerPath := filepath.Join(optOutputPath, "pkg", "controller", svcAlias)
12085
// TODO(muvaf): goimports don't allow to be included as a library. Make sure
12186
// goimports binary exists.
12287
if err := exec.Command("goimports", "-w", apiPath, controllerPath).Run(); err != nil {

cmd/ack-generate/command/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func init() {
116116
&optMetadataConfigPath, "metadata-config-path", "", "Path to file containing service metadata to use",
117117
)
118118
rootCmd.PersistentFlags().StringVarP(
119-
&optOutputPath, "output", "o", "", "Path to directory to output generated files.",
119+
&optOutputPath, "output", "o", "", "Path to directory to output generated files (if generating crossplane providers, this should be the root of the aws-crossplane directory)",
120120
)
121121
rootCmd.PersistentFlags().StringVar(
122122
&optAWSSDKGoVersion, "aws-sdk-go-version", "", "Version of github.com/aws/aws-sdk-go used to generate apis and controllers files",

templates/crossplane/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ This folder includes the templates to generate AWS Crossplane Provider. Run the
44
following to generate:
55

66
```console
7-
go run -tags codegen cmd/ack-generate/main.go crossplane apis ecr --provider-dir <directory for provider>
7+
go run -tags codegen cmd/ack-generate/main.go crossplane apis ecr --output <directory for provider>
88
```
99

1010
See [Contributing New Resource Using ACK](https://github.com/crossplane/provider-aws/blob/master/CODE_GENERATION.md)
11-
for details.
11+
for details.

0 commit comments

Comments
 (0)