Skip to content

Commit 3b33696

Browse files
authored
Allow upgrading GKE versions and provide better error message handling (hashicorp#291)
* Better error handling for GKE operations * Handle GKE version upgrades * clarify log message
1 parent a4335a3 commit 3b33696

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

google/container_operation.go

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func (w *ContainerOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
3333
return nil, "", err
3434
}
3535

36+
if resp.StatusMessage != "" {
37+
return resp, resp.Status, fmt.Errorf(resp.StatusMessage)
38+
}
39+
3640
log.Printf("[DEBUG] Progress of operation %q: %q", w.Op.Name, resp.Status)
3741

3842
return resp, resp.Status, err

google/resource_container_cluster.go

+25-3
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,10 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
552552
if d.HasChange("node_version") {
553553
desiredNodeVersion := d.Get("node_version").(string)
554554

555+
// The master must be updated before the nodes
555556
req := &container.UpdateClusterRequest{
556557
Update: &container.ClusterUpdate{
557-
DesiredNodeVersion: desiredNodeVersion,
558+
DesiredMasterVersion: desiredNodeVersion,
558559
},
559560
}
560561
op, err := config.clientContainer.Projects.Zones.Clusters.Update(
@@ -564,12 +565,33 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
564565
}
565566

566567
// Wait until it's updated
567-
waitErr := containerOperationWait(config, op, project, zoneName, "updating GKE cluster version", timeoutInMinutes, 2)
568+
waitErr := containerOperationWait(config, op, project, zoneName, "updating GKE master version", timeoutInMinutes, 2)
569+
if waitErr != nil {
570+
return waitErr
571+
}
572+
573+
log.Printf("[INFO] GKE cluster %s: master has been updated to %s", d.Id(),
574+
desiredNodeVersion)
575+
576+
// Update the nodes
577+
req = &container.UpdateClusterRequest{
578+
Update: &container.ClusterUpdate{
579+
DesiredNodeVersion: desiredNodeVersion,
580+
},
581+
}
582+
op, err = config.clientContainer.Projects.Zones.Clusters.Update(
583+
project, zoneName, clusterName, req).Do()
584+
if err != nil {
585+
return err
586+
}
587+
588+
// Wait until it's updated
589+
waitErr = containerOperationWait(config, op, project, zoneName, "updating GKE node version", timeoutInMinutes, 2)
568590
if waitErr != nil {
569591
return waitErr
570592
}
571593

572-
log.Printf("[INFO] GKE cluster %s has been updated to %s", d.Id(),
594+
log.Printf("[INFO] GKE cluster %s: nodes have been updated to %s", d.Id(),
573595
desiredNodeVersion)
574596

575597
d.SetPartial("node_version")

google/resource_container_cluster_test.go

+52-3
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,41 @@ func TestAccContainerCluster_withLegacyAbac(t *testing.T) {
121121
}
122122

123123
func TestAccContainerCluster_withVersion(t *testing.T) {
124+
clusterName := fmt.Sprintf("cluster-test-%s", acctest.RandString(10))
125+
124126
resource.Test(t, resource.TestCase{
125127
PreCheck: func() { testAccPreCheck(t) },
126128
Providers: testAccProviders,
127129
CheckDestroy: testAccCheckContainerClusterDestroy,
128130
Steps: []resource.TestStep{
129131
{
130-
Config: testAccContainerCluster_withVersion,
132+
Config: testAccContainerCluster_withVersion(clusterName),
133+
Check: resource.ComposeTestCheckFunc(
134+
testAccCheckContainerCluster(
135+
"google_container_cluster.with_version"),
136+
),
137+
},
138+
},
139+
})
140+
}
141+
142+
func TestAccContainerCluster_updateVersion(t *testing.T) {
143+
clusterName := fmt.Sprintf("cluster-test-%s", acctest.RandString(10))
144+
145+
resource.Test(t, resource.TestCase{
146+
PreCheck: func() { testAccPreCheck(t) },
147+
Providers: testAccProviders,
148+
CheckDestroy: testAccCheckContainerClusterDestroy,
149+
Steps: []resource.TestStep{
150+
{
151+
Config: testAccContainerCluster_withLowerVersion(clusterName),
152+
Check: resource.ComposeTestCheckFunc(
153+
testAccCheckContainerCluster(
154+
"google_container_cluster.with_version"),
155+
),
156+
},
157+
{
158+
Config: testAccContainerCluster_withVersion(clusterName),
131159
Check: resource.ComposeTestCheckFunc(
132160
testAccCheckContainerCluster(
133161
"google_container_cluster.with_version"),
@@ -586,7 +614,8 @@ resource "google_container_cluster" "with_legacy_abac" {
586614
}`, clusterName)
587615
}
588616

589-
var testAccContainerCluster_withVersion = fmt.Sprintf(`
617+
func testAccContainerCluster_withVersion(clusterName string) string {
618+
return fmt.Sprintf(`
590619
data "google_container_engine_versions" "central1a" {
591620
zone = "us-central1-a"
592621
}
@@ -601,7 +630,27 @@ resource "google_container_cluster" "with_version" {
601630
username = "mr.yoda"
602631
password = "adoy.rm"
603632
}
604-
}`, acctest.RandString(10))
633+
}`, clusterName)
634+
}
635+
636+
func testAccContainerCluster_withLowerVersion(clusterName string) string {
637+
return fmt.Sprintf(`
638+
data "google_container_engine_versions" "central1a" {
639+
zone = "us-central1-a"
640+
}
641+
642+
resource "google_container_cluster" "with_version" {
643+
name = "cluster-test-%s"
644+
zone = "us-central1-a"
645+
node_version = "${data.google_container_engine_versions.central1a.valid_master_versions.1}"
646+
initial_node_count = 1
647+
648+
master_auth {
649+
username = "mr.yoda"
650+
password = "adoy.rm"
651+
}
652+
}`, clusterName)
653+
}
605654

606655
var testAccContainerCluster_withNodeConfig = fmt.Sprintf(`
607656
resource "google_container_cluster" "with_node_config" {

0 commit comments

Comments
 (0)