Skip to content

Commit de0851f

Browse files
authored
Merge pull request #4 from terraform-google-modules/initial-release
Initial release
2 parents 0833bd8 + 456b347 commit de0851f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3524
-2
lines changed

.gitignore

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# OSX leaves these everywhere on SMB shares
2+
._*
3+
4+
# OSX trash
5+
.DS_Store
6+
7+
# Python
8+
*.pyc
9+
10+
# Emacs save files
11+
*~
12+
\#*\#
13+
.\#*
14+
15+
# Vim-related files
16+
[._]*.s[a-w][a-z]
17+
[._]s[a-w][a-z]
18+
*.un~
19+
Session.vim
20+
.netrwhist
21+
22+
### https://raw.github.com/github/gitignore/90f149de451a5433aebd94d02d11b0e28843a1af/Terraform.gitignore
23+
24+
# Local .terraform directories
25+
**/.terraform/*
26+
27+
# .tfstate files
28+
*.tfstate
29+
*.tfstate.*
30+
31+
# Crash log files
32+
crash.log
33+
34+
# Kitchen files
35+
**/inspec.lock
36+
**.gem
37+
**/.kitchen
38+
**/.kitchen.local.yml
39+
**/Gemfile.lock
40+
41+
# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
42+
# .tfvars files are managed as part of configuration and so should be included in
43+
# version control.
44+
#
45+
# example.tfvars
46+
**/terraform.tfvars
47+
48+
test/integration/gcloud/config.sh
49+
test/integration/tmp

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
Apache License
23
Version 2.0, January 2004
34
http://www.apache.org/licenses/

Makefile

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Make will use bash instead of sh
16+
SHELL := /usr/bin/env bash
17+
18+
# All is the first target in the file so it will get picked up when you just run 'make' on its own
19+
all: check_shell check_python check_golang check_terraform check_docker check_base_files test_check_headers check_headers check_trailing_whitespace generate_docs
20+
21+
# The .PHONY directive tells make that this isn't a real target and so
22+
# the presence of a file named 'check_shell' won't cause this target to stop
23+
# working
24+
.PHONY: check_shell
25+
check_shell:
26+
@source test/make.sh && check_shell
27+
28+
.PHONY: check_python
29+
check_python:
30+
@source test/make.sh && check_python
31+
32+
.PHONY: check_golang
33+
check_golang:
34+
@source test/make.sh && golang
35+
36+
.PHONY: check_terraform
37+
check_terraform:
38+
@source test/make.sh && check_terraform
39+
40+
.PHONY: check_docker
41+
check_docker:
42+
@source test/make.sh && docker
43+
44+
.PHONY: check_base_files
45+
check_base_files:
46+
@source test/make.sh && basefiles
47+
48+
.PHONY: check_shebangs
49+
check_shebangs:
50+
@source test/make.sh && check_bash
51+
52+
.PHONY: check_trailing_whitespace
53+
check_trailing_whitespace:
54+
@source test/make.sh && check_trailing_whitespace
55+
56+
.PHONY: test_check_headers
57+
test_check_headers:
58+
@echo "Testing the validity of the header check"
59+
@python test/test_verify_boilerplate.py
60+
61+
.PHONY: check_headers
62+
check_headers:
63+
@echo "Checking file headers"
64+
@python test/verify_boilerplate.py
65+
66+
.PHONY: generate_docs
67+
generate_docs:
68+
@source test/make.sh && generate_docs
69+
70+
# Integration tests
71+
72+
.PHONY: regional_test_integration
73+
regional_test_integration:
74+
./test/integration/gcloud/run.sh regional
75+
76+
.PHONY: zonal_test_integration
77+
zonal_test_integration:
78+
./test/integration/gcloud/run.sh zonal
79+
80+
.PHONY: test_integration
81+
test_integration: regional_test_integration zonal_test_integration
82+
@echo "Running tests for regional and zonal clusters"

README.md

