From 7a6b6083a85f10b17dd717528a7e3228fd346f2a Mon Sep 17 00:00:00 2001 From: Moe Sy Date: Wed, 24 Jun 2020 18:32:58 -1000 Subject: [PATCH] Fix failing GKE cluster creation in custom VPC (Terraform) 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/agones#1641 - https://www.terraform.io/docs/providers/google/r/container_cluster.html --- examples/terraform-submodules/gke/.gitignore | 2 + examples/terraform-submodules/gke/README.md | 43 +++++++++++ examples/terraform-submodules/gke/auth.tf | 22 ++++++ examples/terraform-submodules/gke/module.tf | 74 ++----------------- examples/terraform-submodules/gke/output.tf | 9 +++ .../terraform-submodules/gke/variables.tf | 59 +++++++++++++++ install/terraform/modules/gke/cluster.tf | 10 ++- install/terraform/modules/gke/variables.tf | 1 + 8 files changed, 149 insertions(+), 71 deletions(-) create mode 100644 examples/terraform-submodules/gke/.gitignore create mode 100644 examples/terraform-submodules/gke/README.md create mode 100644 examples/terraform-submodules/gke/auth.tf create mode 100644 examples/terraform-submodules/gke/output.tf create mode 100644 examples/terraform-submodules/gke/variables.tf diff --git a/examples/terraform-submodules/gke/.gitignore b/examples/terraform-submodules/gke/.gitignore new file mode 100644 index 0000000000..6a06e43229 --- /dev/null +++ b/examples/terraform-submodules/gke/.gitignore @@ -0,0 +1,2 @@ +development.tfvars +production.tfvars \ No newline at end of file diff --git a/examples/terraform-submodules/gke/README.md b/examples/terraform-submodules/gke/README.md new file mode 100644 index 0000000000..f90d469dbb --- /dev/null +++ b/examples/terraform-submodules/gke/README.md @@ -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="" [-var agones_version="1.6.0"]``` + +## Create an Agones cluster in a custom VPC. +Required fields: +- project +- network +- subnetwork + + +```terraform apply -var project="" -var network="" -var subnetwork="" [-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``` + diff --git a/examples/terraform-submodules/gke/auth.tf b/examples/terraform-submodules/gke/auth.tf new file mode 100644 index 0000000000..0bda16e243 --- /dev/null +++ b/examples/terraform-submodules/gke/auth.tf @@ -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" +} \ No newline at end of file diff --git a/examples/terraform-submodules/gke/module.tf b/examples/terraform-submodules/gke/module.tf index cca6939a36..9c91b24209 100644 --- a/examples/terraform-submodules/gke/module.tf +++ b/examples/terraform-submodules/gke/module.tf @@ -12,64 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. - -// Run: -// terraform apply -var project="" [-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 @@ -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" @@ -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 -} diff --git a/examples/terraform-submodules/gke/output.tf b/examples/terraform-submodules/gke/output.tf new file mode 100644 index 0000000000..52bcb11066 --- /dev/null +++ b/examples/terraform-submodules/gke/output.tf @@ -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 +} \ No newline at end of file diff --git a/examples/terraform-submodules/gke/variables.tf b/examples/terraform-submodules/gke/variables.tf new file mode 100644 index 0000000000..b0c440b1ad --- /dev/null +++ b/examples/terraform-submodules/gke/variables.tf @@ -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 = "" +} \ No newline at end of file diff --git a/install/terraform/modules/gke/cluster.tf b/install/terraform/modules/gke/cluster.tf index 181447c7c4..955a9f4d1d 100644 --- a/install/terraform/modules/gke/cluster.tf +++ b/install/terraform/modules/gke/cluster.tf @@ -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") } @@ -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 diff --git a/install/terraform/modules/gke/variables.tf b/install/terraform/modules/gke/variables.tf index 4ce17b3aaf..c77d8ab3d8 100644 --- a/install/terraform/modules/gke/variables.tf +++ b/install/terraform/modules/gke/variables.tf @@ -31,6 +31,7 @@ variable "cluster" { "initialNodeCount" = "4" "project" = "agones" "network" = "default" + "subnetwork" = "" "kubernetesVersion" = "1.15" } }