Skip to content

Commit 388d7ab

Browse files
committed
feat: Switch to native Terraform resources for hub registration and ACM
1 parent 151c8c4 commit 388d7ab

Some content is hidden

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

54 files changed

+905
-1161
lines changed

docs/upgrading_to_v21.0.md

+127-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Upgrading to v21.0
2-
32
The v21.0 release of *kubernetes-engine* is a backwards incompatible
43
release.
54

@@ -14,3 +13,130 @@ The [Terraform Kubernetes Engine Module](https://github.com/terraform-google-mod
1413
### Kubernetes Provider upgrade
1514
The Terraform Kubernetes Engine module now requires version 2.10 or higher of
1615
the Kubernetes Provider.
16+
17+
### Hub module rewrite
18+
The old [Hub submodule](https://github.com/terraform-google-modules/terraform-google-kubernetes-engine/tree/v20.0.0/modules/hub)
19+
has been renamed to `hub-legacy` and deprecated. It is replaced with a new [fleet membership](https://github.com/terraform-google-modules/terraform-google-kubernetes-engine/tree/master/modules/fleet-membership)
20+
module to handle registering GKE clusters to [fleets](https://cloud.google.com/anthos/multicluster-management/fleets) using the native API.
21+
22+
The new module relies exclusively on native Terraform resources and should therefore be more robust.
23+
24+
### Migrating
25+
For GKE clusters, you should update your configuration as follows:
26+
27+
```diff
28+
module "register" {
29+
- source = "terraform-google-modules/kubernetes-engine/google//modules/hub"
30+
- version = "~> 20.0"
31+
+ source = "terraform-google-modules/kubernetes-engine/google//modules/fleet-membership"
32+
+ version = "~> 21.0"
33+
34+
project_id = "my-project-id"
35+
cluster_name = "my-cluster-name"
36+
- gke_hub_membership_name = "gke-membership"
37+
+ membership_name = "gke-hub-membership"
38+
location = module.gke.location
39+
- cluster_endpoint = module.gke.endpoint
40+
- gke_hub_sa_name = "sa-for-kind-cluster-membership"
41+
- use_kubeconfig = true
42+
- labels = "testlabel=usekubecontext"
43+
}
44+
```
45+
46+
You also need to follow these migration steps:
47+
48+
1. Remove the old module from your state:
49+
50+
```
51+
terraform state rm module.register
52+
```
53+
54+
2. Remove the cluster from the fleet:
55+
56+
```
57+
gcloud container fleet memberships delete gke-hub-membership-name
58+
```
59+
60+
3. Apply the new configuration to re-register the cluster:
61+
62+
```
63+
terraform apply
64+
```
65+
66+
#### Legacy module
67+
**The native API only supports registering GKE clusters**. Therefore, the old hub module is preserved as `hub-legacy`.
68+
69+
You can continue using it by updating your configuration to point to the new location.
70+
71+
```diff
72+
module "register" {
73+
- source = "terraform-google-modules/kubernetes-engine/google//modules/hub"
74+
- version = "~> 20.0"
75+
+ source = "terraform-google-modules/kubernetes-engine/google//modules/hub-legacy"
76+
+ version = "~> 21.0"
77+
78+
project_id = "my-project-id"
79+
cluster_name = "my-cluster-name"
80+
location = module.gke.location
81+
cluster_endpoint = module.gke.endpoint
82+
}
83+
```
84+
85+
### Anthos Config Management (ACM) and Config Sync Module Rewrite
86+
Together with the rewrite of the Hub module, the ACM module also has been rewritten to use native resources.
87+
88+
You will need to follow these migration steps:
89+
90+
1. Update your configuration to use the new module:
91+
92+
```diff
93+
module "acm" {
94+
source = "terraform-google-modules/kubernetes-engine/google//modules/acm"
95+
- version = "~> 20.0"
96+
+ version = "~> 21.0"
97+
98+
project_id = "my-project-id"
99+
cluster_name = "simple-zonal-cluster"
100+
location = "us-central1-a"
101+
- cluster_endpoint = module.auth.host
102+
103+
sync_repo = "git@github.com:GoogleCloudPlatform/csp-config-management.git"
104+
sync_branch = "1.0.0"
105+
policy_dir = "foo-corp"
106+
107+
secret_type = "ssh"
108+
}
109+
```
110+
111+
1. Make sure you have the `kubernetes` provider configured:
112+
113+
```hcl
114+
provider "kubernetes" {
115+
cluster_ca_certificate = module.auth.cluster_ca_certificate
116+
host = module.auth.host
117+
token = module.auth.token
118+
}
119+
```
120+
121+
1. Remove the old module from your state:
122+
123+
```
124+
terraform state rm module.acm
125+
```
126+
127+
2. Import the old `git-creds` secret into Terraform:
128+
129+
```
130+
terraform import 'module.acm.module.acm_operator.kubernetes_secret_v1.creds' 'config-management-system/git-creds'
131+
```
132+
133+
3. Apply the new configuration to re-register ACM and confirm everything is working:
134+
135+
```
136+
terraform apply
137+
```
138+
139+
#### Feature Activation
140+
141+
Only the first cluster in a fleet should activate the ACM fleet feature.
142+
Other clusters should disable feature activation by setting `enable_fleet_feature = false`.

examples/simple_zonal_with_acm/README.md

+26-11
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,38 @@ This example illustrates how to create a simple cluster and install [Anthos Conf
44

55
It incorporates the standard cluster module and the [ACM install module](../../modules/acm).
66

7+
## Verifying Success
8+
9+
After applying the Terraform configuration, you can run the following commands to verify that your cluster has synced correctly:
10+
11+
1. Check ACM install status:
12+
13+
```
14+
gcloud config set project $(terraform output --raw project_id)
15+
gcloud alpha container hub config-management status
16+
```
17+
18+
2. Connect to the cluster:
19+
20+
```
21+
gcloud container clusters get-credentials $(terraform output --raw cluster_name) --zone=$(terraform output --raw location)
22+
```
23+
24+
3. Confirm the `shipping-dev` namespace was created:
25+
26+
```
27+
kubectl describe ns shipping-dev
28+
```
29+
730
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
831
## Inputs
932
1033
| Name | Description | Type | Default | Required |
1134
|------|-------------|------|---------|:--------:|
12-
| acm\_policy\_dir | Subfolder containing configs in ACM Git repo | `string` | `"foo-corp"` | no |
13-
| acm\_sync\_branch | Anthos config management Git branch | `string` | `"1.0.0"` | no |
14-
| acm\_sync\_repo | Anthos config management Git repo | `string` | `"git@github.com:GoogleCloudPlatform/csp-config-management.git"` | no |
1535
| cluster\_name\_suffix | A suffix to append to the default cluster name | `string` | `""` | no |
16-
| ip\_range\_pods | The secondary ip range to use for pods | `any` | n/a | yes |
17-
| ip\_range\_services | The secondary ip range to use for services | `any` | n/a | yes |
18-
| network | The VPC network to host the cluster in | `any` | n/a | yes |
19-
| operator\_path | Path to the operator yaml config. If unset, will download from GCS releases. | `string` | `null` | no |
2036
| project\_id | The project ID to host the cluster in | `any` | n/a | yes |
21-
| region | The region to host the cluster in | `any` | n/a | yes |
22-
| subnetwork | The subnetwork to host the cluster in | `any` | n/a | yes |
23-
| zones | The zone to host the cluster in (required if is a zonal cluster) | `list(string)` | n/a | yes |
37+
| region | The region to host the cluster in | `string` | `"us-central1"` | no |
38+
| zone | The zone to host the cluster in | `string` | `"us-central1-a"` | no |
2439
2540
## Outputs
2641
@@ -36,7 +51,7 @@ It incorporates the standard cluster module and the [ACM install module](../../m
3651
| location | n/a |
3752
| master\_kubernetes\_version | The master Kubernetes version |
3853
| network | n/a |
39-
| project\_id | n/a |
54+
| project\_id | Standard test outputs |
4055
| region | n/a |
4156
| service\_account | The default service account used for running nodes. |
4257
| subnetwork | n/a |

examples/simple_zonal_with_acm/acm.tf

+10-9
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
*/
1616

1717
module "acm" {
18-
source = "../../modules/acm"
19-
project_id = var.project_id
20-
location = module.gke.location
21-
cluster_name = module.gke.name
22-
sync_repo = var.acm_sync_repo
23-
sync_branch = var.acm_sync_branch
24-
policy_dir = var.acm_policy_dir
25-
cluster_endpoint = module.gke.endpoint
26-
operator_path = var.operator_path
18+
source = "../../modules/acm"
19+
project_id = var.project_id
20+
location = module.gke.location
21+
cluster_name = module.gke.name
22+
23+
sync_repo = "git@github.com:GoogleCloudPlatform/csp-config-management.git"
24+
sync_branch = "1.0.0"
25+
policy_dir = "foo-corp"
26+
27+
secret_type = "ssh"
2728
}

examples/simple_zonal_with_acm/main.tf

+18-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ locals {
1818
cluster_type = "simple-zonal"
1919
}
2020

21+
provider "google" {
22+
region = var.region
23+
}
24+
2125
data "google_client_config" "default" {}
2226

2327
provider "kubernetes" {
@@ -27,17 +31,20 @@ provider "kubernetes" {
2731
}
2832

2933
module "gke" {
30-
source = "../../"
31-
project_id = var.project_id
32-
name = "${local.cluster_type}-cluster${var.cluster_name_suffix}"
33-
regional = false
34-
region = var.region
35-
zones = var.zones
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-
service_account = "create"
34+
source = "../../"
35+
project_id = var.project_id
36+
regional = false
37+
region = var.region
38+
zones = [var.zone]
39+
40+
name = "${local.cluster_type}-cluster${var.cluster_name_suffix}"
41+
42+
network = google_compute_network.main.name
43+
subnetwork = google_compute_subnetwork.main.name
44+
ip_range_pods = google_compute_subnetwork.main.secondary_ip_range[0].range_name
45+
ip_range_services = google_compute_subnetwork.main.secondary_ip_range[1].range_name
46+
47+
service_account = "create"
4148
node_pools = [
4249
{
4350
name = "acm-node-pool"

test/fixtures/simple_zonal/network.tf renamed to examples/simple_zonal_with_acm/network.tf

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2018 Google LLC
2+
* Copyright 2021 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,16 +20,14 @@ resource "random_string" "suffix" {
2020
upper = false
2121
}
2222

23-
provider "google" {
24-
project = var.project_ids[1]
25-
}
26-
2723
resource "google_compute_network" "main" {
24+
project = var.project_id
2825
name = "cft-gke-test-${random_string.suffix.result}"
2926
auto_create_subnetworks = false
3027
}
3128

3229
resource "google_compute_subnetwork" "main" {
30+
project = var.project_id
3331
name = "cft-gke-test-${random_string.suffix.result}"
3432
ip_cidr_range = "10.0.0.0/17"
3533
region = var.region

examples/simple_zonal_with_acm/outputs.tf

+47-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ output "client_token" {
2525
}
2626

2727
output "ca_certificate" {
28-
value = module.gke.ca_certificate
28+
value = module.gke.ca_certificate
29+
sensitive = true
2930
}
3031

3132
output "service_account" {
@@ -38,3 +39,48 @@ output "acm_git_creds_public" {
3839
value = module.acm.git_creds_public
3940
}
4041

42+
# Standard test outputs
43+
output "project_id" {
44+
value = var.project_id
45+
}
46+
47+
output "region" {
48+
value = module.gke.region
49+
}
50+
51+
output "cluster_name" {
52+
description = "Cluster name"
53+
value = module.gke.name
54+
}
55+
56+
output "network" {
57+
value = google_compute_network.main.name
58+
}
59+
60+
output "subnetwork" {
61+
value = google_compute_subnetwork.main.name
62+
}
63+
64+
output "location" {
65+
value = module.gke.location
66+
}
67+
68+
output "ip_range_pods" {
69+
description = "The secondary IP range used for pods"
70+
value = google_compute_subnetwork.main.secondary_ip_range[0].range_name
71+
}
72+
73+
output "ip_range_services" {
74+
description = "The secondary IP range used for services"
75+
value = google_compute_subnetwork.main.secondary_ip_range[1].range_name
76+
}
77+
78+
output "zones" {
79+
description = "List of zones in which the cluster resides"
80+
value = module.gke.zones
81+
}
82+
83+
output "master_kubernetes_version" {
84+
description = "The master Kubernetes version"
85+
value = module.gke.master_version
86+
}

examples/simple_zonal_with_acm/test_outputs.tf

-1
This file was deleted.

0 commit comments

Comments
 (0)