Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private Cluster Configuration #51

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ suites:
backend: local
provisioner:
name: terraform
- name: "simple_regional_private"
driver:
name: "terraform"
command_timeout: 1800
root_module_directory: test/fixtures/simple_regional_private
verifier:
name: terraform
color: false
systems:
- name: simple_regional_private
backend: local
provisioner:
name: terraform
- name: "simple_zonal"
driver:
name: "terraform"
Expand All @@ -89,6 +102,19 @@ suites:
backend: local
provisioner:
name: terraform
- name: "simple_zonal_private"
driver:
name: "terraform"
command_timeout: 1800
root_module_directory: test/fixtures/simple_zonal_private
verifier:
name: terraform
color: false
systems:
- name: simple_zonal_private
backend: local
provisioner:
name: terraform
- name: "stub_domains"
driver:
name: "terraform"
Expand Down
4 changes: 3 additions & 1 deletion auth.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
/******************************************
Retrieve authentication token
*****************************************/
data "google_client_config" "default" {}
data "google_client_config" "default" {
provider = "google-beta"
}

/******************************************
Configure provider
Expand Down
10 changes: 6 additions & 4 deletions cluster_regional.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
Create regional cluster
*****************************************/
resource "google_container_cluster" "primary" {
count = "${var.regional ? 1 : 0}"
provider = "google-beta"
count = "${(local.cluster_deployment_type == "regional") ? 1 : 0}"
name = "${var.name}"
description = "${var.description}"
project = "${var.project_id}"
Expand All @@ -33,7 +34,7 @@ resource "google_container_cluster" "primary" {
logging_service = "${var.logging_service}"
monitoring_service = "${var.monitoring_service}"

master_authorized_networks_config = "${var.master_authorized_networks_config}"
master_authorized_networks_config = ["${var.master_authorized_networks_config}"]

addons_config {
http_load_balancing {
Expand Down Expand Up @@ -89,7 +90,8 @@ resource "google_container_cluster" "primary" {
Create regional node pools
*****************************************/
resource "google_container_node_pool" "pools" {
count = "${var.regional ? length(var.node_pools) : 0}"
provider = "google-beta"
count = "${(local.cluster_deployment_type == "regional") ? length(var.node_pools) : 0}"
name = "${lookup(var.node_pools[count.index], "name")}"
project = "${var.project_id}"
region = "${var.region}"
Expand Down Expand Up @@ -139,7 +141,7 @@ resource "google_container_node_pool" "pools" {
}

resource "null_resource" "wait_for_regional_cluster" {
count = "${var.regional ? 1 : 0}"
count = "${(local.cluster_deployment_type == "regional") ? 1 : 0}"

provisioner "local-exec" {
command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}"
Expand Down
161 changes: 161 additions & 0 deletions cluster_regional_private.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/**
* 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.
*/

/******************************************
Create regional cluster
*****************************************/
resource "google_container_cluster" "primary_private" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for needing a separate cluster config for private clusters? Ideally we should minimize how many different google_container_cluster resources we have?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, a commit was missing from this. 719ee8b shows the actual configuration of the private cluster.

The issue here is that Terraform 0.11.x doesn't support conditionally adding this block. Hopefully Terraform 0.12 will make this less painful, but for now I'm not sure there's a better option than duplicating the resource.

Copy link
Contributor

@morgante morgante Jan 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, the private_cluster_config block itself doesn't force anything. If you set all its arguments to false, you would have a standard public cluster.

it's the enable_private_nodes piece which is what is primarily meant by a private cluster usually.

In terms of arguments, I'd actually like to add 3 new ones:

  • enable_private_endpoint
  • enable_private_nodes
  • master_ipv4_cidr_block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@morgante Unfortunately, the provider requires a master_ipv4_cidr_block to be provided as a valid CIDR whenever the private_cluster_config block is specified, and overriding it from the default (unspecified) value can have hard-to-reason-about results. If we simply don't provide that attribute, things work, but we fall into the Terraform 0.11.x trap where an empty string is not considered the same as null, and we have no way to represent null when it should be so.

I would suggest that we merge this as-is, understanding that there is additional complexity as a result of this, and file an upstream issue to make the validation on master_ipv4_cidr_block less stringent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should hold off as this is a change which will cause breakage later on if we walk it back and instead file an upstream provider issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's approach this by making a submodule as @aaron-lane suggested.

provider = "google-beta"
count = "${(local.cluster_deployment_type == "regional_private") ? 1 : 0}"
name = "${var.name}"
description = "${var.description}"
project = "${var.project_id}"

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

network = "${replace(data.google_compute_network.gke_network.self_link, "https://www.googleapis.com/compute/v1/", "")}"
subnetwork = "${replace(data.google_compute_subnetwork.gke_subnetwork.self_link, "https://www.googleapis.com/compute/v1/", "")}"
min_master_version = "${local.kubernetes_version}"

logging_service = "${var.logging_service}"
monitoring_service = "${var.monitoring_service}"

master_authorized_networks_config = ["${var.master_authorized_networks_config}"]

addons_config {
http_load_balancing {
disabled = "${var.http_load_balancing ? 0 : 1}"
}

horizontal_pod_autoscaling {
disabled = "${var.horizontal_pod_autoscaling ? 0 : 1}"
}

kubernetes_dashboard {
disabled = "${var.kubernetes_dashboard ? 0 : 1}"
}

network_policy_config {
disabled = "${var.network_policy ? 0 : 1}"
}
}

ip_allocation_policy {
cluster_secondary_range_name = "${var.ip_range_pods}"
services_secondary_range_name = "${var.ip_range_services}"
}

maintenance_policy {
daily_maintenance_window {
start_time = "${var.maintenance_start_time}"
}
}

lifecycle {
ignore_changes = ["node_pool"]
}

timeouts {
create = "30m"
update = "30m"
delete = "30m"
}

node_pool {
name = "default-pool"

node_config {
service_account = "${lookup(var.node_pools[0], "service_account", var.service_account)}"
}
}

private_cluster_config {
enable_private_endpoint = "${var.private_enable_private_endpoint}"
enable_private_nodes = "${var.private_enable_private_nodes}"
master_ipv4_cidr_block = "${var.private_master_ipv4_cidr_block}"
}

remove_default_node_pool = "${var.remove_default_node_pool}"
}

/******************************************
Create regional node pools
*****************************************/
resource "google_container_node_pool" "pools_private" {
provider = "google-beta"
count = "${(local.cluster_deployment_type == "regional_private") ? length(var.node_pools) : 0}"
name = "${lookup(var.node_pools[count.index], "name")}"
project = "${var.project_id}"
region = "${var.region}"
cluster = "${var.name}"
version = "${lookup(var.node_pools[count.index], "auto_upgrade", false) ? "" : lookup(var.node_pools[count.index], "version", local.node_version)}"
initial_node_count = "${lookup(var.node_pools[count.index], "min_count", 1)}"

autoscaling {
min_node_count = "${lookup(var.node_pools[count.index], "min_count", 1)}"
max_node_count = "${lookup(var.node_pools[count.index], "max_count", 100)}"
}

management {
auto_repair = "${lookup(var.node_pools[count.index], "auto_repair", true)}"
auto_upgrade = "${lookup(var.node_pools[count.index], "auto_upgrade", true)}"
}

node_config {
image_type = "${lookup(var.node_pools[count.index], "image_type", "COS")}"
machine_type = "${lookup(var.node_pools[count.index], "machine_type", "n1-standard-2")}"
labels = "${merge(map("cluster_name", var.name), map("node_pool", lookup(var.node_pools[count.index], "name")), var.node_pools_labels["all"], var.node_pools_labels[lookup(var.node_pools[count.index], "name")])}"
taint = "${concat(var.node_pools_taints["all"], var.node_pools_taints[lookup(var.node_pools[count.index], "name")])}"
tags = ["${concat(list("gke-${var.name}"), list("gke-${var.name}-${lookup(var.node_pools[count.index], "name")}"), var.node_pools_tags["all"], var.node_pools_tags[lookup(var.node_pools[count.index], "name")])}"]

disk_size_gb = "${lookup(var.node_pools[count.index], "disk_size_gb", 100)}"
disk_type = "${lookup(var.node_pools[count.index], "disk_type", "pd-standard")}"
service_account = "${lookup(var.node_pools[count.index], "service_account", var.service_account)}"
preemptible = "${lookup(var.node_pools[count.index], "preemptible", false)}"

oauth_scopes = [
"https://www.googleapis.com/auth/cloud-platform",
]
}

lifecycle {
ignore_changes = ["initial_node_count"]
}

timeouts {
create = "30m"
update = "30m"
delete = "30m"
}

depends_on = ["google_container_cluster.primary_private"]
}

resource "null_resource" "wait_for_private_regional_cluster" {
count = "${(local.cluster_deployment_type == "regional_private") ? 1 : 0}"

provisioner "local-exec" {
command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}"
}

provisioner "local-exec" {
when = "destroy"
command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}"
}

depends_on = ["google_container_cluster.primary_private", "google_container_node_pool.pools_private"]
}
10 changes: 6 additions & 4 deletions cluster_zonal.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
Create zonal cluster
*****************************************/
resource "google_container_cluster" "zonal_primary" {
count = "${var.regional ? 0 : 1}"
provider = "google-beta"
count = "${(local.cluster_deployment_type == "zonal") ? 1 : 0}"
name = "${var.name}"
description = "${var.description}"
project = "${var.project_id}"
Expand All @@ -33,7 +34,7 @@ resource "google_container_cluster" "zonal_primary" {
logging_service = "${var.logging_service}"
monitoring_service = "${var.monitoring_service}"

master_authorized_networks_config = "${var.master_authorized_networks_config}"
master_authorized_networks_config = ["${var.master_authorized_networks_config}"]

addons_config {
http_load_balancing {
Expand Down Expand Up @@ -89,7 +90,8 @@ resource "google_container_cluster" "zonal_primary" {
Create zonal node pools
*****************************************/
resource "google_container_node_pool" "zonal_pools" {
count = "${var.regional ? 0 : length(var.node_pools)}"
provider = "google-beta"
count = "${(local.cluster_deployment_type == "zonal") ? length(var.node_pools) : 0}"
name = "${lookup(var.node_pools[count.index], "name")}"
project = "${var.project_id}"
zone = "${var.zones[0]}"
Expand Down Expand Up @@ -139,7 +141,7 @@ resource "google_container_node_pool" "zonal_pools" {
}

resource "null_resource" "wait_for_zonal_cluster" {
count = "${var.regional ? 0 : 1}"
count = "${(local.cluster_deployment_type == "zonal") ? 1 : 0}"

provisioner "local-exec" {
command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}"
Expand Down
Loading