Skip to content

Commit e371c49

Browse files
committed
feat: Allow manual setting of AWS client struct and interface names
While developing the ACK pipes-controller, we encountered an issue where the generated code didn't compile mainly because the code-generator assumed that the AWS SDK client/interface/import-paths always followed the same naming convetions. However, we discovered that pipes was the exception as they renamed the model, import path and client struct/interface. This patch introduces a new feature that enables users to manually set the client structg and interface names int he `generator.yaml` file. This update allows for greater flexibvility in the AWS client naming within pipes-controller and possibly other controllers in the future. In this patch, we've also introduced a breaking change by renaming `model_name` to `sdk_names.model`. Mianly to keep all the SDK related naming convetions in the same place. Signed-off-by: Amine Hilaly <[email protected]>
1 parent 14dcc10 commit e371c49

File tree

8 files changed

+64
-17
lines changed

8 files changed

+64
-17
lines changed

cmd/ack-generate/command/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func loadModel(svcAlias string, apiVersion string, apiGroup string, defaultCfg a
4545
return nil, err
4646
}
4747

48-
modelName := strings.ToLower(cfg.ModelName)
48+
modelName := strings.ToLower(cfg.SDKNames.Model)
4949
if modelName == "" {
5050
modelName = svcAlias
5151
}

pkg/config/config.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,28 @@ type Config struct {
3939
// SetManyOutput function fails with NotFound error.
4040
// Default is "return nil, ackerr.NotFound"
4141
SetManyOutputNotFoundErrReturn string `json:"set_many_output_notfound_err_return,omitempty"`
42+
// SDKNames lets you specify SDK object names. This configuration field was
43+
// introduces when we learned that the EventBridgePipes service renamed, not
44+
// only the service model name, but also the iface name to `PipesAPI`. See
45+
// https://github.com/aws/aws-sdk-go/blob/main/service/pipes/pipesiface/interface.go#L62
46+
SDKNames SDKNames `json:"sdk_names"`
47+
}
48+
49+
// SDKNames contains information on the SDK Client package. More precisely
50+
// it holds information on how to correctly import the {service}/iface main
51+
// interface and client structs.
52+
type SDKNames struct {
4253
// ModelName lets you specify the path used to identify the AWS service API
4354
// in the aws-sdk-go's models/apis/ directory. This field is optional and
4455
// only needed for services such as the opensearchservice service where the
4556
// model name is `opensearch` and the service package is called
4657
// `opensearchservice`.
47-
ModelName string `json:"model_name,omitempty"`
58+
Model string `json:"model_name,omitempty"`
59+
// ClientInterface is the name of the interface that defines the "shape" of
60+
// the a sdk service client. e.g PipesAPI, LambdaAPI, etc...
61+
ClientInterface string `json:"client_interface,omitempty"`
62+
// ClientStruct is the name of the service client struct. e.g Lambda, Pipes, etc...
63+
ClientStruct string `json:"client_struct,omitempty"`
4864
}
4965

5066
// IgnoreSpec represents instructions to the ACK code generator to

pkg/generate/templateset/vars.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ type MetaVars struct {
3535
// for custom resources, e.g. "sns.services.k8s.aws" or
3636
// "sfn.services.k8s.aws"
3737
APIGroup string
38-
// APIInterfaceTypeName is the name of the interface type used by the
38+
// ClientInterfaceTypeName is the name of the interface type used by the
3939
// aws-sdk-go services/$SERVICE/api.go file
40-
APIInterfaceTypeName string
40+
ClientInterfaceTypeName string
41+
// ClientStructTypeName is the name of the struct type defining the service
42+
// sdk client.
43+
ClientStructTypeName string
4144
//CRDNames contains all crds names lowercased and in plural
4245
CRDNames []string
4346
}

