Skip to content

Commit 7b9e538

Browse files
cgrantbharathkkb
andauthored
Hub submodule (#611)
* initial hub commit * initial hub commit * initial hub commit * initial hub commit * initial hub commit * initial hub commit * Added wait output and general cleanup * linter tweaks * hub fixes Co-authored-by: bharathkkb <[email protected]>
1 parent e53a9ba commit 7b9e538

File tree

14 files changed

+501
-37
lines changed

14 files changed

+501
-37
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Simple Zonal Cluster
2+
3+
This example illustrates how to create a simple cluster and register it with [Anthos](https://cloud.google.com/anthos/multicluster-management/environs)
4+
5+
It incorporates the standard cluster module and the [Hub registration module](../../modules/hub).
6+
7+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
8+
## Inputs
9+
10+
| Name | Description | Type | Default | Required |
11+
|------|-------------|:----:|:-----:|:-----:|
12+
| cluster\_name\_suffix | A suffix to append to the default cluster name | string | `""` | no |
13+
| ip\_range\_pods | The secondary ip range to use for pods | string | `""` | no |
14+
| ip\_range\_services | The secondary ip range to use for services | string | `""` | no |
15+
| network | The VPC network to host the cluster in | string | `"default"` | no |
16+
| project\_id | The project ID to host the cluster in | string | n/a | yes |
17+
| region | The region to host the cluster in | string | n/a | yes |
18+
| subnetwork | The subnetwork to host the cluster in | string | `"default"` | no |
19+
| zones | The zone to host the cluster in (required if is a zonal cluster) | list(string) | n/a | yes |
20+
21+
## Outputs
22+
23+
| Name | Description |
24+
|------|-------------|
25+
| ca\_certificate | |
26+
| client\_token | |
27+
| cluster\_name | Cluster name |
28+
| ip\_range\_pods | The secondary IP range used for pods |
29+
| ip\_range\_services | The secondary IP range used for services |
30+
| kubernetes\_endpoint | |
31+
| location | |
32+
| master\_kubernetes\_version | The master Kubernetes version |
33+
| network | |
34+
| project\_id | |
35+
| region | |
36+
| service\_account | The default service account used for running nodes. |
37+
| subnetwork | |
38+
| zones | List of zones in which the cluster resides |
39+
40+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
41+
42+
To provision this example, run the following from within this directory:
43+
- `terraform init` to get the plugins
44+
- `terraform plan` to see the infrastructure plan
45+
- `terraform apply` to apply the infrastructure build
46+
- `terraform destroy` to destroy the built infrastructure
47+
48+
Example:
49+
50+
```
51+
terraform init
52+
53+
terraform apply \
54+
-var project_id=${PROJECT} \
55+
-var region="us-central1" \
56+
-var zones='["us-central1-c"]'
57+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
module "hub" {
18+
source = "../../modules/hub"
19+
project_id = var.project_id
20+
location = module.gke.location
21+
cluster_name = module.gke.name
22+
cluster_endpoint = module.gke.endpoint
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 = "simple-zonal"
19+
}
20+
21+
provider "google" {
22+
version = "~> 3.16.0"
23+
region = var.region
24+
}
25+
26+
module "gke" {
27+
source = "../../"
28+
project_id = var.project_id
29+
name = "${local.cluster_type}-cluster${var.cluster_name_suffix}"
30+
regional = false
31+
region = var.region
32+
zones = var.zones
33+
network = var.network
34+
subnetwork = var.subnetwork
35+
ip_range_pods = var.ip_range_pods
36+
ip_range_services = var.ip_range_services
37+
service_account = "create"
38+
}
39+
40+
data "google_client_config" "default" {
41+
}
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 default service account used for running nodes."
33+
value = module.gke.service_account
34+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
// These outputs are used to test the module with kitchen-terraform
18+
// They do not need to be included in real-world uses of this module
19+
20+
output "project_id" {
21+
value = var.project_id
22+
}
23+
24+
output "region" {
25+
value = module.gke.region
26+
}
27+
28+
output "cluster_name" {
29+
description = "Cluster name"
30+
value = module.gke.name
31+
}
32+
33+
output "network" {
34+
value = var.network
35+
}
36+
37+
output "subnetwork" {
38+
value = var.subnetwork
39+
}
40+
41+
output "location" {
42+
value = module.gke.location
43+
}
44+
45+
output "ip_range_pods" {
46+
description = "The secondary IP range used for pods"
47+
value = var.ip_range_pods
48+
}
49+
50+
output "ip_range_services" {
51+
description = "The secondary IP range used for services"
52+
value = var.ip_range_services
53+
}
54+
55+
output "zones" {
56+
description = "List of zones in which the cluster resides"
57+
value = module.gke.zones
58+
}
59+
60+
output "master_kubernetes_version" {
61+
description = "The master Kubernetes version"
62+
value = module.gke.master_version
63+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 "cluster_name_suffix" {
22+
description = "A suffix to append to the default cluster name"
23+
default = ""
24+
}
25+
26+
variable "region" {
27+
description = "The region to host the cluster in"
28+
}
29+
30+
variable "zones" {
31+
type = list(string)
32+
description = "The zone to host the cluster in (required if is a zonal cluster)"
33+
}
34+
35+
variable "network" {
36+
description = "The VPC network to host the cluster in"
37+
default = "default"
38+
}
39+
40+
variable "subnetwork" {
41+
description = "The subnetwork to host the cluster in"
42+
default = "default"
43+
}
44+
45+
variable "ip_range_pods" {
46+
description = "The secondary ip range to use for pods"
47+
default = ""
48+
}
49+
50+
variable "ip_range_services" {
51+
description = "The secondary ip range to use for services"
52+
default = ""
53+
}

modules/asm/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,11 @@ To deploy this config:
5353
| project\_id | The project in which the resource belongs. | string | n/a | yes |
5454
| skip\_gcloud\_download | Whether to skip downloading gcloud (assumes gcloud and kubectl already available outside the module) | bool | `"true"` | no |
5555

56+
## Outputs
57+
58+
| Name | Description |
59+
|------|-------------|
60+
| asm\_wait | An output to use when you want to depend on ASM finishing |
61+
| hub\_wait | An output to use when you want to depend on GKE hub finishing |
62+
5663
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

modules/asm/main.tf

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
locals {
18-
gke_hub_sa_key = var.enable_gke_hub_registration ? google_service_account_key.gke_hub_key[0].private_key : ""
19-
}
20-
2117
module "asm_install" {
2218
source = "terraform-google-modules/gcloud/google//modules/kubectl-wrapper"
2319
version = "~> 1.4"
@@ -36,38 +32,17 @@ module "asm_install" {
3632
kubectl_destroy_command = "kubectl delete ns istio-system"
3733
}
3834

39-
resource "google_service_account" "gke_hub_sa" {
40-
count = var.enable_gke_hub_registration ? 1 : 0
41-
account_id = var.gke_hub_sa_name
42-
project = var.project_id
43-
display_name = "Service Account for GKE Hub Registration"
44-
}
45-
46-
resource "google_project_iam_member" "gke_hub_member" {
47-
count = var.enable_gke_hub_registration ? 1 : 0
48-
project = var.project_id
49-
role = "roles/gkehub.connect"
50-
member = "serviceAccount:${google_service_account.gke_hub_sa[0].email}"
51-
}
52-
53-
resource "google_service_account_key" "gke_hub_key" {
54-
count = var.enable_gke_hub_registration ? 1 : 0
55-
service_account_id = google_service_account.gke_hub_sa[0].name
56-
}
57-
5835
module "gke_hub_registration" {
59-
source = "terraform-google-modules/gcloud/google"
60-
version = "~> 1.2"
61-
62-
platform = "linux"
63-
gcloud_sdk_version = var.gcloud_sdk_version
64-
skip_download = var.skip_gcloud_download
65-
upgrade = true
66-
enabled = var.enable_gke_hub_registration
67-
module_depends_on = [module.asm_install.wait]
36+
source = "../hub"
37+
38+
project_id = var.project_id
39+
cluster_name = var.cluster_name
40+
cluster_endpoint = var.cluster_endpoint
41+
location = var.location
42+
skip_gcloud_download = var.skip_gcloud_download
43+
gcloud_sdk_version = var.gcloud_sdk_version
44+
enable_gke_hub_registration = var.enable_gke_hub_registration
45+
gke_hub_sa_name = var.gke_hub_sa_name
46+
gke_hub_membership_name = var.gke_hub_membership_name
6847

69-
create_cmd_entrypoint = "${path.module}/scripts/gke_hub_registration.sh"
70-
create_cmd_body = "${var.gke_hub_membership_name} ${var.location} ${var.cluster_name} ${local.gke_hub_sa_key}"
71-
destroy_cmd_entrypoint = "gcloud"
72-
destroy_cmd_body = "container hub memberships unregister ${var.gke_hub_membership_name} --gke-cluster=${var.location}/${var.cluster_name} --project ${var.project_id}"
7348
}

modules/asm/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
17+
output "asm_wait" {
18+
description = "An output to use when you want to depend on ASM finishing"
19+
value = module.asm_install.wait
20+
}
21+
22+
output "hub_wait" {
23+
description = "An output to use when you want to depend on GKE hub finishing"
24+
value = module.gke_hub_registration.wait
25+
}

0 commit comments

Comments
 (0)