From 5b2da5795f70b2ebfbabddc6d9f3555bc3c93d1f Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Wed, 26 May 2021 16:04:00 +0200 Subject: [PATCH 1/2] chore(docs): add google cloud kubernetes example --- README.md | 1 + examples/go/google/.gitignore | 21 +++++++ examples/go/google/cdktf.json | 23 ++++++++ examples/go/google/go.mod | 12 ++++ examples/go/google/go.sum | 17 ++++++ examples/go/google/help | 24 ++++++++ examples/go/google/main.go | 100 ++++++++++++++++++++++++++++++++ examples/go/google/package.json | 10 ++++ 8 files changed, 208 insertions(+) create mode 100644 examples/go/google/.gitignore create mode 100644 examples/go/google/cdktf.json create mode 100644 examples/go/google/go.mod create mode 100644 examples/go/google/go.sum create mode 100644 examples/go/google/help create mode 100644 examples/go/google/main.go create mode 100644 examples/go/google/package.json diff --git a/README.md b/README.md index 129003b9c2..e5e6f17c2d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/go/google/.gitignore b/examples/go/google/.gitignore new file mode 100644 index 0000000000..4033944b19 --- /dev/null +++ b/examples/go/google/.gitignore @@ -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 diff --git a/examples/go/google/cdktf.json b/examples/go/google/cdktf.json new file mode 100644 index 0000000000..3adf6e212d --- /dev/null +++ b/examples/go/google/cdktf.json @@ -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" + } +} diff --git a/examples/go/google/go.mod b/examples/go/google/go.mod new file mode 100644 index 0000000000..bd7b48b8ac --- /dev/null +++ b/examples/go/google/go.mod @@ -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 diff --git a/examples/go/google/go.sum b/examples/go/google/go.sum new file mode 100644 index 0000000000..818e42f18c --- /dev/null +++ b/examples/go/google/go.sum @@ -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= diff --git a/examples/go/google/help b/examples/go/google/help new file mode 100644 index 0000000000..8640d244ec --- /dev/null +++ b/examples/go/google/help @@ -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 + +======================================================================================================== \ No newline at end of file diff --git a/examples/go/google/main.go b/examples/go/google/main.go new file mode 100644 index 0000000000..cef153d866 --- /dev/null +++ b/examples/go/google/main.go @@ -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() +} diff --git a/examples/go/google/package.json b/examples/go/google/package.json new file mode 100644 index 0000000000..0a9734bb88 --- /dev/null +++ b/examples/go/google/package.json @@ -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" + } + } \ No newline at end of file From 21bc3d52621a4e40aeac5ae70ebe946dc2d48503 Mon Sep 17 00:00:00 2001 From: Ansgar Mertens Date: Tue, 15 Jun 2021 15:57:49 +0200 Subject: [PATCH 2/2] fix(examples): build go examples sequentially as we do with java ones adding the example for google cloud might have caused the build to fail as generating the aws provider already requires almost all available memory and simultaneously also generating the google provider might have been to much for the 7 GB of memory available in Github Actions --- package.json | 4 ++-- ...uild-examples-java.sh => build-examples-sequential.sh} | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) rename tools/{build-examples-java.sh => build-examples-sequential.sh} (76%) diff --git a/package.json b/package.json index f815827f9f..7dd58b48b2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tools/build-examples-java.sh b/tools/build-examples-sequential.sh similarity index 76% rename from tools/build-examples-java.sh rename to tools/build-examples-sequential.sh index 6879143487..10cb052c21 100755 --- a/tools/build-examples-java.sh +++ b/tools/build-examples-sequential.sh @@ -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