Lines changed: 254 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,254 @@
1-
# terraform-google-kubernetes-engine
2-
A Cloud Foundation Toolkit Module: Opinionated Google Cloud Platform project creation and configuration with Shared VPC, IAM, APIs, etc.
1+
# Terraform Kubernetes Engine Module
2+
3+
This module handles opinionated Google Cloud Platform Kubernetes Engine cluster creation and configuration with Node Pools, IP MASQ, Network Policy, etc.
4+
5+
## Requirements
6+
### Kubectl
7+
- [kubectl](https://github.com/kubernetes/kubernetes/releases) 1.9.x
8+
### Terraform plugins
9+
- [Terraform](https://www.terraform.io/downloads.html) 0.10.x
10+
- [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) plugin v1.8.0
11+
12+
### Configure a Service Account
13+
In order to execute this module you must have a Service Account with the following:
14+
15+
#### Roles
16+
The service account with the following roles:
17+
- roles/compute.viewer on the project
18+
- roles/container.clusterAdmin on the project
19+
20+
### Enable API's
21+
In order to operate with the Service Account you must activate the following APIs on the project where the Service Account was created:
22+
23+
- Compute Engine API - compute.googleapis.com
24+
- Kubernetes Engine API - container.googleapis.com
25+
26+
## Install
27+
28+
### Terraform
29+
Be sure you have the correct Terraform version (0.10.x), you can choose the binary here:
30+
- https://releases.hashicorp.com/terraform/
31+
32+
## Usage
33+
There are multiple examples included in the [examples](./examples/) folder but simple usage is as follows:
34+
35+
```hcl
36+
module "gke" {
37+
source = "github.com/terraform-google-modules/terraform-google-kubernetes-engine"
38+
project_id = "<PROJECT ID>"
39+
name = "gke-test-1"
40+
region = "us-central1"
41+
zones = ["us-central1-a", "us-central1-b", "us-central1-f"]
42+
network = "vpc-01"
43+
subnetwork = "us-central1-01"
44+
ip_range_pods = "us-central1-01-gke-01-pods"
45+
ip_range_services = "us-central1-01-gke-01-services"
46+
http_load_balancing = false
47+
horizontal_pod_autoscaling = true
48+
kubernetes_dashboard = true
49+
network_policy = true
50+
51+
node_pools = [
52+
{
53+
name = "default-node-pool"
54+
machine_type = "n1-standard-2"
55+
min_count = 1
56+
max_count = 100
57+
disk_size_gb = 100
58+
disk_type = "pd-standard"
59+
image_type = "COS"
60+
auto_repair = true
61+
auto_upgrade = true
62+
service_account = "project-service-account@<PROJECT ID>.iam.gserviceaccount.com"
63+
},
64+
]
65+
66+
node_pools_labels = {
67+
all = {}
68+
69+
default-node-pool = {
70+
default-node-pool = "true"
71+
}
72+
}
73+
74+
node_pools_taints = {
75+
all = []
76+
77+
default-node-pool = [
78+
{
79+
key = "default-node-pool"
80+
value = "true"
81+
effect = "PREFER_NO_SCHEDULE"
82+
},
83+
]
84+
}
85+
86+
node_pools_tags = {
87+
all = []
88+
89+
default-node-pool = [
90+
"default-node-pool",
91+
]
92+
}
93+
}
94+
```
95+
96+
Then perform the following commands on the root folder:
97+
98+
- `terraform init` to get the plugins
99+
- `terraform plan` to see the infrastructure plan
100+
- `terraform apply` to apply the infrastructure build
101+
- `terraform destroy` to destroy the built infrastructure
102+
[^]: (autogen_docs_start)
103+
104+
105+
## Inputs
106+
107+
| Name | Description | Type | Default | Required |
108+
|------|-------------|:----:|:-----:|:-----:|
109+
| description | The description of the cluster | string | `` | no |
110+
| horizontal_pod_autoscaling | Enable horizontal pod autoscaling addon | string | `false` | no |
111+
| http_load_balancing | Enable httpload balancer addon | string | `true` | no |
112+
| ip_masq_link_local | Whether to masquerade traffic to the link-local prefix (169.254.0.0/16). | string | `false` | no |
113+
| ip_masq_resync_interval | The interval at which the agent attempts to sync its ConfigMap file from the disk. | string | `60s` | no |
114+
| ip_range_pods | The secondary ip range to use for pods | string | - | yes |
115+
| ip_range_services | The secondary ip range to use for pods | string | - | yes |
116+
| kubernetes_dashboard | Enable kubernetes dashboard addon | string | `false` | no |
117+
| kubernetes_version | The Kubernetes version of the masters. If set to 'latest' it will pull latest available version in the selected region. | string | `1.10.6-gke.2` | no |
118+
| maintenance_start_time | Time window specified for daily maintenance operations in RFC3339 format | string | `05:00` | no |
119+
| name | The name of the cluster (required) | string | - | yes |
120+
| network | The VPC network to host the cluster in (required) | string | - | yes |
121+
| network_policy | Enable network policy addon | string | `false` | no |
122+
| network_project_id | The project ID of the shared VPC's host (for shared vpc support) | string | `` | no |
123+
| node_pools | List of maps containing node pools | list | `<list>` | no |
124+
| node_pools_labels | Map of maps containing node labels by node-pool name | map | `<map>` | no |
125+
| node_pools_tags | Map of lists containing node network tags by node-pool name | map | `<map>` | no |
126+
| node_pools_taints | Map of lists containing node taints by node-pool name | map | `<map>` | no |
127+
| node_version | The Kubernetes version of the node pools. Defaults kubernetes_version (master) variable and can be overridden for individual node pools by setting the `version` key on them. Must be empyty or set the same as master at cluster creation. | string | `` | no |
128+
| non_masquerade_cidrs | List of strings in CIDR notation that specify the IP address ranges that do not use IP masquerading. | list | `<list>` | no |
129+
| project_id | The project ID to host the cluster in (required) | string | - | yes |
130+
| region | The region to host the cluster in (required) | string | - | yes |
131+
| regional | Whether is a regional cluster (zonal cluster if set false. WARNING: changing this after cluster creation is destructive!) | string | `true` | no |
132+
| stub_domains | Map of stub domains and their resolvers to forward DNS queries for a certain domain to an external DNS server | map | `<map>` | no |
133+
| subnetwork | The subnetwork to host the cluster in (required) | string | - | yes |
134+
| zones | The zones to host the cluster in (optional if regional cluster / required if zonal) | list | `<list>` | no |
135+
136+
## Outputs
137+
138+
| Name | Description |
139+
|------|-------------|
140+
| ca_certificate | Cluster ca certificate (base64 encoded) |
141+
| endpoint | Cluster endpoint |
142+
| horizontal_pod_autoscaling_enabled | Whether horizontal pod autoscaling enabled |
143+
| http_load_balancing_enabled | Whether http load balancing enabled |
144+
| kubernetes_dashboard_enabled | Whether kubernetes dashboard enabled |
145+
| location | Cluster location (region if regional cluster, zone if zonal cluster) |
146+
| master_version | Current master kubernetes version |
147+
| min_master_version | Minimum master kubernetes version |
148+
| name | Cluster name |
149+
| network_policy_enabled | Whether network policy enabled |
150+
| node_pools_names | List of node pools names |
151+
| node_pools_versions | List of node pools versions |
152+
| region | Cluster region |
153+
| type | Cluster type (regional / zonal) |
154+
| zones | List of zones in which the cluster resides |
155+
156+
[^]: (autogen_docs_end)
157+
158+
## Infrastructure
159+
The resources/services/activations/deletions that this module will create/trigger are:
160+
- Create a GKE cluster with the provided addons
161+
- Create GKE Node Pool(s) with provided configuration and attach to cluster
162+
- Replace the default kube-dns configmap if `stub_domains` are provided
163+
- Activate network policy if `network_policy` is true
164+
- Add `ip-masq-agent` configmap with provided `non_masquerade_cidrs` if `network_policy` is true
165+
166+
## File structure
167+
The project has the following folders and files:
168+
169+
- /: root folder
170+
- /examples: examples for using this module
171+
- /scripts: Scripts for specific tasks on module (see Infrastructure section on this file)
172+
- /test: Folders with files for testing the module (see Testing section on this file)
173+
- /main.tf: main file for this module, contains all the resources to create
174+
- /variables.tf: all the variables for the module
175+
- /output.tf: the outputs of the module
176+
- /readme.MD: this file
177+
178+
## Testing
179+
180+
### Requirements
181+
- [bundler](https://github.com/bundler/bundler)
182+
- [gcloud](https://cloud.google.com/sdk/install)
183+
- [jq](https://stedolan.github.io/jq/) 1.5
184+
- [terraform-docs](https://github.com/segmentio/terraform-docs/releases) 0.3.0
185+
186+
### Autogeneration of documentation from .tf files
187+
Run
188+
```
189+
make generate_docs
190+
```
191+
192+
### Integration test
193+
#### Terraform integration tests
194+
The integration tests for this module leverage [kitchen-terraform](https://github.com/newcontext-oss/kitchen-terraform) and [kitchen-inspec](https://github.com/inspec/kitchen-inspec).
195+
196+
The tests will do the following:
197+
- Perform `bundle install` command
198+
- Installs `kitchen-terraform` and `kitchen-inspec` gems
199+
- Perform `kitchen create` command
200+
- Performs a `terraform init`
201+
- Perform `kitchen converge` command
202+
- Performs a `terraform apply -auto-approve`
203+
- Perform `kitchen validate` command
204+
- Performs inspec tests.
205+
- Shell out to `gcloud` to validate expected resources in GCP.
206+
- Shell out to `kubectl` to validate expected resource in Kubernetes.
207+
- Shell out to `terraform` to validate outputs.
208+
- Permos `kitchen destroy` command
209+
- Performs a `terraform destroy -force`
210+
211+
You can use the following command to run the integration test in the root folder
212+
213+
`make test_integration`
214+
215+
### Linting
216+
The makefile in this project will lint or sometimes just format any shell,
217+
Python, golang, Terraform, or Dockerfiles. The linters will only be run if
218+
the makefile finds files with the appropriate file extension.
219+
220+
All of the linter checks are in the default make target, so you just have to
221+
run
222+
223+
```
224+
make -s
225+
```
226+
227+
The -s is for 'silent'. Successful output looks like this
228+
229+
```
230+
Running shellcheck
231+
Running flake8
232+
Running go fmt and go vet
233+
Running terraform validate
234+
Running hadolint on Dockerfiles
235+
Checking for required files
236+
Testing the validity of the header check
237+
..
238+
----------------------------------------------------------------------
239+
Ran 2 tests in 0.026s
240+
241+
OK
242+
Checking file headers
243+
The following lines have trailing whitespace
244+
```
245+
246+
The linters
247+
are as follows:
248+
* Shell - shellcheck. Can be found in homebrew
249+
* Python - flake8. Can be installed with 'pip install flake8'
250+
* Golang - gofmt. gofmt comes with the standard golang installation. golang
251+
is a compiled language so there is no standard linter.
252+
* Terraform - terraform has a built-in linter in the 'terraform validate'
253+
command.
254+
* Dockerfiles - hadolint. Can be found in homebrew

0 commit comments

Comments
 (0)