Skip to content

Commit a9de2d7

Browse files
feat(TF>=1.1)!: Configure ASM management mode (#1702)
Co-authored-by: Andrew Peabody <[email protected]>
1 parent f4a1a3a commit a9de2d7

File tree

6 files changed

+77
-5
lines changed

6 files changed

+77
-5
lines changed

modules/asm/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module "asm" {
3434
```
3535

3636
Note that the [`mesh_id` label on the cluster](https://cloud.google.com/service-mesh/docs/managed/auto-control-plane-with-fleet#apply_the_mesh_id_label) is required for metrics to get displayed on the Anthos Service Mesh pages in the Cloud console (Topology, etc.). Illustrated with the full example mentioned above, here is an example of what your cluster should have:
37+
3738
```tf
3839
module "gke" {
3940
...
@@ -58,9 +59,10 @@ To deploy this config:
5859
| enable\_cni | Determines whether to enable CNI for this ASM installation. Required to use Managed Data Plane (MDP). | `bool` | `false` | no |
5960
| enable\_fleet\_registration | Determines whether the module registers the cluster to the fleet. | `bool` | `false` | no |
6061
| enable\_mesh\_feature | Determines whether the module enables the mesh feature on the fleet. | `bool` | `false` | no |
61-
| enable\_vpc\_sc | Determines whether to enable VPC-SC for this ASM installation. For more information read https://cloud.google.com/service-mesh/docs/managed/vpc-sc | `bool` | `false` | no |
62+
| enable\_vpc\_sc | Determines whether to enable VPC-SC for this ASM installation. For more information read [VPC Service Controls for Managed Anthos Service Mesh](https://cloud.google.com/service-mesh/docs/managed/vpc-sc) | `bool` | `false` | no |
6263
| fleet\_id | The fleet to use for this ASM installation. | `string` | `""` | no |
6364
| internal\_ip | Use internal ip for the cluster endpoint when running kubectl commands. | `bool` | `false` | no |
65+
| mesh\_management | ASM Management mode. For more information, see the [gke\_hub\_feature\_membership resource documentation](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/gke_hub_feature_membership#nested_mesh) | `string` | `""` | no |
6466
| module\_depends\_on | List of modules or resources this module depends on. If multiple, all items must be the same type. | `list(any)` | `[]` | no |
6567
| multicluster\_mode | [Preview] Determines whether remote secrets should be autogenerated across fleet cluster. | `string` | `"manual"` | no |
6668
| project\_id | The project in which the resource belongs. | `string` | n/a | yes |

modules/asm/hub.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,15 @@ resource "google_gke_hub_feature" "mesh" {
3333
location = "global"
3434
provider = google-beta
3535
}
36+
37+
resource "google_gke_hub_feature_membership" "mesh_feature_membership" {
38+
count = var.enable_fleet_registration && var.enable_mesh_feature && var.mesh_management != "" ? 1 : 0
39+
40+
location = "global"
41+
feature = google_gke_hub_feature.mesh[0].name
42+
membership = google_gke_hub_membership.membership[0].membership_id
43+
mesh {
44+
management = var.mesh_management
45+
}
46+
provider = google-beta
47+
}

modules/asm/main.tf

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ data "google_container_cluster" "asm" {
3434
}
3535

3636
resource "kubernetes_namespace" "system" {
37-
count = var.create_system_namespace ? 1 : 0
37+
count = var.create_system_namespace && var.mesh_management != "MANAGEMENT_AUTOMATIC" ? 1 : 0
3838

3939
metadata {
4040
name = "istio-system"
4141
}
4242
}
4343

4444
resource "kubernetes_config_map" "asm_options" {
45+
count = var.mesh_management != "MANAGEMENT_AUTOMATIC" ? 1 : 0
46+
4547
metadata {
4648
name = "asm-options"
4749
namespace = try(kubernetes_namespace.system[0].metadata[0].name, "istio-system")
@@ -56,6 +58,8 @@ resource "kubernetes_config_map" "asm_options" {
5658
}
5759

5860
module "cpr" {
61+
count = var.mesh_management != "MANAGEMENT_AUTOMATIC" ? 1 : 0
62+
5963
source = "terraform-google-modules/gcloud/google//modules/kubectl-wrapper"
6064
version = "~> 3.1"
6165

@@ -69,3 +73,42 @@ module "cpr" {
6973

7074
module_depends_on = [kubernetes_config_map.asm_options]
7175
}
76+
77+
# Wait for the ControlPlaneRevision custom resource to be ready.
78+
# Add an explicit "retry until the resource is created" until
79+
module "kubectl_asm_wait_for_controlplanerevision_custom_resource_definition" {
80+
count = var.mesh_management == "MANAGEMENT_AUTOMATIC" ? 1 : 0
81+
82+
source = "terraform-google-modules/gcloud/google//modules/kubectl-wrapper"
83+
version = "~> 3.1"
84+
85+
project_id = var.project_id
86+
cluster_name = var.cluster_name
87+
cluster_location = var.cluster_location
88+
kubectl_create_command = "/bin/sh -c 'while ! kubectl wait crd/controlplanerevisions.mesh.cloud.google.com --for condition=established --timeout=60m --all-namespaces; do echo \"crd/controlplanerevisions.mesh.cloud.google.com not yet available, waiting...\"; sleep 5; done'"
89+
kubectl_destroy_command = ""
90+
91+
module_depends_on = [
92+
google_gke_hub_feature_membership.mesh_feature_membership
93+
]
94+
}
95+
96+
# Wait for the ASM control plane revision to be ready so we can safely deploy resources that depend
97+
# on ASM mutating webhooks.
98+
# Add an explicit "retry until the resource is created" until
99+
module "kubectl_asm_wait_for_controlplanerevision" {
100+
count = var.mesh_management == "MANAGEMENT_AUTOMATIC" ? 1 : 0
101+
102+
source = "terraform-google-modules/gcloud/google//modules/kubectl-wrapper"
103+
version = "~> 3.1"
104+
105+
project_id = var.project_id
106+
cluster_name = var.cluster_name
107+
cluster_location = var.cluster_location
108+
kubectl_create_command = "/bin/sh -c 'while ! kubectl -n istio-system wait ControlPlaneRevision --all --timeout=60m --for condition=Reconciled; do echo \"ControlPlaneRevision not yet available, waiting...\"; sleep 5; done'"
109+
kubectl_destroy_command = ""
110+
111+
module_depends_on = [
112+
module.kubectl_asm_wait_for_controlplanerevision_custom_resource_definition[0].wait
113+
]
114+
}

modules/asm/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ output "revision_name" {
2020
}
2121

2222
output "wait" {
23-
value = module.cpr.wait
23+
value = var.mesh_management == "MANAGEMENT_AUTOMATIC" ? module.kubectl_asm_wait_for_controlplanerevision[0].wait : module.cpr[0].wait
2424
description = "An output to use when depending on the ASM installation finishing."
2525
}

modules/asm/variables.tf

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ variable "channel" {
5050
default = ""
5151
}
5252

53+
variable "mesh_management" {
54+
default = ""
55+
description = "ASM Management mode. For more information, see the [gke_hub_feature_membership resource documentation](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/gke_hub_feature_membership#nested_mesh)"
56+
type = string
57+
validation {
58+
condition = anytrue([
59+
var.mesh_management == null,
60+
var.mesh_management == "",
61+
var.mesh_management == "MANAGEMENT_AUTOMATIC",
62+
var.mesh_management == "MANAGEMENT_MANUAL",
63+
])
64+
error_message = "Must be null, empty, or one of MANAGEMENT_AUTOMATIC or MANAGEMENT_MANUAL."
65+
}
66+
}
67+
5368
variable "multicluster_mode" {
5469
description = "[Preview] Determines whether remote secrets should be autogenerated across fleet cluster."
5570
type = string
@@ -70,7 +85,7 @@ variable "enable_cni" {
7085
}
7186

7287
variable "enable_vpc_sc" {
73-
description = "Determines whether to enable VPC-SC for this ASM installation. For more information read https://cloud.google.com/service-mesh/docs/managed/vpc-sc"
88+
description = "Determines whether to enable VPC-SC for this ASM installation. For more information read [VPC Service Controls for Managed Anthos Service Mesh](https://cloud.google.com/service-mesh/docs/managed/vpc-sc)"
7489
type = bool
7590
default = false
7691
}

modules/asm/versions.tf

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

1818
terraform {
19-
required_version = ">= 0.14.0"
19+
required_version = ">= 1.1"
2020

2121
required_providers {
2222
kubernetes = {

0 commit comments

Comments
 (0)