Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform: update GKE cluster version, use locals and lookup to set default values #1482

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion build/includes/terraform.mk
Expand Up @@ -81,5 +81,9 @@ endif
GCP_CLUSTER_NAME=$(GCP_TF_CLUSTER_NAME) $(MAKE) gcloud-auth-cluster

gcloud-terraform-destroy-cluster:
ifndef GCP_PROJECT
$(eval GCP_PROJECT=$(shell sh -c "gcloud config get-value project 2> /dev/null"))
endif
$(DOCKER_RUN) bash -c 'cd $(mount_path)/build/terraform/gke && \
terraform destroy -target module.helm_agones.helm_release.agones -auto-approve && sleep 60 && terraform destroy -auto-approve'
terraform destroy -var project=$(GCP_PROJECT) -target module.helm_agones.helm_release.agones -auto-approve && sleep 60 && \
markmandel marked this conversation as resolved.
Show resolved Hide resolved
terraform destroy -var project=$(GCP_PROJECT) -auto-approve'
16 changes: 10 additions & 6 deletions build/terraform/gke/module.tf
Expand Up @@ -97,17 +97,21 @@ variable "feature_gates" {
default = ""
}

variable "kubernetes_version" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming we're choosing 1.15 to start work on #1478 ?

Copy link
Collaborator Author

@aLekSer aLekSer Apr 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable was used here so that we can make changes to this file more rare. And imagine the situation when between Agones releases Kubernetes version would be deprecated but with Terraform we are cloning the Agones repo (as in docs) only at release points. Another thing is if we want to compare Terraform configs of Agones at version 1.6.0 and 1.7.0 to perform regression testing for example, then we can select the same Kubernetes version to compare at the same environment.
This would be useful when we select Kubernetes patch version (say 1.14.8), and then there is a new patch with a fix, so we can adjust the docs and switch to a new one.

default = "1.15"
}

module "gke_cluster" {
source = "../../../install/terraform/modules/gke"

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

Expand Down
1 change: 0 additions & 1 deletion examples/terraform-submodules/gke/module.tf
Expand Up @@ -46,7 +46,6 @@ variable "machine_type" {
variable "node_count" {
default = "4"
}

variable "zone" {
default = "us-west1-c"
description = "The GCP zone to create the cluster in"
Expand Down
40 changes: 26 additions & 14 deletions install/terraform/modules/gke/cluster.tf
Expand Up @@ -19,37 +19,49 @@ 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisst you are our resident TF expert. This look good to you? If so, I'm happy to approve this PR 👍

Copy link
Collaborator Author

@aLekSer aLekSer May 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I have checked that removing kubernetesVersion from the cluster map
does not affect the result, as it was before.
https://github.com/aLekSer/agones/blob/1f83575dc0f0a7150a9231d9a5402c2d935f1aac/examples/terraform-submodules/gke/module.tf#L74-L81

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

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")
}

# echo command used for debugging purpose
# Run `terraform taint null_resource.test-setting-variables` before second execution
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 = "1.14"
min_master_version = local.kubernetesVersion

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 @@ -132,9 +144,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
13 changes: 7 additions & 6 deletions install/terraform/modules/gke/variables.tf
Expand Up @@ -25,11 +25,12 @@ variable "cluster" {
type = map

default = {
"zone" = "us-west1-c"
"name" = "test-cluster"
"machineType" = "n1-standard-4"
"initialNodeCount" = "4"
"project" = "agones"
"network" = "default"
"zone" = "us-west1-c"
"name" = "test-cluster"
"machineType" = "n1-standard-4"
"initialNodeCount" = "4"
"project" = "agones"
"network" = "default"
"kubernetesVersion" = "1.15"
markmandel marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion site/content/en/docs/Installation/Terraform/gke.md
Expand Up @@ -139,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