Skip to content

Commit ec0d6a9

Browse files
authored
Merge pull request #207 from marko7460/upstream_nameservers
Add support for upstreamNameservers
2 parents c424503 + c77334c commit ec0d6a9

File tree

50 files changed

+1290
-11
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1290
-11
lines changed

.kitchen.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ suites:
104104
systems:
105105
- name: stub_domains_private
106106
backend: local
107+
- name: "upstream_nameservers"
108+
driver:
109+
root_module_directory: test/fixtures/upstream_nameservers
110+
verifier:
111+
systems:
112+
- name: upstream_nameservers
113+
backend: local
114+
- name: "stub_domains_upstream_nameservers"
115+
driver:
116+
root_module_directory: test/fixtures/stub_domains_upstream_nameservers
117+
verifier:
118+
systems:
119+
- name: stub_domains_upstream_nameservers
120+
backend: local
107121
- name: "workload_metadata_config"
108122
driver:
109123
root_module_directory: test/fixtures/workload_metadata_config

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ In either case, upgrading to module version `v1.0.0` will trigger a recreation o
154154
| service\_account | The service account to run nodes as if not overridden in `node_pools`. The default value will cause a cluster-specific service account to be created. | string | `"create"` | no |
155155
| stub\_domains | Map of stub domains and their resolvers to forward DNS queries for a certain domain to an external DNS server | map | `<map>` | no |
156156
| subnetwork | The subnetwork to host the cluster in (required) | string | n/a | yes |
157+
| upstream\_nameservers | If specified, the values replace the nameservers taken by default from the node’s /etc/resolv.conf | list | `<list>` | no |
157158
| zones | The zones to host the cluster in (optional if regional cluster / required if zonal) | list | `<list>` | no |
158159

159160
## Outputs

