Skip to content

Commit

Permalink
Add GKE Resource Consumption Metering, promote resource export… (#3303)…
Browse files Browse the repository at this point in the history
… (#1901)

* Add GKE Resource Consumption Metering, promote resource export to GA

* Add docs

* Spacing

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Mar 26, 2020
1 parent 2a9b040 commit 39ad077
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changelog/3303.txt
Original file line number Diff line number Diff line change
@@ -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)
```
24 changes: 21 additions & 3 deletions google-beta/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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},
},
Expand Down
43 changes: 30 additions & 13 deletions google-beta/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1653,15 +1653,23 @@ 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",
ImportState: true,
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",
Expand Down Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/container_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -664,13 +664,20 @@ 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:

```hcl
resource_usage_export_config {
enable_network_egress_metering = false
enable_resource_consumption_metering = true
bigquery_destination {
dataset_id = "cluster_resource_usage"
Expand Down

0 comments on commit 39ad077

Please sign in to comment.