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 Terraform Support for GKE Auto-Monitoring #12688

Merged
merged 21 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
940c673
Add auto_monitoring_config to schema and flatteners/expanders
uol-jlin Jan 5, 2025
911627c
Update managed_prometheus config to include auto_monitoring_config
uol-jlin Jan 5, 2025
31ab597
Add auto_monitoring_config description to managed_prometheus block
uol-jlin Jan 5, 2025
6325707
Add missing scope in auto_monitoring_config test
uol-jlin Jan 5, 2025
9410f2b
Add scope test skeletons
uol-jlin Jan 5, 2025
3f73cdc
Add tests for scope = "ALL" / "NONE"
uol-jlin Jan 6, 2025
b80523a
Specify that auto_monitoring_config is non-GA
uol-jlin Jan 6, 2025
3e81ffa
Fix spacing
uol-jlin Jan 8, 2025
7484b73
Version guard schema change
uol-jlin Jan 9, 2025
ae95bd3
Version guard flatteners/expanders
uol-jlin Jan 9, 2025
c38497a
Version guard acceptance tests
uol-jlin Jan 9, 2025
46a8837
Cast auto-monitoring to *schema.Set instead of []interface{}
uol-jlin Jan 10, 2025
87ac9c3
Cast auto-monitoring config to schema.Set before using
uol-jlin Jan 10, 2025
8d4db40
Convert spaces to tabs for managed_prometheus schema
uol-jlin Jan 20, 2025
98be33f
Create schema.Set using NewSet convenience method
uol-jlin Jan 20, 2025
b936a7d
Remove old code block
uol-jlin Jan 20, 2025
528250c
Update resource_container_cluster.go.tmpl
uol-jlin Jan 31, 2025
8f3e685
Use TypeList instead of TypeSet, and set Computed: true
uol-jlin Jan 31, 2025
04eb9ba
Change expander to use TypeList instead of TypeSet
uol-jlin Feb 3, 2025
9111f27
Update flatten to use TypeList
uol-jlin Feb 3, 2025
fabbeed
Update docs
uol-jlin Feb 3, 2025
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 @@ -1273,6 +1273,24 @@ func ResourceContainerCluster() *schema.Resource {
Required: true,
uol-jlin marked this conversation as resolved.
Show resolved Hide resolved
Description: `Whether or not the managed collection is enabled.`,
},
{{- if ne $.TargetVersionName "ga" }}
"auto_monitoring_config": {
Type: schema.TypeSet,
uol-jlin marked this conversation as resolved.
Show resolved Hide resolved
Optional: true,
MaxItems: 1,
Description: `Configuration for GKE Workload Auto-Monitoring.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"scope": {
uol-jlin marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Required: true,
Description: `The scope of auto-monitoring.`,
ValidateFunc: validation.StringInSlice([]string{"ALL", "NONE"}, false),
},
},
},
},
{{- end }}
},
},
},
Expand Down Expand Up @@ -5894,6 +5912,21 @@ func expandMonitoringConfig(configured interface{}) *container.MonitoringConfig
mc.ManagedPrometheusConfig = &container.ManagedPrometheusConfig{
Enabled: managed_prometheus["enabled"].(bool),
}
{{- if ne $.TargetVersionName "ga" }}
if autoMonitoring, ok := managed_prometheus["auto_monitoring_config"]; ok {
if autoMonitoringSet, ok := autoMonitoring.(*schema.Set); ok {
autoMonitoringList := autoMonitoringSet.List()
if len(autoMonitoringList) > 0 {
autoMonitoringConfig := autoMonitoringList[0].(map[string]interface{})
if scope, ok := autoMonitoringConfig["scope"].(string); ok {
mc.ManagedPrometheusConfig.AutoMonitoringConfig = &container.AutoMonitoringConfig{
Scope: scope,
}
}
}
}
}
{{- end }}
}

if v, ok := config["advanced_datapath_observability_config"]; ok && len(v.([]interface{})) > 0 {
Expand Down Expand Up @@ -6871,11 +6904,32 @@ func flattenAdvancedDatapathObservabilityConfig(c *container.AdvancedDatapathObs
}

func flattenManagedPrometheusConfig(c *container.ManagedPrometheusConfig) []map[string]interface{} {
return []map[string]interface{}{
{
"enabled": c != nil && c.Enabled,
},
if c == nil {
return nil
}

result := make(map[string]interface{})

result["enabled"] = c.Enabled

{{- if ne $.TargetVersionName "ga" }}
if c.AutoMonitoringConfig != nil {
autoMonitoringMap := make(map[string]interface{})
if c.AutoMonitoringConfig.Scope != "" {
autoMonitoringMap["scope"] = c.AutoMonitoringConfig.Scope
}

// Constructing a schema.Set using NewSet
set := schema.NewSet(schema.HashString, []interface{}{})
if c.AutoMonitoringConfig.Scope != "" {
set.Add(c.AutoMonitoringConfig.Scope)
}

result["auto_monitoring_config"] = set
}
{{- end }}

return []map[string]interface{}{result}
}

func flattenNodePoolAutoConfig(c *container.NodePoolAutoConfig) []map[string]interface{} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3966,6 +3966,32 @@ func TestAccContainerCluster_withMonitoringConfig(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version", "deletion_protection"},
},
{{- if ne $.TargetVersionName "ga" }}
{
Config: testAccContainerCluster_withMonitoringConfigScopeAll(clusterName, networkName, subnetworkName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_container_cluster.primary", "monitoring_config.0.managed_prometheus.0.auto_monitoring_config.0.scope", "ALL"),
),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version", "deletion_protection"},
},
{
Config: testAccContainerCluster_withMonitoringConfigScopeNone(clusterName, networkName, subnetworkName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_container_cluster.primary", "monitoring_config.0.managed_prometheus.0.auto_monitoring_config.0.scope", "NONE"),
),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version", "deletion_protection"},
},
{{- end }}
// Back to basic settings to test setting Prometheus on its own
{
Config: testAccContainerCluster_basic(clusterName, networkName, subnetworkName),
Expand Down Expand Up @@ -10786,6 +10812,50 @@ resource "google_container_cluster" "primary" {
`, name, networkName, subnetworkName)
}

