Skip to content

Commit

Permalink
feat: add support for policy bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
apeabody committed Jan 13, 2023
1 parent d7f71f6 commit 406f9b4
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 15 deletions.
10 changes: 8 additions & 2 deletions examples/simple_zonal_with_acm/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Simple Zonal Cluster

This example illustrates how to create a simple cluster and install [Anthos Config Management](https://cloud.google.com/anthos-config-management/docs/).
This example illustrates how to create a simple cluster and install [Anthos Config Management](https://cloud.google.com/anthos-config-management/docs/)'s [Config Sync](https://cloud.google.com/anthos-config-management/docs/config-sync-overview) and [Policy Controller](https://cloud.google.com/anthos-config-management/docs/concepts/policy-controller) with the [Policy Essentials v2022 policy bundle](https://cloud.google.com/anthos-config-management/docs/how-to/using-policy-essentials-v2022).

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

Expand All @@ -27,13 +27,19 @@ After applying the Terraform configuration, you can run the following commands t
kubectl describe ns shipping-dev
```

4. You can also use `kubectl` to view any policy violations on the cluster:

```
kubectl get constraint -l policycontroller.gke.io/bundleName=policy-essentials-v2022 -o json | jq -cC '.items[]| [.metadata.name,.status.totalViolations]'
```

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| cluster\_name\_suffix | A suffix to append to the default cluster name | `string` | `""` | no |
| project\_id | The project ID to host the cluster in | `any` | n/a | yes |
| project\_id | The project ID to host the cluster in | `string` | n/a | yes |
| region | The region to host the cluster in | `string` | `"us-central1"` | no |
| zone | The zone to host the cluster in | `string` | `"us-central1-a"` | no |

Expand Down
2 changes: 2 additions & 0 deletions examples/simple_zonal_with_acm/acm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ module "acm" {
policy_dir = "foo-corp"

secret_type = "ssh"

policy_bundles = "https://github.com/GoogleCloudPlatform/acm-policy-controller-library.git/bundles/policy-essentials-v2022"
}
3 changes: 3 additions & 0 deletions examples/simple_zonal_with_acm/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@

variable "project_id" {
description = "The project ID to host the cluster in"
type = string
}

variable "cluster_name_suffix" {
description = "A suffix to append to the default cluster name"
type = string
default = ""
}

variable "region" {
description = "The region to host the cluster in"
type = string
default = "us-central1"
}

Expand Down
7 changes: 6 additions & 1 deletion examples/simple_zonal_with_acm/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ terraform {
version = "~> 4.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
source = "hashicorp/kubernetes"
version = "~> 2.10"
}
random = {
source = "hashicorp/random"
version = ">= 2.1"
}
}
required_version = ">= 0.13"
Expand Down
4 changes: 3 additions & 1 deletion modules/acm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ data "google_client_config" "default" {}
| cluster\_name | GCP cluster Name used to reach cluster and which becomes the cluster name in the Config Sync kubernetes custom resource. | `string` | n/a | yes |
| configmanagement\_version | Version of ACM. | `string` | `""` | no |
| create\_ssh\_key | Controls whether a key will be generated for Git authentication | `bool` | `true` | no |
| enable\_config\_sync | Whether to enable the ACM Config Sync on the cluster | `bool` | `true` | no |
| enable\_fleet\_feature | Whether to enable the ACM feature on the fleet. | `bool` | `true` | no |
| enable\_fleet\_registration | Whether to create a new membership. | `bool` | `true` | no |
| enable\_log\_denies | Whether to enable logging of all denies and dryrun failures for ACM Policy Controller. | `bool` | `false` | no |
Expand All @@ -77,13 +78,14 @@ data "google_client_config" "default" {}
| https\_proxy | URL for the HTTPS proxy to be used when communicating with the Git repo. | `string` | `null` | no |
| install\_template\_library | Whether to install the default Policy Controller template library | `bool` | `true` | no |
| location | GCP location used to reach cluster. | `string` | n/a | yes |
| policy\_bundles | A space separated list of Policy Controller policy bundles urls to install on the cluster. | `string` | `null` | no |
| policy\_dir | Subfolder containing configs in ACM Git repo. If un-set, uses Config Management default. | `string` | `""` | no |
| project\_id | GCP project\_id used to reach cluster. | `string` | n/a | yes |
| secret\_type | git authentication secret type, is passed through to ConfigManagement spec.git.secretType. Overriden to value 'ssh' if `create_ssh_key` is true | `string` | `"ssh"` | no |
| source\_format | Configures a non-hierarchical repo if set to 'unstructured'. Uses [ACM defaults](https://cloud.google.com/anthos-config-management/docs/how-to/installing#configuring-config-management-operator) when unset. | `string` | `""` | no |
| ssh\_auth\_key | Key for Git authentication. Overrides 'create\_ssh\_key' variable. Can be set using 'file(path/to/file)'-function. | `string` | `null` | no |
| sync\_branch | ACM repo Git branch. If un-set, uses Config Management default. | `string` | `""` | no |
| sync\_repo | ACM Git repo address | `string` | n/a | yes |
| sync\_repo | ACM Git repo address | `string` | `""` | no |
| sync\_revision | ACM repo Git revision. If un-set, uses Config Management default. | `string` | `""` | no |

## Outputs
Expand Down
2 changes: 1 addition & 1 deletion modules/acm/creds.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ resource "kubernetes_secret_v1" "creds" {
}

data = {
"${local.k8sop_creds_secret_key}" = local.private_key
"local.k8sop_creds_secret_key" = local.private_key
}
}
22 changes: 13 additions & 9 deletions modules/acm/feature.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ resource "google_gke_hub_feature_membership" "main" {
configmanagement {
version = var.configmanagement_version

config_sync {
source_format = var.source_format != "" ? var.source_format : null
dynamic "config_sync" {
for_each = var.enable_config_sync ? [{ enabled = true }] : []

git {
sync_repo = var.sync_repo
policy_dir = var.policy_dir != "" ? var.policy_dir : null
sync_branch = var.sync_branch != "" ? var.sync_branch : null
sync_rev = var.sync_revision != "" ? var.sync_revision : null
secret_type = var.secret_type
https_proxy = var.https_proxy
content {
source_format = var.source_format != "" ? var.source_format : null

git {
sync_repo = var.sync_repo
policy_dir = var.policy_dir != "" ? var.policy_dir : null
sync_branch = var.sync_branch != "" ? var.sync_branch : null
sync_rev = var.sync_revision != "" ? var.sync_revision : null
secret_type = var.secret_type
https_proxy = var.https_proxy
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/acm/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

output "git_creds_public" {
description = "Public key of SSH keypair to allow the Anthos Config Management Operator to authenticate to your Git repository."
value = var.create_ssh_key ? coalesce(tls_private_key.k8sop_creds.*.public_key_openssh...) : null
value = var.create_ssh_key ? coalesce(tls_private_key.k8sop_creds[*].public_key_openssh...) : null
}

output "configmanagement_version" {
Expand Down
33 changes: 33 additions & 0 deletions modules/acm/policy_bundles.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

locals {
policy_bundles = var.policy_bundles != null ? var.policy_bundles : ""
}

module "policy_bundles" {
source = "terraform-google-modules/gcloud/google//modules/kubectl-wrapper"
version = "~> 3.1"

project_id = var.project_id
cluster_name = var.cluster_name
cluster_location = var.location
enabled = var.policy_bundles != null ? true : false
kubectl_create_command = "kubectl apply -k ${local.policy_bundles}"
kubectl_destroy_command = "kubectl delete -k ${local.policy_bundles}"

module_depends_on = ["google_gke_hub_feature_membership.main"]
}
13 changes: 13 additions & 0 deletions modules/acm/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ variable "configmanagement_version" {
variable "sync_repo" {
description = "ACM Git repo address"
type = string
default = ""
}

variable "sync_branch" {
Expand Down Expand Up @@ -108,6 +109,12 @@ variable "ssh_auth_key" {
default = null
}

variable "enable_config_sync" {
description = "Whether to enable the ACM Config Sync on the cluster"
type = bool
default = true
}

# Policy Controller config
variable "enable_policy_controller" {
description = "Whether to enable the ACM Policy Controller on the cluster"
Expand Down Expand Up @@ -139,3 +146,9 @@ variable "enable_referential_rules" {
type = bool
default = true
}

variable "policy_bundles" {
description = "A space separated list of Policy Controller policy bundles urls to install on the cluster."
type = string
default = null
}

0 comments on commit 406f9b4

Please sign in to comment.