Skip to content

Commit

Permalink
Merge pull request #742 from hashicorp/go-example-google-gke
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt authored Jun 15, 2021
2 parents 33b5132 + 21bc3d5 commit b8326b7
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Choose a language:
* [docker](./examples/go/docker)
* [aws](./examples/go/aws) ⚠️ _High memory usage: the provider generation currently needs ~6 GB of memory. Hence the maximum for Node.js is [currently set to 8GB](https://github.com/hashicorp/terraform-cdk/blob/11d2e783d1fe94e50abd116ba73689c02590a391/packages/cdktf-cli/lib/get/constructs-maker.ts#L279)_
* [google cloud kubernetes engine + kubernetes](./examples/go/google)

## Documentation

Expand Down
21 changes: 21 additions & 0 deletions examples/go/google/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

# cdktf and terraform ignores
.terraform
cdktf.out
cdktf.log
*terraform.*.tfstate*
generated

# src https://github.com/github/gitignore/blob/master/Go.gitignore
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
23 changes: 23 additions & 0 deletions examples/go/google/cdktf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"language": "go",
"app": "go run main.go",
"codeMakerOutput": "generated",
"terraformProviders": [
"hashicorp/kubernetes@ ~> 2.2.0",
"kreuzwerker/docker@ ~> 2.11.0",
"hashicorp/local@ ~> 2.1.0",
"hashicorp/helm@ ~> 2.1.2",
"hashicorp/google@ ~> 3.69.0"
],
"terraformModules": [
{
"name": "gkeAuth",
"source": "terraform-google-modules/kubernetes-engine/google//modules/auth",
"version": "14.3.0"
}
],
"context": {
"excludeStackIdFromLogicalIds": "true",
"allowSepCharsInLogicalIds": "true"
}
}
12 changes: 12 additions & 0 deletions examples/go/google/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/hashicorp/terraform-cdk/examples/go/google

go 1.16

require github.com/aws/constructs-go/constructs/v3 v3.3.75

require (
github.com/aws/jsii-runtime-go v1.29.0
github.com/hashicorp/terraform-cdk-go/cdktf v0.0.0
)

replace github.com/hashicorp/terraform-cdk-go/cdktf => ../../../packages/cdktf/dist/go/cdktf
17 changes: 17 additions & 0 deletions examples/go/google/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/aws/constructs-go/constructs/v3 v3.3.75 h1:sunQ8/mQwVoV0OdGYmSh/s8Z2XyUXr8hCc/bKzz6SoU=
github.com/aws/constructs-go/constructs/v3 v3.3.75/go.mod h1:gh+X5UyxGPp/fWyp8iVMa75yMDQev4639pQ93HXXMoA=
github.com/aws/jsii-runtime-go v1.28.0/go.mod h1:6tZnlstx8bAB3vnLFF9n8bbkI//LDblAek9zFyMXV3E=
github.com/aws/jsii-runtime-go v1.29.0 h1:JH4GHSauYskfzREWaQMAMTXDRgNR2e5XeLbjD/xf9J0=
github.com/aws/jsii-runtime-go v1.29.0/go.mod h1:6tZnlstx8bAB3vnLFF9n8bbkI//LDblAek9zFyMXV3E=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
24 changes: 24 additions & 0 deletions examples/go/google/help
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
========================================================================================================

Your cdktf go project is ready!

cat help Prints this message

Compile:
go build Builds your go project

Synthesize:
cdktf synth [stack] Synthesize Terraform resources to cdktf.out/

Diff:
cdktf diff [stack] Perform a diff (terraform plan) for the given stack

Deploy:
cdktf deploy [stack] Deploy the given stack

Destroy:
cdktf destroy [stack] Destroy the given stack

Learn more about using modules and providers https://cdk.tf/modules-and-providers

========================================================================================================
100 changes: 100 additions & 0 deletions examples/go/google/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
constructs "github.com/aws/constructs-go/constructs/v3"
"github.com/hashicorp/terraform-cdk-go/cdktf"

jsii "github.com/aws/jsii-runtime-go"
"github.com/hashicorp/terraform-cdk/examples/go/google/generated/gkeAuth"
"github.com/hashicorp/terraform-cdk/examples/go/google/generated/hashicorp/google"
"github.com/hashicorp/terraform-cdk/examples/go/google/generated/hashicorp/helm"
"github.com/hashicorp/terraform-cdk/examples/go/google/generated/hashicorp/kubernetes"
"github.com/hashicorp/terraform-cdk/examples/go/google/generated/hashicorp/local"
)

