Skip to content

Commit

Permalink
Creates a separate terraform module for the upgrade test clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
igooch committed Sep 20, 2024
1 parent 224b91e commit 591a49f
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 47 deletions.
4 changes: 1 addition & 3 deletions build/terraform/e2e/gke-autopilot/module.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,18 @@ terraform {

variable "project" {}
variable "kubernetesVersion" {}
variable "testName" {}
variable "location" {}
variable "releaseChannel" {}

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

cluster = {
"name" = format("gke-autopilot-%s-test-cluster-%s", var.testName, replace(var.kubernetesVersion, ".", "-"))
"name" = format("gke-autopilot-e2e-test-cluster-%s", replace(var.kubernetesVersion, ".", "-"))
"project" = var.project
"location" = var.location
"releaseChannel" = var.releaseChannel
"kubernetesVersion" = var.kubernetesVersion
"testName" = var.testName
"deletionProtection" = false
"maintenanceExclusionStartTime" = timestamp()
"maintenanceExclusionEndTime" = timeadd(timestamp(), "2640h") # 110 days
Expand Down
9 changes: 5 additions & 4 deletions build/terraform/e2e/gke-standard/module.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ terraform {

variable "project" {}
variable "kubernetesVersion" {}
variable "testName" {}
variable "location" {}
variable "releaseChannel" {}
variable "initialNodeCount" {}

variable "machineType" {
default = "e2-standard-4"
}

variable "initialNodeCount" {
default = 10
}

variable "overrideName" {
default = ""
}
Expand All @@ -49,15 +51,14 @@ module "gke_cluster" {
source = "../../../../install/terraform/modules/gke"

cluster = {
"name" = var.overrideName != "" ? var.overrideName : format("standard-%s-test-cluster-%s", var.testName, replace(var.kubernetesVersion, ".", "-"))
"name" = var.overrideName != "" ? var.overrideName : format("standard-e2e-test-cluster-%s", replace(var.kubernetesVersion, ".", "-"))
"location" = var.location
"releaseChannel" = var.releaseChannel
"machineType" = var.machineType
"initialNodeCount" = var.initialNodeCount
"enableImageStreaming" = true
"project" = var.project
"kubernetesVersion" = var.kubernetesVersion
"testName" = var.testName
"maintenanceExclusionStartTime" = timestamp()
"maintenanceExclusionEndTime" = timeadd(timestamp(), "2640h") # 110 days
}
Expand Down
52 changes: 12 additions & 40 deletions build/terraform/e2e/module.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,50 +62,22 @@ variable "kubernetes_versions" {
}
}

variable "test_names" {
description = "Use the same terraform templates for both e2e and upgrade tests. Includes test name and initial node counts for standard clusters."
type = map(number)
default = {
"e2e" = 10
"upgrade" = 4
}
}

// Handle nested loop in terraform. Flatten combines the two maps into a list.
locals {
test_versions = distinct(flatten([
for name, nodes in var.test_names : [
for version, val in var.kubernetes_versions : {
test = name
version = version
location = val[0]
releaseChannel = val[1]
numNodes = nodes
}
]
]))
}

module "gke_standard_cluster" {
// local.test_versions is a list, but to use `for_each` it need to be a changed to a map.
for_each = { for config in local.test_versions: "${config.test}.${config.version}" => config }
source = "./gke-standard"
project = var.project
testName = each.value.test
kubernetesVersion = each.value.version
location = each.value.location
releaseChannel = each.value.releaseChannel
initialNodeCount = each.value.numNodes
for_each = var.kubernetes_versions
source = "./gke-standard"
project = var.project
kubernetesVersion = each.key
location = each.value[0]
releaseChannel = each.value[1]
}

module "gke_autopilot_cluster" {
for_each = { for entry in local.test_versions: "${entry.test}.${entry.version}" => entry }
source = "./gke-autopilot"
project = var.project
testName = each.value.test
kubernetesVersion = each.value.version
location = each.value.location
releaseChannel = each.value.releaseChannel
for_each = var.kubernetes_versions
source = "./gke-autopilot"
project = var.project
kubernetesVersion = each.key
location = each.value[0]
releaseChannel = each.value[1]
}

resource "google_compute_firewall" "udp" {
Expand Down
53 changes: 53 additions & 0 deletions build/terraform/upgrade/gke-autopilot/module.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 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.


// Run:
// terraform apply -var project="<YOUR_GCP_ProjectID>"

terraform {
required_version = ">= 1.0.0"
required_providers {
google = {
source = "hashicorp/google"
version = "~> 4.25.0"
}
helm = {
source = "hashicorp/helm"
version = "~> 2.3"
}
}
}

variable "project" {}
variable "kubernetesVersion" {}
variable "location" {}
variable "releaseChannel" {}

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

cluster = {
"name" = format("gke-autopilot-upgrade-test-cluster-%s", replace(var.kubernetesVersion, ".", "-"))
"project" = var.project
"location" = var.location
"releaseChannel" = var.releaseChannel
"kubernetesVersion" = var.kubernetesVersion
"deletionProtection" = false
"maintenanceExclusionStartTime" = timestamp()
"maintenanceExclusionEndTime" = timeadd(timestamp(), "2640h") # 110 days
}

udpFirewall = false // firewall is created at the project module level
}
67 changes: 67 additions & 0 deletions build/terraform/upgrade/gke-standard/module.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2024 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.


// Run:
// terraform apply -var project="<YOUR_GCP_ProjectID>"

terraform {
required_version = ">= 1.0.0"
required_providers {
google = {
source = "hashicorp/google"
version = "~> 4.25.0"
}
helm = {
source = "hashicorp/helm"
version = "~> 2.3"
}
}
}

variable "project" {}
variable "kubernetesVersion" {}
variable "location" {}
variable "releaseChannel" {}

variable "machineType" {
default = "e2-standard-4"
}

variable "initialNodeCount" {
default = 4
}

variable "overrideName" {
default = ""
}

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

cluster = {
"name" = var.overrideName != "" ? var.overrideName : format("standard-upgrade-test-cluster-%s", replace(var.kubernetesVersion, ".", "-"))
"location" = var.location
"releaseChannel" = var.releaseChannel
"machineType" = var.machineType
"initialNodeCount" = var.initialNodeCount
"enableImageStreaming" = true
"project" = var.project
"kubernetesVersion" = var.kubernetesVersion
"maintenanceExclusionStartTime" = timestamp()
"maintenanceExclusionEndTime" = timeadd(timestamp(), "2640h") # 110 days
}

udpFirewall = false // firewall is created at the project module level
}
109 changes: 109 additions & 0 deletions build/terraform/upgrade/module.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2024 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.


// Run:
// terraform init -backend-config="bucket=<YOUR_GCP_ProjectID>-upgrade-infra-bucket-tfstate" -backend-config="prefix=terraform/state"
// terraform apply -var project="<YOUR_GCP_ProjectID>"

terraform {
required_version = ">= 1.0.0"
required_providers {
google = {
source = "hashicorp/google"
version = "~> 4.25.0"
}
helm = {
source = "hashicorp/helm"
version = "~> 2.3"
}
}
backend "gcs" {
}
}

variable "project" {}
variable "kubernetes_versions" {
description = "Create upgrade test clusters with these k8s versions in these regions"
type = map(list(string))
default = {
"1.28" = ["us-west1", "RAPID"]
"1.29" = ["europe-west1", "RAPID"]
"1.30" = ["asia-east1", "RAPID"]
// "1.31" = ["us-east1", "RAPID"]
//
// Before merge: When adding Kubernetes version 1.{N}, first uncomment the line above, extending
// the infrastructure to 4 versions temporarily. Come back to these instructions after the
// update PR merges.
//
// After merge: After the Kubernetes update PR merges, and all active PRs are updated:
//
// * Move the 1.{N-3} line to the bottom and comment it out
// * Change the (commented out) 1.{N-3} to 1.{N+1}
// * You should now have 3 versions uncommented (versions 1.{N-2} .. 1.{N}),
// and 1.{N+1} commented out for the next update. The new, commented out 1.{N+1}
// should be using the region of the previous 1.{N-3} - this region will become
// unused.
//
// Rationale: We cycle the regions us-east1 -> us-west1 -> europe-west1 -> asia-east1 -> us-east1
// as versions are added, using 4 regions so that the PR adding 1.{N} is in a unique region to
// 1.{N-3} .. 1.{N-1}, meaning versions never need to share a region in CI.
}
}

module "gke_standard_cluster" {
for_each = var.kubernetes_versions
source = "./gke-standard"
project = var.project
kubernetesVersion = each.key
location = each.value[0]
releaseChannel = each.value[1]
}

module "gke_autopilot_cluster" {
for_each = var.kubernetes_versions
source = "./gke-autopilot"
project = var.project
kubernetesVersion = each.key
location = each.value[0]
releaseChannel = each.value[1]
}

resource "google_compute_firewall" "udp" {
name = "gke-game-server-firewall"
project = var.project
network = "default"

allow {
protocol = "udp"
ports = ["7000-8000"]
}

target_tags = ["game-server"]
source_ranges = ["0.0.0.0/0"]
}

resource "google_compute_firewall" "tcp" {
name = "gke-game-server-firewall-tcp"
project = var.project
network = "default"

allow {
protocol = "tcp"
ports = ["7000-8000"]
}

target_tags = ["game-server"]
source_ranges = ["0.0.0.0/0"]
}
43 changes: 43 additions & 0 deletions build/terraform/upgrade/state-bucket/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2024 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.


// Run:
// terraform apply -var project="<YOUR_GCP_ProjectID>"

// GCS bucket for holding the Terraform state of the upgrade test Terraform config.

terraform {
required_version = ">= 1.0.0"
required_providers {
google = {
source = "hashicorp/google"
version = "~> 4.25.0"
}
}
}

variable "project" {}

resource "google_storage_bucket" "default" {
project = var.project
name = "${var.project}-upgrade-infra-bucket-tfstate"
force_destroy = false
uniform_bucket_level_access = true
location = "US"
storage_class = "STANDARD"
versioning {
enabled = true
}
}

0 comments on commit 591a49f

Please sign in to comment.