pkg/model/model.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ type Model struct {
5252
// service API
5353
func (m *Model) MetaVars() templateset.MetaVars {
5454
return templateset.MetaVars{
55-
ServicePackageName: m.servicePackageName,
56-
ServiceID: m.SDKAPI.ServiceID(),
57-
ServiceModelName: m.cfg.ModelName,
58-
APIGroup: m.APIGroup(),
59-
APIVersion: m.apiVersion,
60-
APIInterfaceTypeName: m.SDKAPI.APIInterfaceTypeName(),
61-
CRDNames: m.crdNames(),
55+
ServicePackageName: m.servicePackageName,
56+
ServiceID: m.SDKAPI.ServiceID(),
57+
ServiceModelName: m.cfg.SDKNames.Model,
58+
APIGroup: m.APIGroup(),
59+
APIVersion: m.apiVersion,
60+
ClientInterfaceTypeName: m.ClientInterfaceTypeName(),
61+
ClientStructTypeName: m.ClientStructTypeName(),
62+
CRDNames: m.crdNames(),
6263
}
6364
}
6465

@@ -866,6 +867,24 @@ func (m *Model) APIGroup() string {
866867
return fmt.Sprintf("%s.%s", m.servicePackageName, suffix)
867868
}
868869

870+
// ClientInterfaceTypeName returns the name of the aws-sdk-go primary API
871+
// interface type name.
872+
func (m *Model) ClientInterfaceTypeName() string {
873+
if m.cfg.SDKNames.ClientInterface != "" {
874+
return m.cfg.SDKNames.ClientInterface
875+
}
876+
return m.SDKAPI.ClientInterfaceTypeName()
877+
}
878+
879+
// ClientStructTypeName returns the name of the aws-sdk-go primary client
880+
// struct type name.
881+
func (m *Model) ClientStructTypeName() string {
882+
if m.cfg.SDKNames.ClientStruct != "" {
883+
return m.cfg.SDKNames.ClientStruct
884+
}
885+
return m.SDKAPI.ClientStructTypeName()
886+
}
887+
869888
// New returns a new Model struct for a supplied API model.
870889
// Optionally, pass a file path to a generator config file that can be used to
871890
// instruct the code generator how to handle the API properly

pkg/model/sdk_api.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,18 @@ func (a *SDKAPI) GetServiceFullName() string {
327327
return a.API.Metadata.ServiceFullName
328328
}
329329

330-
// APIInterfaceTypeName returns the name of the aws-sdk-go primary API
330+
// ClientInterfaceTypeName returns the name of the aws-sdk-go primary API
331331
// interface type name.
332-
func (a *SDKAPI) APIInterfaceTypeName() string {
332+
func (a *SDKAPI) ClientInterfaceTypeName() string {
333+
if a == nil || a.API == nil {
334+
return ""
335+
}
336+
return fmt.Sprintf("%s%s", a.API.StructName(), "API")
337+
}
338+
339+
// ClientStructTypeName returns the name of the aws-sdk-go primary API
340+
// struct type name.
341+
func (a *SDKAPI) ClientStructTypeName() string {
333342
if a == nil || a.API == nil {
334343
return ""
335344
}

templates/crossplane/pkg/controller.go.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func (e *external) Delete(ctx context.Context, mg cpresource.Managed) error {
177177

178178
type option func(*external)
179179

180-
func newExternal(kube client.Client, client svcsdkapi.{{ .APIInterfaceTypeName }}API, opts []option) *external {
180+
func newExternal(kube client.Client, client svcsdkapi.{{ .ClientInterfaceTypeName }}, opts []option) *external {
181181
e := &external{
182182
kube: kube,
183183
client: client,
@@ -223,7 +223,7 @@ func newExternal(kube client.Client, client svcsdkapi.{{ .APIInterfaceTypeName }
223223

224224
type external struct {
225225
kube client.Client
226-
client svcsdkapi.{{ .APIInterfaceTypeName }}API
226+
client svcsdkapi.{{ .ClientInterfaceTypeName }}
227227
{{- if .CRD.Ops.ReadOne }}
228228
preObserve func(context.Context, *svcapitypes.{{ .CRD.Names.Camel }}, *svcsdk.{{ .CRD.Ops.ReadOne.InputRef.Shape.ShapeName }}) error
229229
postObserve func(context.Context, *svcapitypes.{{ .CRD.Names.Camel }}, *svcsdk.{{ .CRD.Ops.ReadOne.OutputRef.Shape.ShapeName }}, managed.ExternalObservation, error) (managed.ExternalObservation, error)

templates/pkg/resource/manager.go.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type resourceManager struct {
6565
sess *session.Session
6666
// sdk is a pointer to the AWS service API interface exposed by the
6767
// aws-sdk-go/services/{alias}/{alias}iface package.
68-
sdkapi svcsdkapi.{{ .APIInterfaceTypeName }}API
68+
sdkapi svcsdkapi.{{ .ClientInterfaceTypeName }}
6969
}
7070

7171
// concreteResource returns a pointer to a resource from the supplied

templates/pkg/resource/sdk.go.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828
_ = &metav1.Time{}
2929
_ = strings.ToLower("")
3030
_ = &aws.JSONValue{}
31-
_ = &svcsdk.{{ .APIInterfaceTypeName}}{}
31+
_ = &svcsdk.{{ .ClientStructTypeName }}{}
3232
_ = &svcapitypes.{{ .CRD.Names.Camel }}{}
3333
_ = ackv1alpha1.AWSAccountID("")
3434
_ = &ackerr.NotFound

0 commit comments

Comments
 (0)