Skip to content

Commit c6e82bf

Browse files
Prometheus (#6104) (#4373)
* Add support for Managed Service for Prometheus * Simplify expandMonitoringConfig * Add testing enabling managed prometheus on its own * Fix flattenMonitorinConfig for optional fields * Fixes for compile errors * Remove MaxItems 1 from monitoring_config.enable_components * Fix unnecessary conversion for enable_components Co-authored-by: Michael Barrientos <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Michael Barrientos <[email protected]>
1 parent 2e8e88f commit c6e82bf

File tree

4 files changed

+181
-8
lines changed

4 files changed

+181
-8
lines changed

.changelog/6104.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
container: add `managed_prometheus` to `monitoring_config` in `google_container_cluster` (beta)
3+
```

google-beta/resource_container_cluster.go

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -721,13 +721,30 @@ func resourceContainerCluster() *schema.Resource {
721721
Schema: map[string]*schema.Schema{
722722
"enable_components": {
723723
Type: schema.TypeList,
724-
Required: true,
724+
Optional: true,
725+
Computed: true,
725726
Description: `GKE components exposing metrics. Valid values include SYSTEM_COMPONENTS and WORKLOADS.`,
726727
Elem: &schema.Schema{
727728
Type: schema.TypeString,
728729
ValidateFunc: validation.StringInSlice([]string{"SYSTEM_COMPONENTS", "WORKLOADS"}, false),
729730
},
730731
},
732+
"managed_prometheus": {
733+
Type: schema.TypeList,
734+
Optional: true,
735+
Computed: true,
736+
MaxItems: 1,
737+
Description: `Configuration for Google Cloud Managed Services for Prometheus.`,
738+
Elem: &schema.Resource{
739+
Schema: map[string]*schema.Schema{
740+
"enabled": {
741+
Type: schema.TypeBool,
742+
Required: true,
743+
Description: `Whether or not the managed collection is enabled.`,
744+
},
745+
},
746+
},
747+
},
731748
},
732749
},
733750
},
@@ -3598,13 +3615,22 @@ func expandMonitoringConfig(configured interface{}) *container.MonitoringConfig
35983615
if len(l) == 0 || l[0] == nil {
35993616
return nil
36003617
}
3601-
3618+
mc := &container.MonitoringConfig{}
36023619
config := l[0].(map[string]interface{})
3603-
return &container.MonitoringConfig{
3604-
ComponentConfig: &container.MonitoringComponentConfig{
3605-
EnableComponents: convertStringArr(config["enable_components"].([]interface{})),
3606-
},
3620+
3621+
if v, ok := config["enable_components"]; ok && len(v.([]interface{})) > 0 {
3622+
enable_components := v.([]interface{})
3623+
mc.ComponentConfig = &container.MonitoringComponentConfig{
3624+
EnableComponents: convertStringArr(enable_components),
3625+
}
3626+
}
3627+
if v, ok := config["managed_prometheus"]; ok && len(v.([]interface{})) > 0 {
3628+
managed_prometheus := v.([]interface{})[0].(map[string]interface{})
3629+
mc.ManagedPrometheusConfig = &container.ManagedPrometheusConfig{
3630+
Enabled: managed_prometheus["enabled"].(bool),
3631+
}
36073632
}
3633+
return mc
36083634
}
36093635

36103636
func flattenNotificationConfig(c *container.NotificationConfig) []map[string]interface{} {
@@ -4112,9 +4138,20 @@ func flattenMonitoringConfig(c *container.MonitoringConfig) []map[string]interfa
41124138
return nil
41134139
}
41144140

4141+
result := make(map[string]interface{})
4142+
if c.ComponentConfig != nil {
4143+
result["enable_components"] = c.ComponentConfig.EnableComponents
4144+
}
4145+
if c.ManagedPrometheusConfig != nil {
4146+
result["managed_prometheus"] = flattenManagedPrometheusConfig(c.ManagedPrometheusConfig)
4147+
}
4148+
return []map[string]interface{}{result}
4149+
}
4150+
4151+
func flattenManagedPrometheusConfig(c *container.ManagedPrometheusConfig) []map[string]interface{} {
41154152
return []map[string]interface{}{
41164153
{
4117-
"enable_components": c.ComponentConfig.EnableComponents,
4154+
"enabled": c != nil && c.Enabled,
41184155
},
41194156
}
41204157
}

google-beta/resource_container_cluster_test.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,76 @@ func TestAccContainerCluster_withLoggingConfig(t *testing.T) {
19971997
})
19981998
}
19991999

2000+
func TestAccContainerCluster_withMonitoringConfig(t *testing.T) {
2001+
t.Parallel()
2002+
2003+
clusterName := fmt.Sprintf("tf-test-cluster-%s", randString(t, 10))
2004+
vcrTest(t, resource.TestCase{
2005+
PreCheck: func() { testAccPreCheck(t) },
2006+
Providers: testAccProviders,
2007+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
2008+
Steps: []resource.TestStep{
2009+
{
2010+
Config: testAccContainerCluster_basic(clusterName),
2011+
},
2012+
{
2013+
ResourceName: "google_container_cluster.primary",
2014+
ImportState: true,
2015+
ImportStateVerify: true,
2016+
},
2017+
{
2018+
Config: testAccContainerCluster_withMonitoringConfigEnabled(clusterName),
2019+
},
2020+
{
2021+
ResourceName: "google_container_cluster.primary",
2022+
ImportState: true,
2023+
ImportStateVerify: true,
2024+
},
2025+
{
2026+
Config: testAccContainerCluster_withMonitoringConfigUpdated(clusterName),
2027+
},
2028+
{
2029+
ResourceName: "google_container_cluster.primary",
2030+
ImportState: true,
2031+
ImportStateVerify: true,
2032+
},
2033+
{
2034+
Config: testAccContainerCluster_withMonitoringConfigPrometheusUpdated(clusterName),
2035+
},
2036+
{
2037+
ResourceName: "google_container_cluster.primary",
2038+
ImportState: true,
2039+
ImportStateVerify: true,
2040+
},
2041+
// Back to basic settings to test setting Prometheus on its own
2042+
{
2043+
Config: testAccContainerCluster_basic(clusterName),
2044+
},
2045+
{
2046+
ResourceName: "google_container_cluster.primary",
2047+
ImportState: true,
2048+
ImportStateVerify: true,
2049+
},
2050+
{
2051+
Config: testAccContainerCluster_withMonitoringConfigPrometheusOnly(clusterName),
2052+
},
2053+
{
2054+
ResourceName: "google_container_cluster.primary",
2055+
ImportState: true,
2056+
ImportStateVerify: true,
2057+
},
2058+
{
2059+
Config: testAccContainerCluster_basic(clusterName),
2060+
},
2061+
{
2062+
ResourceName: "google_container_cluster.primary",
2063+
ImportState: true,
2064+
ImportStateVerify: true,
2065+
},
2066+
},
2067+
})
2068+
}
2069+
20002070
func TestAccContainerCluster_withSoleTenantGroup(t *testing.T) {
20012071
t.Parallel()
20022072

@@ -5114,6 +5184,63 @@ resource "google_container_cluster" "primary" {
51145184
`, name)
51155185
}
51165186

