Skip to content
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 @@ -5966,13 +5966,17 @@ func expandClusterAutoscaling(configured interface{}, d *schema.ResourceData) *c
Enabled: defaultCCEnabled.(bool),
}
}
var defaults *container.AutoprovisioningNodePoolDefaults
if shouldInclude(d, "cluster_autoscaling.0.auto_provisioning_defaults") {
defaults = expandAutoProvisioningDefaults(config["auto_provisioning_defaults"], d)
}
return &container.ClusterAutoscaling{
EnableNodeAutoprovisioning: config["enabled"].(bool),
ResourceLimits: resourceLimits,
DefaultComputeClassConfig: defaultCCConfig,
AutoscalingProfile: config["autoscaling_profile"].(string),
AutoprovisioningNodePoolDefaults: expandAutoProvisioningDefaults(config["auto_provisioning_defaults"], d),
AutoprovisioningLocations: tpgresource.ConvertStringArr(config["auto_provisioning_locations"].([]interface{})),
EnableNodeAutoprovisioning: config["enabled"].(bool),
ResourceLimits: resourceLimits,
DefaultComputeClassConfig: defaultCCConfig,
AutoscalingProfile: config["autoscaling_profile"].(string),
AutoprovisioningNodePoolDefaults: defaults,
AutoprovisioningLocations: tpgresource.ConvertStringArr(config["auto_provisioning_locations"].([]interface{})),
}
}

Expand All @@ -5983,15 +5987,23 @@ func expandAutoProvisioningDefaults(configured interface{}, d *schema.ResourceDa
}
config := l[0].(map[string]interface{})

var management *container.NodeManagement
if shouldInclude(d, "cluster_autoscaling.0.auto_provisioning_defaults.0.management") {
management = expandManagement(config["management"])
}
var upgradeSettings *container.UpgradeSettings
if shouldInclude(d, "cluster_autoscaling.0.auto_provisioning_defaults.0.upgrade_settings") {
upgradeSettings = expandUpgradeSettings(config["upgrade_settings"], d)
}
npd := &container.AutoprovisioningNodePoolDefaults{
OauthScopes: tpgresource.ConvertStringArr(config["oauth_scopes"].([]interface{})),
ServiceAccount: config["service_account"].(string),
DiskSizeGb: int64(config["disk_size"].(int)),
DiskType: config["disk_type"].(string),
ImageType: config["image_type"].(string),
BootDiskKmsKey: config["boot_disk_kms_key"].(string),
Management: expandManagement(config["management"]),
UpgradeSettings: expandUpgradeSettings(config["upgrade_settings"]),
OauthScopes: tpgresource.ConvertStringArr(config["oauth_scopes"].([]interface{})),
ServiceAccount: config["service_account"].(string),
DiskSizeGb: int64(config["disk_size"].(int)),
DiskType: config["disk_type"].(string),
ImageType: config["image_type"].(string),
BootDiskKmsKey: config["boot_disk_kms_key"].(string),
Management: management,
UpgradeSettings: upgradeSettings,
}

