Skip to content

Commit 6558224

Browse files
authored
Merge pull request hashicorp#11018 from unguiculus/feature/additional_zones
provider/google: [GKE] Allow additional zones to be configured
2 parents 006e9b1 + 69316a8 commit 6558224

3 files changed

Lines changed: 96 additions & 1 deletion

File tree

builtin/providers/google/resource_container_cluster.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ func resourceContainerCluster() *schema.Resource {
9292
ForceNew: true,
9393
},
9494

95+
"additional_zones": &schema.Schema{
96+
Type: schema.TypeList,
97+
Optional: true,
98+
ForceNew: true,
99+
Elem: &schema.Schema{Type: schema.TypeString},
100+
},
101+
95102
"cluster_ipv4_cidr": &schema.Schema{
96103
Type: schema.TypeString,
97104
Optional: true,
@@ -282,6 +289,24 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
282289
cluster.InitialClusterVersion = v.(string)
283290
}
284291

292+
if v, ok := d.GetOk("additional_zones"); ok {
293+
locationsList := v.([]interface{})
294+
locations := []string{}
295+
zoneInLocations := false
296+
for _, v := range locationsList {
297+
location := v.(string)
298+
locations = append(locations, location)
299+
if location == zoneName {
300+
zoneInLocations = true
301+
}
302+
}
303+
if !zoneInLocations {
304+
// zone must be in locations if specified separately
305+
locations = append(locations, zoneName)
306+
}
307+
cluster.Locations = locations
308+
}
309+
285310
if v, ok := d.GetOk("cluster_ipv4_cidr"); ok {
286311
cluster.ClusterIpv4Cidr = v.(string)
287312
}
@@ -419,6 +444,7 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
419444

420445
d.Set("name", cluster.Name)
421446
d.Set("zone", cluster.Zone)
447+
d.Set("additional_zones", cluster.Locations)
422448
d.Set("endpoint", cluster.Endpoint)
423449

424450
masterAuth := []map[string]interface{}{

builtin/providers/google/resource_container_cluster_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/hashicorp/terraform/helper/acctest"
88
"github.com/hashicorp/terraform/helper/resource"
99
"github.com/hashicorp/terraform/terraform"
10+
"strconv"
1011
)
1112

1213
func TestAccContainerCluster_basic(t *testing.T) {
@@ -26,6 +27,25 @@ func TestAccContainerCluster_basic(t *testing.T) {
2627
})
2728
}
2829

30+
func TestAccContainerCluster_withAdditionalZones(t *testing.T) {
31+
resource.Test(t, resource.TestCase{
32+
PreCheck: func() { testAccPreCheck(t) },
33+
Providers: testAccProviders,
34+
CheckDestroy: testAccCheckContainerClusterDestroy,
35+
Steps: []resource.TestStep{
36+
resource.TestStep{
37+
Config: testAccContainerCluster_withAdditionalZones,
38+
Check: resource.ComposeTestCheckFunc(
39+
testAccCheckContainerClusterExists(
40+
"google_container_cluster.with_additional_zones"),
41+
testAccCheckContainerClusterAdditionalZonesExist(
42+
"google_container_cluster.with_additional_zones"),
43+
),
44+
},
45+
},
46+
})
47+
}
48+
2949
func TestAccContainerCluster_withVersion(t *testing.T) {
3050
resource.Test(t, resource.TestCase{
3151
PreCheck: func() { testAccPreCheck(t) },
@@ -143,6 +163,29 @@ func testAccCheckContainerClusterExists(n string) resource.TestCheckFunc {
143163
}
144164
}
145165

166+
func testAccCheckContainerClusterAdditionalZonesExist(n string) resource.TestCheckFunc {
167+
return func(s *terraform.State) error {
168+
rs, ok := s.RootModule().Resources[n]
169+
if !ok {
170+
return fmt.Errorf("Not found: %s", n)
171+
}
172+
173+
var (
174+
additionalZonesSize int
175+
err error
176+
)
177+
178+
if additionalZonesSize, err = strconv.Atoi(rs.Primary.Attributes["additional_zones.#"]); err != nil {
179+
return err
180+
}
181+
if additionalZonesSize != 2 {
182+
return fmt.Errorf("number of additional zones did not match 2")
183+
}
184+
185+
return nil
186+
}
187+
}
188+
146189
var testAccContainerCluster_basic = fmt.Sprintf(`
147190
resource "google_container_cluster" "primary" {
148191
name = "cluster-test-%s"
@@ -155,6 +198,23 @@ resource "google_container_cluster" "primary" {
155198
}
156199
}`, acctest.RandString(10))
157200

201+
var testAccContainerCluster_withAdditionalZones = fmt.Sprintf(`
202+
resource "google_container_cluster" "with_additional_zones" {
203+
name = "cluster-test-%s"
204+
zone = "us-central1-a"
205+
initial_node_count = 1
206+
207+
additional_zones = [
208+
"us-central1-b",
209+
"us-central1-c"
210+
]
211+
212+
master_auth {
213+
username = "mr.yoda"
214+
password = "adoy.rm"
215+
}
216+
}`, acctest.RandString(10))
217+
158218
var testAccContainerCluster_withVersion = fmt.Sprintf(`
159219
resource "google_container_cluster" "with_version" {
160220
name = "cluster-test-%s"

website/source/docs/providers/google/r/container_cluster.html.markdown

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ resource "google_container_cluster" "primary" {
2020
zone = "us-central1-a"
2121
initial_node_count = 3
2222

23+
additional_zones = [
24+
"us-central1-b",
25+
"us-central1-c"
26+
]
27+
2328
master_auth {
2429
username = "mr.yoda"
2530
password = "adoy.rm"
@@ -47,9 +52,13 @@ resource "google_container_cluster" "primary" {
4752
* `name` - (Required) The name of the cluster, unique within the project and
4853
zone.
4954

50-
* `zone` - (Required) The zone that all resources should be created in.
55+
* `zone` - (Required) The zone that the master and the number of nodes specified
56+
in `initial_node_count` should be created in.
5157

5258
- - -
59+
* `additional_zones` - (Optional) If additional zones are configured, the number
60+
of nodes specified in `initial_node_count` is created in all specified zones.
61+
5362
* `addons_config` - (Optional) The configuration for addons supported by Google
5463
Container Engine
5564

0 commit comments

Comments
 (0)