5187+
func testAccContainerCluster_withMonitoringConfigEnabled(name string) string {
5188+
return fmt.Sprintf(`
5189+
resource "google_container_cluster" "primary" {
5190+
name = "%s"
5191+
location = "us-central1-a"
5192+
initial_node_count = 1
5193+
monitoring_config {
5194+
enable_components = [ "SYSTEM_COMPONENTS" ]
5195+
}
5196+
}
5197+
`, name)
5198+
}
5199+
5200+
func testAccContainerCluster_withMonitoringConfigUpdated(name string) string {
5201+
return fmt.Sprintf(`
5202+
resource "google_container_cluster" "primary" {
5203+
name = "%s"
5204+
location = "us-central1-a"
5205+
initial_node_count = 1
5206+
monitoring_config {
5207+
enable_components = [ "SYSTEM_COMPONENTS", "WORKLOADS" ]
5208+
}
5209+
}
5210+
`, name)
5211+
}
5212+
5213+
func testAccContainerCluster_withMonitoringConfigPrometheusUpdated(name string) string {
5214+
return fmt.Sprintf(`
5215+
resource "google_container_cluster" "primary" {
5216+
name = "%s"
5217+
location = "us-central1-a"
5218+
initial_node_count = 1
5219+
monitoring_config {
5220+
enable_components = [ "SYSTEM_COMPONENTS", "WORKLOADS" ]
5221+
managed_prometheus {
5222+
enabled = true
5223+
}
5224+
}
5225+
}
5226+
`, name)
5227+
}
5228+
5229+
func testAccContainerCluster_withMonitoringConfigPrometheusOnly(name string) string {
5230+
return fmt.Sprintf(`
5231+
resource "google_container_cluster" "primary" {
5232+
name = "%s"
5233+
location = "us-central1-a"
5234+
initial_node_count = 1
5235+
monitoring_config {
5236+
managed_prometheus {
5237+
enabled = true
5238+
}
5239+
}
5240+
}
5241+
`, name)
5242+
}
5243+
51175244
func testAccContainerCluster_withSoleTenantGroup(name string) string {
51185245
return fmt.Sprintf(`
51195246
resource "google_compute_node_template" "soletenant-tmpl" {

website/docs/r/container_cluster.html.markdown

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,13 @@ as "Intel Haswell" or "Intel Sandy Bridge".
489489

490490
<a name="nested_monitoring_config"></a>The `monitoring_config` block supports:
491491

492-
* `enable_components` - (Required) The GKE components exposing logs. `SYSTEM_COMPONENTS` and in beta provider, both `SYSTEM_COMPONENTS` and `WORKLOADS` are supported.
492+
* `enable_components` - (Optional) The GKE components exposing metrics. `SYSTEM_COMPONENTS` and in beta provider, both `SYSTEM_COMPONENTS` and `WORKLOADS` are supported. (`WORKLOADS` is deprecated and removed in GKE 1.24.)
493+
494+
* `managed_prometheus` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) Configuration for Managed Service for Prometheus. Structure is [documented below](#nested_managed_prometheus).
495+
496+
<a name="nested_managed_prometheus"></a>The `managed_prometheus` block supports:
497+
498+
* `enabled` - (Required) Whether or not the managed collection is enabled.
493499

494500
<a name="nested_maintenance_policy"></a>The `maintenance_policy` block supports:
495501
* `daily_maintenance_window` - (Optional) structure documented below.

0 commit comments

Comments
 (0)