Skip to content

Commit d7c5ee1

Browse files
Merge pull request #7 from ankur6405/custom-credentials
added variable to store secret and pass custom credentials
2 parents 6b722ef + 4e97e5a commit d7c5ee1

File tree

8 files changed

+94
-66
lines changed

8 files changed

+94
-66
lines changed

README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,30 @@ This module creates a Redis master and one or more Redis slaves, depending on th
1414

1515
| Redis Helm Chart Version | K8s supported version |
1616
| :-----: | :--- |
17-
| **16.13.2** | **1.23,1.24,1.25** |
17+
| **16.13.2** | **1.23,1.24,1.25,1.26,1.27** |
1818

1919
## Usage Example
2020

2121
```hcl
2222
module "redis" {
2323
source = "squareops/redis/kubernetes"
2424
redis_config = {
25-
name = "redis"
26-
values_yaml = ""
27-
environment = "prod"
28-
architecture = "replication"
29-
slave_volume_size = "10Gi"
30-
master_volume_size = "10Gi"
31-
storage_class_name = "gp3"
32-
slave_replica_count = 2
25+
name = "redis"
26+
values_yaml = ""
27+
environment = "prod"
28+
architecture = "replication"
29+
slave_volume_size = "10Gi"
30+
master_volume_size = "10Gi"
31+
storage_class_name = "gp3"
32+
slave_replica_count = 2
33+
store_password_to_secret_manager = true
3334
}
3435
grafana_monitoring_enabled = true
3536
recovery_window_aws_secret = 0
37+
custom_credentials_enabled = true
38+
custom_credentials_config = {
39+
password = "aajdhgduy3873683dh"
40+
}
3641
}
3742
3843
```
@@ -85,18 +90,19 @@ No modules.
8590
| <a name="input_app_version"></a> [app\_version](#input\_app\_version) | Version of the Redis application that will be deployed. | `string` | `"6.2.7-debian-11-r11"` | no |
8691
| <a name="input_chart_version"></a> [chart\_version](#input\_chart\_version) | Version of the chart for the Redis application that will be deployed. | `string` | `"16.13.2"` | no |
8792
| <a name="input_create_namespace"></a> [create\_namespace](#input\_create\_namespace) | Specify whether or not to create the namespace if it does not already exist. Set it to true to create the namespace. | `string` | `true` | no |
93+
| <a name="input_custom_credentials_config"></a> [custom\_credentials\_config](#input\_custom\_credentials\_config) | Specify the configuration settings for Redis to pass custom credentials during creation. | `any` | <pre>{<br> "password": ""<br>}</pre> | no |
94+
| <a name="input_custom_credentials_enabled"></a> [custom\_credentials\_enabled](#input\_custom\_credentials\_enabled) | Specifies whether to enable custom credentials for Redis. | `bool` | `false` | no |
8895
| <a name="input_grafana_monitoring_enabled"></a> [grafana\_monitoring\_enabled](#input\_grafana\_monitoring\_enabled) | Specify whether or not to deploy Redis exporter to collect Redis metrics for monitoring in Grafana. | `bool` | `false` | no |
8996
| <a name="input_namespace"></a> [namespace](#input\_namespace) | Namespace where the Redis resources will be deployed. | `string` | `"redis"` | no |
9097
| <a name="input_recovery_window_aws_secret"></a> [recovery\_window\_aws\_secret](#input\_recovery\_window\_aws\_secret) | Number of days that AWS Secrets Manager will wait before it can delete the secret. The value can be 0 to force deletion without recovery, or a range from 7 to 30 days. | `number` | `0` | no |
91-
| <a name="input_redis_config"></a> [redis\_config](#input\_redis\_config) | Specify the configuration settings for Redis, including the name, environment, storage options, replication settings, and custom YAML values. | `any` | <pre>{<br> "architecture": "replication",<br> "environment": "",<br> "master_volume_size": "",<br> "name": "",<br> "slave_replica_count": 1,<br> "slave_volume_size": "",<br> "storage_class_name": "",<br> "values_yaml": ""<br>}</pre> | no |
98+
| <a name="input_redis_config"></a> [redis\_config](#input\_redis\_config) | Specify the configuration settings for Redis, including the name, environment, storage options, replication settings, store password to secret manager and custom YAML values. | `any` | <pre>{<br> "architecture": "replication",<br> "environment": "",<br> "master_volume_size": "",<br> "name": "",<br> "slave_replica_count": 1,<br> "slave_volume_size": "",<br> "storage_class_name": "",<br> "store_password_to_secret_manager": "",<br> "values_yaml": ""<br>}</pre> | no |
9299

93100
## Outputs
94101

95102
| Name | Description |
96103
|------|-------------|
97-
| <a name="output_redis_master_endpoint"></a> [redis\_master\_endpoint](#output\_redis\_master\_endpoint) | The endpoint for the Redis Master Service, which is the primary node in the Redis cluster responsible for handling read-write operations. |
98-
| <a name="output_redis_port"></a> [redis\_port](#output\_redis\_port) | The port number on which Redis is running. |
99-
| <a name="output_redis_slave_endpoint"></a> [redis\_slave\_endpoint](#output\_redis\_slave\_endpoint) | The endpoint for the Redis Slave Service, which is a secondary node in the Redis cluster responsible for handling read-only operations. |
104+
| <a name="output_redis_credential"></a> [redis\_credential](#output\_redis\_credential) | Redis credentials used for accessing the database. |
105+
| <a name="output_redis_endpoints"></a> [redis\_endpoints](#output\_redis\_endpoints) | Redis endpoints in the Kubernetes cluster. |
100106
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
101107

102108
## Contribution & Issue Reporting

examples/complete/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ No requirements.
2121

2222
| Name | Source | Version |
2323
|------|--------|---------|
24-
| <a name="module_redis"></a> [redis](#module\_redis) | squareops/redis/kubernetes.git | n/a |
24+
| <a name="module_redis"></a> [redis](#module\_redis) | squareops/redis/kubernetes | n/a |
2525

2626
## Resources
2727

@@ -38,7 +38,6 @@ No inputs.
3838

3939
| Name | Description |
4040
|------|-------------|
41-
| <a name="output_redis_master_endpoint"></a> [redis\_master\_endpoint](#output\_redis\_master\_endpoint) | The endpoint for the Redis Master Service, which is the primary node in the Redis cluster responsible for handling read-write operations. |
42-
| <a name="output_redis_port"></a> [redis\_port](#output\_redis\_port) | The port number on which Redis is running. |
43-
| <a name="output_redis_slave_endpoint"></a> [redis\_slave\_endpoint](#output\_redis\_slave\_endpoint) | The endpoint for the Redis Slave Service, which is a secondary node in the Redis cluster responsible for handling read-only operations. |
41+
| <a name="output_redis_credential"></a> [redis\_credential](#output\_redis\_credential) | Redis credentials used for accessing the database. |
42+
| <a name="output_redis_endpoints"></a> [redis\_endpoints](#output\_redis\_endpoints) | Redis endpoints in the Kubernetes cluster. |
4443
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/complete/main.tf

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,26 @@ locals {
77
Expires = "Never"
88
Department = "Engineering"
99
}
10+
store_password_to_secret_manager = true
1011
}
1112

1213
module "redis" {
1314
source = "squareops/redis/kubernetes"
1415
redis_config = {
15-
name = local.name
16-
values_yaml = file("./helm/values.yaml")
17-
environment = local.environment
18-
architecture = "replication"
19-
slave_volume_size = "10Gi"
20-
master_volume_size = "10Gi"
21-
storage_class_name = "gp3"
22-
slave_replica_count = 2
16+
name = local.name
17+
values_yaml = file("./helm/values.yaml")
18+
environment = local.environment
19+
architecture = "replication"
20+
slave_volume_size = "10Gi"
21+
master_volume_size = "10Gi"
22+
storage_class_name = "gp3"
23+
slave_replica_count = 2
24+
store_password_to_secret_manager = local.store_password_to_secret_manager
2325
}
2426
grafana_monitoring_enabled = true
2527
recovery_window_aws_secret = 0
28+
custom_credentials_enabled = true
29+
custom_credentials_config = {
30+
password = "aajdhgduy3873683dh"
31+
}
2632
}

examples/complete/output.tf

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
output "redis_port" {
2-
value = "6379"
3-
description = "The port number on which Redis is running."
1+
output "redis_endpoints" {
2+
description = "Redis endpoints in the Kubernetes cluster."
3+
value = module.redis.redis_endpoints
44
}
55

6-
output "redis_master_endpoint" {
7-
value = module.redis.redis_master_endpoint
8-
description = "The endpoint for the Redis Master Service, which is the primary node in the Redis cluster responsible for handling read-write operations."
9-
}
10-
11-
output "redis_slave_endpoint" {
12-
value = module.redis.redis_slave_endpoint
13-
description = "The endpoint for the Redis Slave Service, which is a secondary node in the Redis cluster responsible for handling read-only operations."
6+
output "redis_credential" {
7+
description = "Redis credentials used for accessing the database."
8+
value = local.store_password_to_secret_manager ? null : module.redis.redis_credential
149
}

examples/complete/provider.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ provider "kubernetes" {
1818
host = data.aws_eks_cluster.cluster.endpoint
1919
cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
2020
token = data.aws_eks_cluster_auth.cluster.token
21-
2221
}
2322

2423
provider "helm" {
2524
kubernetes {
2625
host = data.aws_eks_cluster.cluster.endpoint
2726
cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
2827
token = data.aws_eks_cluster_auth.cluster.token
29-
3028
}
3129
}

main.tf

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
resource "random_password" "redis_password" {
2+
count = var.custom_credentials_enabled ? 0 : 1
23
length = 20
34
special = false
45
}
56

67
resource "aws_secretsmanager_secret" "redis_password" {
8+
count = var.redis_config.store_password_to_secret_manager ? 1 : 0
79
name = format("%s/%s/%s", var.redis_config.environment, var.redis_config.name, "redis")
810
recovery_window_in_days = var.recovery_window_aws_secret
911
}
1012

1113
resource "aws_secretsmanager_secret_version" "redis_password" {
12-
secret_id = aws_secretsmanager_secret.redis_password.id
13-
secret_string = <<EOF
14-
{
15-
"username": "root",
16-
"password": "${random_password.redis_password.result}"
17-
}
18-
EOF
14+
count = var.redis_config.store_password_to_secret_manager ? 1 : 0
15+
secret_id = aws_secretsmanager_secret.redis_password[0].id
16+
secret_string = var.custom_credentials_enabled ? jsonencode(
17+
{
18+
"redis_username" : "root",
19+
"redis_password" : "${var.custom_credentials_config.password}"
20+
21+
}) : jsonencode(
22+
{
23+
"redis_username" : "root",
24+
"redis_password" : "${random_password.redis_password[0].result}"
25+
})
1926
}
2027

2128
resource "kubernetes_namespace" "redis" {
@@ -38,7 +45,7 @@ resource "helm_release" "redis" {
3845
templatefile("${path.module}/helm/values/values.yaml", {
3946
app_version = var.app_version,
4047
architecture = var.redis_config.architecture,
41-
redis_password = random_password.redis_password.result,
48+
redis_password = var.custom_credentials_enabled ? var.custom_credentials_config.password : random_password.redis_password[0].result,
4249
slave_volume_size = var.redis_config.slave_volume_size,
4350
slave_replicacount = var.redis_config.slave_replica_count,
4451
storage_class_name = var.redis_config.storage_class_name,

output.tf

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
output "redis_port" {
2-
value = "6379"
3-
description = "The port number on which Redis is running."
1+
output "redis_endpoints" {
2+
description = "Redis endpoints in the Kubernetes cluster."
3+
value = {
4+
redis_port = "6379",
5+
redis_master_endpoint = "redis-master.${var.namespace}.svc.cluster.local",
6+
redis_slave_endpoint = "redis-replicas.${var.namespace}.svc.cluster.local"
7+
}
48
}
59

6-
output "redis_master_endpoint" {
7-
value = "redis-master.${var.namespace}.svc.cluster.local"
8-
description = "The endpoint for the Redis Master Service, which is the primary node in the Redis cluster responsible for handling read-write operations."
9-
}
10-
11-
output "redis_slave_endpoint" {
12-
value = "redis-replicas.${var.namespace}.svc.cluster.local"
13-
description = "The endpoint for the Redis Slave Service, which is a secondary node in the Redis cluster responsible for handling read-only operations."
10+
output "redis_credential" {
11+
description = "Redis credentials used for accessing the database."
12+
value = var.redis_config.store_password_to_secret_manager ? null : {
13+
redis_username = "root",
14+
redis_password = var.custom_credentials_enabled ? var.custom_credentials_config.password : nonsensitive(random_password.redis_password[0].result)
15+
}
1416
}

variables.tf

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
variable "redis_config" {
22
type = any
33
default = {
4-
name = ""
5-
environment = ""
6-
master_volume_size = ""
7-
architecture = "replication"
8-
slave_replica_count = 1
9-
slave_volume_size = ""
10-
storage_class_name = ""
11-
values_yaml = ""
4+
name = ""
5+
environment = ""
6+
master_volume_size = ""
7+
architecture = "replication"
8+
slave_replica_count = 1
9+
slave_volume_size = ""
10+
storage_class_name = ""
11+
store_password_to_secret_manager = ""
12+
values_yaml = ""
1213
}
13-
description = "Specify the configuration settings for Redis, including the name, environment, storage options, replication settings, and custom YAML values."
14+
description = "Specify the configuration settings for Redis, including the name, environment, storage options, replication settings, store password to secret manager and custom YAML values."
1415
}
1516

1617
variable "chart_version" {
@@ -48,3 +49,17 @@ variable "create_namespace" {
4849
description = "Specify whether or not to create the namespace if it does not already exist. Set it to true to create the namespace."
4950
default = true
5051
}
52+
53+
variable "custom_credentials_enabled" {
54+
type = bool
55+
default = false
56+
description = "Specifies whether to enable custom credentials for Redis."
57+
}
58+
59+
variable "custom_credentials_config" {
60+
type = any
61+
default = {
62+
password = ""
63+
}
64+
description = "Specify the configuration settings for Redis to pass custom credentials during creation."
65+
}

0 commit comments

Comments
 (0)