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

Addlogging_config and monitoring_config to container cluster #5217

Merged
merged 2 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
140 changes: 140 additions & 0 deletions mmv1/third_party/terraform/resources/resource_container_cluster.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,27 @@ func resourceContainerCluster() *schema.Resource {
Description: `The number of nodes to create in this cluster's default node pool. In regional or multi-zonal clusters, this is the number of nodes per zone. Must be set if node_pool is not set. If you're using google_container_node_pool objects with no default node pool, you'll need to set this to a value of at least 1, alongside setting remove_default_node_pool to true.`,
},

"logging_config": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Description: `Logging configuration for the cluster.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enable_components": {
Type: schema.TypeList,
Required: true,
Description: `GKE components exposing logs. Valid values include SYSTEM_COMPONENTS and WORKLOADS.`,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{"SYSTEM_COMPONENTS", "WORKLOADS"}, false),
},
},
},
},
},

"logging_service": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -648,6 +669,27 @@ func resourceContainerCluster() *schema.Resource {
},
},

"monitoring_config": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Description: `Monitoring configuration for the cluster.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enable_components": {
Type: schema.TypeList,
Required: true,
Description: `GKE components exposing metrics. Valid values include SYSTEM_COMPONENTS.`,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{"SYSTEM_COMPONENTS"}, false),
},
},
},
},
},

<% unless version == 'ga' -%>
"notification_config": {
Type: schema.TypeList,
Expand Down Expand Up @@ -1542,6 +1584,14 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
cluster.ResourceUsageExportConfig = expandResourceUsageExportConfig(v)
}

if v, ok := d.GetOk("logging_config"); ok {
cluster.LoggingConfig = expandContainerClusterLoggingConfig(v)
}

if v, ok := d.GetOk("monitoring_config"); ok {
cluster.MonitoringConfig = expandMonitoringConfig(v)
}

req := &containerBeta.CreateClusterRequest{
Cluster: cluster,
}
Expand Down Expand Up @@ -1885,6 +1935,14 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
}
<% end -%>

if err := d.Set("logging_config", flattenContainerClusterLoggingConfig(cluster.LoggingConfig)); err != nil {
return err
}

if err := d.Set("monitoring_config", flattenMonitoringConfig(cluster.MonitoringConfig)); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -2651,6 +2709,36 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
log.Printf("[INFO] GKE cluster %s workload identity config has been updated", d.Id())
}

if d.HasChange("logging_config") {
req := &containerBeta.UpdateClusterRequest{
Update: &containerBeta.ClusterUpdate{
DesiredLoggingConfig: expandContainerClusterLoggingConfig(d.Get("logging_config")),
},
}
updateF := updateFunc(req, "updating GKE cluster logging config")
// Call update serially.
if err := lockedCall(lockKey, updateF); err != nil {
return err
}

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

if d.HasChange("monitoring_config") {
req := &containerBeta.UpdateClusterRequest{
Update: &containerBeta.ClusterUpdate{
DesiredMonitoringConfig: expandMonitoringConfig(d.Get("monitoring_config")),
},
}
updateF := updateFunc(req, "updating GKE cluster monitoring config")
// Call update serially.
if err := lockedCall(lockKey, updateF); err != nil {
return err
}

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

if d.HasChange("resource_labels") {
resourceLabels := d.Get("resource_labels").(map[string]interface{})
labelFingerprint := d.Get("label_fingerprint").(string)
Expand Down Expand Up @@ -3519,6 +3607,34 @@ func expandDnsConfig(configured interface{}) *containerBeta.DNSConfig {
}
<% end -%>

func expandContainerClusterLoggingConfig(configured interface{}) *containerBeta.LoggingConfig {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil
}

config := l[0].(map[string]interface{})
return &containerBeta.LoggingConfig{
ComponentConfig: &containerBeta.LoggingComponentConfig{
EnableComponents: convertStringArr(config["enable_components"].([]interface{})),
},
}
}

func expandMonitoringConfig(configured interface{}) *containerBeta.MonitoringConfig {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil
}

config := l[0].(map[string]interface{})
return &containerBeta.MonitoringConfig{
ComponentConfig: &containerBeta.MonitoringComponentConfig{
EnableComponents: config["enable_components"].([]string),
},
}
}

<% unless version == 'ga' -%>
func flattenNotificationConfig(c *containerBeta.NotificationConfig) []map[string]interface{} {
if c == nil {
Expand Down Expand Up @@ -3984,6 +4100,30 @@ func flattenDnsConfig(c *containerBeta.DNSConfig) []map[string]interface{} {
}
<% end -%>

func flattenContainerClusterLoggingConfig(c *containerBeta.LoggingConfig) []map[string]interface{} {
if c == nil {
return nil
}

return []map[string]interface{}{
{
"enable_components": c.ComponentConfig.EnableComponents,
},
}
}

func flattenMonitoringConfig(c *containerBeta.MonitoringConfig) []map[string]interface{} {
if c == nil {
return nil
}

return []map[string]interface{}{
{
"enable_components": c.ComponentConfig.EnableComponents,
},
}
}

func resourceContainerClusterStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1709,7 +1709,52 @@ func TestAccContainerCluster_withWorkloadIdentityConfig(t *testing.T) {
},
},
})
}


func TestAccContainerCluster_withLoggingConfig(t *testing.T) {
t.Parallel()

clusterName := fmt.Sprintf("tf-test-cluster-%s", randString(t, 10))
vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_basic(clusterName),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccContainerCluster_withLoggingConfigEnabled(clusterName),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccContainerCluster_withLoggingConfigUpdated(clusterName),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccContainerCluster_basic(clusterName),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

<% unless version == 'ga' -%>
Expand Down Expand Up @@ -4609,3 +4654,35 @@ resource "google_container_cluster" "with_dns_config" {
`, clusterName, clusterDns, clusterDnsDomain, clusterDnsScope)
}
<% end -%>

func testAccContainerCluster_withLoggingConfigEnabled(name string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "primary" {
name = "%s"
location = "us-central1-a"
initial_node_count = 1
logging_config {
enable_components = [ "SYSTEM_COMPONENTS" ]
}
monitoring_config {
enable_components = [ "SYSTEM_COMPONENTS" ]
}
}
`, name)
}

func testAccContainerCluster_withLoggingConfigUpdated(name string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "primary" {
name = "%s"
location = "us-central1-a"
initial_node_count = 1
logging_config {
enable_components = [ "SYSTEM_COMPONENTS", "WORKLOADS" ]
}
monitoring_config {
enable_components = [ "SYSTEM_COMPONENTS" ]
}
}
`, name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ cluster.
* `name_prefix` - (Optional) Creates a unique name for the node pool beginning
with the specified prefix. Conflicts with `name`.

* `node_config` - (Optional) The network configuration of the pool. See
* `node_config` - (Optional) Parameters used in creating the default node pool. See
[google_container_cluster](container_cluster.html) for schema.

* `network_config` - (Optional) The network configuration of the pool. See
Expand Down