Skip to content

Commit

Permalink
feat(google_container_cluster): allow enabling cost allocation (Googl…
Browse files Browse the repository at this point in the history
  • Loading branch information
renescheepers authored and hao-nan-li committed Aug 31, 2022
1 parent ae400bd commit 1ea3938
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,25 @@ func resourceContainerCluster() *schema.Resource {
Computed: true,
},

<% unless version == "ga" -%>
"cost_management_config": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
Description: `Cost management configuration for the cluster.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Required: true,
Description: `Whether to enable GKE cost allocation. When you enable GKE cost allocation, the cluster name and namespace of your GKE workloads appear in the labels field of the billing export to BigQuery. Defaults to false.`,
},
},
},
},
<% end -%>

"resource_usage_export_config": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -1683,6 +1702,9 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
ResourceLabels: expandStringMap(d, "resource_labels"),
<% unless version == 'ga' -%>
NodePoolAutoConfig: expandNodePoolAutoConfig(d.Get("node_pool_auto_config")),
<% end -%>
<% unless version == 'ga' -%>
CostManagementConfig: expandCostManagementConfig(d.Get("cost_management_config")),
<% end -%>
}

Expand Down Expand Up @@ -2066,6 +2088,11 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
if err := d.Set("enable_l4_ilb_subsetting", cluster.NetworkConfig.EnableL4ilbSubsetting); err != nil {
return fmt.Errorf("Error setting enable_l4_ilb_subsetting: %s", err)
}
<% end -%>
<% unless version == 'ga' %>
if err := d.Set("cost_management_config", flattenManagementConfig(cluster.CostManagementConfig)); err != nil {
return fmt.Errorf("Error setting cost_management_config: %s", err)
}
<% end -%>
if err := d.Set("confidential_nodes", flattenConfidentialNodes(cluster.ConfidentialNodes)); err != nil {
return err
Expand Down Expand Up @@ -2480,6 +2507,25 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
}
<% end -%>
<% unless version == 'ga' -%>
if d.HasChange("cost_management_config") {
c := d.Get("cost_management_config")
req := &container.UpdateClusterRequest{
Update: &container.ClusterUpdate{
DesiredCostManagementConfig: expandCostManagementConfig(c),
},
}

updateF := updateFunc(req, "updating cost management config")
// Call update serially.
if err := lockedCall(lockKey, updateF); err != nil {
return err
}

log.Printf("[INFO] GKE cluster %s cost management config has been updated", d.Id())
}

<% end -%>
if d.HasChange("authenticator_groups_config") {
req := &container.UpdateClusterRequest{
Update: &container.ClusterUpdate{
Expand Down Expand Up @@ -3947,6 +3993,22 @@ func expandDefaultMaxPodsConstraint(v interface{}) *container.MaxPodsConstraint
MaxPodsPerNode: int64(v.(int)),
}
}

<% unless version == 'ga' -%>
func expandCostManagementConfig(configured interface{}) *container.CostManagementConfig {
l := configured.([]interface{})
if len(l) == 0 {
return nil
}

config := l[0].(map[string]interface{})
return &container.CostManagementConfig{
Enabled: config["enabled"].(bool),
ForceSendFields: []string{"Enabled"},
}
}
<% end -%>

func expandResourceUsageExportConfig(configured interface{}) *container.ResourceUsageExportConfig {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down Expand Up @@ -4019,7 +4081,7 @@ func expandMonitoringConfig(configured interface{}) *container.MonitoringConfig
EnableComponents: convertStringArr(enable_components),
}
}
<% if version == 'beta' -%>
<% unless version == 'ga' -%>
if v, ok := config["managed_prometheus"]; ok && len(v.([]interface{})) > 0 {
managed_prometheus := v.([]interface{})[0].(map[string]interface{})
mc.ManagedPrometheusConfig = &container.ManagedPrometheusConfig{
Expand Down Expand Up @@ -4599,6 +4661,19 @@ func flattenMeshCertificates(c *container.MeshCertificates) []map[string]interfa
}
}

<% unless version == 'ga' -%>
func flattenManagementConfig(c *container.CostManagementConfig) []map[string]interface{} {
if c == nil {
return nil
}
return []map[string]interface{}{
{
"enabled": c.Enabled,
},
}
}

<% end -%>
func flattenDatabaseEncryption(c *container.DatabaseEncryption) []map[string]interface{} {
if c == nil {
return nil
Expand Down Expand Up @@ -4645,15 +4720,15 @@ func flattenMonitoringConfig(c *container.MonitoringConfig) []map[string]interfa
if c.ComponentConfig != nil {
result["enable_components"] = c.ComponentConfig.EnableComponents
}
<% if version == 'beta' -%>
<% unless version == 'ga' -%>
if c.ManagedPrometheusConfig != nil {
result["managed_prometheus"] = flattenManagedPrometheusConfig(c.ManagedPrometheusConfig)
}
<% end -%>
return []map[string]interface{}{result}
}

<% if version == 'beta' -%>
<% unless version == 'ga' -%>
func flattenManagedPrometheusConfig(c *container.ManagedPrometheusConfig) []map[string]interface{} {
return []map[string]interface{}{
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2697,6 +2697,39 @@ func TestAccContainerCluster_withMeshCertificatesConfig(t *testing.T) {
})
}

<% unless version == 'ga' -%>
func TestAccContainerCluster_withCostManagementConfig(t *testing.T) {
t.Parallel()

clusterName := fmt.Sprintf("tf-test-cluster-%s", randString(t, 10))
pid := getTestProjectFromEnv()

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_updateCostManagementConfig(pid, clusterName, true),
},
{
ResourceName: "google_container_cluster.with_cost_management_config",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccContainerCluster_updateCostManagementConfig(pid, clusterName, false),
},
{
ResourceName: "google_container_cluster.with_cost_management_config",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

<% end -%>
func TestAccContainerCluster_withDatabaseEncryption(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -5564,6 +5597,24 @@ func testAccContainerCluster_updateMeshCertificatesConfig(projectID string, clus
}`, projectID, clusterName, enabled)
}

<% unless version == 'ga' -%>
func testAccContainerCluster_updateCostManagementConfig(projectID string, clusterName string, enabled bool) string {
return fmt.Sprintf(`
data "google_project" "project" {
project_id = "%s"
}

resource "google_container_cluster" "with_cost_management_config" {
name = "%s"
location = "us-central1-a"
initial_node_count = 1
cost_management_config {
enabled = %v
}
}`, projectID, clusterName, enabled)
}

<% end -%>
func testAccContainerCluster_withDatabaseEncryption(clusterName string, kmsData bootstrappedKMS) string {
return fmt.Sprintf(`
data "google_project" "project" {
Expand Down

0 comments on commit 1ea3938

Please sign in to comment.