Skip to content

Commit f4166ae

Browse files
Consolidate service naming nomenclature (#213)
Issue #, if available: aws-controllers-k8s/community#994 Description of changes: This pull request replaces all templates and variables with a defined nomenclature. The current code-generator uses service name terms interchangeably, which eventually lead to the linked issue. While PR #211 solved the logical issue of overriding the service model name, in some cases, it did not do a clear job of clarifying what names exist for any service and when they are applicable. There are 3 different names that are used to programmatically reference a service. Here are the terms I am using in this PR to refer to them, and their given purpose: - Service ID - The `metadata.serviceID` field from the model `api-2.json` file - Service model name - The path to the model `api-2.json` file - Service package name - The AWS SDK Go package path - Used by the ACK controller's for their name Some examples on when these differ: - Step Functions uses `sfn` for its ID and alias, but `states` for its model name - Elastic Load Balancing v2 uses `elbv2` for its alias, but `elasticloadbalancingv2` for its ID and model name There are also 2 names defined in the controller metadata file that are not used in any parts of the code, but are used in documentation: - Service name - The full name of the service as defined by the product's documentation page (eg. `Amazon Elastic Cloud Compute`) - Service short name - An informal, shortened name of the service (eg. `EC2`) By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 4b30ff5 commit f4166ae

32 files changed

+104
-108
lines changed

cmd/ack-generate/command/crossplane.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ func generateCrossplane(_ *cobra.Command, args []string) error {
7474
sdkHelper.APIGroupSuffix = "aws.crossplane.io"
7575
sdkAPI, err := sdkHelper.API(svcAlias)
7676
if err != nil {
77-
newSvcAlias, err := FallBackFindServiceID(sdkDir, svcAlias)
77+
retryModelName, err := FallBackFindServiceID(sdkDir, svcAlias)
7878
if err != nil {
7979
return err
8080
}
81-
sdkAPI, err = sdkHelper.API(newSvcAlias) // retry with serviceID
81+
// Retry using path found by querying service ID
82+
sdkAPI, err = sdkHelper.API(retryModelName)
8283
if err != nil {
8384
return fmt.Errorf("cannot get the API model for service %s", svcAlias)
8485
}

cmd/ack-generate/command/olm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var olmCmd = &cobra.Command{
4242
Short: `Generate an Operator Lifecycle Manager's ClusterServiceVersion
4343
resources for AWS service API. Expects a configuration file to be passed in
4444
via the --olm-config option, or otherwise reads the config at the root of the
45-
project with the filename <servicealias>-olmconfig.yaml`,
45+
project with the filename <servicepackage>-olmconfig.yaml`,
4646
RunE: generateOLMAssets,
4747
}
4848

pkg/generate/crossplane/crossplane.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func Crossplane(
145145
for _, path := range apisGenericTemplatesPaths {
146146
outPath := filepath.Join(
147147
"apis",
148-
metaVars.ServiceAlias,
148+
metaVars.ServicePackageName,
149149
metaVars.APIVersion,
150150
"zz_"+strings.TrimSuffix(filepath.Base(path), ".tpl"),
151151
)
@@ -155,7 +155,7 @@ func Crossplane(
155155
}
156156
for _, crd := range crds {
157157
crdFileName := filepath.Join(
158-
"apis", metaVars.ServiceAlias, metaVars.APIVersion,
158+
"apis", metaVars.ServicePackageName, metaVars.APIVersion,
159159
"zz_"+strcase.ToSnake(crd.Kind)+".go",
160160
)
161161
crdVars := &templateCRDVars{
@@ -170,7 +170,7 @@ func Crossplane(
170170
// Next add the controller package for each CRD
171171
for _, crd := range crds {
172172
outPath := filepath.Join(
173-
"pkg", "controller", metaVars.ServiceAlias, crd.Names.Lower,
173+
"pkg", "controller", metaVars.ServicePackageName, crd.Names.Lower,
174174
"zz_controller.go",
175175
)
176176
crdVars := &templateCRDVars{
@@ -181,7 +181,7 @@ func Crossplane(
181181
return nil, err
182182
}
183183
outPath = filepath.Join(
184-
"pkg", "controller", metaVars.ServiceAlias, crd.Names.Lower,
184+
"pkg", "controller", metaVars.ServicePackageName, crd.Names.Lower,
185185
"zz_conversions.go",
186186
)
187187
crdVars = &templateCRDVars{

pkg/generate/olm/olm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func BundleAssets(
7979

8080
csvBaseOutPath := fmt.Sprintf(
8181
"config/manifests/bases/ack-%s-controller.clusterserviceversion.yaml",
82-
m.MetaVars().ServiceAlias)
82+
m.MetaVars().ServicePackageName)
8383
if err := ts.Add(csvBaseOutPath, "config/manifests/bases/clusterserviceversion.yaml.tpl", olmVars); err != nil {
8484
return nil, err
8585
}

pkg/generate/templateset/vars.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@ package templateset
1616
// MetaVars contains template variables that most templates need access to
1717
// that describe the service alias, its package name, etc
1818
type MetaVars struct {
19-
// ServiceAlias contains the exact string used to identify the AWS service
20-
// API in the aws-sdk-go `service/` directory. It is also used as the
21-
// identifier for the ACK controller's name and packages.
22-
ServiceAlias string
23-
// ServiceID is the exact string that appears in the AWS service API's
24-
// api-2.json descriptor file under `metadata.serviceId`
25-
ServiceID string
2619
// ServiceModelName contains the exact string used to identify the AWS
2720
// service API in the aws-sdk-go's models/apis/ directory. Note that some
2821
// APIs this name does not match the ServiceID. e.g. The AWS Step Functions
2922
// API has a ServiceID of "SFN" and a service model name of "states"...
3023
ServiceModelName string
24+
// ServicePackageName contains the exact string used to identify the AWS
25+
// service API in the aws-sdk-go `service/` directory. It is also used as
26+
// the identifier for the ACK controller's name and packages.
27+
ServicePackageName string
28+
// ServiceID is the exact string that appears in the AWS service API's
29+
// api-2.json descriptor file under `metadata.serviceId`
30+
ServiceID string
3131
// APIVersion contains the version of the Kubernetes API resources, e.g.
3232
// "v1alpha1"
3333
APIVersion string
3434
// APIGroup contains the normalized name of the Kubernetes APIGroup used
3535
// for custom resources, e.g. "sns.services.k8s.aws" or
3636
// "sfn.services.k8s.aws"
3737
APIGroup string
38-
// SDKAPIInterfaceTypeName is the name of the interface type used by the
38+
// APIInterfaceTypeName is the name of the interface type used by the
3939
// aws-sdk-go services/$SERVICE/api.go file
40-
SDKAPIInterfaceTypeName string
40+
APIInterfaceTypeName string
4141
//CRDNames contains all crds names lowercased and in plural
4242
CRDNames []string
4343
}

pkg/model/model.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ 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+
servicePackageName string
39+
apiVersion string
40+
crds []*CRD
41+
typeDefs []*TypeDef
42+
typeImports map[string]string
43+
typeRenames map[string]string
4444
// Instructions to the code generator how to handle the API and its
4545
// resources
4646
cfg *ackgenconfig.Config
@@ -50,13 +50,13 @@ type Model struct {
5050
// service API
5151
func (m *Model) MetaVars() templateset.MetaVars {
5252
return templateset.MetaVars{
53-
ServiceAlias: m.serviceAlias,
54-
ServiceID: m.SDKAPI.ServiceID(),
55-
ServiceModelName: m.cfg.ModelName,
56-
APIGroup: m.APIGroup(),
57-
APIVersion: m.apiVersion,
58-
SDKAPIInterfaceTypeName: m.SDKAPI.SDKAPIInterfaceTypeName(),
59-
CRDNames: m.crdNames(),
53+
ServicePackageName: m.servicePackageName,
54+
ServiceID: m.SDKAPI.ServiceID(),
55+
ServiceModelName: m.cfg.ModelName,
56+
APIGroup: m.APIGroup(),
57+
APIVersion: m.apiVersion,
58+
APIInterfaceTypeName: m.SDKAPI.APIInterfaceTypeName(),
59+
CRDNames: m.crdNames(),
6060
}
6161
}
6262

@@ -694,28 +694,27 @@ func (m *Model) GetConfig() *ackgenconfig.Config {
694694
// APIGroup returns the normalized Kubernetes APIGroup for the AWS service API,
695695
// e.g. "sns.services.k8s.aws"
696696
func (m *Model) APIGroup() string {
697-
serviceAlias := m.serviceAlias
698697
suffix := "services.k8s.aws"
699698
if m.SDKAPI.apiGroupSuffix != "" {
700699
suffix = m.SDKAPI.apiGroupSuffix
701700
}
702-
return fmt.Sprintf("%s.%s", serviceAlias, suffix)
701+
return fmt.Sprintf("%s.%s", m.servicePackageName, suffix)
703702
}
704703

705704
// New returns a new Model struct for a supplied API model.
706705
// Optionally, pass a file path to a generator config file that can be used to
707706
// instruct the code generator how to handle the API properly
708707
func New(
709708
SDKAPI *SDKAPI,
710-
serviceAlias string,
709+
servicePackageName string,
711710
apiVersion string,
712711
cfg ackgenconfig.Config,
713712
) (*Model, error) {
714713
m := &Model{
715-
SDKAPI: SDKAPI,
716-
serviceAlias: serviceAlias,
717-
apiVersion: apiVersion,
718-
cfg: &cfg,
714+
SDKAPI: SDKAPI,
715+
servicePackageName: servicePackageName,
716+
apiVersion: apiVersion,
717+
cfg: &cfg,
719718
}
720719
m.ApplyShapeIgnoreRules()
721720
return m, nil

pkg/model/multiversion/manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type APIVersionManager struct {
5050
func NewAPIVersionManager(
5151
sdkCacheDir string,
5252
metadataPath string,
53-
serviceAlias string,
53+
servicePackageName string,
5454
hubVersion string,
5555
apisInfo map[string]ackmetadata.APIInfo,
5656
defaultConfig ackgenconfig.Config,
@@ -99,14 +99,14 @@ func NewAPIVersionManager(
9999
return nil, err
100100
}
101101

102-
SDKAPI, err := SDKAPIHelper.API(serviceAlias)
102+
SDKAPI, err := SDKAPIHelper.API(servicePackageName)
103103
if err != nil {
104104
return nil, err
105105
}
106106

107107
i, err := ackmodel.New(
108108
SDKAPI,
109-
serviceAlias,
109+
servicePackageName,
110110
version.APIVersion,
111111
cfg,
112112
)

pkg/model/sdk_helper.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ func (h *SDKHelper) ModelAndDocsPath(
139139

140140
// FirstAPIVersion returns the first found API version for a service API.
141141
// (e.h. "2012-10-03")
142-
func (h *SDKHelper) FirstAPIVersion(serviceAlias string) (string, error) {
143-
versions, err := h.GetAPIVersions(serviceAlias)
142+
func (h *SDKHelper) FirstAPIVersion(serviceModelName string) (string, error) {
143+
versions, err := h.GetAPIVersions(serviceModelName)
144144
if err != nil {
145145
return "", err
146146
}
@@ -149,8 +149,8 @@ func (h *SDKHelper) FirstAPIVersion(serviceAlias string) (string, error) {
149149
}
150150

151151
// GetAPIVersions returns the list of API Versions found in a service directory.
152-
func (h *SDKHelper) GetAPIVersions(serviceAlias string) ([]string, error) {
153-
apiPath := filepath.Join(h.basePath, "models", "apis", serviceAlias)
152+
func (h *SDKHelper) GetAPIVersions(serviceModelName string) ([]string, error) {
153+
apiPath := filepath.Join(h.basePath, "models", "apis", serviceModelName)
154154
versionDirs, err := ioutil.ReadDir(apiPath)
155155
if err != nil {
156156
return nil, err
@@ -354,30 +354,26 @@ func (a *SDKAPI) HasConflictingTypeName(typeName string, cfg *ackgenconfig.Confi
354354
}
355355

356356
// ServiceID returns the exact `metadata.serviceId` attribute for the AWS
357-
// service APi's api-2.json file
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`
358360
func (a *SDKAPI) ServiceID() 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-
// SDKAPIInterfaceTypeName returns the name of the aws-sdk-go primary API
374+
// APIInterfaceTypeName returns the name of the aws-sdk-go primary API
379375
// interface type name.
380-
func (a *SDKAPI) SDKAPIInterfaceTypeName() string {
376+
func (a *SDKAPI) APIInterfaceTypeName() string {
381377
if a == nil || a.API == nil {
382378
return ""
383379
}

pkg/testutil/schema_helper.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ func (o *TestingModelOptions) SetDefaults() {
5050
}
5151

5252
// NewModelForService returns a new *ackmodel.Model used for testing purposes.
53-
func NewModelForService(t *testing.T, serviceAlias string) *ackmodel.Model {
54-
return NewModelForServiceWithOptions(t, serviceAlias, &TestingModelOptions{})
53+
func NewModelForService(t *testing.T, servicePackageName string) *ackmodel.Model {
54+
return NewModelForServiceWithOptions(t, servicePackageName, &TestingModelOptions{})
5555
}
5656

5757
// NewModelForServiceWithOptions returns a new *ackmodel.Model used for testing purposes.
58-
func NewModelForServiceWithOptions(t *testing.T, serviceAlias string, options *TestingModelOptions) *ackmodel.Model {
58+
func NewModelForServiceWithOptions(t *testing.T, servicePackageName string, options *TestingModelOptions) *ackmodel.Model {
5959
path, err := os.Getwd()
6060
if err != nil {
6161
t.Fatal(err)
@@ -75,19 +75,19 @@ func NewModelForServiceWithOptions(t *testing.T, serviceAlias string, options *T
7575
options.SetDefaults()
7676
sdkHelper := model.NewSDKHelper(path)
7777
sdkHelper.WithAPIVersion(options.ServiceAPIVersion)
78-
sdkAPI, err := sdkHelper.API(serviceAlias)
78+
sdkAPI, err := sdkHelper.API(servicePackageName)
7979
if err != nil {
8080
t.Fatal(err)
8181
}
82-
generatorConfigPath := filepath.Join(path, "models", "apis", serviceAlias, options.ServiceAPIVersion, options.GeneratorConfigFile)
82+
generatorConfigPath := filepath.Join(path, "models", "apis", servicePackageName, options.ServiceAPIVersion, options.GeneratorConfigFile)
8383
if _, err := os.Stat(generatorConfigPath); os.IsNotExist(err) {
8484
generatorConfigPath = ""
8585
}
8686
cfg, err := ackgenconfig.New(generatorConfigPath, ackgenerate.DefaultConfig)
8787
if err != nil {
8888
t.Fatal(err)
8989
}
90-
m, err := ackmodel.New(sdkAPI, serviceAlias, options.APIVersion, cfg)
90+
m, err := ackmodel.New(sdkAPI, servicePackageName, options.APIVersion, cfg)
9191
if err != nil {
9292
t.Fatal(err)
9393
}

templates/cmd/controller/main.go.tpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ import (
1414
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
1515
ctrlrt "sigs.k8s.io/controller-runtime"
1616
ctrlrtmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
17-
svcsdk "github.com/aws/aws-sdk-go/service/{{ .ServiceAlias }}"
17+
svcsdk "github.com/aws/aws-sdk-go/service/{{ .ServicePackageName }}"
1818

1919
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
20-
svcresource "github.com/aws-controllers-k8s/{{ .ServiceAlias }}-controller/pkg/resource"
21-
svctypes "github.com/aws-controllers-k8s/{{ .ServiceAlias }}-controller/apis/{{ .APIVersion }}"
20+
svcresource "github.com/aws-controllers-k8s/{{ .ServicePackageName }}-controller/pkg/resource"
21+
svctypes "github.com/aws-controllers-k8s/{{ .ServicePackageName }}-controller/apis/{{ .APIVersion }}"
2222
{{/* TODO(a-hilaly): import apis/* packages to register webhooks */}}
23-
{{ $serviceIDClean := .ServiceAlias }} {{range $crdName := .SnakeCasedCRDNames }}_ "github.com/aws-controllers-k8s/{{ $serviceIDClean }}-controller/pkg/resource/{{ $crdName }}"
23+
{{ $serviceAlias := .ServicePackageName }} {{range $crdName := .SnakeCasedCRDNames }}_ "github.com/aws-controllers-k8s/{{ $serviceAlias }}-controller/pkg/resource/{{ $crdName }}"
2424
{{end}}
2525
)
2626

2727
var (
2828
awsServiceAPIGroup = "{{ .APIGroup }}"
29-
awsServiceAlias = "{{ .ServiceAlias }}"
29+
awsServiceAlias = "{{ .ServicePackageName }}"
3030
awsServiceEndpointsID = svcsdk.EndpointsID
3131
scheme = runtime.NewScheme()
3232
setupLog = ctrlrt.Log.WithName("setup")

0 commit comments

Comments
 (0)