Skip to content

Commit

Permalink
Fix for permadiff in container cluster caused by beta resource. (#2488)
Browse files Browse the repository at this point in the history
  • Loading branch information
modular-magician authored and nat-henderson committed Nov 19, 2018
1 parent e28676c commit 7ca9028
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 113 deletions.
79 changes: 3 additions & 76 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,9 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
d.Set("monitoring_service", cluster.MonitoringService)
d.Set("network", cluster.NetworkConfig.Network)
d.Set("subnetwork", cluster.NetworkConfig.Subnetwork)
if err := d.Set("cluster_autoscaling", nil); err != nil {
return err
}
if err := d.Set("node_config", flattenNodeConfig(cluster.NodeConfig)); err != nil {
return err
}
Expand Down Expand Up @@ -1453,63 +1456,6 @@ func expandMaintenancePolicy(configured interface{}) *containerBeta.MaintenanceP
}
}

func expandClusterAutoscaling(configured interface{}, d *schema.ResourceData) *containerBeta.ClusterAutoscaling {
l, ok := configured.([]interface{})
if !ok || l == nil || len(l) == 0 || l[0] == nil {
// Before master version 1.11.2, we must send 'nil' values if autoscaling isn't
// turned on - the cluster will return an error even if we're setting
// EnableNodeAutoprovisioning to false.
cmv, err := version.NewVersion(d.Get("master_version").(string))
if err != nil {
log.Printf("[DEBUG] Could not parse master_version into version (%q), trying min_master_version.", d.Get("master_version").(string))
cmv, err = version.NewVersion(d.Get("min_master_version").(string))
if err != nil {
log.Printf("[DEBUG] Could not parse min_master_version into version (%q), assuming we are not already using cluster autoscaling.", d.Get("min_master_version").(string))
// This deserves a little explanation. The only reason we would ever want to send
// `EnableNodeAutoprovisioning: false` is because we think we might need to
// disable it (e.g. it is already enabled). Otherwise, there is no difference
// between sending `nil` and sending `EnableNodeAutoprovisioning: false`.
// The only circumstance in which neither master_version nor min_master_version
// can be parsed into version objects would be if the user has not set either one,
// and we have not yet had a `read` call. e.g. first-time creates, and possibly
// some circumstance related to import. It is probably safe to assume that
// we are not going to be changing cluster autoscaling from on to off in those
// circumstances. Therefore, if we don't know what version we're running, and
// the user has not requested cluster autoscaling, we'll fail "safe" and not touch
// it.
cmv, _ = version.NewVersion("0.0.0")
}
}
dmv, _ := version.NewVersion("1.11.2")
if cmv.LessThan(dmv) {
return nil
} else {
return &containerBeta.ClusterAutoscaling{
EnableNodeAutoprovisioning: false,
ForceSendFields: []string{"EnableNodeAutoprovisioning"},
}
}
}
r := &containerBeta.ClusterAutoscaling{}
if config, ok := l[0].(map[string]interface{}); ok {
r.EnableNodeAutoprovisioning = config["enabled"].(bool)
if limits, ok := config["resource_limits"]; ok {
if lmts, ok := limits.([]interface{}); ok {
for _, v := range lmts {
limit := v.(map[string]interface{})
r.ResourceLimits = append(r.ResourceLimits, &containerBeta.ResourceLimit{
ResourceType: limit["resource_type"].(string),
// Here we're relying on *not* setting ForceSendFields for 0-values.
Minimum: int64(limit["minimum"].(int)),
Maximum: int64(limit["maximum"].(int)),
})
}
}
}
}
return r
}

func expandMasterAuth(configured interface{}) *containerBeta.MasterAuth {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down Expand Up @@ -1732,25 +1678,6 @@ func flattenMasterAuth(ma *containerBeta.MasterAuth) []map[string]interface{} {
return masterAuth
}

func flattenClusterAutoscaling(a *containerBeta.ClusterAutoscaling) []map[string]interface{} {
r := make(map[string]interface{})
if a == nil || !a.EnableNodeAutoprovisioning {
r["enabled"] = false
} else {
resourceLimits := make([]interface{}, 0, len(a.ResourceLimits))
for _, rl := range a.ResourceLimits {
resourceLimits = append(resourceLimits, map[string]interface{}{
"resource_type": rl.ResourceType,
"minimum": rl.Minimum,
"maximum": rl.Maximum,
})
}
r["resource_limits"] = resourceLimits
r["enabled"] = true
}
return []map[string]interface{}{r}
}

func flattenMasterAuthorizedNetworksConfig(c *containerBeta.MasterAuthorizedNetworksConfig) []map[string]interface{} {
if c == nil {
return nil
Expand Down
37 changes: 0 additions & 37 deletions google/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1995,43 +1995,6 @@ resource "google_container_cluster" "with_node_pool" {
}`, cluster, nodePool)
}

func testAccContainerCluster_autoprovisioning(cluster string, autoprovisioning bool) string {
config := fmt.Sprintf(`
data "google_container_engine_versions" "central1a" {
zone = "us-central1-a"
}
resource "google_container_cluster" "with_autoprovisioning" {
name = "%s"
zone = "us-central1-a"
min_master_version = "${data.google_container_engine_versions.central1a.latest_master_version}"
node_version = "${data.google_container_engine_versions.central1a.latest_node_version}"
initial_node_count = 3
`, cluster)
if autoprovisioning {
config += `
cluster_autoscaling {
enabled = true
resource_limits {
resource_type = "cpu"
maximum = 2
}
resource_limits {
resource_type = "memory"
maximum = 2048
}
}`
} else {
config += `
cluster_autoscaling {
enabled = false
}`
}
config += `
}`
return config
}

func testAccContainerCluster_withNodePoolAutoscaling(cluster, np string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_node_pool" {
Expand Down

0 comments on commit 7ca9028

Please sign in to comment.