Skip to content

Commit

Permalink
Merge pull request terraform-google-modules#19 from Jberlinsky/zones-…
Browse files Browse the repository at this point in the history
…optional-for-regional-cluster

Make zone configuration optional when creating a regional cluster
  • Loading branch information
morgante committed Oct 10, 2018
2 parents 07b9643 + 4c8f53e commit 2a44dec
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cluster_regional.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ resource "google_container_cluster" "primary" {
project = "${var.project_id}"

region = "${var.region}"
additional_zones = "${var.zones}"
additional_zones = ["${coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result))}"]

network = "${data.google_compute_network.gke_network.self_link}"
subnetwork = "${data.google_compute_subnetwork.gke_subnetwork.self_link}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Simple Cluster
# Simple Regional Cluster

This example illustrates how to create a simple cluster.

Expand Down
3 changes: 2 additions & 1 deletion examples/simple/main.tf → examples/simple_regional/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ provider "google" {
module "gke" {
source = "../../"
project_id = "${var.project_id}"
name = "simple-sample-cluster"
name = "simple-regional-cluster"
regional = true
region = "${var.region}"
network = "${var.network}"
subnetwork = "${var.subnetwork}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,3 @@ output "location_example" {
description = "Cluster location"
value = "${module.gke.location}"
}

output "zones_example" {
description = "List of zones in which the cluster resides"
value = "${module.gke.zones}"
}
File renamed without changes.
1 change: 1 addition & 0 deletions examples/simple_zonal/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ locals {

provider "google" {
credentials = "${file(local.credentials_file_path)}"
region = "${var.region}"
}

module "gke" {
Expand Down
5 changes: 5 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ data "google_compute_zones" "available" {
region = "${var.region}"
}

resource "random_shuffle" "available_zones" {
input = ["${data.google_compute_zones.available.names}"]
result_count = 3
}

locals {
kubernetes_version = "${var.kubernetes_version != "latest" ? var.kubernetes_version : data.google_container_engine_versions.region.latest_node_version}"
node_version = "${var.node_version != "" ? var.node_version : local.kubernetes_version}"
Expand Down
2 changes: 2 additions & 0 deletions test/integration/gcloud/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
ruby '2.4.2'

source 'https://rubygems.org/' do
gem 'googleauth'
gem 'google-api-client'
gem 'kitchen-terraform', '~> 3.3'
gem 'kitchen-inspec', :git => 'https://github.com/inspec/kitchen-inspec.git', :ref => '0590f1b'
end
16 changes: 16 additions & 0 deletions test/integration/gcloud/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,34 @@ function export_vars() {
export TEST_ID="modules_gke_integration_gcloud_${RANDOM}"
export KUBECONFIG="${TEMPDIR}/${CLUSTER_TYPE}/${TEST_ID}.kubeconfig"
if [[ $CLUSTER_TYPE = "regional" ]]; then
if [ -f "./regional_config.sh" ]; then
source ./regional_config.sh
fi
export CLUSTER_REGIONAL="true"
export CLUSTER_LOCATION="$REGIONAL_LOCATION"
export CLUSTER_NAME="$REGIONAL_CLUSTER_NAME"
export IP_RANGE_PODS="$REGIONAL_IP_RANGE_PODS"
export IP_RANGE_SERVICES="$REGIONAL_IP_RANGE_SERVICES"
else
if [ -f "./zonal_config.sh" ]; then
source ./zonal_config.sh
fi
if [ -z "${ZONE}" ]; then
echo "Can not create a zonal cluster without specifying \$ZONE. Aborting..."
exit 1
fi
export CLUSTER_REGIONAL="false"
export CLUSTER_LOCATION="$ZONAL_LOCATION"
export CLUSTER_NAME="$ZONAL_CLUSTER_NAME"
export IP_RANGE_PODS="$ZONAL_IP_RANGE_PODS"
export IP_RANGE_SERVICES="$ZONAL_IP_RANGE_SERVICES"
fi

if [ "${ZONE}" = "" ] && [ "${ADDITIONAL_ZONES}" = "" ]; then
export ZONES=""
else
export ZONES="\"$ZONE\",$ADDITIONAL_ZONES"
fi
}

# Activate test working directory
Expand Down
2 changes: 1 addition & 1 deletion test/integration/gcloud/sample.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export CLUSTER_NAME="int-test-cluster-01"
export REGION="us-east4"
export ZONE="us-east4-a"
export ADDITIONAL_ZONES='"us-east4-b","us-east4-c"'
export ZONES="\"$ZONE\",$ADDITIONAL_ZONES"
export KUBERNETES_VERSION="1.10.6-gke.2"
export NODE_POOL_SERVICE_ACCOUNT=""
export REGIONAL_CLUSTER_NAME="int-test-regional-01"
export REGIONAL_LOCATION="$REGION"
export ZONAL_CLUSTER_NAME="int-test-zonal-01"
export ZONAL_LOCATION="$ZONE"
export CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE=$CREDENTIALS_PATH
export GOOGLE_APPLICATION_CREDENTIALS=$CREDENTIALS_PATH
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

require_relative '../../../../test/support/google_cloud.rb'

# Test the name output
describe command('terraform output name_example') do
its('stdout.strip') { should eq ENV['CLUSTER_NAME'] }
Expand All @@ -34,7 +36,19 @@

# Test the zones output
describe command('terraform output -json zones_example | jq -cre \'.value\'') do
its('stdout.strip') { should eq '[' + ENV['ZONES'] + ']' }
if ENV['ZONES'] != ''
its('stdout.strip') { should eq '[' + ENV['ZONES'] + ']' }
else
it "should be 3 zones in the region" do
zones = JSON.parse(subject.stdout.strip)
zones.count.should be 3

available_zones = google_compute_service.get_region(ENV['PROJECT_ID'], ENV['REGION']).zones.map { |z| z.split("/").last }
zones.each do |z|
available_zones.should include z
end
end
end
end

# Test the endpoint output
Expand Down
22 changes: 22 additions & 0 deletions test/integration/gcloud/test/support/google_cloud.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'googleauth'
require 'google/apis/compute_v1'

def google_compute_service
Google::Apis::ComputeV1::ComputeService.new.tap do |service|
service.authorization = Google::Auth.get_application_default(['https://www.googleapis.com/auth/cloud-platform'])
end
end
25 changes: 25 additions & 0 deletions test/integration/gcloud/zonal_config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


#################################################################
# PLEASE FILL THE VARIABLES WITH VALID VALUES FOR TESTING #
# DO NOT REMOVE ANY OF THE VARIABLES #
#################################################################

export ZONE="us-east4-a"
export ADDITIONAL_ZONES='"us-east4-b","us-east4-c"'
export ZONAL_LOCATION="$ZONE"

0 comments on commit 2a44dec

Please sign in to comment.