Skip to content

Commit

Permalink
Fix failing GKE cluster creation in custom VPC (Terraform)
Browse files Browse the repository at this point in the history
When creating a GKE cluster in terraform with a non-default (custom) VPC
the subnetwork must be specified otherwise the operation will fail with an expected error.

Expected error when subnetwork isn't set:
- "Error: googleapi: Error 400: Network "MY-VPC-NAME" requires specifying a subnetwork., badRequest"

User should follow the instructions in README.md and specify the required fields based on whether
the cluster will run in a default VPC or custom VPC.

Tests performed:
- Able to create cluster in default VPC in a backwards compatible way - passed
- Able to create cluster in a user defined non-default network and subnetwork - passed

Reference
- googleforgames#1641
- https://www.terraform.io/docs/providers/google/r/container_cluster.html
  • Loading branch information
Moe Sy committed Jun 25, 2020
1 parent c8d6dd4 commit 7a6b608
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 71 deletions.
2 changes: 2 additions & 0 deletions examples/terraform-submodules/gke/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
development.tfvars
production.tfvars
43 changes: 43 additions & 0 deletions examples/terraform-submodules/gke/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
This terraform module creates an Agones cluster on GKE.

# Prerequisites
- Terraform 0.12.x
- Helm 2.x
- An existing GCP Project
- Enabled service API(s)

# Enabling Service APIs

Enable each required API below by following [the enabling APIs guide](https://cloud.google.com/endpoints/docs/openapi/enable-api#gcloud).

Required APIs:
- container.googleapis.com (Kubernetes)

# Examples

## Create an Agones in a default VPC.


Required fields:
- project

```terraform apply -var project="<YOUR_GCP_ProjectID>" [-var agones_version="1.6.0"]```

## Create an Agones cluster in a custom VPC.
Required fields:
- project
- network
- subnetwork


```terraform apply -var project="<YOUR_GCP_ProjectID>" -var network="<YOUR_NETWORK_NAME>" -var subnetwork="<YOUR_SUBNETWORK_NAME>" [-var agones_version="1.6.0"]```

# Troubleshooting

## Unknown command "init" for "helm"
```Error: Error running command 'helm init --client-only': exit status 1. Output: Error: unknown command "init" for "helm```

The above error occurs when helm3 is installed instead of helm2. You must use helm v2.x. Validate your version by running the command bellow:

```helm version```

22 changes: 22 additions & 0 deletions examples/terraform-submodules/gke/auth.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

// Copyright 2020 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

provider "google" {
version = "~> 2.10"
}

provider "google-beta" {
version = "~> 2.10"
}
74 changes: 7 additions & 67 deletions examples/terraform-submodules/gke/module.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.


// Run:
// terraform apply -var project="<YOUR_GCP_ProjectID>" [-var agones_version="1.4.0"]

provider "google" {
version = "~> 2.10"
}

provider "google-beta" {
version = "~> 2.10"
}

variable "project" {
default = ""
}

variable "name" {
default = "agones-terraform-example"
}

// Install latest version of agones
variable "agones_version" {
default = ""
}

variable "machine_type" {
default = "n1-standard-4"
}

// Note: This is the number of gameserver nodes. The Agones module will automatically create an additional
// two node pools with 1 node each for "agones-system" and "agones-metrics".
variable "node_count" {
default = "4"
}
variable "zone" {
default = "us-west1-c"
description = "The GCP zone to create the cluster in"
}

variable "network" {
default = "default"
description = "The name of the VPC network to attach the cluster and firewall rule to"
}

variable "log_level" {
default = "info"
}

variable "feature_gates" {
default = ""
}

module "gke_cluster" {
// ***************************************************************************************************
// Update ?ref= to the agones release you are installing. For example, ?ref=release-1.3.0 corresponds
// to Agones version 1.3.0
// Update ?ref= to the agones release you are installing. For example, ?ref=release-1.6.0 corresponds
// to Agones version 1.6.0
// ***************************************************************************************************
source = "git::https://github.com/googleforgames/agones.git//install/terraform/modules/gke/?ref=master"
#source = "git::https://github.com/googleforgames/agones.git//install/terraform/modules/gke/?ref=master"
source = "../../../install/terraform/modules/gke"

cluster = {
"name" = var.name
Expand All @@ -78,13 +27,14 @@ module "gke_cluster" {
"initialNodeCount" = var.node_count
"project" = var.project
"network" = var.network
"subnetwork" = var.subnetwork
}
}

module "helm_agones" {
// ***************************************************************************************************
// Update ?ref= to the agones release you are installing. For example, ?ref=release-1.3.0 corresponds
// to Agones version 1.3.0
// Update ?ref= to the agones release you are installing. For example, ?ref=release-1.6.0 corresponds
// to Agones version 1.6.0
// ***************************************************************************************************
source = "git::https://github.com/googleforgames/agones.git//install/terraform/modules/helm/?ref=master"

Expand All @@ -97,13 +47,3 @@ module "helm_agones" {
cluster_ca_certificate = module.gke_cluster.cluster_ca_certificate
log_level = var.log_level
}

output "host" {
value = module.gke_cluster.host
}
output "token" {
value = module.gke_cluster.token
}
output "cluster_ca_certificate" {
value = module.gke_cluster.cluster_ca_certificate
}
9 changes: 9 additions & 0 deletions examples/terraform-submodules/gke/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "host" {
value = module.gke_cluster.host
}
output "token" {
value = module.gke_cluster.token
}
output "cluster_ca_certificate" {
value = module.gke_cluster.cluster_ca_certificate
}
59 changes: 59 additions & 0 deletions examples/terraform-submodules/gke/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2020 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

variable "project" {
default = ""
}

variable "name" {
default = "agones-terraform-example"
}

// Install latest version of agones
variable "agones_version" {
default = ""
}

variable "machine_type" {
default = "n1-standard-4"
}

// Note: This is the number of gameserver nodes. The Agones module will automatically create an additional
// two node pools with 1 node each for "agones-system" and "agones-metrics".
variable "node_count" {
default = "4"
description = "The number of gameserver nodes."
}

variable "zone" {
default = "us-west1-c"
description = "The GCP zone to create the cluster in"
}

variable "network" {
description = "The name of the VPC network to attach the cluster and firewall rule to"
}

variable "subnetwork" {
default = ""
description = "The subnetwork to host the cluster in. Required field if network value isn't 'default'."
}

variable "log_level" {
default = "info"
}

variable "feature_gates" {
default = ""
}
10 changes: 6 additions & 4 deletions install/terraform/modules/gke/cluster.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ locals {
machineType = lookup(var.cluster, "machineType", "n1-standard-4")
initialNodeCount = lookup(var.cluster, "initialNodeCount", "4")
network = lookup(var.cluster, "network", "default")
subnetwork = lookup(var.cluster, "subnetwork")
kubernetesVersion = lookup(var.cluster, "kubernetesVersion", "1.15")
}

Expand All @@ -45,10 +46,11 @@ local.zone)}
}

resource "google_container_cluster" "primary" {
name = local.name
location = local.zone
project = local.project
network = local.network
name = local.name
location = local.zone
project = local.project
network = local.network
subnetwork = local.subnetwork

min_master_version = local.kubernetesVersion

Expand Down
1 change: 1 addition & 0 deletions install/terraform/modules/gke/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ variable "cluster" {
"initialNodeCount" = "4"
"project" = "agones"
"network" = "default"
"subnetwork" = ""
"kubernetesVersion" = "1.15"
}
}

0 comments on commit 7a6b608

Please sign in to comment.