{{- if ne $.TargetVersionName "ga" }}
func testAccContainerCluster_withMonitoringConfigScopeAll(name, networkName, subnetworkName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "primary" {
name = "%s"
location = "us-central1-a"
initial_node_count = 1
monitoring_config {
managed_prometheus {
enabled = true
auto_monitoring_config {
scope = "ALL"
}
}
}
deletion_protection = false
network = "%s"
subnetwork = "%s"
}
`, name, networkName, subnetworkName)
}

func testAccContainerCluster_withMonitoringConfigScopeNone(name, networkName, subnetworkName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "primary" {
name = "%s"
location = "us-central1-a"
initial_node_count = 1
monitoring_config {
managed_prometheus {
enabled = true
auto_monitoring_config {
scope = "NONE"
}
}
}
deletion_protection = false
network = "%s"
subnetwork = "%s"
}
`, name, networkName, subnetworkName)
}
{{- end }}

func testAccContainerCluster_withMonitoringConfigAdvancedDatapathObservabilityConfigEnabled(name string) string {
return fmt.Sprintf(`
resource "google_compute_network" "container_network" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ This block also contains several computed attributes, documented below.
<a name="nested_managed_prometheus"></a>The `managed_prometheus` block supports:

* `enabled` - (Required) Whether or not the managed collection is enabled.
* `auto_monitoring_config` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) Configuration for GKE Auto-Monitoring which includes a `Scope` field (string). Supported values include: `ALL` and `NONE`.
uol-jlin marked this conversation as resolved.
Show resolved Hide resolved

<a name="nested_advanced_datapath_observability_config"></a>The `advanced_datapath_observability_config` block supports:

Expand Down
Loading