Skip to content

Commit 9359961

Browse files
authored
feat: Switch to native Terraform resources for hub registration and ACM (#947)
1 parent 2316e77 commit 9359961

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

+903
-1161
lines changed

docs/upgrading_to_v21.0.md

Lines changed: 132 additions & 1 deletion
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,135 @@ 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+
- module_depends_on = [module.gke]
44+
}
45+
```
46+
47+
You also need to follow these migration steps:
48+
49+
1. Remove the old module from your state:
50+
51+
```
52+
terraform state rm module.register
53+
```
54+
55+
2. Remove the cluster from the fleet:
56+
57+
```
58+
gcloud container fleet memberships delete gke-hub-membership-name
59+
```
60+
61+
3. Apply the new configuration to re-register the cluster:
62+
63+
```
64+
terraform apply
65+
```
66+
67+
#### Legacy module
68+
**The native API only supports registering GKE clusters**. Therefore, the old hub module is preserved as `hub-legacy`.
69+
70+
You can continue using it by updating your configuration to point to the new location.
71+
72+
```diff
73+
module "register" {
74+
- source = "terraform-google-modules/kubernetes-engine/google//modules/hub"
75+
- version = "~> 20.0"
76+
+ source = "terraform-google-modules/kubernetes-engine/google//modules/hub-legacy"
77+
+ version = "~> 21.0"
78+
79+
project_id = "my-project-id"
80+
cluster_name = "my-cluster-name"
81+
location = module.gke.location
82+
cluster_endpoint = module.gke.endpoint
83+
}
84+
```
85+
86+
### Anthos Config Management (ACM) and Config Sync Module Rewrite
87+
Together with the rewrite of the Hub module, the ACM module also has been rewritten to use native resources.
88+
89+
You will need to follow these migration steps:
90+
91+
1. Update your configuration to use the new module:
92+
93+
```diff
94+
module "acm" {
95+
source = "terraform-google-modules/kubernetes-engine/google//modules/acm"
96+
- version = "~> 20.0"
97+
+ version = "~> 21.0"
98+
99+
project_id = "my-project-id"
100+
cluster_name = "simple-zonal-cluster"
101+
location = "us-central1-a"
102+
- cluster_endpoint = module.auth.host
103+
104+
sync_repo = "git@github.com:GoogleCloudPlatform/csp-config-management.git"
105+
sync_branch = "1.0.0"
106+
policy_dir = "foo-corp"
107+
108+
secret_type = "ssh"
109+
}
110+
```
111+
112+
1. Make sure you have the `kubernetes` provider configured:
113+
114+
```hcl
115+
provider "kubernetes" {
116+
cluster_ca_certificate = module.auth.cluster_ca_certificate
117+
host = module.auth.host
118+
token = module.auth.token
119+
}
120+
```
121+
122+
1. Remove the old module from your state:
123+
124+
```
125+
terraform state rm module.acm
126+
```
127+
128+
2. Import the old `git-creds` secret into Terraform:
129+
130+
```
131+
terraform import 'module.acm.module.acm_operator.kubernetes_secret_v1.creds' 'config-management-system/git-creds'
132+
```
133+
134+
3. Apply the new configuration to re-register ACM and confirm everything is working:
135+
136+
```
137+
terraform apply
138+
```
139+
140+
#### Feature Activation
141+
142+
Only the first cluster in a fleet should activate the ACM fleet feature.
143+
Other clusters should disable feature activation by setting `enable_fleet_feature = false`.
144+
145+
#### Config Sync Module Removed
146+
The dedicated Config Sync submodule has been removed.
147+
To use Config Sync, just invoke the ACM module with `enable_policy_controller = false`.

examples/simple_zonal_with_acm/README.md

Lines changed: 26 additions & 11 deletions
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

Lines changed: 10 additions & 9 deletions
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

Lines changed: 18 additions & 11 deletions
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

Lines changed: 3 additions & 5 deletions
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

Lines changed: 47 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)