Skip to content

Commit

Permalink
Add locals and lookup to handle missing keys
Browse files Browse the repository at this point in the history
Avoid situation when missing one key could break deployment.
  • Loading branch information
aLekSer committed May 4, 2020
1 parent 218acc6 commit 9b00da8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
17 changes: 6 additions & 11 deletions examples/terraform-submodules/gke/module.tf
Expand Up @@ -64,10 +64,6 @@ variable "feature_gates" {
default = ""
}

variable "kubernetes_version" {
default = "1.15"
}

module "gke_cluster" {
// ***************************************************************************************************
// Update ?ref= to the agones release you are installing. For example, ?ref=release-1.3.0 corresponds
Expand All @@ -76,13 +72,12 @@ module "gke_cluster" {
source = "git::https://github.com/googleforgames/agones.git//install/terraform/modules/gke/?ref=master"

cluster = {
"name" = var.name
"zone" = var.zone
"machineType" = var.machine_type
"initialNodeCount" = var.node_count
"project" = var.project
"network" = var.network
"kubernetesVersion" = var.kubernetes_version
"name" = var.name
"zone" = var.zone
"machineType" = var.machine_type
"initialNodeCount" = var.node_count
"project" = var.project
"network" = var.network
}
}

Expand Down
42 changes: 27 additions & 15 deletions install/terraform/modules/gke/cluster.tf
Expand Up @@ -19,10 +19,22 @@ terraform {

data "google_client_config" "default" {}

# A list of all parameters used in interpolation var.cluster
# Set values to default if not key was not set in original map
locals {
project = lookup(var.cluster, "project", "agones")
zone = lookup(var.cluster, "zone", "us-west1-c")
name = lookup(var.cluster, "name", "test-cluster")
machineType = lookup(var.cluster, "machineType", "n1-standard-4")
initialNodeCount = lookup(var.cluster, "initialNodeCount", "4")
network = lookup(var.cluster, "network", "default")
kubernetesVersion = lookup(var.cluster, "kubernetesVersion", "1.15")
}

data "google_container_engine_versions" "kubernetes" {
project = var.cluster["project"]
location = var.cluster["zone"]
version_prefix = var.cluster["kubernetesVersion"]
project = local.project
location = local.zone
version_prefix = local.kubernetesVersion
}

# echo command used for debugging purpose
Expand All @@ -31,33 +43,33 @@ resource "null_resource" "test-setting-variables" {
provisioner "local-exec" {
command = <<EOT
${format("echo Current variables set as following - name: %s, project: %s, machineType: %s, initialNodeCount: %s, network: %s, zone: %s",
var.cluster["name"], var.cluster["project"],
var.cluster["machineType"], var.cluster["initialNodeCount"], var.cluster["network"],
var.cluster["zone"])}
local.name, local.project,
local.machineType, local.initialNodeCount, local.network,
local.zone)}
EOT
}
}

resource "google_container_cluster" "primary" {
name = var.cluster["name"]
location = var.cluster["zone"]
project = var.cluster["project"]
network = var.cluster["network"]
name = local.name
location = local.zone
project = local.project
network = local.network

min_master_version = data.google_container_engine_versions.kubernetes.latest_master_version

node_version = data.google_container_engine_versions.kubernetes.latest_node_version

node_pool {
name = "default"
node_count = var.cluster["initialNodeCount"]
node_count = local.initialNodeCount

management {
auto_upgrade = false
}

node_config {
machine_type = var.cluster["machineType"]
machine_type = local.machineType

oauth_scopes = [
"https://www.googleapis.com/auth/devstorage.read_only",
Expand Down Expand Up @@ -140,9 +152,9 @@ resource "google_container_cluster" "primary" {
}

resource "google_compute_firewall" "default" {
name = "game-server-firewall-firewall-${var.cluster["name"]}"
project = var.cluster["project"]
network = var.cluster["network"]
name = "game-server-firewall-firewall-${local.name}"
project = local.project
network = local.network

allow {
protocol = "udp"
Expand Down
3 changes: 1 addition & 2 deletions site/content/en/docs/Installation/Terraform/gke.md
Expand Up @@ -92,7 +92,6 @@ Configurable parameters:
- network - the name of the VPC network you want your cluster and firewall rules to be connected to (default is "default")
- log_level - possible values: Fatal, Error, Warn, Info, Debug (default is "info")
- feature_gates - a list of alpha and beta version features to enable. For example, "PlayerTracking=true&ContainerPortAllocation=true"
- kubernetes_version - a Kubernetes version of the GKE cluster

{{% alert title="Warning" color="warning"%}}
On the lines that read `source = "git::https://github.com/googleforgames/agones.git//install/terraform/modules/gke/?ref=master"`
Expand Down Expand Up @@ -140,7 +139,7 @@ You should have 6 nodes in `Ready` state.

To delete all resources provisioned by Terraform:
```
terraform destroy
terraform destroy -var project="<YOUR_GCP_ProjectID>"
```

## Next Steps
Expand Down

0 comments on commit 9b00da8

Please sign in to comment.