Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GKE Resource Consumption Metering, promote resource export to GA #2705

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions third_party/terraform/resources/resource_container_cluster.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ func resourceContainerCluster() *schema.Resource {
},
},
},

<% end -%>
"resource_usage_export_config": {
Type: schema.TypeList,
MaxItems: 1,
Expand All @@ -881,6 +881,20 @@ func resourceContainerCluster() *schema.Resource {
Optional: true,
Default: false,
},
"enable_resource_consumption_metering": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you break this schema out so that it's just the following? As well as make the required transformations in code to send/read the right request. enable_shielded_nodes is an example where we made a similar transformation, I believe.

"enable_resource_consumption_metering": {
    Type:     schema.TypeBool,
    Optional: true,
    Default:  true,
},

(defaulting to whatever the API does)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'm trying to find the time for this - I'm having a hard time figuring how to nest the Shielded style code into another resource block.
I do think more fields are coming to this block in the future, in case that matters.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not aware of any additional subfields upcoming, so I'd rather we pull it up. We can add them in a compatible way if any are added.

hashicorp/terraform-provider-google-beta@6e56d74 is a good reference for what adding enable_shielded_nodes looked like (although it's munged with shielded_instance_config. Only the parts in google_container_cluster.go matter.)

Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
},
},
},
"bigquery_destination": {
Type: schema.TypeList,
MaxItems: 1,
Expand All @@ -897,7 +911,6 @@ func resourceContainerCluster() *schema.Resource {
},
},
},
<% end -%>

"enable_intranode_visibility": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -2454,7 +2467,6 @@ func expandDefaultMaxPodsConstraint(v interface{}) *containerBeta.MaxPodsConstra
}
}

<% unless version == 'ga' -%>
func expandResourceUsageExportConfig(configured interface{}) *containerBeta.ResourceUsageExportConfig {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand All @@ -2477,9 +2489,18 @@ func expandResourceUsageExportConfig(configured interface{}) *containerBeta.Reso
}
}
}
if _, ok := resourceUsageConfig["enable_resource_consumption_metering"]; ok {
if len(resourceUsageConfig["enable_resource_consumption_metering"].([]interface{})) > 0 {
consumptionMeteringConfig := resourceUsageConfig["enable_resource_consumption_metering"].([]interface{})[0].(map[string]interface{})
if _, ok := consumptionMeteringConfig["enabled"]; ok {
result.ConsumptionMeteringConfig = &containerBeta.ConsumptionMeteringConfig{
Enabled: consumptionMeteringConfig["enabled"].(bool),
}
}
}
}
return result
}
<% end -%>

func flattenNetworkPolicy(c *containerBeta.NetworkPolicy) []map[string]interface{} {
result := []map[string]interface{}{}
Expand Down Expand Up @@ -2757,6 +2778,7 @@ func flattenPodSecurityPolicyConfig(c *containerBeta.PodSecurityPolicyConfig) []
},
}
}
<% end -%>

func flattenResourceUsageExportConfig(c *containerBeta.ResourceUsageExportConfig) []map[string]interface{} {
if c == nil {
Expand All @@ -2765,13 +2787,15 @@ func flattenResourceUsageExportConfig(c *containerBeta.ResourceUsageExportConfig
return []map[string]interface{}{
{
"enable_network_egress_metering": c.EnableNetworkEgressMetering,
"enable_resource_consumption_metering": []map[string]interface{}{
{"enabled": c.ConsumptionMeteringConfig.Enabled},
},
"bigquery_destination": []map[string]interface{}{
{"dataset_id": c.BigqueryDestination.DatasetId},
},
},
}
}
<% end -%>

<% unless version == 'ga' -%>
func flattenDatabaseEncryption(c *containerBeta.DatabaseEncryption) []map[string]interface{} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2867,13 +2867,15 @@ resource "google_container_cluster" "with_ip_allocation_policy" {
}`, cluster, cluster)
}

<% unless version == 'ga' -%>
func testAccContainerCluster_withResourceUsageExportConfig(clusterName, datasetId string, resourceUsage bool) string {
resourceUsageConfig := ""
if resourceUsage {
resourceUsageConfig = `
resource_usage_export_config {
enable_network_egress_metering = true
enable_resource_consumption_metering {
enabled = false
}

bigquery_destination {
dataset_id = "${google_bigquery_dataset.default.dataset_id}"
Expand All @@ -2896,7 +2898,6 @@ func testAccContainerCluster_withResourceUsageExportConfig(clusterName, datasetI
}`, datasetId, clusterName, resourceUsageConfig)
return config
}
<% end -%>

func testAccContainerCluster_withPrivateClusterConfigMissingCidrBlock(clusterName string) string {
return fmt.Sprintf(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,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) - Parameters for managing resource consumption metering.

* `enable_resource_consumption_metering.enabled` (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` if `resource_usage_export_config` block is present.

* `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:
Expand All @@ -634,6 +640,10 @@ The `resource_usage_export_config` block supports:
resource_usage_export_config {
enable_network_egress_metering = false

enable_resource_consumption_metering {
enabled = false
}

bigquery_destination {
dataset_id = "cluster_resource_usage"
}
Expand Down