Skip to content

Commit d2e82b9

Browse files
authored
feat: added support to optionally add a prefix to all resources deployed by the solutions. This can be achieved by passing a value for the new input variable prefix (#33)
1 parent 430d733 commit d2e82b9

File tree

6 files changed

+50
-31
lines changed

6 files changed

+50
-31
lines changed

solutions/agents/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ module "observability_agents" {
1717
cluster_config_endpoint_type = var.cluster_config_endpoint_type
1818
# Log Analysis Agent
1919
log_analysis_enabled = var.log_analysis_enabled
20-
log_analysis_agent_name = var.log_analysis_agent_name
20+
log_analysis_agent_name = var.prefix != null ? "${var.prefix}-${var.log_analysis_agent_name}" : var.log_analysis_agent_name
2121
log_analysis_agent_namespace = var.log_analysis_agent_namespace
2222
log_analysis_instance_region = var.log_analysis_instance_region
2323
log_analysis_ingestion_key = var.log_analysis_ingestion_key
24-
log_analysis_secret_name = var.log_analysis_secret_name
24+
log_analysis_secret_name = var.prefix != null ? "${var.prefix}-${var.log_analysis_secret_name}" : var.log_analysis_secret_name
2525
log_analysis_agent_tolerations = var.log_analysis_agent_tolerations
2626
log_analysis_agent_tags = var.log_analysis_agent_tags
2727
log_analysis_endpoint_type = var.log_analysis_endpoint_type
@@ -33,11 +33,11 @@ module "observability_agents" {
3333
log_analysis_agent_custom_line_inclusion = var.log_analysis_agent_custom_line_exclusion
3434
# Cloud Monitoring (Sysdig) Agent
3535
cloud_monitoring_enabled = var.cloud_monitoring_enabled
36-
cloud_monitoring_agent_name = var.cloud_monitoring_agent_name
36+
cloud_monitoring_agent_name = var.prefix != null ? "${var.prefix}-${var.cloud_monitoring_agent_name}" : var.cloud_monitoring_agent_name
3737
cloud_monitoring_agent_namespace = var.cloud_monitoring_agent_namespace
3838
cloud_monitoring_endpoint_type = var.cloud_monitoring_endpoint_type
3939
cloud_monitoring_access_key = var.cloud_monitoring_access_key
40-
cloud_monitoring_secret_name = var.cloud_monitoring_secret_name
40+
cloud_monitoring_secret_name = var.prefix != null ? "${var.prefix}-${var.cloud_monitoring_secret_name}" : var.cloud_monitoring_secret_name
4141
cloud_monitoring_metrics_filter = var.cloud_monitoring_metrics_filter
4242
cloud_monitoring_agent_tags = var.cloud_monitoring_agent_tags
4343
cloud_monitoring_instance_region = var.cloud_monitoring_instance_region

solutions/agents/variables.tf

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ variable "ibmcloud_api_key" {
44
sensitive = true
55
}
66

7+
variable "prefix" {
8+
type = string
9+
description = "(Optional) Prefix to append to all resources created by this solution."
10+
default = null
11+
}
12+
713
##############################################################################
814
# Cluster variables
915
##############################################################################
@@ -62,7 +68,7 @@ variable "log_analysis_ingestion_key" {
6268

6369
variable "log_analysis_secret_name" {
6470
type = string
65-
description = "The name of the secret which will store the ingestion key."
71+
description = "The name of the secret which will store the ingestion key. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
6672
default = "logdna-agent"
6773
nullable = false
6874
}
@@ -96,7 +102,7 @@ variable "log_analysis_agent_custom_line_exclusion" {
96102
}
97103

98104
variable "log_analysis_agent_name" {
99-
description = "Log Analysis agent name. Used for naming all kubernetes and helm resources on the cluster."
105+
description = "Log Analysis agent name. Used for naming all kubernetes and helm resources on the cluster. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
100106
type = string
101107
default = "logdna-agent"
102108
nullable = false
@@ -142,7 +148,7 @@ variable "cloud_monitoring_access_key" {
142148

143149
variable "cloud_monitoring_secret_name" {
144150
type = string
145-
description = "The name of the secret which will store the access key."
151+
description = "The name of the secret which will store the access key. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
146152
default = "sysdig-agent"
147153
nullable = false
148154
}
@@ -190,7 +196,7 @@ variable "cloud_monitoring_add_cluster_name" {
190196
}
191197

192198
variable "cloud_monitoring_agent_name" {
193-
description = "Cloud Monitoring agent name. Used for naming all kubernetes and helm resources on the cluster."
199+
description = "Cloud Monitoring agent name. Used for naming all kubernetes and helm resources on the cluster. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
194200
type = string
195201
default = "sysdig-agent"
196202
}

solutions/instances/main.tf

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,30 @@ locals {
66
archive_api_key = var.log_archive_api_key == null ? var.ibmcloud_api_key : var.log_archive_api_key
77
default_cos_region = var.cos_region != null ? var.cos_region : var.region
88

9+
cos_key_ring_name = var.prefix != null ? "${var.prefix}-${var.cos_key_ring_name}" : var.cos_key_ring_name
10+
cos_key_name = var.prefix != null ? "${var.prefix}-${var.cos_key_name}" : var.cos_key_name
11+
log_archive_cos_bucket_name = var.prefix != null ? "${var.prefix}-${var.log_archive_cos_bucket_name}" : var.log_archive_cos_bucket_name
12+
at_cos_target_bucket_name = var.prefix != null ? "${var.prefix}-${var.at_cos_target_bucket_name}" : var.at_cos_target_bucket_name
13+
914
cos_instance_crn = var.existing_cos_instance_crn != null ? var.existing_cos_instance_crn : module.cos_instance[0].cos_instance_crn
1015
existing_kms_guid = var.existing_kms_instance_crn != null ? element(split(":", var.existing_kms_instance_crn), length(split(":", var.existing_kms_instance_crn)) - 3) : length(local.bucket_config_map) == 2 ? null : tobool("The CRN of the existing KMS is not provided.")
1116
cos_instance_guid = var.existing_cos_instance_crn == null ? module.cos_instance[0].cos_instance_guid : element(split(":", var.existing_cos_instance_crn), length(split(":", var.existing_cos_instance_crn)) - 3)
12-
archive_cos_bucket_name = var.existing_log_archive_cos_bucket_name != null ? var.existing_log_archive_cos_bucket_name : module.cos_bucket[0].buckets[var.log_archive_cos_bucket_name].bucket_name
13-
archive_cos_bucket_endpoint = var.existing_log_archive_cos_bucket_endpoint != null ? var.existing_log_archive_cos_bucket_endpoint : module.cos_bucket[0].buckets[var.log_archive_cos_bucket_name].s3_endpoint_private
14-
cos_kms_key_crn = (var.existing_log_archive_cos_bucket_name != null && var.existing_at_cos_target_bucket_name != null) ? null : var.existing_cos_kms_key_crn != null ? var.existing_cos_kms_key_crn : module.kms[0].keys[format("%s.%s", var.cos_key_ring_name, var.cos_key_name)].crn
17+
archive_cos_bucket_name = var.existing_log_archive_cos_bucket_name != null ? var.existing_log_archive_cos_bucket_name : module.cos_bucket[0].buckets[local.log_archive_cos_bucket_name].bucket_name
18+
archive_cos_bucket_endpoint = var.existing_log_archive_cos_bucket_endpoint != null ? var.existing_log_archive_cos_bucket_endpoint : module.cos_bucket[0].buckets[local.log_archive_cos_bucket_name].s3_endpoint_private
19+
cos_kms_key_crn = (var.existing_log_archive_cos_bucket_name != null && var.existing_at_cos_target_bucket_name != null) ? null : var.existing_cos_kms_key_crn != null ? var.existing_cos_kms_key_crn : module.kms[0].keys[format("%s.%s", local.cos_key_ring_name, local.cos_key_name)].crn
1520

16-
cos_target_bucket_name = var.existing_at_cos_target_bucket_name != null ? var.existing_at_cos_target_bucket_name : module.cos_bucket[0].buckets[var.at_cos_target_bucket_name].bucket_name
17-
cos_target_bucket_endpoint = var.existing_at_cos_target_bucket_endpoint != null ? var.existing_at_cos_target_bucket_endpoint : module.cos_bucket[0].buckets[var.at_cos_target_bucket_name].s3_endpoint_private
21+
cos_target_bucket_name = var.existing_at_cos_target_bucket_name != null ? var.existing_at_cos_target_bucket_name : module.cos_bucket[0].buckets[local.at_cos_target_bucket_name].bucket_name
22+
cos_target_bucket_endpoint = var.existing_at_cos_target_bucket_endpoint != null ? var.existing_at_cos_target_bucket_endpoint : module.cos_bucket[0].buckets[local.at_cos_target_bucket_name].s3_endpoint_private
1823

1924
bucket_config_1 = var.existing_log_archive_cos_bucket_name == null ? {
2025
class = var.log_archive_cos_bucket_class
21-
name = var.log_archive_cos_bucket_name
26+
name = local.log_archive_cos_bucket_name
2227
tag = var.archive_bucket_access_tags
2328
} : null
2429

2530
bucket_config_2 = var.existing_at_cos_target_bucket_name == null ? {
2631
class = var.at_cos_target_bucket_class
27-
name = var.at_cos_target_bucket_name
32+
name = local.at_cos_target_bucket_name
2833
tag = var.at_cos_bucket_access_tags
2934
} : null
3035

@@ -61,7 +66,7 @@ locals {
6166
module "resource_group" {
6267
source = "terraform-ibm-modules/resource-group/ibm"
6368
version = "1.1.5"
64-
resource_group_name = var.use_existing_resource_group == false ? var.resource_group_name : null
69+
resource_group_name = var.use_existing_resource_group == false ? (var.prefix != null ? "${var.prefix}-${var.resource_group_name}" : var.resource_group_name) : null
6570
existing_resource_group_name = var.use_existing_resource_group == true ? var.resource_group_name : null
6671
}
6772

@@ -82,7 +87,7 @@ module "observability_instance" {
8287
ibmcloud_api_key = local.archive_api_key
8388
# Log Analysis
8489
log_analysis_provision = true
85-
log_analysis_instance_name = var.log_analysis_instance_name
90+
log_analysis_instance_name = var.prefix != null ? "${var.prefix}-${var.log_analysis_instance_name}" : var.log_analysis_instance_name
8691
log_analysis_plan = var.log_analysis_plan
8792
log_analysis_tags = var.log_analysis_tags
8893
log_analysis_service_endpoints = var.log_analysis_service_endpoints
@@ -91,7 +96,7 @@ module "observability_instance" {
9196
log_analysis_cos_bucket_endpoint = local.archive_cos_bucket_endpoint
9297
# IBM Cloud Monitoring
9398
cloud_monitoring_provision = true
94-
cloud_monitoring_instance_name = var.cloud_monitoring_instance_name
99+
cloud_monitoring_instance_name = var.prefix != null ? "${var.prefix}-${var.cloud_monitoring_instance_name}" : var.cloud_monitoring_instance_name
95100
cloud_monitoring_plan = var.cloud_monitoring_plan
96101
cloud_monitoring_tags = var.cloud_monitoring_tags
97102
cloud_monitoring_service_endpoints = var.cloud_monitoring_service_endpoints
@@ -140,12 +145,12 @@ module "kms" {
140145
key_endpoint_type = var.kms_endpoint_type
141146
keys = [
142147
{
143-
key_ring_name = var.cos_key_ring_name
148+
key_ring_name = local.cos_key_ring_name
144149
existing_key_ring = false
145150
force_delete_key_ring = true
146151
keys = [
147152
{
148-
key_name = var.cos_key_name
153+
key_name = local.cos_key_name
149154
standard_key = false
150155
rotation_interval_month = 3
151156
dual_auth_delete_enabled = false
@@ -190,7 +195,7 @@ module "cos_instance" {
190195
resource_group_id = module.resource_group.resource_group_id
191196
create_cos_instance = true
192197
create_resource_key = false
193-
cos_instance_name = var.cos_instance_name
198+
cos_instance_name = var.prefix != null ? "${var.prefix}-${var.cos_instance_name}" : var.cos_instance_name
194199
cos_tags = var.cos_instance_tags
195200
existing_cos_instance_id = var.existing_cos_instance_crn
196201
access_tags = var.cos_instance_access_tags

solutions/instances/outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ output "cos_instance_crn" {
6969

7070
## COS Buckets
7171
output "log_archive_cos_bucket_name" {
72-
value = var.existing_log_archive_cos_bucket_name == null ? module.cos_bucket[0].buckets[var.log_archive_cos_bucket_name].bucket_name : var.existing_log_archive_cos_bucket_name
72+
value = var.existing_log_archive_cos_bucket_name == null ? module.cos_bucket[0].buckets[local.log_archive_cos_bucket_name].bucket_name : var.existing_log_archive_cos_bucket_name
7373
description = "The name of log archive COS bucket"
7474
}
7575

7676
output "at_cos_target_bucket_name" {
77-
value = var.existing_at_cos_target_bucket_name == null ? module.cos_bucket[0].buckets[var.at_cos_target_bucket_name].bucket_name : var.existing_at_cos_target_bucket_name
77+
value = var.existing_at_cos_target_bucket_name == null ? module.cos_bucket[0].buckets[local.at_cos_target_bucket_name].bucket_name : var.existing_at_cos_target_bucket_name
7878
description = "The name of the AT target COS bucket"
7979
}
8080

solutions/instances/variables.tf

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ variable "use_existing_resource_group" {
1616

1717
variable "resource_group_name" {
1818
type = string
19-
description = "The name of a new or an existing resource group in which to provision resources to."
19+
description = "The name of a new or an existing resource group in which to provision resources to. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
2020
}
2121

2222
variable "region" {
@@ -30,13 +30,19 @@ variable "region" {
3030
}
3131
}
3232

33+
variable "prefix" {
34+
type = string
35+
description = "(Optional) Prefix to append to all resources created by this solution."
36+
default = null
37+
}
38+
3339
##############################################################################
3440
# Log Analysis Variables
3541
##############################################################################
3642

3743
variable "log_analysis_instance_name" {
3844
type = string
39-
description = "The name of the IBM Cloud Logging instance to create."
45+
description = "The name of the IBM Cloud Logging instance to create. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
4046
default = "log-analysis"
4147
}
4248

@@ -86,7 +92,7 @@ variable "log_archive_api_key" {
8692

8793
variable "cloud_monitoring_instance_name" {
8894
type = string
89-
description = "The name of the IBM Cloud Monitoring instance to create."
95+
description = "The name of the IBM Cloud Monitoring instance to create. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
9096
default = "cloud-monitoring"
9197
}
9298

@@ -136,7 +142,7 @@ variable "cos_region" {
136142
variable "cos_instance_name" {
137143
type = string
138144
default = "observability-cos"
139-
description = "The name to use when creating the Cloud Object Storage instance."
145+
description = "The name to use when creating the Cloud Object Storage instance. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
140146
}
141147

142148
variable "cos_instance_tags" {
@@ -154,13 +160,13 @@ variable "cos_instance_access_tags" {
154160
variable "log_archive_cos_bucket_name" {
155161
type = string
156162
default = "log-archive-cos-bucket"
157-
description = "The name to use when creating the Cloud Object Storage bucket for storing log archives (NOTE: bucket names are globally unique). If 'add_bucket_name_suffix' is set to true, a random 4 characters will be added to this name to help ensure bucket name is globally unique."
163+
description = "The name to use when creating the Cloud Object Storage bucket for storing log archives (NOTE: bucket names are globally unique). If 'add_bucket_name_suffix' is set to true, a random 4 characters will be added to this name to help ensure bucket name is globally unique. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
158164
}
159165

160166
variable "at_cos_target_bucket_name" {
161167
type = string
162168
default = "at-events-cos-bucket"
163-
description = "The name to use when creating the Cloud Object Storage bucket for cos target (NOTE: bucket names are globally unique). If 'add_bucket_name_suffix' is set to true, a random 4 characters will be added to this name to help ensure bucket name is globally unique."
169+
description = "The name to use when creating the Cloud Object Storage bucket for cos target (NOTE: bucket names are globally unique). If 'add_bucket_name_suffix' is set to true, a random 4 characters will be added to this name to help ensure bucket name is globally unique. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
164170
}
165171

166172
variable "archive_bucket_access_tags" {
@@ -275,11 +281,11 @@ variable "kms_endpoint_type" {
275281
variable "cos_key_ring_name" {
276282
type = string
277283
default = "observability-cos-key-ring"
278-
description = "The name to give the Key Ring which will be created for the COS bucket Key. Will be used by both log archive bucket and AT COS bucket. Not used if supplying an existing Key."
284+
description = "The name to give the Key Ring which will be created for the COS bucket Key. Will be used by both log archive bucket and AT COS bucket. Not used if supplying an existing Key. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
279285
}
280286

281287
variable "cos_key_name" {
282288
type = string
283289
default = "observability-cos-key"
284-
description = "The name to give the Key which will be created for the COS bucket. Will be used by both log archive bucket and AT COS bucket. Not used if supplying an existing Key."
290+
description = "The name to give the Key which will be created for the COS bucket. Will be used by both log archive bucket and AT COS bucket. Not used if supplying an existing Key. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
285291
}

tests/pr_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func TestInstancesInSchematics(t *testing.T) {
8787
{Name: "cos_instance_access_tags", Value: permanentResources["accessTags"], DataType: "list(string)"},
8888
{Name: "archive_bucket_access_tags", Value: permanentResources["accessTags"], DataType: "list(string)"},
8989
{Name: "at_cos_bucket_access_tags", Value: permanentResources["accessTags"], DataType: "list(string)"},
90+
{Name: "prefix", Value: options.Prefix, DataType: "string"},
9091
}
9192

9293
err := options.RunSchematicTest()
@@ -181,6 +182,7 @@ func TestAgentsSolutionInSchematics(t *testing.T) {
181182
{Name: "cluster_resource_group_id", Value: terraform.Output(t, existingTerraformOptions, "cluster_resource_group_id"), DataType: "string"},
182183
{Name: "log_analysis_ingestion_key", Value: terraform.Output(t, existingTerraformOptions, "log_analysis_ingestion_key"), DataType: "string", Secure: true},
183184
{Name: "cloud_monitoring_access_key", Value: terraform.Output(t, existingTerraformOptions, "cloud_monitoring_access_key"), DataType: "string", Secure: true},
185+
{Name: "prefix", Value: options.Prefix, DataType: "string"},
184186
}
185187

186188
err := options.RunSchematicTest()

0 commit comments

Comments
 (0)