From ce718ba597f8b8ebadd7bd1e800447ca838b9f8b Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Thu, 26 Mar 2020 19:10:39 +0000 Subject: [PATCH] =?UTF-8?q?Add=20GKE=20Resource=20Consumption=20Metering,?= =?UTF-8?q?=20promote=20resource=20export=E2=80=A6=20(#3303)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add GKE Resource Consumption Metering, promote resource export to GA * Add docs * Spacing Signed-off-by: Modular Magician --- .changelog/3303.txt | 6 +++ google-beta/resource_container_cluster.go | 24 +++++++++-- .../resource_container_cluster_test.go | 43 +++++++++++++------ .../docs/r/container_cluster.html.markdown | 7 +++ 4 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 .changelog/3303.txt diff --git a/.changelog/3303.txt b/.changelog/3303.txt new file mode 100644 index 0000000000..d452e60b48 --- /dev/null +++ b/.changelog/3303.txt @@ -0,0 +1,6 @@ +```release-note:enhancement +container: added `resource_usage_export_config` to `google_container_cluster`, previously only available in `google-beta` (ga only) +``` +```release-note:enhancement +container: added `enable_resource_consumption_metering` to `resource_usage_export_config` in `google_container_cluster` (beta only) +``` diff --git a/google-beta/resource_container_cluster.go b/google-beta/resource_container_cluster.go index 54616470fe..2d38fafcf0 100644 --- a/google-beta/resource_container_cluster.go +++ b/google-beta/resource_container_cluster.go @@ -885,6 +885,11 @@ func resourceContainerCluster() *schema.Resource { Optional: true, Default: false, }, + "enable_resource_consumption_metering": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, "bigquery_destination": { Type: schema.TypeList, MaxItems: 1, @@ -1107,6 +1112,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er if v, ok := d.GetOk("workload_identity_config"); ok { cluster.WorkloadIdentityConfig = expandWorkloadIdentityConfig(v) } + if v, ok := d.GetOk("resource_usage_export_config"); ok { cluster.ResourceUsageExportConfig = expandResourceUsageExportConfig(v) } @@ -1336,6 +1342,7 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro if err := d.Set("resource_usage_export_config", flattenResourceUsageExportConfig(cluster.ResourceUsageExportConfig)); err != nil { return err } + return nil } @@ -1964,6 +1971,7 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er d.SetPartial("resource_usage_export_config") } + d.Partial(false) if _, err := containerClusterAwaitRestingState(config, project, location, clusterName, d.Timeout(schema.TimeoutUpdate)); err != nil { @@ -2488,7 +2496,6 @@ func expandDefaultMaxPodsConstraint(v interface{}) *containerBeta.MaxPodsConstra MaxPodsPerNode: int64(v.(int)), } } - func expandResourceUsageExportConfig(configured interface{}) *containerBeta.ResourceUsageExportConfig { l := configured.([]interface{}) if len(l) == 0 || l[0] == nil { @@ -2499,7 +2506,11 @@ func expandResourceUsageExportConfig(configured interface{}) *containerBeta.Reso result := &containerBeta.ResourceUsageExportConfig{ EnableNetworkEgressMetering: resourceUsageConfig["enable_network_egress_metering"].(bool), - ForceSendFields: []string{"EnableNetworkEgressMetering"}, + ConsumptionMeteringConfig: &containerBeta.ConsumptionMeteringConfig{ + Enabled: resourceUsageConfig["enable_resource_consumption_metering"].(bool), + ForceSendFields: []string{"Enabled"}, + }, + ForceSendFields: []string{"EnableNetworkEgressMetering"}, } if _, ok := resourceUsageConfig["bigquery_destination"]; ok { if len(resourceUsageConfig["bigquery_destination"].([]interface{})) > 0 { @@ -2808,9 +2819,16 @@ func flattenResourceUsageExportConfig(c *containerBeta.ResourceUsageExportConfig if c == nil { return nil } + + enableResourceConsumptionMetering := false + if c.ConsumptionMeteringConfig != nil && c.ConsumptionMeteringConfig.Enabled == true { + enableResourceConsumptionMetering = true + } + return []map[string]interface{}{ { - "enable_network_egress_metering": c.EnableNetworkEgressMetering, + "enable_network_egress_metering": c.EnableNetworkEgressMetering, + "enable_resource_consumption_metering": enableResourceConsumptionMetering, "bigquery_destination": []map[string]interface{}{ {"dataset_id": c.BigqueryDestination.DatasetId}, }, diff --git a/google-beta/resource_container_cluster_test.go b/google-beta/resource_container_cluster_test.go index 8edf1d9bfe..3b615c0545 100644 --- a/google-beta/resource_container_cluster_test.go +++ b/google-beta/resource_container_cluster_test.go @@ -1653,7 +1653,7 @@ func TestAccContainerCluster_withResourceUsageExportConfig(t *testing.T) { CheckDestroy: testAccCheckContainerClusterDestroy, Steps: []resource.TestStep{ { - Config: testAccContainerCluster_withResourceUsageExportConfig(clusterName, datesetId, true), + Config: testAccContainerCluster_withResourceUsageExportConfig(clusterName, datesetId, "true"), }, { ResourceName: "google_container_cluster.with_resource_usage_export_config", @@ -1661,7 +1661,15 @@ func TestAccContainerCluster_withResourceUsageExportConfig(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccContainerCluster_withResourceUsageExportConfig(clusterName, datesetId, false), + Config: testAccContainerCluster_withResourceUsageExportConfig(clusterName, datesetId, "false"), + }, + { + ResourceName: "google_container_cluster.with_resource_usage_export_config", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccContainerCluster_withResourceUsageExportConfigNoConfig(clusterName, datesetId), }, { ResourceName: "google_container_cluster.with_resource_usage_export_config", @@ -3228,20 +3236,31 @@ resource "google_container_cluster" "with_ip_allocation_policy" { `, containerNetName, clusterName) } -func testAccContainerCluster_withResourceUsageExportConfig(clusterName, datasetId string, resourceUsage bool) string { - resourceUsageConfig := "" - if resourceUsage { - resourceUsageConfig = ` +func testAccContainerCluster_withResourceUsageExportConfig(clusterName, datasetId, enableMetering string) string { + return fmt.Sprintf(` +resource "google_bigquery_dataset" "default" { + dataset_id = "%s" + description = "gke resource usage dataset tests" + delete_contents_on_destroy = true +} + +resource "google_container_cluster" "with_resource_usage_export_config" { + name = "%s" + location = "us-central1-a" + initial_node_count = 1 resource_usage_export_config { enable_network_egress_metering = true - + enable_resource_consumption_metering = %s bigquery_destination { dataset_id = google_bigquery_dataset.default.dataset_id } - }` - } + } +} +`, datasetId, clusterName, enableMetering) +} - config := fmt.Sprintf(` +func testAccContainerCluster_withResourceUsageExportConfigNoConfig(clusterName, datasetId string) string { + return fmt.Sprintf(` resource "google_bigquery_dataset" "default" { dataset_id = "%s" description = "gke resource usage dataset tests" @@ -3252,10 +3271,8 @@ resource "google_container_cluster" "with_resource_usage_export_config" { name = "%s" location = "us-central1-a" initial_node_count = 1 - %s } -`, datasetId, clusterName, resourceUsageConfig) - return config +`, datasetId, clusterName) } func testAccContainerCluster_withPrivateClusterConfigMissingCidrBlock(containerNetName string, clusterName string) string { diff --git a/website/docs/r/container_cluster.html.markdown b/website/docs/r/container_cluster.html.markdown index 737433cd3e..30cb375229 100644 --- a/website/docs/r/container_cluster.html.markdown +++ b/website/docs/r/container_cluster.html.markdown @@ -664,6 +664,12 @@ The `resource_usage_export_config` block supports: * `enable_network_egress_metering` (Optional) - Whether to enable network egress metering for this cluster. If enabled, a daemonset will be created in the cluster to meter network egress traffic. +* `enable_resource_consumption_metering` (Optional) - Whether to enable resource +consumption metering on this cluster. When enabled, a table will be created in +the resource export BigQuery dataset to store resource consumption data. The +resulting table can be joined with the resource usage table or with BigQuery +billing export. Defaults to `true`. + * `bigquery_destination` (Required) - Parameters for using BigQuery as the destination of resource usage export. * `bigquery_destination.dataset_id` (Required) - The ID of a BigQuery Dataset. For Example: @@ -671,6 +677,7 @@ The `resource_usage_export_config` block supports: ```hcl resource_usage_export_config { enable_network_egress_metering = false + enable_resource_consumption_metering = true bigquery_destination { dataset_id = "cluster_resource_usage"