Skip to content
This repository was archived by the owner on Dec 16, 2020. It is now read-only.

Commit 720588a

Browse files
authored
Merge pull request #43 from gruntwork-io/yori-address-create-resources
Add test for checking create_resources does not actually create any resources
2 parents 1bd61be + c478a7b commit 720588a

File tree

14 files changed

+102
-61
lines changed

14 files changed

+102
-61
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defaults: &defaults
88
KUBERGRUNT_VERSION: v0.5.1
99
HELM_VERSION: v2.12.2
1010
MODULE_CI_VERSION: v0.14.1
11-
TERRAFORM_VERSION: 0.12.9
11+
TERRAFORM_VERSION: 0.12.11
1212
TERRAGRUNT_VERSION: NONE
1313
PACKER_VERSION: NONE
1414
GOLANG_VERSION: 1.11.2

examples/k8s-namespace-with-service-account/main.tf

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ module "namespace" {
2828
# source = "git::https://github.com/gruntwork-io/terraform-kubernetes-helm.git//modules/k8s-namespace?ref=v0.0.1"
2929
source = "../../modules/k8s-namespace"
3030

31-
name = var.name
31+
create_resources = var.create_resources
32+
name = var.name
3233
}
3334

3435
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -41,9 +42,10 @@ module "service_account_access_all" {
4142
# source = "git::https://github.com/gruntwork-io/terraform-kubernetes-helm.git//modules/k8s-service-account?ref=v0.0.1"
4243
source = "../../modules/k8s-service-account"
4344

44-
name = "${var.name}-admin"
45-
namespace = module.namespace.name
46-
num_rbac_roles = 1
45+
create_resources = var.create_resources
46+
name = "${var.name}-admin"
47+
namespace = module.namespace.name
48+
num_rbac_roles = 1
4749

4850
rbac_roles = [
4951
{
@@ -64,9 +66,10 @@ module "service_account_access_read_only" {
6466
# source = "git::https://github.com/gruntwork-io/terraform-kubernetes-helm.git//modules/k8s-service-account?ref=v0.0.1"
6567
source = "../../modules/k8s-service-account"
6668

67-
name = "${var.name}-read-only"
68-
namespace = module.namespace.name
69-
num_rbac_roles = 1
69+
create_resources = var.create_resources
70+
name = "${var.name}-read-only"
71+
namespace = module.namespace.name
72+
num_rbac_roles = 1
7073

7174
rbac_roles = [
7275
{

examples/k8s-namespace-with-service-account/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@ variable "kubectl_config_path" {
1919
type = string
2020
default = "~/.kube/config"
2121
}
22+
23+
# ---------------------------------------------------------------------------------------------------------------------
24+
# TEST PARAMETERS
25+
# These variables are only used for testing purposes and should not be touched in normal operations.
26+
# ---------------------------------------------------------------------------------------------------------------------
27+
28+
variable "create_resources" {
29+
description = "Set to false to have this module skip creating resources."
30+
type = bool
31+
default = true
32+
}

modules/k8s-namespace-roles/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ variable "annotations" {
2626
}
2727

2828
variable "create_resources" {
29-
description = "Set to false to have this module create no resources. This weird parameter exists solely because Terraform does not support conditional modules. Therefore, this is a hack to allow you to conditionally decide if the Namespace roles should be created or not."
29+
description = "Set to false to have this module skip creating resources. This weird parameter exists solely because Terraform does not support conditional modules. Therefore, this is a hack to allow you to conditionally decide if the Namespace roles should be created or not."
3030
type = bool
3131
default = true
3232
}

modules/k8s-namespace/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ resource "kubernetes_namespace" "namespace" {
4949
module "namespace_roles" {
5050
source = "../k8s-namespace-roles"
5151

52-
namespace = kubernetes_namespace.namespace[0].id
52+
namespace = var.create_resources ? kubernetes_namespace.namespace[0].id : ""
5353
labels = var.labels
5454
annotations = var.annotations
5555

modules/k8s-namespace/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ variable "annotations" {
2626
}
2727

2828
variable "create_resources" {
29-
description = "Set to false to have this module create no resources. This weird parameter exists solely because Terraform does not support conditional modules. Therefore, this is a hack to allow you to conditionally decide if the Namespace should be created or not."
29+
description = "Set to false to have this module skip creating resources. This weird parameter exists solely because Terraform does not support conditional modules. Therefore, this is a hack to allow you to conditionally decide if the Namespace should be created or not."
3030
type = bool
3131
default = true
3232
}

modules/k8s-service-account/main.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ resource "null_resource" "dependency_getter" {
3131
# ---------------------------------------------------------------------------------------------------------------------
3232

3333
resource "kubernetes_service_account" "service_account" {
34+
count = var.create_resources ? 1 : 0
35+
3436
metadata {
3537
name = var.name
3638
namespace = var.namespace
@@ -62,7 +64,7 @@ resource "kubernetes_service_account" "service_account" {
6264
# ---------------------------------------------------------------------------------------------------------------------
6365

6466
resource "kubernetes_role_binding" "service_account_role_binding" {
65-
count = var.num_rbac_roles
67+
count = var.create_resources ? var.num_rbac_roles : 0
6668

6769
metadata {
6870
name = "${var.name}-${var.rbac_roles[count.index]["name"]}-role-binding"
@@ -80,7 +82,7 @@ resource "kubernetes_role_binding" "service_account_role_binding" {
8082
subject {
8183
api_group = ""
8284
kind = "ServiceAccount"
83-
name = kubernetes_service_account.service_account.metadata[0].name
85+
name = kubernetes_service_account.service_account[0].metadata[0].name
8486
namespace = var.namespace
8587
}
8688

modules/k8s-service-account/outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
output "name" {
22
description = "The name of the created service account"
3-
value = kubernetes_service_account.service_account.metadata[0].name
3+
value = var.create_resources ? kubernetes_service_account.service_account[0].metadata[0].name : ""
44

55
depends_on = [kubernetes_role_binding.service_account_role_binding]
66
}
77

88
output "token_secret_name" {
99
description = "The name of the secret that holds the default ServiceAccount token that can be used to authenticate to the Kubernetes API."
10-
value = kubernetes_service_account.service_account.default_secret_name
10+
value = var.create_resources ? kubernetes_service_account.service_account[0].default_secret_name : ""
1111

1212
depends_on = [kubernetes_role_binding.service_account_role_binding]
1313
}

modules/k8s-service-account/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ variable "secrets_for_pods" {
6868
default = []
6969
}
7070

71+
variable "create_resources" {
72+
description = "Set to false to have this module skip creating resources. This weird parameter exists solely because Terraform does not support conditional modules. Therefore, this is a hack to allow you to conditionally decide if the Namespace should be created or not."
73+
type = bool
74+
default = true
75+
}
76+
7177
# ---------------------------------------------------------------------------------------------------------------------
7278
# MODULE DEPENDENCIES
7379
# Workaround Terraform limitation where there is no module depends_on.

test/Gopkg.lock

Lines changed: 5 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)