From 116460e07810be8e79e58b1ca558df02101af4a8 Mon Sep 17 00:00:00 2001 From: Mohammad Varmazyar Date: Tue, 1 Feb 2022 16:08:08 +0100 Subject: [PATCH] Add support for image type configuration on the GKE NAP (#5644) * Add support for image type configuration on the GKE NAP * Add GA and Beta changes for image_type support on NAP * Update validation for image_type on NAP * Put image_type support on GA and change default its value to containerd * Set ignoreCase as true on Validation function for image_type Co-authored-by: megan07 * Image_type Capitalization Co-authored-by: megan07 --- .../resource_container_cluster.go.erb | 9 +++ .../resource_container_cluster_test.go.erb | 65 +++++++++++++++++++ .../docs/r/container_cluster.html.markdown | 6 +- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/mmv1/third_party/terraform/resources/resource_container_cluster.go.erb b/mmv1/third_party/terraform/resources/resource_container_cluster.go.erb index 6077dbfcd24c..0eacebf6bd9f 100644 --- a/mmv1/third_party/terraform/resources/resource_container_cluster.go.erb +++ b/mmv1/third_party/terraform/resources/resource_container_cluster.go.erb @@ -457,6 +457,13 @@ func resourceContainerCluster() *schema.Resource { Default: "default", Description: `The Google Cloud Platform Service Account to be used by the node VMs.`, }, + "image_type": { + Type: schema.TypeString, + Optional: true, + Default: "COS_CONTAINERD", + Description: `The default image type used by NAP once a new node pool is being created.`, + ValidateFunc: validation.StringInSlice([]string{"COS_CONTAINERD", "COS", "UBUNTU_CONTAINERD", "UBUNTU"}, false), + }, <% unless version == 'ga' -%> "min_cpu_platform": { Type: schema.TypeString, @@ -3283,6 +3290,7 @@ func expandAutoProvisioningDefaults(configured interface{}, d *schema.ResourceDa npd := &container.AutoprovisioningNodePoolDefaults{ OauthScopes: convertStringArr(config["oauth_scopes"].([]interface{})), ServiceAccount: config["service_account"].(string), + ImageType: config["image_type"].(string), } <% unless version == 'ga' -%> @@ -4023,6 +4031,7 @@ func flattenAutoProvisioningDefaults(a *container.AutoprovisioningNodePoolDefaul r := make(map[string]interface{}) r["oauth_scopes"] = a.OauthScopes r["service_account"] = a.ServiceAccount + r["image_type"] = a.ImageType <% unless version == 'ga' -%> r["min_cpu_platform"] = a.MinCpuPlatform <% end -%> diff --git a/mmv1/third_party/terraform/tests/resource_container_cluster_test.go.erb b/mmv1/third_party/terraform/tests/resource_container_cluster_test.go.erb index 43bdabf6e664..3a52c20dec0f 100644 --- a/mmv1/third_party/terraform/tests/resource_container_cluster_test.go.erb +++ b/mmv1/third_party/terraform/tests/resource_container_cluster_test.go.erb @@ -2037,6 +2037,39 @@ func TestAccContainerCluster_withFlexiblePodCIDR(t *testing.T) { } <% end -%> +func TestAccContainerCluster_nodeAutoprovisioningDefaultsImageType(t *testing.T) { + t.Parallel() + + clusterName := fmt.Sprintf("tf-test-cluster-%s", randString(t, 10)) + includeImageType := true + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckContainerClusterDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccContainerCluster_autoprovisioningDefaultsImageType(clusterName, includeImageType), + }, + { + ResourceName: "google_container_cluster.with_autoprovisioning", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"min_master_version"}, + }, + { + Config: testAccContainerCluster_autoprovisioningDefaultsImageType(clusterName, !includeImageType), + }, + { + ResourceName: "google_container_cluster.with_autoprovisioning", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"min_master_version"}, + }, + }, + }) +} + func TestAccContainerCluster_errorCleanDanglingCluster(t *testing.T) { t.Parallel() @@ -3779,6 +3812,38 @@ resource "google_container_cluster" "with_autoprovisioning" { } <% end -%> +func testAccContainerCluster_autoprovisioningDefaultsImageType(cluster string, includeImageType bool) string { + imageTypeCfg := "" + if includeImageType { + imageTypeCfg = `image_type = "COS_CONTAINERD"` + } + + return fmt.Sprintf(` +data "google_container_engine_versions" "central1a" { + location = "us-central1-a" +} +resource "google_container_cluster" "with_autoprovisioning" { + name = "%s" + location = "us-central1-a" + initial_node_count = 1 + min_master_version = data.google_container_engine_versions.central1a.latest_master_version + cluster_autoscaling { + enabled = true + resource_limits { + resource_type = "cpu" + maximum = 2 + } + resource_limits { + resource_type = "memory" + maximum = 2048 + } + auto_provisioning_defaults { + %s + } + } +}`, cluster, imageTypeCfg) +} + func testAccContainerCluster_withNodePoolAutoscaling(cluster, np string) string { return fmt.Sprintf(` resource "google_container_cluster" "with_node_pool" { diff --git a/mmv1/third_party/terraform/website/docs/r/container_cluster.html.markdown b/mmv1/third_party/terraform/website/docs/r/container_cluster.html.markdown index d2a53ceaaea3..9128eb288fc2 100644 --- a/mmv1/third_party/terraform/website/docs/r/container_cluster.html.markdown +++ b/mmv1/third_party/terraform/website/docs/r/container_cluster.html.markdown @@ -197,7 +197,7 @@ and requires the `ip_allocation_policy` block to be defined. By default when thi * `master_auth` - (Optional) The authentication information for accessing the Kubernetes master. Some values in this block are only returned by the API if your service account has permission to get credentials for your GKE cluster. If -you see an unexpected diff unsetting your client cert, ensure you have the +you see an unexpected diff unsetting your client cert, ensure you have the `container.clusters.getCredentials` permission. Structure is [documented below](#nested_master_auth). @@ -473,6 +473,8 @@ as "Intel Haswell" or "Intel Sandy Bridge". * `service_account` - (Optional) The Google Cloud Platform Service Account to be used by the node VMs. +* `image_type` - (Optional) The default image type used by NAP once a new node pool is being created. Please note that according to the [official documentation](https://cloud.google.com/kubernetes-engine/docs/how-to/node-auto-provisioning#default-image-type) the value must be one of the [COS_CONTAINERD, COS, UBUNTU_CONTAINERD, UBUNTU]. + The `authenticator_groups_config` block supports: * `security_group` - (Required) The name of the RBAC security group for use with Google security groups in Kubernetes RBAC. Group name must be in format `gke-security-groups@yourdomain.com`. @@ -687,7 +689,7 @@ gcfs_config { are preemptible. See the [official documentation](https://cloud.google.com/container-engine/docs/preemptible-vm) for more information. Defaults to false. -* `spot` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) A boolean +* `spot` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) A boolean that represents whether the underlying node VMs are spot. See the [official documentation](https://cloud.google.com/kubernetes-engine/docs/concepts/spot-vms) for more information. Defaults to false.