From cf76744f957c8f418d38ccf56cd639f53c725783 Mon Sep 17 00:00:00 2001 From: Julio Castillo Date: Tue, 28 Jun 2022 12:21:50 +0200 Subject: [PATCH] Fix addons for autopilot clusters, add tests for gke-cluster. Fixes second part of #702 --- modules/gke-cluster/main.tf | 19 +++++++-- tests/modules/gke_cluster/__init__.py | 13 +++++++ tests/modules/gke_cluster/fixture/main.tf | 28 +++++++++++++ .../modules/gke_cluster/fixture/variables.tf | 38 ++++++++++++++++++ tests/modules/gke_cluster/test_plan.py | 39 +++++++++++++++++++ tests/modules/gke_nodepool/test_plan.py | 3 +- 6 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 tests/modules/gke_cluster/__init__.py create mode 100644 tests/modules/gke_cluster/fixture/main.tf create mode 100644 tests/modules/gke_cluster/fixture/variables.tf create mode 100644 tests/modules/gke_cluster/test_plan.py diff --git a/modules/gke-cluster/main.tf b/modules/gke-cluster/main.tf index 64762b6748..50de4e25db 100644 --- a/modules/gke-cluster/main.tf +++ b/modules/gke-cluster/main.tf @@ -60,7 +60,12 @@ resource "google_container_cluster" "cluster" { # TODO(ludomagno): compute addons map in locals and use a single dynamic block addons_config { dynamic "dns_cache_config" { - for_each = var.enable_autopilot ? [] : [""] + # Pass the user-provided value when autopilot is disabled. When + # autopilot is enabled, pass the value only when the addon is + # set to true. This will fail but warns the user that autopilot + # doesn't support this option, instead of silently discarding + # and hiding the error + for_each = !var.enable_autopilot || (var.enable_autopilot && var.addons.dns_cache_config) ? [""] : [] content { enabled = var.addons.dns_cache_config } @@ -87,8 +92,16 @@ resource "google_container_cluster" "cluster" { gce_persistent_disk_csi_driver_config { enabled = var.addons.gce_persistent_disk_csi_driver_config } - gcp_filestore_csi_driver_config { - enabled = var.addons.gcp_filestore_csi_driver_config + dynamic "gcp_filestore_csi_driver_config" { + # Pass the user-provided value when autopilot is disabled. When + # autopilot is enabled, pass the value only when the addon is + # set to true. This will fail but warns the user that autopilot + # doesn't support this option, instead of silently discarding + # and hiding the error + for_each = !var.enable_autopilot || (var.enable_autopilot && var.addons.gcp_filestore_csi_driver_config) ? [""] : [] + content { + enabled = var.addons.gcp_filestore_csi_driver_config + } } kalm_config { enabled = var.addons.kalm_config diff --git a/tests/modules/gke_cluster/__init__.py b/tests/modules/gke_cluster/__init__.py new file mode 100644 index 0000000000..6d6d1266c3 --- /dev/null +++ b/tests/modules/gke_cluster/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2022 Google LLC +# +# 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. diff --git a/tests/modules/gke_cluster/fixture/main.tf b/tests/modules/gke_cluster/fixture/main.tf new file mode 100644 index 0000000000..078d470cd3 --- /dev/null +++ b/tests/modules/gke_cluster/fixture/main.tf @@ -0,0 +1,28 @@ +/** + * Copyright 2022 Google LLC + * + * 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. + */ + +module "test" { + source = "../../../../modules/gke-cluster" + project_id = "my-project" + name = "cluster-1" + location = "europe-west1-b" + network = "mynetwork" + subnetwork = "mysubnet" + secondary_range_pods = "pods" + secondary_range_services = "services" + enable_autopilot = var.enable_autopilot + addons = var.addons +} diff --git a/tests/modules/gke_cluster/fixture/variables.tf b/tests/modules/gke_cluster/fixture/variables.tf new file mode 100644 index 0000000000..104054df87 --- /dev/null +++ b/tests/modules/gke_cluster/fixture/variables.tf @@ -0,0 +1,38 @@ +/** + * Copyright 2022 Google LLC + * + * 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 "enable_autopilot" { + default = false +} + +variable "addons" { + default = { + cloudrun_config = false + dns_cache_config = false + horizontal_pod_autoscaling = true + http_load_balancing = true + istio_config = { + enabled = false + tls = false + } + network_policy_config = false + gce_persistent_disk_csi_driver_config = false + gcp_filestore_csi_driver_config = false + config_connector_config = false + kalm_config = false + gke_backup_agent_config = false + } +} diff --git a/tests/modules/gke_cluster/test_plan.py b/tests/modules/gke_cluster/test_plan.py new file mode 100644 index 0000000000..947448c184 --- /dev/null +++ b/tests/modules/gke_cluster/test_plan.py @@ -0,0 +1,39 @@ +# Copyright 2022 Google LLC +# +# 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. + + +def test_standard(plan_runner): + "Test resources created with variable defaults." + _, resources = plan_runner() + assert len(resources) == 1 + + cluster_config = resources[0]['values'] + assert cluster_config['name'] == "cluster-1" + assert cluster_config['network'] == "mynetwork" + assert cluster_config['subnetwork'] == "mysubnet" + assert cluster_config['enable_autopilot'] is None + # assert 'service_account' not in node_config + + +def test_autopilot(plan_runner): + "Test resources created with variable defaults." + _, resources = plan_runner(enable_autopilot="true") + assert len(resources) == 1 + + cluster_config = resources[0]['values'] + assert cluster_config['name'] == "cluster-1" + assert cluster_config['network'] == "mynetwork" + assert cluster_config['subnetwork'] == "mysubnet" + assert cluster_config['enable_autopilot'] == True + # assert 'service_account' not in node_config diff --git a/tests/modules/gke_nodepool/test_plan.py b/tests/modules/gke_nodepool/test_plan.py index c629b0d2ad..86b1d462eb 100644 --- a/tests/modules/gke_nodepool/test_plan.py +++ b/tests/modules/gke_nodepool/test_plan.py @@ -17,7 +17,8 @@ 'https://www.googleapis.com/auth/devstorage.read_only', 'https://www.googleapis.com/auth/logging.write', 'https://www.googleapis.com/auth/monitoring', - 'https://www.googleapis.com/auth/monitoring.write'] + 'https://www.googleapis.com/auth/monitoring.write' +] def test_defaults(plan_runner):