Skip to content

Conversation

@jaypipes
Copy link
Collaborator

@jaypipes jaypipes commented Mar 6, 2021

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:

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:

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:

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.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.


BOILERPLATE_TXT_PATH="$ROOT_DIR/templates/boilerplate.txt"
DEFAULT_TEMPLATE_DIRS="$ROOT_DIR/templates"
# If the service controller source repository has a templates/ directory, add
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very minor: Maybe adding a comment regarding the priority of template override here will be helpful. If script run does not produce desired results, the developer first comes to check this script rather than reading the priority related comment in go files.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vijtrip2 great suggestion. added!

Copy link
Contributor

@vijtrip2 vijtrip2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

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.
@jaypipes jaypipes force-pushed the multi-template-base-path branch from 21275b3 to 907b085 Compare March 9, 2021 00:17
@jaypipes jaypipes merged commit d7348ce into aws-controllers-k8s:main Mar 9, 2021
Copy link
Contributor

@surajkota surajkota left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! Thanks for PR, this is more readable than code in generator config. Also gives flexibility if developer wants to write a separate requeueIfNotRunning method or insert the code in place if its a few lines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants