Skip to content

Commit 907b085

Browse files
committed
add multiple template base path functionality
We want different teams to have the ability to specify template overrides for their specific controller or generated codebase. There are a number of use cases where having the ability to specify multiple "template search paths" will be useful. Imagine that the ElastiCache ACK controller team wants to change their boilerplate.txt content to mention something specific to ElastiCache. Instead of adding a bunch of conditional statements to the code generator or new fields to the generator configuration object (which is already complicated), what if the maintainer team could add their custom boilerplate text to a `templates/boilerplate.txt` file in the elasticache-controller source repository and call `ack-generate` commands like this: ```bash CODE_GEN_REPO=$GOPATH/src/github.com/aws-controllers-k8s/code-generator CONTROLLER_REPO=$GOPATH/src/github.com/aws-controllers-k8s/elasticache-controller ack-generate apis --template-dirs $CONTROLLER_REPO/templates,$CODE_GEN_REPO/templates` ``` and the code generator would simply pick up the boilerplate.txt file in the elasticache-controller repository and use that instead of the one in the code-generator repo. Another use case might be for a service controller team to experiment with some Go code changes to the standard template files without needing to modify any code in the code-generator repo itself. For example, let's say that the S3 controller requires a radically different set of Go code for its update code path than all the other controllers and we want to test some ideas but not update the code-generator repo itself. We could add a `templates/pkg/resource/sdk_update.go.tpl` file to the s3-controller repository, rebuild the controller and iterate more quickly. Finally, there is the use case of having templates containing "hook code" that is specific to one service controller. Imagine the following generator config snippet: ```yaml resources: Broker: hooks: set_update_pre_build_update_request: templatePath: hooks/sdk_update_pre_build_update_request.go.tpl ``` And in the service controller, we had a file `templates/hooks/sdk_update_pre_build_update_request.go.tpl` with the contents: ```go if err := rm.requeueIfNotRunning; err != nil { return nil, err } ``` We could call the `ack-generate` passing in multiple template search base paths like shown above and in this way, we could have service-specific code placed inside the service controller source repositories and have our code generation be much more flexible.
1 parent cb1d017 commit 907b085

File tree

13 files changed

+134
-62
lines changed

13 files changed

+134
-62
lines changed

cmd/ack-generate/command/apis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func generateAPIs(cmd *cobra.Command, args []string) error {
8585
if err != nil {
8686
return err
8787
}
88-
ts, err := ackgenerate.APIs(g, optTemplatesDir)
88+
ts, err := ackgenerate.APIs(g, optTemplateDirs)
8989
if err != nil {
9090
return err
9191
}

cmd/ack-generate/command/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func generateController(cmd *cobra.Command, args []string) error {
8282
if err != nil {
8383
return err
8484
}
85-
ts, err := ackgenerate.Controller(g, optTemplatesDir)
85+
ts, err := ackgenerate.Controller(g, optTemplateDirs)
8686
if err != nil {
8787
return err
8888
}

cmd/ack-generate/command/crossplane.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func generateCrossplane(_ *cobra.Command, args []string) error {
5353
if err := ensureSDKRepo(optCacheDir); err != nil {
5454
return err
5555
}
56-
optTemplatesDir = filepath.Join(optTemplatesDir, "crossplane")
5756
svcAlias := strings.ToLower(args[0])
5857
sdkHelper := model.NewSDKHelper(sdkDir)
5958
sdkHelper.APIGroupSuffix = "aws.crossplane.io"
@@ -83,7 +82,7 @@ func generateCrossplane(_ *cobra.Command, args []string) error {
8382
return err
8483
}
8584

86-
ts, err := cpgenerate.Crossplane(g, optTemplatesDir)
85+
ts, err := cpgenerate.Crossplane(g, optTemplateDirs)
8786
if err != nil {
8887
return err
8988
}

cmd/ack-generate/command/olm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func generateOLMAssets(cmd *cobra.Command, args []string) error {
138138
}
139139

140140
// generate templates
141-
ts, err := olmgenerate.BundleAssets(g, commonMeta, svcConf, version, optTemplatesDir)
141+
ts, err := olmgenerate.BundleAssets(g, commonMeta, svcConf, version, optTemplateDirs)
142142
if err != nil {
143143
return err
144144
}

cmd/ack-generate/command/release.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func generateRelease(cmd *cobra.Command, args []string) error {
8787
return err
8888
}
8989
ts, err := ackgenerate.Release(
90-
g, optTemplatesDir,
90+
g, optTemplateDirs,
9191
releaseVersion, optImageRepository, optServiceAccountName,
9292
)
9393
if err != nil {

cmd/ack-generate/command/root.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ var (
3838
optCacheDir string
3939
optRefreshCache bool
4040
optAWSSDKGoVersion string
41-
defaultTemplatesDir string
42-
optTemplatesDir string
41+
defaultTemplateDirs []string
42+
optTemplateDirs []string
4343
defaultServicesDir string
4444
optServicesDir string
4545
optDryRun bool
@@ -80,7 +80,7 @@ func init() {
8080
for _, tryPath := range tryPaths {
8181
if fi, err := os.Stat(tryPath); err == nil {
8282
if fi.IsDir() {
83-
defaultTemplatesDir = tryPath
83+
defaultTemplateDirs = append(defaultTemplateDirs, tryPath)
8484
break
8585
}
8686
}
@@ -100,8 +100,8 @@ func init() {
100100
rootCmd.PersistentFlags().BoolVar(
101101
&optDryRun, "dry-run", false, "If true, outputs all files to stdout",
102102
)
103-
rootCmd.PersistentFlags().StringVar(
104-
&optTemplatesDir, "templates-dir", defaultTemplatesDir, "Path to directory with templates to use in code generation",
103+
rootCmd.PersistentFlags().StringSliceVar(
104+
&optTemplateDirs, "template-dirs", defaultTemplateDirs, "Paths to directories with templates to use in code generation. Note that the order in which directories is specified will be used to provide override functionality.",
105105
)
106106
rootCmd.PersistentFlags().StringVar(
107107
&optServicesDir, "services-dir", defaultServicesDir, "Path to directory to output service-specific code",

pkg/generate/ack/apis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var (
4646
// generating ACK service controller's apis/ contents
4747
func APIs(
4848
g *generate.Generator,
49-
templateBasePath string,
49+
templateBasePaths []string,
5050
) (*templateset.TemplateSet, error) {
5151
enumDefs, err := g.GetEnumDefs()
5252
if err != nil {
@@ -62,7 +62,7 @@ func APIs(
6262
}
6363

6464
ts := templateset.New(
65-
templateBasePath,
65+
templateBasePaths,
6666
apisIncludePaths,
6767
apisCopyPaths,
6868
apisFuncMap,

pkg/generate/ack/controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ var (
112112
// for generating ACK service controller implementations
113113
func Controller(
114114
g *generate.Generator,
115-
templateBasePath string,
115+
templateBasePaths []string,
116116
) (*templateset.TemplateSet, error) {
117117
crds, err := g.GetCRDs()
118118
if err != nil {
119119
return nil, err
120120
}
121121

122122
ts := templateset.New(
123-
templateBasePath,
123+
templateBasePaths,
124124
controllerIncludePaths,
125125
controllerCopyPaths,
126126
controllerFuncMap,

pkg/generate/ack/release.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var (
4747
// generating an ACK service controller release (Helm artifacts, etc)
4848
func Release(
4949
g *generate.Generator,
50-
templateBasePath string,
50+
templateBasePaths []string,
5151
// releaseVersion is the SemVer string describing the release that the Helm
5252
// chart will install
5353
releaseVersion string,
@@ -59,7 +59,7 @@ func Release(
5959
serviceAccountName string,
6060
) (*templateset.TemplateSet, error) {
6161
ts := templateset.New(
62-
templateBasePath,
62+
templateBasePaths,
6363
releaseIncludePaths,
6464
releaseCopyPaths,
6565
releaseFuncMap,

pkg/generate/crossplane/crossplane.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ import (
2727

2828
var (
2929
apisTemplatePaths = []string{
30-
"apis/doc.go.tpl",
31-
"apis/enums.go.tpl",
32-
"apis/groupversion_info.go.tpl",
33-
"apis/types.go.tpl",
30+
"crossplane/apis/doc.go.tpl",
31+
"crossplane/apis/enums.go.tpl",
32+
"crossplane/apis/groupversion_info.go.tpl",
33+
"crossplane/apis/types.go.tpl",
3434
}
3535
includePaths = []string{
36-
"boilerplate.go.tpl",
37-
"apis/enum_def.go.tpl",
38-
"apis/type_def.go.tpl",
39-
"pkg/sdk_find_read_one.go.tpl",
40-
"pkg/sdk_find_read_many.go.tpl",
41-
"pkg/sdk_find_get_attributes.go.tpl",
36+
"crossplane/boilerplate.go.tpl",
37+
"crossplane/apis/enum_def.go.tpl",
38+
"crossplane/apis/type_def.go.tpl",
39+
"crossplane/pkg/sdk_find_read_one.go.tpl",
40+
"crossplane/pkg/sdk_find_read_many.go.tpl",
41+
"crossplane/pkg/sdk_find_get_attributes.go.tpl",
4242
}
4343
copyPaths = []string{}
4444
funcMap = ttpl.FuncMap{
@@ -111,7 +111,7 @@ type templateCRDVars struct {
111111
// generating Crossplane API types and controller code for an AWS service API
112112
func Crossplane(
113113
g *generate.Generator,
114-
templateBasePath string,
114+
templateBasePaths []string,
115115
) (*templateset.TemplateSet, error) {
116116
enumDefs, err := g.GetEnumDefs()
117117
if err != nil {
@@ -127,7 +127,7 @@ func Crossplane(
127127
}
128128

129129
ts := templateset.New(
130-
templateBasePath,
130+
templateBasePaths,
131131
includePaths,
132132
copyPaths,
133133
funcMap,

0 commit comments

Comments
 (0)