func NewMyStack(scope constructs.Construct, id string) cdktf.TerraformStack {
stack := cdktf.NewTerraformStack(scope, &id)
google.NewGoogleProvider(stack, jsii.String("google"), &google.GoogleProviderConfig{
Zone: jsii.String("us-west1"),
Project: jsii.String("dschmidt-cdk-test"),
})

sa := google.NewServiceAccount(stack, jsii.String("sa"), &google.ServiceAccountConfig{
AccountId: jsii.String("cluster-admin"),
DisplayName: jsii.String("Cluster Admin"),
})

cluster := google.NewContainerCluster(stack, jsii.String("cluster"), &google.ContainerClusterConfig{
Name: jsii.String("cluster"),
RemoveDefaultNodePool: jsii.Bool(true),
InitialNodeCount: jsii.Number(1),
})

google.NewContainerNodePool(stack, jsii.String("main-pool"), &google.ContainerNodePoolConfig{
Name: jsii.String("main"),
Cluster: cluster.Name(),
NodeConfig: &[]*google.ContainerNodePoolNodeConfig{
{
MachineType: jsii.String("e2-medium"),
Preemptible: jsii.Bool(true),
ServiceAccount: sa.Email(),
OauthScopes: &[]*string{jsii.String("https://www.googleapis.com/auth/cloud-platform")},
},
},
})

auth := gkeAuth.NewGkeAuth(stack, jsii.String("auth"), &gkeAuth.GkeAuthOptions{
ClusterName: cluster.Name(),
Location: cluster.Location(),
ProjectId: cluster.Project(),
})

local.NewFile(stack, jsii.String("kubeconfig"), &local.FileConfig{
Filename: jsii.String("kubeconfig.yaml"),
Content: auth.KubeconfigRawOutput(),
})

kubernetes.NewKubernetesProvider(stack, jsii.String("kubernetes"), &kubernetes.KubernetesProviderConfig{
ClusterCaCertificate: auth.ClusterCaCertificateOutput(),
Host: auth.HostOutput(),
Token: auth.TokenOutput(),
})

namespaceName := "development"
kubernetes.NewNamespace(stack, jsii.String("namespace"), &kubernetes.NamespaceConfig{
Metadata: &[]*kubernetes.NamespaceMetadata{
{
Name: jsii.String(namespaceName),
},
},
})

helm.NewHelmProvider(stack, jsii.String("helm"), &helm.HelmProviderConfig{
Kubernetes: &[]*helm.HelmProviderKubernetes{
{
ClusterCaCertificate: auth.ClusterCaCertificateOutput(),
Host: auth.HostOutput(),
Token: auth.TokenOutput(),
},
},
})

helm.NewRelease(stack, jsii.String("cert-manager"), &helm.ReleaseConfig{
Name: jsii.String("cert-manager"),
Repository: jsii.String("https://charts.jetstack.io"),
Chart: jsii.String("cert-manager"),
CreateNamespace: jsii.Bool(true),
Namespace: jsii.String("cert-manager"),
Version: jsii.String("v1.3.1"),
})

return stack
}

func main() {
app := cdktf.NewApp(nil)

NewMyStack(app, "google")

app.Synth()
}
10 changes: 10 additions & 0 deletions examples/go/google/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@examples/go-google",
"version": "0.0.0",
"license": "MPL-2.0",
"scripts": {
"reinstall": "rm go.sum",
"build": "cdktf get && go mod tidy",
"synth": "cdktf synth"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"package-windows": "lerna run package && tools\\collect-dist.bat",
"bootstrap-plugin-cache": "./test/run-against-dist ./tools/bootstrap-plugin-cache.sh",
"examples:integration": "test/run-against-dist tools/build-examples.sh",
"examples:integration:java": "test/run-against-dist tools/build-examples-java.sh",
"examples:integration:java": "test/run-against-dist tools/build-examples-sequential.sh java",
"examples:integration:csharp": "test/run-against-dist tools/build-examples.sh csharp",
"examples:integration:python": "test/run-against-dist tools/build-examples.sh python",
"examples:integration:typescript": "test/run-against-dist tools/build-examples.sh typescript",
"examples:integration:go": "test/run-against-dist tools/build-examples.sh go",
"examples:integration:go": "test/run-against-dist tools/build-examples-sequential.sh go",
"test": "lerna run --scope cdktf* --scope @cdktf* test",
"watch": "lerna run --parallel --stream --scope cdktf* watch-preserve-output",
"link-packages": "lerna exec --scope cdktf* --scope @cdktf* yarn link",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ cd $(dirname $0)/..
# Disable spinner even when we have a TTY
export CI='1'

npx lerna run --stream --concurrency 1 --no-bail --scope @examples/java* reinstall
npx lerna run --stream --concurrency 1 --no-bail --scope @examples/java* build
npx lerna run --stream --concurrency 1 --no-bail --scope @examples/java* synth
BUILD_TARGET=$1

npx lerna run --stream --concurrency 1 --no-bail --scope @examples/${BUILD_TARGET}* reinstall
npx lerna run --stream --concurrency 1 --no-bail --scope @examples/${BUILD_TARGET}* build
npx lerna run --stream --concurrency 1 --no-bail --scope @examples/${BUILD_TARGET}* synth

0 comments on commit b8326b7

Please sign in to comment.