Skip to content

Commit 28c28dd

Browse files
committed
Make zone configuration optional when creating a regional cluster
1 parent 2b91ad5 commit 28c28dd

File tree

12 files changed

+198
-3
lines changed

12 files changed

+198
-3
lines changed

cluster_regional.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ resource "google_container_cluster" "primary" {
2424
project = "${var.project_id}"
2525

2626
region = "${var.region}"
27-
additional_zones = "${var.zones}"
27+
additional_zones = ["${coalescelist(compact(var.zones), data.google_compute_zones.available.names)}"]
2828

2929
network = "${data.google_compute_network.gke_network.self_link}"
3030
subnetwork = "${data.google_compute_subnetwork.gke_subnetwork.self_link}"

examples/simple_regional/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Simple Regional Cluster
2+
3+
This example illustrates how to create a simple cluster.
4+
5+
Expected variables:
6+
- `project_id`
7+
- `region`
8+
- `network`
9+
- `subnetwork`
10+
- `ip_range_pods`
11+
- `ip_range_services`
12+
13+
To provision this example, run the following from within this directory:
14+
- `terraform init` to get the plugins
15+
- `terraform plan` to see the infrastructure plan
16+
- `terraform apply` to apply the infrastructure build
17+
- `terraform destroy` to destroy the built infrastructure

examples/simple_regional/main.tf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
locals {
18+
credentials_file_path = "${path.module}/sa-key.json"
19+
}
20+
21+
provider "google" {
22+
credentials = "${file(local.credentials_file_path)}"
23+
region = "us-east4" # TODO remove
24+
}
25+
26+
module "gke" {
27+
source = "../../"
28+
project_id = "${var.project_id}"
29+
name = "simple-regional-cluster"
30+
regional = true
31+
region = "${var.region}"
32+
network = "${var.network}"
33+
subnetwork = "${var.subnetwork}"
34+
ip_range_pods = "${var.ip_range_pods}"
35+
ip_range_services = "${var.ip_range_services}"
36+
}

examples/simple_regional/outputs.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "name_example" {
18+
description = "Cluster name"
19+
value = "${module.gke.name}"
20+
}
21+
22+
output "endpoint_example" {
23+
sensitive = true
24+
description = "Cluster endpoint"
25+
value = "${module.gke.endpoint}"
26+
}
27+
28+
output "location_example" {
29+
description = "Cluster location"
30+
value = "${module.gke.location}"
31+
}

examples/simple_regional/variables.tf

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "project_id" {
18+
description = "The project ID to host the cluster in"
19+
}
20+
21+
variable "region" {
22+
description = "The region to host the cluster in"
23+
}
24+
25+
variable "network" {
26+
description = "The VPC network to host the cluster in"
27+
}
28+
29+
variable "subnetwork" {
30+
description = "The subnetwork to host the cluster in"
31+
}
32+
33+
variable "ip_range_pods" {
34+
description = "The secondary ip range to use for pods"
35+
}
36+
37+
variable "ip_range_services" {
38+
description = "The secondary ip range to use for pods"
39+
}

examples/simple_zonal/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ locals {
2020

2121
provider "google" {
2222
credentials = "${file(local.credentials_file_path)}"
23+
region = "${var.region}"
2324
}
2425

2526
module "gke" {

test/integration/gcloud/Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
ruby '2.4.2'
1616

1717
source 'https://rubygems.org/' do
18+
gem 'googleauth'
19+
gem 'google-api-client'
1820
gem 'kitchen-terraform', '~> 3.3'
1921
gem 'kitchen-inspec', :git => 'https://github.com/inspec/kitchen-inspec.git', :ref => '0590f1b'
2022
end

test/integration/gcloud/run.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,34 @@ function export_vars() {
2727
export TEST_ID="modules_gke_integration_gcloud_${RANDOM}"
2828
export KUBECONFIG="${TEMPDIR}/${CLUSTER_TYPE}/${TEST_ID}.kubeconfig"
2929
if [[ $CLUSTER_TYPE = "regional" ]]; then
30+
if [ -f "./regional_config.sh" ]; then
31+
source ./regional_config.sh
32+
fi
3033
export CLUSTER_REGIONAL="true"
3134
export CLUSTER_LOCATION="$REGIONAL_LOCATION"
3235
export CLUSTER_NAME="$REGIONAL_CLUSTER_NAME"
3336
export IP_RANGE_PODS="$REGIONAL_IP_RANGE_PODS"
3437
export IP_RANGE_SERVICES="$REGIONAL_IP_RANGE_SERVICES"
3538
else
39+
if [ -f "./zonal_config.sh" ]; then
40+
source ./zonal_config.sh
41+
fi
42+
if [ -z "${ZONE}" ]; then
43+
echo "Can not create a zonal cluster without specifying \$ZONE. Aborting..."
44+
exit 1
45+
fi
3646
export CLUSTER_REGIONAL="false"
3747
export CLUSTER_LOCATION="$ZONAL_LOCATION"
3848
export CLUSTER_NAME="$ZONAL_CLUSTER_NAME"
3949
export IP_RANGE_PODS="$ZONAL_IP_RANGE_PODS"
4050
export IP_RANGE_SERVICES="$ZONAL_IP_RANGE_SERVICES"
4151
fi
52+
53+
if [ "${ZONE}" = "" ] && [ "${ADDITIONAL_ZONES}" = "" ]; then
54+
export ZONES=""
55+
else
56+
export ZONES="\"$ZONE\",$ADDITIONAL_ZONES"
57+
fi
4258
}
4359

4460
# Activate test working directory

test/integration/gcloud/sample.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ export CLUSTER_NAME="int-test-cluster-01"
3434
export REGION="us-east4"
3535
export ZONE="us-east4-a"
3636
export ADDITIONAL_ZONES='"us-east4-b","us-east4-c"'
37-
export ZONES="\"$ZONE\",$ADDITIONAL_ZONES"
3837
export KUBERNETES_VERSION="1.10.6-gke.2"
3938
export NODE_POOL_SERVICE_ACCOUNT=""
4039
export REGIONAL_CLUSTER_NAME="int-test-regional-01"
4140
export REGIONAL_LOCATION="$REGION"
4241
export ZONAL_CLUSTER_NAME="int-test-zonal-01"
4342
export ZONAL_LOCATION="$ZONE"
4443
export CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=$CREDENTIALS_PATH
44+
export GOOGLE_APPLICATION_CREDENTIALS=$CREDENTIALS_PATH

test/integration/gcloud/test/integration/default/inspec/terraform.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
require_relative '../../../../test/support/google_cloud.rb'
16+
1517
# Test the name output
1618
describe command('terraform output name_example') do
1719
its('stdout.strip') { should eq ENV['CLUSTER_NAME'] }
@@ -34,7 +36,11 @@
3436

3537
# Test the zones output
3638
describe command('terraform output -json zones_example | jq -cre \'.value\'') do
37-
its('stdout.strip') { should eq '[' + ENV['ZONES'] + ']' }
39+
if ENV['ZONES'] != ''
40+
its('stdout.strip') { should eq '[' + ENV['ZONES'] + ']' }
41+
else
42+
its('stdout.strip') { should eq google_compute_service.get_region(ENV['PROJECT_ID'], ENV['REGION']).zones.map { |z| z.split("/").last }.to_json }
43+
end
3844
end
3945

4046
# Test the endpoint output
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require 'googleauth'
16+
require 'google/apis/compute_v1'
17+
18+
def google_compute_service
19+
Google::Apis::ComputeV1::ComputeService.new.tap do |service|
20+
service.authorization = Google::Auth.get_application_default(['https://www.googleapis.com/auth/cloud-platform'])
21+
end
22+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
# Copyright 2018 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
17+
#################################################################
18+
# PLEASE FILL THE VARIABLES WITH VALID VALUES FOR TESTING #
19+
# DO NOT REMOVE ANY OF THE VARIABLES #
20+
#################################################################
21+
22+
export ZONE="us-east4-a"
23+
export ADDITIONAL_ZONES='"us-east4-b","us-east4-c"'
24+
export ZONAL_LOCATION="$ZONE"
25+

0 commit comments

Comments
 (0)