Skip to content

Commit bc41a98

Browse files
authored
chore: Adding the code for Part3 of the ACM GKE blog - enabling Config Connector and initializing GCP resources (terraform-google-modules#980)
1 parent 6042fd6 commit bc41a98

34 files changed

+1029
-18
lines changed

examples/acm-terraform-blog-part1/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ Subsequent articles will discuss other aspects of ACM to manage your GCP infrast
3636
3737
# continue in /terraform directory
3838
cd terraform
39-
39+
export TF_VAR_project=$PROJECT_ID
4040
terraform init
41-
terraform plan -var=project=$PROJECT_ID
42-
terraform apply -var=project=$PROJECT_ID
41+
terraform plan
42+
terraform apply
4343
```
4444
NOTE: if you get an error due to default network not being present, run `gcloud compute networks create default --subnet-mode=auto` and retry the commands.
4545

examples/acm-terraform-blog-part1/terraform/main.tf

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

1717
resource "google_gke_hub_membership" "membership" {
1818
provider = google-beta
19-
membership_id = "membership-hub"
19+
membership_id = "membership-hub-${module.gke.name}"
2020
endpoint {
2121
gke_cluster {
2222
resource_link = "//container.googleapis.com/${module.gke.cluster_id}"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright 2021 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+
region = "us-central1"
18+
zone = "us-central1-c"
19+
sync_repo = "https://github.com/terraform-google-modules/terraform-google-kubernetes-engine.git"
20+
sync_branch = "master"
21+
policy_dir = "examples/acm-terraform-blog-part1/config-root"

examples/acm-terraform-blog-part1/terraform/variables.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,24 @@ variable "project" {
2222
variable "region" {
2323
type = string
2424
description = "the GCP region where the cluster will be created"
25-
default = "us-central1"
2625
}
2726

2827
variable "zone" {
2928
type = string
3029
description = "the GCP zone in the region where the cluster will be created"
31-
default = "us-central1-c"
3230
}
3331

3432
variable "sync_repo" {
3533
type = string
3634
description = "git URL for the repo which will be sync'ed into the cluster via Config Management"
37-
default = "https://github.com/terraform-google-modules/terraform-google-kubernetes-engine.git"
3835
}
3936

4037
variable "sync_branch" {
4138
type = string
4239
description = "the git branch in the repo to sync"
43-
default = "master"
4440
}
4541

4642
variable "policy_dir" {
4743
type = string
4844
description = "the root directory in the repo branch that contains the resources."
49-
default = "examples/acm-terraform-blog-part1/config-root"
5045
}

examples/acm-terraform-blog-part2/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ Subsequent articles will discuss other aspects of ACM to manage your GCP infrast
2424
2525
# continue in /terraform directory
2626
cd terraform
27-
27+
export TF_VAR_project=$PROJECT_ID
2828
terraform init
29-
terraform plan -var=project=$PROJECT_ID
30-
terraform apply -var=project=$PROJECT_ID
29+
terraform plan
30+
terraform apply
3131
```
3232
NOTE: if you get an error due to default network not being present, run `gcloud compute networks create default --subnet-mode=auto` and retry the commands.
3333

examples/acm-terraform-blog-part2/terraform/main.tf

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

1717
resource "google_gke_hub_membership" "membership" {
1818
provider = google-beta
19-
membership_id = "membership-hub"
19+
membership_id = "membership-hub-${module.gke.name}"
2020
endpoint {
2121
gke_cluster {
2222
resource_link = "//container.googleapis.com/${module.gke.cluster_id}"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright 2021 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+
region = "us-central1"
18+
zone = "us-central1-c"
19+
sync_repo = "https://github.com/terraform-google-modules/terraform-google-kubernetes-engine.git"
20+
sync_branch = "master"
21+
policy_dir = "examples/acm-terraform-blog-part2/config-root"

examples/acm-terraform-blog-part2/terraform/variables.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,24 @@ variable "project" {
2222
variable "region" {
2323
type = string
2424
description = "the GCP region where the cluster will be created"
25-
default = "us-central1"
2625
}
2726

2827
variable "zone" {
2928
type = string
3029
description = "the GCP zone in the region where the cluster will be created"
31-
default = "us-central1-c"
3230
}
3331

3432
variable "sync_repo" {
3533
type = string
3634
description = "git URL for the repo which will be sync'ed into the cluster via Config Management"
37-
default = "https://github.com/terraform-google-modules/terraform-google-kubernetes-engine.git"
3835
}
3936

4037
variable "sync_branch" {
4138
type = string
4239
description = "the git branch in the repo to sync"
43-
default = "master"
4440
}
4541

4642
variable "policy_dir" {
4743
type = string
4844
description = "the root directory in the repo branch that contains the resources."
49-
default = "examples/acm-terraform-blog-part1/config-root"
5045
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Enable ACM features with Terraform - Part 3
2+
3+
This is part three of the tutorial to accompany a short series of blog articles explaining how to enable [Anthos Config Management (ACM)](https://cloud.google.com/anthos/config-management) with Terraform.
4+
5+
In the [first part](../acm-terraform-blog-part1), we explained how to use Terraform to create a cluster and manage its config from git via [Config Sync](https://cloud.google.com/anthos-config-management/docs/config-sync-overview).
6+
7+
In the [second part](../acm-terraform-blog-part2) we added guard rails for the cluster configuration via [Policy Controller](https://cloud.google.com/anthos-config-management/docs/concepts/policy-controller).
8+
9+
In this article we'll demonstrate how, using Config Connector, you can provision your GCP cloud resources following the same Kubernetes-native model.
10+
11+
## Provision GCP resources
12+
13+
1. Set the variable for the project from [part two](../acm-terraform-blog-part2). We will re-use that project but create a new cluster since we cleaned up at the end of the first section. If you are working in a different project, enable required GCP APIs, as described in [part one](../part1/README.md).
14+
15+
```bash
16+
PROJECT_ID = [PROJECT_ID]
17+
```
18+
1. Note that [wordpress-bundle.yaml](./config-root/wordpress-bundle) was updated to use GCP MySQL database. Also we added [configconnector.yaml](./config-root/configconnector.yaml) to initialize the instance of Config Connector add-on on the cluster.
19+
20+
1. Use [kpt](https://kpt.dev) to customize the `config-root` directory that will be configured as the source of the objects installed on the cluster.
21+
22+
```bash
23+
kpt fn eval --include-meta-resources --image gcr.io/kpt-fn/set-project-id:v0.1 ./config-root -- "project-id=$PROJECT_ID"
24+
kpt fn render ./config-root
25+
```
26+
1. Submit the updated configuration into your branch.
27+
1. Ensure that `sync_repo` and `sync_branch` variables are updated in [terraform.tfvars](./terraform/terraform.tfvars)
28+
1. Before running Terraform, notice the changes in [gke.tf](./terraform/gke.tf):
29+
- We are using the `[beta-public-cluster](../modules/beta-public-cluster)` module
30+
- `config_connector` variable is set to true
31+
- We are using `workload-identity` module to create a Google Service Account and connect it to a Kubernetes Service Account that is running in Config Connector `cnrm-system` namespace, allowing Config Connector to create GCP resource.
32+
1. As as in the previous part, create the cluster using Terraform:
33+
34+
```bash
35+
# obtain user access credentials to use for Terraform commands
36+
gcloud auth application-default login
37+
38+
# continue in /terraform directory
39+
cd terraform
40+
export TF_VAR_project=$PROJECT_ID
41+
terraform init
42+
terraform plan
43+
terraform apply
44+
```
45+
NOTE: if you get an error due to the default network not being present, run `gcloud compute networks create default --subnet-mode=auto` and retry the commands.
46+
47+
1. To verify things have synced and Policy Controller is installed, you can again use `gcloud` to check status:
48+
49+
```bash
50+
gcloud alpha container hub config-management status --project $PROJECT_ID
51+
```
52+
53+
As things initialize, you may see a few transient `error: KNV1021: No CustomResourceDefinition is defined` messages. This occurs when constraints from the repo are synced before Policy Controller has had a chance to load the appropriate template from the policy library. It will eventually reconcile.
54+
55+
After a short time, in addition to the `Status` showing as `SYNCED` and the `Last_Synced_Token` matching the repo, there should also be a value of `INSTALLED` for `Policy_Controller`.
56+
57+
58+
1. Connect your kubectl instance to the newly created cluster:
59+
60+
```bash
61+
# get values from cluster that was created
62+
export CLUSTER_ZONE=$(terraform output -raw cluster_location)
63+
export CLUSTER_NAME=$(terraform output -raw cluster_name)
64+
65+
# then get creditials for it
66+
gcloud container clusters get-credentials $CLUSTER_NAME --zone $CLUSTER_ZONE --project $PROJECT_ID
67+
68+
```
69+
70+
1. Verify that Config Connector addon is installed and configured:
71+
```bash
72+
kubectl wait -n cnrm-system --for=condition=Ready pod --all
73+
```
74+
75+
Note: The controller Pod can take several minutes to start. Once Config Connector is installed correctly, the output is similar to the following:
76+
77+
```bash
78+
pod/cnrm-controller-manager-0 condition met
79+
```
80+
1. It will take a while for the SQL database to be created. You can check on the status:
81+
```bash
82+
kubectl describe sqlinstance -n wp
83+
```
84+
85+
1. Finally, validate that Wordpress powered Cloud SQL database was created:
86+
87+
```bash
88+
curl -L $( kubectl get service wordpress-external -n wp -o=json | \
89+
jq -r '.status["loadBalancer"]["ingress"][0]["ip"]')
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: kpt.dev/v1
2+
kind: Kptfile
3+
metadata:
4+
name: WordPress powered by Google Cloud SQL
5+
info:
6+
description: create a WordPress in a container and connect it to a GCP-managed MySQL database
7+
pipeline:
8+
mutators:
9+
- image: gcr.io/kpt-fn/apply-setters:v0.1
10+
configMap:
11+
project-id: ""
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
apiVersion: config.gatekeeper.sh/v1alpha1
15+
kind: Config
16+
metadata:
17+
name: config
18+
namespace: "gatekeeper-system"
19+
spec:
20+
match:
21+
- excludedNamespaces: ["kube-system", "kube-public", "kube-node-lease", "config-management-system", "config-management-monitoring", "gatekeeper-system", "resource-group-system"]
22+
processes: ["audit"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
apiVersion: constraints.gatekeeper.sh/v1beta1
15+
kind: K8sRestrictRoleBindings
16+
metadata: # kpt-merge: /restrict-clusteradmin-rolebindings
17+
name: restrict-clusteradmin-rolebindings
18+
annotations:
19+
# This constraint is not certified by CIS.
20+
description: "Restricts use of the cluster-admin role."
21+
spec:
22+
enforcementAction: dryrun # kpt-set: ${enforcementAction}
23+
parameters:
24+
allowedSubjects:
25+
- name: "system:masters"
26+
kind: "Group"
27+
apiGroup: "rbac.authorization.k8s.io"
28+
restrictedRole:
29+
name: "cluster-admin"
30+
kind: "ClusterRole"
31+
apiGroup: "rbac.authorization.k8s.io"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
apiVersion: constraints.gatekeeper.sh/v1beta1
15+
kind: K8sProhibitRoleWildcardAccess
16+
metadata: # kpt-merge: /prohibit-role-wildcard-access
17+
name: prohibit-role-wildcard-access
18+
annotations:
19+
# This constraint is not certified by CIS.
20+
description: "Restricts use of wildcards in Roles and ClusterRoles."
21+
spec:
22+
enforcementAction: dryrun # kpt-set: ${enforcementAction}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
apiVersion: constraints.gatekeeper.sh/v1beta1
15+
kind: K8sPSPPrivilegedContainer
16+
metadata: # kpt-merge: /psp-privileged-container
17+
name: psp-privileged-container
18+
annotations:
19+
# This constraint is not certified by CIS.
20+
description: "Restricts containers with `securityContext.privileged` set to `true`."
21+
spec:
22+
enforcementAction: dryrun # kpt-set: ${enforcementAction}
23+
match:
24+
kinds:
25+
- apiGroups:
26+
- ''
27+
kinds:
28+
- Pod
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
apiVersion: constraints.gatekeeper.sh/v1beta1
15+
kind: K8sPSPHostNamespace
16+
metadata: # kpt-merge: /psp-host-namespace
17+
name: psp-host-namespace
18+
annotations:
19+
# This constraint is not certified by CIS.
20+
description: "Prohibits containers from running with `hostPID` or `hostIPC` set to `true`."
21+
spec:
22+
enforcementAction: dryrun # kpt-set: ${enforcementAction}
23+
match:
24+
kinds:
25+
- apiGroups: [""]
26+
kinds: ["Pod"]

0 commit comments

Comments
 (0)