autogen/dns.tf

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
Delete default kube-dns configmap
2121
*****************************************/
2222
resource "null_resource" "delete_default_kube_dns_configmap" {
23-
count = "${local.custom_kube_dns_config ? 1 : 0}"
23+
count = "${local.custom_kube_dns_config || local.upstream_nameservers_config ? 1 : 0}"
2424

2525
provisioner "local-exec" {
2626
command = "${path.module}/scripts/kubectl_wrapper.sh https://${local.cluster_endpoint} ${data.google_client_config.default.access_token} ${local.cluster_ca_certificate} ${path.module}/scripts/delete-default-resource.sh kube-system configmap kube-dns"
@@ -33,7 +33,7 @@ resource "null_resource" "delete_default_kube_dns_configmap" {
3333
Create kube-dns confimap
3434
*****************************************/
3535
resource "kubernetes_config_map" "kube-dns" {
36-
count = "${local.custom_kube_dns_config ? 1 : 0}"
36+
count = "${local.custom_kube_dns_config && !local.upstream_nameservers_config ? 1 : 0}"
3737

3838
metadata {
3939
name = "kube-dns"
@@ -52,3 +52,48 @@ EOF
5252

5353
depends_on = ["null_resource.delete_default_kube_dns_configmap", "data.google_client_config.default", "google_container_cluster.primary", "google_container_node_pool.pools", "google_container_cluster.zonal_primary", "google_container_node_pool.zonal_pools"]
5454
}
55+
56+
resource "kubernetes_config_map" "kube-dns-upstream-namservers" {
57+
count = "${!local.custom_kube_dns_config && local.upstream_nameservers_config ? 1 : 0}"
58+
59+
metadata {
60+
name = "kube-dns"
61+
namespace = "kube-system"
62+
63+
labels {
64+
maintained_by = "terraform"
65+
}
66+
}
67+
68+
data {
69+
upstreamNameservers = <<EOF
70+
${jsonencode(var.upstream_nameservers)}
71+
EOF
72+
}
73+
74+
depends_on = ["null_resource.delete_default_kube_dns_configmap", "data.google_client_config.default", "google_container_cluster.primary", "google_container_node_pool.pools", "google_container_cluster.zonal_primary", "google_container_node_pool.zonal_pools"]
75+
}
76+
77+
resource "kubernetes_config_map" "kube-dns-upstream-nameservers-and-stub-domains" {
78+
count = "${local.custom_kube_dns_config && local.upstream_nameservers_config ? 1 : 0}"
79+
80+
metadata {
81+
name = "kube-dns"
82+
namespace = "kube-system"
83+
84+
labels {
85+
maintained_by = "terraform"
86+
}
87+
}
88+
89+
data {
90+
upstreamNameservers = <<EOF
91+
${jsonencode(var.upstream_nameservers)}
92+
EOF
93+
stubDomains = <<EOF
94+
${jsonencode(var.stub_domains)}
95+
EOF
96+
}
97+
98+
depends_on = ["null_resource.delete_default_kube_dns_configmap", "data.google_client_config.default", "google_container_cluster.primary", "google_container_node_pool.pools", "google_container_cluster.zonal_primary", "google_container_node_pool.zonal_pools"]
99+
}

autogen/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ locals {
3636
node_version_regional = "${var.node_version != "" && var.regional ? var.node_version : local.kubernetes_version_regional}"
3737
node_version_zonal = "${var.node_version != "" && !var.regional ? var.node_version : local.kubernetes_version_zonal}"
3838
custom_kube_dns_config = "${length(keys(var.stub_domains)) > 0 ? true : false}"
39+
upstream_nameservers_config = "${length(var.upstream_nameservers) > 0 ? true : false}"
3940
network_project_id = "${var.network_project_id != "" ? var.network_project_id : var.project_id}"
4041

4142
cluster_type = "${var.regional ? "regional" : "zonal"}"

autogen/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ variable "stub_domains" {
206206
default = {}
207207
}
208208

209+
variable "upstream_nameservers" {
210+
type = "list"
211+
description = "If specified, the values replace the nameservers taken by default from the node’s /etc/resolv.conf"
212+
default = []
213+
}
214+
209215
variable "non_masquerade_cidrs" {
210216
type = "list"
211217
description = "List of strings in CIDR notation that specify the IP address ranges that do not use IP masquerading."

dns.tf

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
Delete default kube-dns configmap
2121
*****************************************/
2222
resource "null_resource" "delete_default_kube_dns_configmap" {
23-
count = "${local.custom_kube_dns_config ? 1 : 0}"
23+
count = "${local.custom_kube_dns_config || local.upstream_nameservers_config ? 1 : 0}"
2424

2525
provisioner "local-exec" {
2626
command = "${path.module}/scripts/kubectl_wrapper.sh https://${local.cluster_endpoint} ${data.google_client_config.default.access_token} ${local.cluster_ca_certificate} ${path.module}/scripts/delete-default-resource.sh kube-system configmap kube-dns"
@@ -33,7 +33,7 @@ resource "null_resource" "delete_default_kube_dns_configmap" {
3333
Create kube-dns confimap
3434
*****************************************/
3535
resource "kubernetes_config_map" "kube-dns" {
36-
count = "${local.custom_kube_dns_config ? 1 : 0}"
36+
count = "${local.custom_kube_dns_config && !local.upstream_nameservers_config ? 1 : 0}"
3737

3838
metadata {
3939
name = "kube-dns"
@@ -52,3 +52,48 @@ EOF
5252

5353
depends_on = ["null_resource.delete_default_kube_dns_configmap", "data.google_client_config.default", "google_container_cluster.primary", "google_container_node_pool.pools", "google_container_cluster.zonal_primary", "google_container_node_pool.zonal_pools"]
5454
}
55+
56+
resource "kubernetes_config_map" "kube-dns-upstream-namservers" {
57+
count = "${!local.custom_kube_dns_config && local.upstream_nameservers_config ? 1 : 0}"
58+
59+
metadata {
60+
name = "kube-dns"
61+
namespace = "kube-system"
62+
63+
labels {
64+
maintained_by = "terraform"
65+
}
66+
}
67+
68+
data {
69+
upstreamNameservers = <<EOF
70+
${jsonencode(var.upstream_nameservers)}
71+
EOF
72+
}
73+
74+
depends_on = ["null_resource.delete_default_kube_dns_configmap", "data.google_client_config.default", "google_container_cluster.primary", "google_container_node_pool.pools", "google_container_cluster.zonal_primary", "google_container_node_pool.zonal_pools"]
75+
}
76+
77+
resource "kubernetes_config_map" "kube-dns-upstream-nameservers-and-stub-domains" {
78+
count = "${local.custom_kube_dns_config && local.upstream_nameservers_config ? 1 : 0}"
79+
80+
metadata {
81+
name = "kube-dns"
82+
namespace = "kube-system"
83+
84+
labels {
85+
maintained_by = "terraform"
86+
}
87+
}
88+
89+
data {
90+
upstreamNameservers = <<EOF
91+
${jsonencode(var.upstream_nameservers)}
92+
EOF
93+
stubDomains = <<EOF
94+
${jsonencode(var.stub_domains)}
95+
EOF
96+
}
97+
98+
depends_on = ["null_resource.delete_default_kube_dns_configmap", "data.google_client_config.default", "google_container_cluster.primary", "google_container_node_pool.pools", "google_container_cluster.zonal_primary", "google_container_node_pool.zonal_pools"]
99+
}

examples/stub_domains_private/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
provider "google-beta" {
18-
version = "~> 2.2"
18+
version = "~> 2.9.0"
1919
region = "${var.region}"
2020
}
2121

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Stub Domains and Upstream Nameservers Cluster
2+
3+
This example illustrates how to create a cluster that adds custom stub domains and custom upstream nameservers to kube-dns.
4+
5+
It will:
6+
- Create a cluster
7+
- Remove the default kube-dns configmap
8+
- Add a new kube-dns configmap with custom stub domains and upstream nameservers
9+
10+
[^]: (autogen_docs_start)
11+
12+
## Inputs
13+
14+
| Name | Description | Type | Default | Required |
15+
|------|-------------|:----:|:-----:|:-----:|
16+
| cluster\_name\_suffix | A suffix to append to the default cluster name | string | `""` | no |
17+
| compute\_engine\_service\_account | Service account to associate to the nodes in the cluster | string | n/a | yes |
18+
| ip\_range\_pods | The secondary ip range to use for pods | string | n/a | yes |
19+
| ip\_range\_services | The secondary ip range to use for pods | string | n/a | yes |
20+
| network | The VPC network to host the cluster in | string | n/a | yes |
21+
| project\_id | The project ID to host the cluster in | string | n/a | yes |
22+
| region | The region to host the cluster in | string | n/a | yes |
23+
| subnetwork | The subnetwork to host the cluster in | string | n/a | yes |
24+
25+
## Outputs
26+
27+
| Name | Description |
28+
|------|-------------|
29+
| ca\_certificate | |
30+
| client\_token | |
31+
| cluster\_name | Cluster name |
32+
| ip\_range\_pods | The secondary IP range used for pods |
33+
| ip\_range\_services | The secondary IP range used for services |
34+
| kubernetes\_endpoint | |
35+
| location | |
36+
| master\_kubernetes\_version | The master Kubernetes version |
37+
| network | |
38+
| project\_id | |
39+
| region | |
40+
| service\_account | The service account to default running nodes as if not overridden in `node_pools`. |
41+
| subnetwork | |
42+
| zones | List of zones in which the cluster resides |
43+
44+
[^]: (autogen_docs_end)
45+
46+
To provision this example, run the following from within this directory:
47+
- `terraform init` to get the plugins
48+
- `terraform plan` to see the infrastructure plan
49+
- `terraform apply` to apply the infrastructure build
50+
- `terraform destroy` to destroy the built infrastructure
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
cluster_type = "domains-nameservers"
19+
}
20+
21+
provider "google" {
22+
version = "~> 2.9.0"
23+
region = "${var.region}"
24+
}
25+
26+
provider "google-beta" {
27+
version = "~> 2.9.0"
28+
region = "${var.region}"
29+
}
30+
31+
module "gke" {
32+
source = "../../"
33+
project_id = "${var.project_id}"
34+
name = "${local.cluster_type}-cluster${var.cluster_name_suffix}"
35+
region = "${var.region}"
36+
network = "${var.network}"
37+
subnetwork = "${var.subnetwork}"
38+
ip_range_pods = "${var.ip_range_pods}"
39+
ip_range_services = "${var.ip_range_services}"
40+
network_policy = true
41+
service_account = "${var.compute_engine_service_account}"
42+
43+
configure_ip_masq = true
44+
45+
stub_domains {
46+
"example.com" = [
47+
"10.254.154.11",
48+
"10.254.154.12",
49+
]
50+
51+
"example.net" = [
52+
"10.254.154.11",
53+
"10.254.154.12",
54+
]
55+
}
56+
57+
upstream_nameservers = ["8.8.8.8", "8.8.4.4"]
58+
}
59+
60+
data "google_client_config" "default" {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 "kubernetes_endpoint" {
18+
sensitive = true
19+
value = "${module.gke.endpoint}"
20+
}
21+
22+
output "client_token" {
23+
sensitive = true
24+
value = "${base64encode(data.google_client_config.default.access_token)}"
25+
}
26+
27+
output "ca_certificate" {
28+
value = "${module.gke.ca_certificate}"
29+
}
30+
31+
output "service_account" {
32+
description = "The service account to default running nodes as if not overridden in `node_pools`."
33+
value = "${module.gke.service_account}"
34+
}

0 commit comments

Comments
 (0)