Skip to content

Commit

Permalink
Add a logging_variant field to serve as a node pool default for GKE…
Browse files Browse the repository at this point in the history
… clusters.

This PR partly implements the feature request from [Add GKE logging variant field for increasing log agent throughput #12667](hashicorp/terraform-provider-google#12667). By adding a logging_variant field within the node_pool_defaults, GKE users will be able to select a cluster-level default value for the logging agent of the node pools in a cluster. For example, by specifying
```terraform
resource "google_container_cluster" "with_logging_variant_node_pool_default" {
  name               = "example-cluster"
  location           = "us-central1-f"
  initial_node_count = 1

  node_pool_defaults {
    node_config_defaults {
      logging_variant = "MAX_THROUGHPUT"
    }
  }
}
```
every node pool in the cluster will have the max throughput logging agent configured by default (see the [GKE docs](https://cloud.google.com/stackdriver/docs/solutions/gke/managing-logs#high_throughput_for_all_nodes_in_a_cluster) for more information).
  • Loading branch information
giuliano-sider committed Oct 26, 2022
1 parent 00669b0 commit 9d20223
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func clusterSchemaNodeConfig() *schema.Schema {
return nodeConfigSch
}

<% unless version == 'ga' -%>
// Defines default nodel pool settings for the entire cluster. These settings are
// overridden if specified on the specific NodePool object.
func clusterSchemaNodePoolDefaults() *schema.Schema {
Expand All @@ -111,16 +110,24 @@ func clusterSchemaNodePoolDefaults() *schema.Schema {
Description: `Subset of NodeConfig message that has defaults.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"gcfs_config": schemaGcfsConfig(false),
Schema: map[string]*schema.Schema{
<% unless version == 'ga' -%>
"gcfs_config": schemaGcfsConfig(false),
<% end -%>
"logging_variant": {
Type: schema.TypeString,
Optional: true,
Description: `Type of logging agent that is used as the default value for node pools in the cluster. Valid values include DEFAULT and MAX_THROUGHPUT.`,
Default: "DEFAULT",
ValidateFunc: validation.StringInSlice([]string{"DEFAULT", "MAX_THROUGHPUT"}, false),
},
},
},
},
},
},
}
}
<% end -%>

func rfc5545RecurrenceDiffSuppress(k, o, n string, d *schema.ResourceData) bool {
// This diff gets applied in the cloud console if you specify
Expand Down Expand Up @@ -1082,9 +1089,9 @@ func resourceContainerCluster() *schema.Resource {
ConflictsWith: []string{"enable_autopilot"},
},

<% unless version == "ga" -%>
"node_pool_defaults": clusterSchemaNodePoolDefaults(),

<% unless version == "ga" -%>
"node_pool_auto_config": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -1842,11 +1849,9 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
cluster.NodeConfig = expandNodeConfig([]interface{}{})
}

<% unless version == 'ga' -%>
if v, ok := d.GetOk("node_pool_defaults"); ok {
cluster.NodePoolDefaults = expandNodePoolDefaults(v)
}
<% end -%>

if v, ok := d.GetOk("node_config"); ok {
cluster.NodeConfig = expandNodeConfig(v)
Expand Down Expand Up @@ -2285,11 +2290,9 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
}
<% end -%>

<% unless version == 'ga' -%>
if err := d.Set("node_pool_defaults", flattenNodePoolDefaults(cluster.NodePoolDefaults)); err != nil {
return err
}
<% end -%>

return nil
}
Expand Down Expand Up @@ -3250,6 +3253,29 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
log.Printf("[INFO] GKE cluster %s resource usage export config has been updated", d.Id())
}

if d.HasChange("node_pool_defaults") && d.HasChange("node_pool_defaults.0.node_config_defaults.0.logging_variant") {
if v, ok := d.GetOk("node_pool_defaults.0.node_config_defaults.0.logging_variant"); ok {
loggingVariant := v.(string)
req := &container.UpdateClusterRequest{
Update: &container.ClusterUpdate{
DesiredNodePoolLoggingConfig: &container.NodePoolLoggingConfig{
VariantConfig: &container.LoggingVariantConfig{
Variant: loggingVariant,
},
},
},
}

updateF := updateFunc(req, "updating GKE cluster desired node pool logging configuration defaults.")
// Call update serially.
if err := lockedCall(lockKey, updateF); err != nil {
return err
}

log.Printf("[INFO] GKE cluster %s node pool logging configuration defaults have been updated", d.Id())
}
}

<% unless version == 'ga' -%>
if d.HasChange("node_pool_defaults") && d.HasChange("node_pool_defaults.0.node_config_defaults.0.gcfs_config") {
if v, ok := d.GetOk("node_pool_defaults.0.node_config_defaults.0.gcfs_config"); ok {
Expand Down Expand Up @@ -4235,7 +4261,6 @@ func expandContainerClusterAuthenticatorGroupsConfig(configured interface{}) *co
}
}

<% unless version == 'ga' -%>
func expandNodePoolDefaults(configured interface{}) *container.NodePoolDefaults {
l, ok := configured.([]interface{})
if !ok || l == nil || len(l) == 0 || l[0] == nil {
Expand All @@ -4255,13 +4280,12 @@ func flattenNodePoolDefaults(c *container.NodePoolDefaults) []map[string]interfa
}

result := make(map[string]interface{})
if c.NodeConfigDefaults != nil && c.NodeConfigDefaults.GcfsConfig != nil {
result["node_config_defaults"] = flattenNodeConfigDefaults(c.NodeConfigDefaults)
if c.NodeConfigDefaults != nil {
result["node_config_defaults"] = flattenNodeConfigDefaults(c.NodeConfigDefaults)
}

return []map[string]interface{}{result}
}
<% end -%>

<% unless version == 'ga' -%>
func expandNodePoolAutoConfig(configured interface{}) *container.NodePoolAutoConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,112 @@ func TestAccContainerCluster_withNodeConfig(t *testing.T) {
})
}

func TestAccContainerCluster_withNoSpecifiedLoggingVariant(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),
Check: resource.TestCheckResourceAttr("google_container_cluster.primary",
"node_pool_defaults.0.node_config_defaults.0.logging_variant",
"DEFAULT"),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccContainerCluster_withDefaultLoggingVariant(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_withLoggingVariantNodePoolDefault(clusterName, "DEFAULT"),
Check: resource.TestCheckResourceAttr("google_container_cluster.with_logging_variant_node_pool_default",
"node_pool_defaults.0.node_config_defaults.0.logging_variant",
"DEFAULT"),
},
{
ResourceName: "google_container_cluster.with_logging_variant_node_pool_default",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccContainerCluster_withMaxThroughputLoggingVariant(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_withLoggingVariantNodePoolDefault(clusterName, "MAX_THROUGHPUT"),
Check: resource.TestCheckResourceAttr("google_container_cluster.with_logging_variant_node_pool_default",
"node_pool_defaults.0.node_config_defaults.0.logging_variant",
"MAX_THROUGHPUT"),
},
{
ResourceName: "google_container_cluster.with_logging_variant_node_pool_default",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccContainerCluster_withLoggingVariantUpdates(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_withLoggingVariantNodePoolDefault(clusterName, "DEFAULT"),
},
{
Config: testAccContainerCluster_withLoggingVariantNodePoolDefault(clusterName, "MAX_THROUGHPUT"),
Check: resource.TestCheckResourceAttr("google_container_cluster.with_logging_variant_node_pool_default",
"node_pool_defaults.0.node_config_defaults.0.logging_variant",
"MAX_THROUGHPUT"),
},
{
ResourceName: "google_container_cluster.with_logging_variant_node_pool_default",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccContainerCluster_withLoggingVariantNodePoolDefault(clusterName, "DEFAULT"),
Check: resource.TestCheckResourceAttr("google_container_cluster.with_logging_variant_node_pool_default",
"node_pool_defaults.0.node_config_defaults.0.logging_variant",
"DEFAULT"),
},
{
ResourceName: "google_container_cluster.with_logging_variant_node_pool_default",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

<% unless version == 'ga' -%>
func TestAccContainerCluster_withNodePoolDefaults(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -4214,6 +4320,22 @@ resource "google_container_cluster" "with_node_config" {
`, clusterName)
}

func testAccContainerCluster_withLoggingVariantNodePoolDefault(clusterName, loggingVariant string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_logging_variant_node_pool_default" {
name = "%s"
location = "us-central1-f"
initial_node_count = 1

node_pool_defaults {
node_config_defaults {
logging_variant = "%s"
}
}
}
`, clusterName, loggingVariant)
}

<% unless version == 'ga' -%>
func testAccContainerCluster_withNodePoolDefaults(clusterName, enabled string) string {
return fmt.Sprintf(`
Expand Down
26 changes: 18 additions & 8 deletions mmv1/third_party/terraform/utils/node_config.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -471,24 +471,31 @@ func schemaNodeConfig() *schema.Schema {
}
}

<% unless version == "ga" -%>
func expandNodeConfigDefaults(configured interface{}) *container.NodeConfigDefaults {
configs := configured.([]interface{})
if len(configs) == 0 || configs[0] == nil {
return nil
}
config := configs[0].(map[string]interface{})

nodeConfigDefaults := &container.NodeConfigDefaults{}
nodeConfigDefaults := &container.NodeConfigDefaults{}
if variant, ok := config["logging_variant"]; ok {
nodeConfigDefaults.LoggingConfig = &container.NodePoolLoggingConfig{
VariantConfig: &container.LoggingVariantConfig{
Variant: variant.(string),
},
}
}
<% unless version == "ga" -%>
if v, ok := config["gcfs_config"]; ok && len(v.([]interface{})) > 0 {
gcfsConfig := v.([]interface{})[0].(map[string]interface{})
nodeConfigDefaults.GcfsConfig = &container.GcfsConfig{
Enabled: gcfsConfig["enabled"].(bool),
}
}
<% end -%>
return nodeConfigDefaults
}
<% end -%>

func expandNodeConfig(v interface{}) *container.NodeConfig {
nodeConfigs := v.([]interface{})
Expand Down Expand Up @@ -762,20 +769,23 @@ func expandLinuxNodeConfig(v interface{}) *container.LinuxNodeConfig {

<% end -%>

<% unless version == 'ga' -%>
func flattenNodeConfigDefaults(c *container.NodeConfigDefaults) []map[string]interface{} {
result := make([]map[string]interface{}, 0, 1)

if c == nil {
return result
}

result = append(result, map[string]interface{}{
"gcfs_config": flattenGcfsConfig(c.GcfsConfig),
})
result = append(result, map[string]interface{}{})
if c.LoggingConfig != nil && c.LoggingConfig.VariantConfig != nil && c.LoggingConfig.VariantConfig.Variant != "" {
result[0]["logging_variant"] = c.LoggingConfig.VariantConfig.Variant
}

<% unless version == 'ga' -%>
result[0]["gcfs_config"] = flattenGcfsConfig(c.GcfsConfig)
<% end -%>
return result
}
<% end -%>

func flattenNodeConfig(c *container.NodeConfig) []map[string]interface{} {
config := make([]map[string]interface{}, 0, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,8 @@ node_pool_auto_config {

The `node_config_defaults` block supports:

* `logging_variant` (Optional) The type of logging agent that is used as the default value for node pools in the cluster. Valid values include DEFAULT and MAX_THROUGHPUT.

* `gcfs_config` (Optional) The default Google Container Filesystem (GCFS) configuration at the cluster level. e.g. enable [image streaming](https://cloud.google.com/kubernetes-engine/docs/how-to/image-streaming) across all the node pools within the cluster. Structure is [documented below](#nested_gcfs_config).

<a name="nested_notification_config"></a>The `notification_config` block supports:
Expand Down

0 comments on commit 9d20223

Please sign in to comment.