From dc1de85697d4ee6c7b8f5fd24447fed13ee2eb82 Mon Sep 17 00:00:00 2001 From: Bharath KKB Date: Tue, 26 Jul 2022 19:14:04 -0500 Subject: [PATCH] fix: support explicit k8s version with unspecified release channel (#1335) * fix: support explicit k8s version with unspecified release channel * update example, add test * Update versions.tf Co-authored-by: Andrew Peabody --- autogen/main/cluster.tf.tmpl | 2 +- cluster.tf | 2 +- examples/safer_cluster/README.md | 1 + examples/safer_cluster/main.tf | 14 ++++++++++++++ examples/safer_cluster/outputs.tf | 5 +++++ examples/safer_cluster/versions.tf | 3 ++- modules/beta-autopilot-private-cluster/cluster.tf | 2 +- modules/beta-autopilot-public-cluster/cluster.tf | 2 +- .../beta-private-cluster-update-variant/cluster.tf | 2 +- modules/beta-private-cluster/cluster.tf | 2 +- .../beta-public-cluster-update-variant/cluster.tf | 2 +- modules/beta-public-cluster/cluster.tf | 2 +- modules/private-cluster-update-variant/cluster.tf | 2 +- modules/private-cluster/cluster.tf | 2 +- test/fixtures/safer_cluster/outputs.tf | 4 ++++ test/integration/safer_cluster/controls/gcloud.rb | 6 ++++++ 16 files changed, 42 insertions(+), 11 deletions(-) diff --git a/autogen/main/cluster.tf.tmpl b/autogen/main/cluster.tf.tmpl index 5a9f63f6b..ab5d52fbd 100644 --- a/autogen/main/cluster.tf.tmpl +++ b/autogen/main/cluster.tf.tmpl @@ -69,7 +69,7 @@ resource "google_container_cluster" "primary" { disabled = var.disable_default_snat } {% endif %} - min_master_version = var.release_channel != null ? null : local.master_version + min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : null {% if beta_cluster and autopilot_cluster != true %} dynamic "cluster_telemetry" { diff --git a/cluster.tf b/cluster.tf index cebee0167..fd49dc15a 100644 --- a/cluster.tf +++ b/cluster.tf @@ -50,7 +50,7 @@ resource "google_container_cluster" "primary" { subnetwork = "projects/${local.network_project_id}/regions/${local.region}/subnetworks/${var.subnetwork}" - min_master_version = var.release_channel != null ? null : local.master_version + min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : null logging_service = var.logging_service monitoring_service = var.monitoring_service diff --git a/examples/safer_cluster/README.md b/examples/safer_cluster/README.md index 556008e95..6f06b8171 100644 --- a/examples/safer_cluster/README.md +++ b/examples/safer_cluster/README.md @@ -17,6 +17,7 @@ This example illustrates how to instantiate the opinionated Safer Cluster module | ca\_certificate | The cluster ca certificate (base64 encoded) | | client\_token | The bearer token for auth | | cluster\_name | Cluster name | +| explicit\_k8s\_version | Explicit version used for cluster creation | | kubernetes\_endpoint | The cluster endpoint | | location | n/a | | master\_kubernetes\_version | Kubernetes version of the master | diff --git a/examples/safer_cluster/main.tf b/examples/safer_cluster/main.tf index d6de0cd27..a2c8ef829 100644 --- a/examples/safer_cluster/main.tf +++ b/examples/safer_cluster/main.tf @@ -38,6 +38,18 @@ provider "kubernetes" { cluster_ca_certificate = base64decode(module.gke.ca_certificate) } +// A random valid k8s version is retrived +// to specify as an explicit version. +data "google_container_engine_versions" "current" { + project = var.project_id + location = var.region +} + +resource "random_shuffle" "version" { + input = data.google_container_engine_versions.current.valid_master_versions + result_count = 1 +} + module "gke" { source = "../../modules/safer-cluster/" project_id = var.project_id @@ -51,6 +63,8 @@ module "gke" { master_ipv4_cidr_block = "172.16.0.0/28" add_cluster_firewall_rules = true firewall_inbound_ports = ["9443", "15017"] + kubernetes_version = random_shuffle.version.result[0] + release_channel = "UNSPECIFIED" master_authorized_networks = [ { diff --git a/examples/safer_cluster/outputs.tf b/examples/safer_cluster/outputs.tf index d2f9706f0..d511b2c48 100644 --- a/examples/safer_cluster/outputs.tf +++ b/examples/safer_cluster/outputs.tf @@ -74,3 +74,8 @@ output "project_id" { description = "The project ID the cluster is in" value = var.project_id } + +output "explicit_k8s_version" { + description = "Explicit version used for cluster creation" + value = random_shuffle.version.result[0] +} diff --git a/examples/safer_cluster/versions.tf b/examples/safer_cluster/versions.tf index 2d448a4b7..60030dba2 100644 --- a/examples/safer_cluster/versions.tf +++ b/examples/safer_cluster/versions.tf @@ -29,7 +29,8 @@ terraform { source = "hashicorp/kubernetes" } random = { - source = "hashicorp/random" + source = "hashicorp/random" + version = "~> 3.0" } } } diff --git a/modules/beta-autopilot-private-cluster/cluster.tf b/modules/beta-autopilot-private-cluster/cluster.tf index 551de2fac..b86d2185d 100644 --- a/modules/beta-autopilot-private-cluster/cluster.tf +++ b/modules/beta-autopilot-private-cluster/cluster.tf @@ -51,7 +51,7 @@ resource "google_container_cluster" "primary" { default_snat_status { disabled = var.disable_default_snat } - min_master_version = var.release_channel != null ? null : local.master_version + min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : null logging_service = var.logging_service monitoring_service = var.monitoring_service diff --git a/modules/beta-autopilot-public-cluster/cluster.tf b/modules/beta-autopilot-public-cluster/cluster.tf index 5faebd706..a0357381a 100644 --- a/modules/beta-autopilot-public-cluster/cluster.tf +++ b/modules/beta-autopilot-public-cluster/cluster.tf @@ -51,7 +51,7 @@ resource "google_container_cluster" "primary" { default_snat_status { disabled = var.disable_default_snat } - min_master_version = var.release_channel != null ? null : local.master_version + min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : null logging_service = var.logging_service monitoring_service = var.monitoring_service diff --git a/modules/beta-private-cluster-update-variant/cluster.tf b/modules/beta-private-cluster-update-variant/cluster.tf index 802ef2f22..e62c1398a 100644 --- a/modules/beta-private-cluster-update-variant/cluster.tf +++ b/modules/beta-private-cluster-update-variant/cluster.tf @@ -59,7 +59,7 @@ resource "google_container_cluster" "primary" { default_snat_status { disabled = var.disable_default_snat } - min_master_version = var.release_channel != null ? null : local.master_version + min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : null dynamic "cluster_telemetry" { for_each = local.cluster_telemetry_type_is_set ? [1] : [] diff --git a/modules/beta-private-cluster/cluster.tf b/modules/beta-private-cluster/cluster.tf index 8d64e2d42..1f1f805a0 100644 --- a/modules/beta-private-cluster/cluster.tf +++ b/modules/beta-private-cluster/cluster.tf @@ -59,7 +59,7 @@ resource "google_container_cluster" "primary" { default_snat_status { disabled = var.disable_default_snat } - min_master_version = var.release_channel != null ? null : local.master_version + min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : null dynamic "cluster_telemetry" { for_each = local.cluster_telemetry_type_is_set ? [1] : [] diff --git a/modules/beta-public-cluster-update-variant/cluster.tf b/modules/beta-public-cluster-update-variant/cluster.tf index 731002cd3..4eea77247 100644 --- a/modules/beta-public-cluster-update-variant/cluster.tf +++ b/modules/beta-public-cluster-update-variant/cluster.tf @@ -59,7 +59,7 @@ resource "google_container_cluster" "primary" { default_snat_status { disabled = var.disable_default_snat } - min_master_version = var.release_channel != null ? null : local.master_version + min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : null dynamic "cluster_telemetry" { for_each = local.cluster_telemetry_type_is_set ? [1] : [] diff --git a/modules/beta-public-cluster/cluster.tf b/modules/beta-public-cluster/cluster.tf index 9d22602e5..729947509 100644 --- a/modules/beta-public-cluster/cluster.tf +++ b/modules/beta-public-cluster/cluster.tf @@ -59,7 +59,7 @@ resource "google_container_cluster" "primary" { default_snat_status { disabled = var.disable_default_snat } - min_master_version = var.release_channel != null ? null : local.master_version + min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : null dynamic "cluster_telemetry" { for_each = local.cluster_telemetry_type_is_set ? [1] : [] diff --git a/modules/private-cluster-update-variant/cluster.tf b/modules/private-cluster-update-variant/cluster.tf index b0cfb538b..ae9e57ae5 100644 --- a/modules/private-cluster-update-variant/cluster.tf +++ b/modules/private-cluster-update-variant/cluster.tf @@ -50,7 +50,7 @@ resource "google_container_cluster" "primary" { subnetwork = "projects/${local.network_project_id}/regions/${local.region}/subnetworks/${var.subnetwork}" - min_master_version = var.release_channel != null ? null : local.master_version + min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : null logging_service = var.logging_service monitoring_service = var.monitoring_service diff --git a/modules/private-cluster/cluster.tf b/modules/private-cluster/cluster.tf index 460b85d7b..e4540e64f 100644 --- a/modules/private-cluster/cluster.tf +++ b/modules/private-cluster/cluster.tf @@ -50,7 +50,7 @@ resource "google_container_cluster" "primary" { subnetwork = "projects/${local.network_project_id}/regions/${local.region}/subnetworks/${var.subnetwork}" - min_master_version = var.release_channel != null ? null : local.master_version + min_master_version = var.release_channel == null || var.release_channel == "UNSPECIFIED" ? local.master_version : null logging_service = var.logging_service monitoring_service = var.monitoring_service diff --git a/test/fixtures/safer_cluster/outputs.tf b/test/fixtures/safer_cluster/outputs.tf index 8948896e1..11c97a05c 100644 --- a/test/fixtures/safer_cluster/outputs.tf +++ b/test/fixtures/safer_cluster/outputs.tf @@ -55,3 +55,7 @@ output "service_account" { description = "The service account to default running nodes as if not overridden in `node_pools`." value = module.example.service_account } + +output "explicit_k8s_version" { + value = module.example.explicit_k8s_version +} diff --git a/test/integration/safer_cluster/controls/gcloud.rb b/test/integration/safer_cluster/controls/gcloud.rb index 9faae675c..1525ab669 100644 --- a/test/integration/safer_cluster/controls/gcloud.rb +++ b/test/integration/safer_cluster/controls/gcloud.rb @@ -15,6 +15,7 @@ project_id = attribute('project_id') location = attribute('location') cluster_name = attribute('cluster_name') +explicit_version = attribute('explicit_k8s_version') control "gcloud" do title "Google Compute Engine GKE configuration" @@ -35,6 +36,11 @@ expect(data['status']).to eq 'RUNNING' end + it "has expected explicit version" do + expect(data['currentMasterVersion']).to eq explicit_version + expect(data['currentNodeVersion']).to eq explicit_version + end + it "is regional" do expect(data['location']).to match(/^.*[1-9]$/) end