Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.0.0
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
src/
.gen/
dist/
node_modules/
lib/
bazel*
3 changes: 0 additions & 3 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
terraform 1.2.6
golang 1.20
nodejs 16.17.1

107 changes: 107 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# gazelle:prefix github.com/sourcegraph/controller-cdktf
# gazelle:build_file_name BUILD.bazel
load("@bazel_gazelle//:def.bzl", "gazelle")
load("//cdktf:cdktf.bzl", "cdktf")

# go stuff
gazelle(name = "gazelle")

gazelle(
name = "gazelle-update-repos",
args = [
"-from_file=go.mod",
"-to_macro=deps.bzl%go_dependencies",
"-prune",
"-build_file_proto_mode=disable_global",
],
command = "update-repos",
)

# cdktf stuff
module_prefix = "github.com/sourcegraph/controller-cdktf"

out_pkg = "gen"

cdktf(
name = "aws",
config = "config/aws.yaml",
module_prefix = module_prefix,
out_pkg = out_pkg,
)

cdktf(
name = "cloudflare",
config = "config/cloudflare.yaml",
module_prefix = module_prefix,
out_pkg = out_pkg,
)

cdktf(
name = "google",
config = "config/google.yaml",
module_prefix = module_prefix,
out_pkg = out_pkg,
)

cdktf(
name = "googlebeta",
config = "config/googlebeta.yaml",
module_prefix = module_prefix,
out_pkg = out_pkg,
)

cdktf(
name = "kubernetes",
config = "config/kubernetes.yaml",
module_prefix = module_prefix,
out_pkg = out_pkg,
)

cdktf(
name = "okta",
config = "config/okta.yaml",
module_prefix = module_prefix,
out_pkg = out_pkg,
)

cdktf(
name = "postgresql",
config = "config/postgresql.yaml",
module_prefix = module_prefix,
out_pkg = out_pkg,
)

cdktf(
name = "random",
config = "config/random.yaml",
module_prefix = module_prefix,
out_pkg = out_pkg,
)

cdktf(
name = "tfe",
config = "config/tfe.yaml",
module_prefix = module_prefix,
out_pkg = out_pkg,
)

cdktf(
name = "awsvpc",
config = "config/awsvpc.yaml",
module_prefix = module_prefix,
out_pkg = out_pkg,
)

cdktf(
name = "gkeprivate",
config = "config/gkeprivate.yaml",
module_prefix = module_prefix,
out_pkg = out_pkg,
)

cdktf(
name = "budget",
config = "config/budget.yaml",
module_prefix = module_prefix,
out_pkg = out_pkg,
)
170 changes: 119 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,148 @@
# controller-cdktf

