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

feat(google_container_cluster): allow enabling cost allocation #6469

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,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 @@ -1666,6 +1685,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 @@ -2045,6 +2067,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 @@ -2455,6 +2482,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 @@ -3883,6 +3929,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 @@ -3955,7 +4017,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 @@ -4524,6 +4586,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 @@ -4570,15 +4645,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 @@ -2666,6 +2666,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 @@ -5517,6 +5550,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 Expand Up @@ -6019,4 +6070,4 @@ func TestValidateNodePoolAutoConfig(t *testing.T) {
}
}
}
<% end -%>
<% end -%>