Skip to content

Commit 15c7f5c

Browse files
Allow separate service ID and alias (#211)
Fixes aws-controllers-k8s/community#994 Description of changes: This pull request supports including an optional `--model-name` command line argument for any `ack-generate` generator verb. The generator will use this argument to override the service name when looking up the API files in `aws-sdk-go/models/apis`. Currently we reference the `metadata.serviceId` field from the `api-2.json` file as the `ServiceIDClean` variable. `ServiceIDClean` is used in all code generator templates as the import path for `aws-sdk-go` and when referencing the controller name (eg. `{{ .ServiceIDClean}}-controller`). This pull request will redirect `ServiceIDClean` to use the service alias provided when calling `ack-generate`, instead. Therefore, all subsequent ACK repositories should be named according to the AWS SDK Go package name, rather than the API file's definition of `serviceId`. `ServiceID` has been removed from the template variables, instead replaced by `AWSSDKModelServiceID` if it is needed. `ServiceIDClean` now refers to the name of the AWS SDK Go package name. For services that need to use the `--model-name` command line argument, such as `opensearchservice` and `elbv2`, developers should use the `ACK_GENERATE_MODEL_NAME` environment variable when calling `make build-controller`. Changes to the CI/CD system will need to be made to accommodate this customisation. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 0466b01 commit 15c7f5c

File tree

13 files changed

+110
-67
lines changed

13 files changed

+110
-67
lines changed

cmd/ack-generate/command/apis.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ const (
3737
)
3838

3939
var (
40-
optGenVersion string
41-
optAPIsInputPath string
42-
apisVersionPath string
40+
optGenVersion string
41+
apisVersionPath string
4342
)
4443

4544
// apiCmd is the command that generates service API types
@@ -97,10 +96,13 @@ func generateAPIs(cmd *cobra.Command, args []string) error {
9796
if err := ensureSDKRepo(ctx, optCacheDir, optRefreshCache); err != nil {
9897
return err
9998
}
99+
if optModelName == "" {
100+
optModelName = svcAlias
101+
}
100102
sdkHelper := ackmodel.NewSDKHelper(sdkDir)
101-
sdkAPI, err := sdkHelper.API(svcAlias)
103+
sdkAPI, err := sdkHelper.API(optModelName)
102104
if err != nil {
103-
newSvcAlias, err := FallBackFindServiceID(sdkDir, svcAlias)
105+
newSvcAlias, err := FallBackFindServiceID(sdkDir, optModelName)
104106
if err != nil {
105107
return err
106108
}
@@ -110,7 +112,7 @@ func generateAPIs(cmd *cobra.Command, args []string) error {
110112
}
111113
}
112114
model, err := ackmodel.New(
113-
sdkAPI, optGenVersion, optGeneratorConfigPath, ackgenerate.DefaultConfig,
115+
sdkAPI, svcAlias, optGenVersion, optGeneratorConfigPath, ackgenerate.DefaultConfig,
114116
)
115117
if err != nil {
116118
return err

cmd/ack-generate/command/controller.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ func generateController(cmd *cobra.Command, args []string) error {
6262
if err := ensureSDKRepo(ctx, optCacheDir, optRefreshCache); err != nil {
6363
return err
6464
}
65+
if optModelName == "" {
66+
optModelName = svcAlias
67+
}
6568
sdkHelper := ackmodel.NewSDKHelper(sdkDir)
66-
sdkAPI, err := sdkHelper.API(svcAlias)
69+
sdkAPI, err := sdkHelper.API(optModelName)
6770
if err != nil {
68-
newSvcAlias, err := FallBackFindServiceID(sdkDir, svcAlias)
71+
newSvcAlias, err := FallBackFindServiceID(sdkDir, optModelName)
6972
if err != nil {
7073
return err
7174
}
@@ -79,7 +82,7 @@ func generateController(cmd *cobra.Command, args []string) error {
7982
return err
8083
}
8184
m, err := ackmodel.New(
82-
sdkAPI, latestAPIVersion, optGeneratorConfigPath, ackgenerate.DefaultConfig,
85+
sdkAPI, svcAlias, latestAPIVersion, optGeneratorConfigPath, ackgenerate.DefaultConfig,
8386
)
8487
if err != nil {
8588
return err

cmd/ack-generate/command/crossplane.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/spf13/cobra"
2727

2828
cpgenerate "github.com/aws-controllers-k8s/code-generator/pkg/generate/crossplane"
29-
"github.com/aws-controllers-k8s/code-generator/pkg/model"
3029
ackmodel "github.com/aws-controllers-k8s/code-generator/pkg/model"
3130
)
3231

@@ -56,17 +55,20 @@ func generateCrossplane(_ *cobra.Command, args []string) error {
5655
return err
5756
}
5857
svcAlias := strings.ToLower(args[0])
59-
sdkHelper := model.NewSDKHelper(sdkDir)
58+
if optModelName == "" {
59+
optModelName = svcAlias
60+
}
61+
sdkHelper := ackmodel.NewSDKHelper(sdkDir)
6062
sdkHelper.APIGroupSuffix = "aws.crossplane.io"
61-
sdkAPI, err := sdkHelper.API(svcAlias)
63+
sdkAPI, err := sdkHelper.API(optModelName)
6264
if err != nil {
63-
newSvcAlias, err := FallBackFindServiceID(sdkDir, svcAlias)
65+
newSvcAlias, err := FallBackFindServiceID(sdkDir, optModelName)
6466
if err != nil {
6567
return err
6668
}
6769
sdkAPI, err = sdkHelper.API(newSvcAlias) // retry with serviceID
6870
if err != nil {
69-
return fmt.Errorf("cannot get the API model for service %s", svcAlias)
71+
return fmt.Errorf("service %s not found", svcAlias)
7072
}
7173
}
7274
cfgPath := filepath.Join(providerDir, "apis", svcAlias, optGenVersion, "generator-config.yaml")
@@ -78,7 +80,7 @@ func generateCrossplane(_ *cobra.Command, args []string) error {
7880
cfgPath = ""
7981
}
8082
m, err := ackmodel.New(
81-
sdkAPI, optGenVersion, cfgPath, cpgenerate.DefaultConfig,
83+
sdkAPI, svcAlias, optGenVersion, cfgPath, cpgenerate.DefaultConfig,
8284
)
8385
if err != nil {
8486
return err

cmd/ack-generate/command/olm.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,13 @@ func generateOLMAssets(cmd *cobra.Command, args []string) error {
8686
if err := ensureSDKRepo(ctx, optCacheDir, optRefreshCache); err != nil {
8787
return err
8888
}
89+
if optModelName == "" {
90+
optModelName = svcAlias
91+
}
8992
sdkHelper := ackmodel.NewSDKHelper(sdkDir)
90-
sdkAPI, err := sdkHelper.API(svcAlias)
93+
sdkAPI, err := sdkHelper.API(optModelName)
9194
if err != nil {
92-
newSvcAlias, err := FallBackFindServiceID(sdkDir, svcAlias)
95+
newSvcAlias, err := FallBackFindServiceID(sdkDir, optModelName)
9396
if err != nil {
9497
return err
9598
}
@@ -104,7 +107,7 @@ func generateOLMAssets(cmd *cobra.Command, args []string) error {
104107
return err
105108
}
106109
m, err := ackmodel.New(
107-
sdkAPI, latestAPIVersion, optGeneratorConfigPath, ackgenerate.DefaultConfig,
110+
sdkAPI, svcAlias, latestAPIVersion, optGeneratorConfigPath, ackgenerate.DefaultConfig,
108111
)
109112
if err != nil {
110113
return err

cmd/ack-generate/command/release.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@ func generateRelease(cmd *cobra.Command, args []string) error {
7474
if err := ensureSDKRepo(ctx, optCacheDir, optRefreshCache); err != nil {
7575
return err
7676
}
77+
if optModelName == "" {
78+
optModelName = svcAlias
79+
}
7780
sdkHelper := ackmodel.NewSDKHelper(sdkDir)
78-
sdkAPI, err := sdkHelper.API(svcAlias)
81+
sdkAPI, err := sdkHelper.API(optModelName)
7982
if err != nil {
80-
newSvcAlias, err := FallBackFindServiceID(sdkDir, svcAlias)
83+
newSvcAlias, err := FallBackFindServiceID(sdkDir, optModelName)
8184
if err != nil {
8285
return err
8386
}
@@ -87,7 +90,7 @@ func generateRelease(cmd *cobra.Command, args []string) error {
8790
}
8891
}
8992
m, err := ackmodel.New(
90-
sdkAPI, "", optGeneratorConfigPath, ackgenerate.DefaultConfig,
93+
sdkAPI, svcAlias, "", optGeneratorConfigPath, ackgenerate.DefaultConfig,
9194
)
9295
if err != nil {
9396
return err

cmd/ack-generate/command/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var (
3434
optCacheDir string
3535
optRefreshCache bool
3636
optAWSSDKGoVersion string
37+
optModelName string
3738
defaultTemplateDirs []string
3839
optTemplateDirs []string
3940
defaultServicesDir string
@@ -121,6 +122,9 @@ func init() {
121122
rootCmd.PersistentFlags().StringVar(
122123
&optAWSSDKGoVersion, "aws-sdk-go-version", "", "Version of github.com/aws/aws-sdk-go used to generate apis and controllers files",
123124
)
125+
rootCmd.PersistentFlags().StringVar(
126+
&optModelName, "model-name", "", "the name of the service model package",
127+
)
124128
}
125129

126130
// Execute adds all child commands to the root command and sets flags

pkg/generate/templateset/vars.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ type MetaVars struct {
2121
// alias does not match the ServiceID. e.g. The AWS Step Functions API has
2222
// a ServiceID of "SFN" and a service alias of "states"...
2323
ServiceAlias string
24-
// ServiceID is the exact string that appears in the AWS service API's
25-
// api-2.json descriptor file under `metadata.serviceId`
26-
ServiceID string
2724
// ServiceIDClean is the ServiceID lowercased and stripped of any
2825
// non-alphanumeric characters
2926
ServiceIDClean string
@@ -34,6 +31,9 @@ type MetaVars struct {
3431
// for custom resources, e.g. "sns.services.k8s.aws" or
3532
// "sfn.services.k8s.aws"
3633
APIGroup string
34+
// AWSSDKModelServiceID is the exact string that appears in the AWS service API's
35+
// api-2.json descriptor file under `metadata.serviceId`
36+
AWSSDKModelServiceID string
3737
// SDKAPIInterfaceTypeName is the name of the interface type used by the
3838
// aws-sdk-go services/$SERVICE/api.go file
3939
SDKAPIInterfaceTypeName string

pkg/model/model.go

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ var (
3434
// Model contains the ACK model for the generator to process and apply
3535
// templates against.
3636
type Model struct {
37-
SDKAPI *SDKAPI
38-
serviceAlias string
39-
apiVersion string
40-
crds []*CRD
41-
typeDefs []*TypeDef
42-
typeImports map[string]string
43-
typeRenames map[string]string
37+
SDKAPI *SDKAPI
38+
servicePackage string
39+
serviceAlias string
40+
apiVersion string
41+
crds []*CRD
42+
typeDefs []*TypeDef
43+
typeImports map[string]string
44+
typeRenames map[string]string
4445
// Instructions to the code generator how to handle the API and its
4546
// resources
4647
cfg *ackgenconfig.Config
@@ -51,10 +52,10 @@ type Model struct {
5152
func (m *Model) MetaVars() templateset.MetaVars {
5253
return templateset.MetaVars{
5354
ServiceAlias: m.serviceAlias,
54-
ServiceID: m.SDKAPI.ServiceID(),
55-
ServiceIDClean: m.SDKAPI.ServiceIDClean(),
56-
APIGroup: m.SDKAPI.APIGroup(),
55+
ServiceIDClean: m.ServiceIDClean(),
56+
APIGroup: m.APIGroup(),
5757
APIVersion: m.apiVersion,
58+
AWSSDKModelServiceID: m.SDKAPI.AWSSDKModelServiceID(),
5859
SDKAPIInterfaceTypeName: m.SDKAPI.SDKAPIInterfaceTypeName(),
5960
CRDNames: m.crdNames(),
6061
}
@@ -691,11 +692,29 @@ func (m *Model) GetConfig() *ackgenconfig.Config {
691692
return m.cfg
692693
}
693694

695+
// APIGroup returns the normalized Kubernetes APIGroup for the AWS service API,
696+
// e.g. "sns.services.k8s.aws"
697+
func (m *Model) APIGroup() string {
698+
serviceID := m.servicePackage
699+
suffix := "services.k8s.aws"
700+
if m.SDKAPI.apiGroupSuffix != "" {
701+
suffix = m.SDKAPI.apiGroupSuffix
702+
}
703+
return fmt.Sprintf("%s.%s", serviceID, suffix)
704+
}
705+
706+
// ServiceIDClean returns a lowercased, whitespace-stripped ServiceID
707+
func (m *Model) ServiceIDClean() string {
708+
serviceID := strings.ToLower(m.servicePackage)
709+
return strings.Replace(serviceID, " ", "", -1)
710+
}
711+
694712
// New returns a new Model struct for a supplied API model.
695713
// Optionally, pass a file path to a generator config file that can be used to
696714
// instruct the code generator how to handle the API properly
697715
func New(
698716
SDKAPI *SDKAPI,
717+
servicePackage string,
699718
apiVersion string,
700719
configPath string,
701720
defaultConfig ackgenconfig.Config,
@@ -705,12 +724,11 @@ func New(
705724
return nil, err
706725
}
707726
m := &Model{
708-
SDKAPI: SDKAPI,
709-
// TODO(jaypipes): Handle cases where service alias and service ID
710-
// don't match (Step Functions)
711-
serviceAlias: SDKAPI.ServiceID(),
712-
apiVersion: apiVersion,
713-
cfg: &cfg,
727+
SDKAPI: SDKAPI,
728+
servicePackage: servicePackage,
729+
serviceAlias: SDKAPI.AWSSDKModelServiceID(),
730+
apiVersion: apiVersion,
731+
cfg: &cfg,
714732
}
715733
m.ApplyShapeIgnoreRules()
716734
return m, nil

pkg/model/multiversion/manager.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func NewAPIVersionManager(
5151
sdkCacheDir string,
5252
metadataPath string,
5353
serviceAlias string,
54+
serviceModelName string,
5455
hubVersion string,
5556
apisInfo map[string]ackmetadata.APIInfo,
5657
defaultConfig ackgenconfig.Config,
@@ -94,13 +95,14 @@ func NewAPIVersionManager(
9495
return nil, err
9596
}
9697

97-
SDKAPI, err := SDKAPIHelper.API(serviceAlias)
98+
SDKAPI, err := SDKAPIHelper.API(serviceModelName)
9899
if err != nil {
99100
return nil, err
100101
}
101102

102103
i, err := ackmodel.New(
103104
SDKAPI,
105+
serviceAlias,
104106
version.APIVersion,
105107
apiInfo.GeneratorConfigPath,
106108
defaultConfig,

pkg/model/sdk_helper.go

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ func (a *SDKAPI) GetOutputShapeRef(
246246
}
247247

248248
// getMemberByPath returns a ShapeRef given a root Shape and a dot-notation
249-
// object search path. Given the explicit type check for list type members
250-
// both ".." and "." notations work currently.
249+
// object search path. Given the explicit type check for list type members
250+
// both ".." and "." notations work currently.
251251
// TODO: Add support for other types such as map.
252252
func getMemberByPath(
253253
shape *awssdkmodel.Shape,
@@ -353,39 +353,24 @@ func (a *SDKAPI) HasConflictingTypeName(typeName string, cfg *ackgenconfig.Confi
353353
util.InStrings(cleanTypeName, crdListResourceNames)
354354
}
355355

356-
// ServiceID returns the exact `metadata.serviceId` attribute for the AWS
357-
// service APi's api-2.json file
358-
func (a *SDKAPI) ServiceID() string {
356+
// AWSSDKModelServiceID returns the exact `metadata.serviceId` attribute for the AWS
357+
// service APi's api-2.json file.
358+
// This MAY NOT MATCH the AWS SDK Go package used by the service. For example:
359+
// AWS SDK Go uses `opensearchservice` whereas the service ID is `opensearch`
360+
func (a *SDKAPI) AWSSDKModelServiceID() string {
359361
if a == nil || a.API == nil {
360362
return ""
361363
}
362364
return awssdkmodel.ServiceID(a.API)
363365
}
364366

365-
// ServiceIDClean returns a lowercased, whitespace-stripped ServiceID
366-
func (a *SDKAPI) ServiceIDClean() string {
367-
serviceID := strings.ToLower(a.ServiceID())
368-
return strings.Replace(serviceID, " ", "", -1)
369-
}
370-
371367
func (a *SDKAPI) GetServiceFullName() string {
372368
if a == nil || a.API == nil {
373369
return ""
374370
}
375371
return a.API.Metadata.ServiceFullName
376372
}
377373

378-
// APIGroup returns the normalized Kubernetes APIGroup for the AWS service API,
379-
// e.g. "sns.services.k8s.aws"
380-
func (a *SDKAPI) APIGroup() string {
381-
serviceID := a.ServiceIDClean()
382-
suffix := "services.k8s.aws"
383-
if a.apiGroupSuffix != "" {
384-
suffix = a.apiGroupSuffix
385-
}
386-
return fmt.Sprintf("%s.%s", serviceID, suffix)
387-
}
388-
389374
// SDKAPIInterfaceTypeName returns the name of the aws-sdk-go primary API
390375
// interface type name.
391376
func (a *SDKAPI) SDKAPIInterfaceTypeName() string {

0 commit comments

Comments
 (0)