This repo contains the generated code for [cdktf](https://github.com/hashicorp/terraform-cdk) in Go. The implementation is being developed in [sourcegraph/controller].

This package is only used internally at Sourcegraph - the generated code is public for ease of use and avoid performance issue with large amount of generated content being tracked in a Git repository.

## Development

### Adding a new provider or module to CDKTF

Edit [`cdktf.json`](./cdktf.json), and make sure modules and providers are pinned to a specific version. e.g.

```json
{
"terraformProviders": [
{
"name": "google",
"source": "hashicorp/google",
"version": "~> 4.38.0"
}
],
"terraformModules": [
{
"name": "googlesqldb",
"source": "git::https://github.com/michaellzc/terraform-google-sql-db//modules/postgresql?ref=feat/support-dynamic-iam-users"
}
]
}
# cdktf-provider-generator

> EXPERIMENTAL

Generate your own pre-built CDKTF providers for Go.

CDKTF publishes pre-built providers but they do not follow the upstream providers version. Usually, you want to pin to a specific version to fit your own needs. This project aims to solve this problem by generating your own pre-built providers.

To do so, we replicate the setup from the CDKTF pre-built provider repo, e.g., https://github.com/cdktf/cdktf-provider-tfe, but use Bazel to generate all assets.

## Usage

If you're just looking to consume the generated code, all available providers are under `gen/` under its own Go module.

Use `hashicorp/tfe` provider:

```sh
go get github.com/controller-cdktf/gen/tfe
```

Run the command below to auto-gen codes for added providers or modules:
Use `hashicorp/google` provider:

```sh
go generate .
go get github.com/controller-cdktf/gen/google
```

Commit all generated changes.
## Tasks

### Upgrading CDKTF
### Add a new provider

Review the [changelog](https://developer.hashicorp.com/terraform/cdktf/release#upgrade-guides) of the target release.
Watch out for breaking changes and adjust the upgrade plan if neccessary.
> NOTE: The name of the provider should always be one word and no special characters, e.g., use `googlebeta` instead of `google-beta`.

Bump the version in [`gen.go`](./gen.go):
For example, let's add the `kubernetes` provider:

> no `v` prefix
Create a config file at `config/kubernetes.yaml`

```yaml
name: kubernetes
provider:
source: registry.terraform.io/hashicorp/kubernetes
version: ~> 2.15.0
```

Add a new target in `BUILD.bazel`

```diff
--- a/gen.go
+++ b/gen.go
@@ -1,3 +1,3 @@
package cdktf
--- a/BUILD.bazel
+++ b/BUILD.bazel

+
+cdktf(
+ name = "kubernetes",
+ config = "kubernetes.yaml",
+ module_name = module_name,
+)
```

Generate the code

```sh
bazel run //:kubernetes
bazel run //:gazelle
```

Finally, commit all generated code and open a pull request.

### Add a new module

-//go:generate go run . --version 0.13.3
+//go:generate go run . --version 0.13.0
For example, let's add the `awsvpc` module:

Create a config file at `config/awsvpc.yaml`

```yaml
name: awsvpc
module:
source: terraform-aws-modules/vpc/aws
version: 3.19.0
```

Add a new target in `BUILD.bazel`

```diff
--- a/BUILD.bazel
+++ b/BUILD.bazel

+
+cdktf(
+ name = "awsvpc",
+ config = "awsvpc.yaml",
+ module_name = module_name,
+)
```

Re-generate the providers and modules code:
Generate the code

```sh
go generate ./
bazel run //:awsvpc
bazel run //:gazelle
```

Commit all changes in this repo. Upon merging the PR, then open another pull request in [sourcegraph/controller] to upgrade all `github.com/sourcegraph/controller-cdktf/gen/*` packages:
Finally, commit all generated code and open a pull request.

### Update a provider or module version

First, update the version in the corresponding config file, e.g., `gen/kubernetes.yaml`.

```diff
--- a/config/kubernetes.yaml
+++ b/config/kubernetes.yaml
@@ -1,4 +1,4 @@
name: kubernetes
provider:
source: registry.terraform.io/hashicorp/kubernetes
- version: 2.15.0
+ version: 2.29.0
```

Then, generate the code:

```sh
go get -u github.com/sourcegraph/controller-cdktf/gen/...
bazel run //:kubernetes
bazel run //:gazelle
```

Finally, commit all generated code and open a pull request.

## Roadmap

- [ ] Add support to configure target `cdktf` and `jsii` version
- [ ] Add support to configure target Terraform version

## FAQ

### Why not use the pre-built providers?
### Why not use `cdktf get`?

We would like to use specific versions of provides, hence we are not using pre-built providers, such as [cdktf/cdktf-provider-google-go](https://github.com/cdktf/cdktf-provider-google-go).
With a centralized `cdktf.json` where you declare all the providers and modules, changes to the file will require re-generating all providers and modules, which is slow.

### Why multiple modules instead of one?
By utilizing Bazel, we can only generate the code for the provider or module that has changed with its caching strategy.

Generated code combined from all providers and modules exceed the limit of `go get`, and this cannot be changed.
### Weird things happen during generation with Bazel?

First, try to clean the Bazel cache:

```sh
bazel clean --expunge
```

[sourcegraph/controller]: https://github.com/sourcegraph/controller
Then, try again.
Loading