Skip to content

Commit

Permalink
docs(k8s): update terraform-gke example for Bonsai (#4000)
Browse files Browse the repository at this point in the history
* Generate kubeconfig for GKE cluster

In order to "one-shot" deploy the example a GKE cluster and our demo
services, we need a kubeconfig. Because of
#3475, a kubeconfig managed by
Garden should be generated with elevated privileges.

* Set hardcoded values to variables

To improve comprehension and expose the values a user should set for a
project at the top of the file, we set the hardcoded values of the
Garden project file to variables.

* Convert 0.12 modules to 0.13

* Specify Terraform one-shot deployments in docs

The README previously assumed a cluster ready to work with. We change
the instructions to assume no pre-existing cluster.

* Simplify and atomize terraform

Previously the GKE example was an all-in-one terraform file. I've chosen
to make use of battle-tested GKE modules and split the terraform for
easy comprehension.
  • Loading branch information
worldofgeese authored Apr 11, 2023
1 parent 94c9d91 commit d4f3ef0
Show file tree
Hide file tree
Showing 13 changed files with 568 additions and 225 deletions.
20 changes: 5 additions & 15 deletions examples/terraform-gke/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,18 @@ The Terraform stack needs some input values from you. You can provide those in a

```tfvars
# terraform.tfvars
gcp_region = "europe-west1"°
gcp_project_id = "my-test-project"
region = "europe-west1"°
project_id = "my-test-project"
```

**B)** Configure the variables directly in the project `garden.yml` (see the `terraform` provider section).

### Step 4 - Initialize the cluster
### Step 4 - Deploy your stack

Install the cluster-wide services Garden needs by running:
Finally, build and deploy your GKE cluster and services to the new cluster:

```sh
garden plugins kubernetes cluster-init
```

This will take a while because the cluster needs to be provisioned, and some services installed when it's ready.

### Step 5 - Deploy your services

Finally, to build and deploy your services to your new GKE cluster, run:

```sh
garden deploy
garden deploy -l2 #the -l2 flag is optional, but recommended to observe the progress of the Terraform deployment
```

And that's it!
Expand Down
27 changes: 16 additions & 11 deletions examples/terraform-gke/backend/garden.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
kind: Module
kind: Build
name: backend
type: container

---

kind: Deploy
name: backend
build: backend
description: Backend service container
type: container
services:
- name: backend
ports:
- name: http
containerPort: 8080
# Maps service:80 -> container:8080
servicePort: 80
ingresses:
- path: /hello-backend
port: http
spec:
ports:
- name: http
containerPort: 8080
servicePort: 80
ingresses:
- path: /hello-backend
port: http
64 changes: 40 additions & 24 deletions examples/terraform-gke/frontend/garden.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
kind: Module
kind: Build
name: frontend
type: container

---
kind: Deploy
name: frontend
build: frontend
description: Frontend service container
type: container
services:
- name: frontend
ports:
- name: http
containerPort: 8080
healthCheck:
httpGet:
path: /hello-frontend
port: http
ingresses:
- path: /hello-frontend
port: http
- path: /call-backend
port: http
dependencies:
- backend
tests:
- name: unit
args: [npm, test]
- name: integ
args: [npm, run, integ]
dependencies:
- frontend
spec:
ports:
- name: http
containerPort: 8080
healthCheck:
httpGet:
path: /hello-frontend
port: http
ingresses:
- path: /hello-frontend
port: http
- path: /call-backend
port: http
dependencies:
- deploy.backend

---
kind: Test
name: frontend-unit
build: frontend
type: container
spec:
args: [npm, test]

---
kind: Test
name: frontend-integ
build: frontend
type: container
spec:
args: [npm, run, integ]
dependencies:
- deploy.backend
46 changes: 33 additions & 13 deletions examples/terraform-gke/garden.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
kind: Project
name: terraform-gke

environments:
- name: dev
defaultNamespace: ${project.name}-${variables.userId}
- name: prod
defaultNamespace: ${project.name}
production: true

variables:
# change these values to your own
username: worldofgeese
gcpProjectId: devrel-348008
gcpRegion: europe-north1
registryHostname: docker.io
registryNamespace: worldofgeese
terraformInitRoot: "./terraform/"
userId: ${local.username}
providers:
- name: terraform
# This must be set to indicate that there is a stack in the project root.
initRoot: "."
# This must be set to indicate that there is a stack in the terraform project folder.
initRoot: "${var.terraformInitRoot}"
autoApply: true
# You can either uncomment and replace these with your own values, or provide the values in a
# terraform.tfvars file in the project root.
# variables:
# gcp_project_id: garden-gke-tf-${var.userId}
# gcp_region: europe-west1
allowDestroy: true
# You can either uncomment and replace these with your own values, or provide the values in a
# terraform.tfvars file in the project root.
variables:
project_id: ${var.gcpProjectId}
region: ${var.gcpRegion}

- name: kubernetes
kubeconfig: ${providers.terraform.outputs.kubeconfig_path}
context: gke
defaultHostname: terraform-gke-${var.userId}.dev-2.sys.garden
buildMode: kaniko
kubeconfig: ${var.terraformInitRoot}/${providers.terraform.outputs.kubeconfig_path}
context: ${providers.terraform.outputs.cluster_name}
# any hostname that points to your cluster's ingress controller
defaultHostname: ${var.userId}-gke.dev-1.sys.garden
# Replace the below values as appropriate
deploymentRegistry:
hostname: eu.gcr.io # <- set this according to the region your cluster runs in
namespace: garden-ci # <- set this to the project ID of the target cluster
imagePullSecrets:
# Make sure this matches the name and namespace of the imagePullSecret you've created
# to authenticate with your registry (if needed)
- name: gcr-config
namespace: default
hostname: ${variables.registryHostname}
namespace: ${variables.registryNamespace}
162 changes: 0 additions & 162 deletions examples/terraform-gke/gke.tf

This file was deleted.

Loading

0 comments on commit d4f3ef0

Please sign in to comment.