if v, ok := config["shielded_instance_config"]; ok && len(v.([]interface{})) > 0 {
Expand All @@ -6012,18 +6024,22 @@ func expandAutoProvisioningDefaults(configured interface{}, d *schema.ResourceDa
return npd
}

func expandUpgradeSettings(configured interface{}) *container.UpgradeSettings {
func expandUpgradeSettings(configured interface{}, d *schema.ResourceData) *container.UpgradeSettings {
l, ok := configured.([]interface{})
if !ok || l == nil || len(l) == 0 || l[0] == nil {
return &container.UpgradeSettings{}
}
config := l[0].(map[string]interface{})

var blueGreenSettings *container.BlueGreenSettings
if shouldInclude(d, "cluster_autoscaling.0.auto_provisioning_defaults.0.upgrade_settings.0.blue_green_settings") {
blueGreenSettings = expandBlueGreenSettings(config["blue_green_settings"])
}
upgradeSettings := &container.UpgradeSettings{
MaxSurge: int64(config["max_surge"].(int)),
MaxUnavailable: int64(config["max_unavailable"].(int)),
Strategy: config["strategy"].(string),
BlueGreenSettings: expandBlueGreenSettings(config["blue_green_settings"]),
BlueGreenSettings: blueGreenSettings,
}

return upgradeSettings
Expand Down Expand Up @@ -6091,6 +6107,10 @@ func expandUpgradeOptions(configured interface{}) *container.AutoUpgradeOptions
return upgradeOptions
}

func shouldInclude(d *schema.ResourceData, key string) bool {
return d.IsNewResource() || d.HasChange(key)
}

func expandAuthenticatorGroupsConfig(configured interface{}) *container.AuthenticatorGroupsConfig {
l := configured.([]interface{})
if len(l) == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4170,10 +4170,14 @@ func TestAccContainerCluster_nodeAutoprovisioning(t *testing.T) {
),
},
{
ResourceName: "google_container_cluster.with_autoprovisioning",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version", "deletion_protection"},
ResourceName: "google_container_cluster.with_autoprovisioning",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"min_master_version",
"deletion_protection",
"node_pool", // cluster_autoscaling (node auto-provisioning) creates new node pools automatically
},
},
{
Config: testAccContainerCluster_autoprovisioning(clusterName, networkName, subnetworkName, true, false, true),
Expand All @@ -4187,10 +4191,14 @@ func TestAccContainerCluster_nodeAutoprovisioning(t *testing.T) {
),
},
{
ResourceName: "google_container_cluster.with_autoprovisioning",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version", "deletion_protection"},
ResourceName: "google_container_cluster.with_autoprovisioning",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"min_master_version",
"deletion_protection",
"node_pool",
},
},
{
Config: testAccContainerCluster_autoprovisioning(clusterName, networkName, subnetworkName, false, false, false),
Expand All @@ -4205,10 +4213,14 @@ func TestAccContainerCluster_nodeAutoprovisioning(t *testing.T) {
),
},
{
ResourceName: "google_container_cluster.with_autoprovisioning",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version", "deletion_protection"},
ResourceName: "google_container_cluster.with_autoprovisioning",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"min_master_version",
"deletion_protection",
"node_pool",
},
},
},
})
Expand Down Expand Up @@ -4238,7 +4250,7 @@ func TestAccContainerCluster_nodeAutoprovisioningDefaults(t *testing.T) {
ResourceName: "google_container_cluster.with_autoprovisioning",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version", "deletion_protection"},
ImportStateVerifyIgnore: []string{"min_master_version", "deletion_protection", "node_pool"},
},
{
Config: testAccContainerCluster_autoprovisioningDefaults(clusterName, networkName, subnetworkName, true),
Expand All @@ -4252,7 +4264,7 @@ func TestAccContainerCluster_nodeAutoprovisioningDefaults(t *testing.T) {
ResourceName: "google_container_cluster.with_autoprovisioning",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version", "deletion_protection"},
ImportStateVerifyIgnore: []string{"min_master_version", "deletion_protection", "node_pool"},
},
{
Config: testAccContainerCluster_autoprovisioningDefaultsMinCpuPlatform(clusterName, networkName, subnetworkName, !includeMinCpuPlatform),
Expand All @@ -4261,7 +4273,7 @@ func TestAccContainerCluster_nodeAutoprovisioningDefaults(t *testing.T) {
ResourceName: "google_container_cluster.with_autoprovisioning",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version", "deletion_protection"},
ImportStateVerifyIgnore: []string{"min_master_version", "deletion_protection", "node_pool"},
},
},
})
Expand All @@ -4286,7 +4298,7 @@ func TestAccContainerCluster_autoprovisioningDefaultsUpgradeSettings(t *testing.
ResourceName: "google_container_cluster.with_autoprovisioning_upgrade_settings",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
ImportStateVerifyIgnore: []string{"deletion_protection", "node_pool"},
},
{
Config: testAccContainerCluster_autoprovisioningDefaultsUpgradeSettings(clusterName, networkName, subnetworkName, 2, 1, "BLUE_GREEN"),
Expand All @@ -4299,7 +4311,7 @@ func TestAccContainerCluster_autoprovisioningDefaultsUpgradeSettings(t *testing.
ResourceName: "google_container_cluster.with_autoprovisioning_upgrade_settings",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
ImportStateVerifyIgnore: []string{"deletion_protection", "node_pool"},
},
},
})
Expand Down Expand Up @@ -4328,7 +4340,7 @@ func TestAccContainerCluster_nodeAutoprovisioningNetworkTags(t *testing.T) {
ResourceName: "google_container_cluster.with_autoprovisioning",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version", "deletion_protection"},
ImportStateVerifyIgnore: []string{"min_master_version", "deletion_protection", "node_pool"},
},
},
})
Expand All @@ -4342,12 +4354,12 @@ func TestAccContainerCluster_withDefaultComputeClassEnabled(t *testing.T) {
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_withDefaultComputeClassEnabled(clusterName, networkName, subnetworkName, true),
Config: testAccContainerCluster_withDefaultComputeClassEnabled(clusterName, networkName, subnetworkName, true, false),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_container_cluster.primary", "cluster_autoscaling.0.default_compute_class_enabled", "true"),
),
Expand All @@ -4356,10 +4368,10 @@ func TestAccContainerCluster_withDefaultComputeClassEnabled(t *testing.T) {
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
ImportStateVerifyIgnore: []string{"deletion_protection", "node_pool"},
},
{
Config: testAccContainerCluster_withDefaultComputeClassEnabled(clusterName, networkName, subnetworkName, false),
Config: testAccContainerCluster_withDefaultComputeClassEnabled(clusterName, networkName, subnetworkName, false, false),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_container_cluster.primary", "cluster_autoscaling.0.default_compute_class_enabled", "false"),
),
Expand All @@ -4368,25 +4380,82 @@ func TestAccContainerCluster_withDefaultComputeClassEnabled(t *testing.T) {
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
ImportStateVerifyIgnore: []string{"deletion_protection", "node_pool"},
},
},
})
}

func testAccContainerCluster_withDefaultComputeClassEnabled(clusterName, networkName, subnetworkName string, enabled bool) string {
return fmt.Sprintf(`
func TestAccContainerCluster_withAutopilotDefaultComputeClassEnabled(t *testing.T) {
t.Parallel()

clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_withDefaultComputeClassEnabled(clusterName, networkName, subnetworkName, true, true),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_container_cluster.primary", "cluster_autoscaling.0.default_compute_class_enabled", "true"),
),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "node_pool"},
},
{
Config: testAccContainerCluster_withDefaultComputeClassEnabled(clusterName, networkName, subnetworkName, false, true),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_container_cluster.primary", "cluster_autoscaling.0.default_compute_class_enabled", "false"),
),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "node_pool"},
},
},
})
}

func testAccContainerCluster_withDefaultComputeClassEnabled(clusterName, networkName, subnetworkName string, enabled, autopilot bool) string {
var location string
if autopilot {
location = "us-central1"
} else {
location = "us-central1-a"
}
res := fmt.Sprintf(`
resource "google_container_cluster" "primary" {
name = "%s"
location = "us-central1-a"
location = "%s"
initial_node_count = 1
network = "%s"
subnetwork = "%s"
deletion_protection = false
`, clusterName, location, networkName, subnetworkName)

if autopilot {
res += `
enable_autopilot = true
`
}

res += fmt.Sprintf(`
cluster_autoscaling {
enabled = true
default_compute_class_enabled = %t
`, enabled)
if !autopilot {
res += `
enabled = true
resource_limits {
resource_type = "cpu"
minimum = 1
Expand All @@ -4397,9 +4466,12 @@ resource "google_container_cluster" "primary" {
minimum = 10
maximum = 100
}
`
}
res += `
}
}
`, clusterName, networkName, subnetworkName, enabled)
}`
return res
}


Expand Down
Loading