From ad0af09d68f16171fd5cfe57109eec1c641f5bc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20=C5=9Awi=C4=85tek?= Date: Thu, 13 Jun 2024 11:10:15 +0200 Subject: [PATCH] chore(setup): pass collector fields as variable --- deploy/helm/sumologic/conf/setup/resources.tf | 9 +---- .../conf/setup/terraform.tfvars.json | 3 +- deploy/helm/sumologic/conf/setup/variables.tf | 10 +++++ .../sumologic/templates/_helpers/_setup.tpl | 38 ------------------- .../terraform/all_fields.output.yaml | 16 ++++++-- .../terraform/collector_fields.output.yaml | 21 +++++++--- .../terraform/conditional_sources.output.yaml | 16 ++++++-- .../goldenfile/terraform/custom.output.yaml | 16 ++++++-- .../goldenfile/terraform/default.output.yaml | 16 ++++++-- .../disable_default_metrics.output.yaml | 16 ++++++-- .../terraform/disabled_dashboards.output.yaml | 16 ++++++-- .../terraform/disabled_monitors.output.yaml | 16 ++++++-- .../terraform/logs_fields.output.yaml | 16 ++++++-- ...itors_with_email_notifications.output.yaml | 16 ++++++-- .../monitors_with_single_email.output.yaml | 16 ++++++-- .../terraform/strip_extrapolation.output.yaml | 16 ++++++-- .../goldenfile/terraform/traces.output.yaml | 16 ++++++-- .../tracing-metrics-disabled.output.yaml | 16 ++++++-- 18 files changed, 199 insertions(+), 90 deletions(-) diff --git a/deploy/helm/sumologic/conf/setup/resources.tf b/deploy/helm/sumologic/conf/setup/resources.tf index 428a6e666..bbae01df0 100644 --- a/deploy/helm/sumologic/conf/setup/resources.tf +++ b/deploy/helm/sumologic/conf/setup/resources.tf @@ -1,12 +1,7 @@ resource "sumologic_collector" "collector" { name = var.collector_name - description = {{ printf "Sumo Logic Kubernetes Collection\nversion: %s" .Chart.Version | quote }} - fields = { - {{- $fields := .Values.sumologic.collector.fields }} - {{- range $name, $value := $fields }} - {{ include "terraform.generate-key" (dict "Name" $name "Value" $value "KeyLength" (include "terraform.max-key-length" $fields)) }} - {{- end}} - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { diff --git a/deploy/helm/sumologic/conf/setup/terraform.tfvars.json b/deploy/helm/sumologic/conf/setup/terraform.tfvars.json index 44914abf5..88461ec54 100644 --- a/deploy/helm/sumologic/conf/setup/terraform.tfvars.json +++ b/deploy/helm/sumologic/conf/setup/terraform.tfvars.json @@ -1,3 +1,4 @@ {{- $fields := concat .Values.sumologic.logs.fields .Values.sumologic.logs.additionalFields -}} -{{- $variables := dict "fields" $fields -}} +{{- $collectorFields := .Values.sumologic.collector.fields -}} +{{- $variables := dict "fields" $fields "collector_fields" $collectorFields -}} {{- toPrettyJson $variables | nindent 2 }} diff --git a/deploy/helm/sumologic/conf/setup/variables.tf b/deploy/helm/sumologic/conf/setup/variables.tf index 3a8d03d4c..e29737816 100644 --- a/deploy/helm/sumologic/conf/setup/variables.tf +++ b/deploy/helm/sumologic/conf/setup/variables.tf @@ -17,3 +17,13 @@ variable "fields" { description = "Log fields to create." type = list(string) } + +variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) +} + +variable "chart_version" { + description = "The Helm Chart version." + type = string +} diff --git a/deploy/helm/sumologic/templates/_helpers/_setup.tpl b/deploy/helm/sumologic/templates/_helpers/_setup.tpl index 715581d7e..b676d3d12 100644 --- a/deploy/helm/sumologic/templates/_helpers/_setup.tpl +++ b/deploy/helm/sumologic/templates/_helpers/_setup.tpl @@ -85,44 +85,6 @@ Example usage: {{- if .Values.sumologic.collectorName }}{{ .Values.sumologic.collectorName }}{{- else}}{{ template "sumologic.clusterNameReplaceSpaceWithDash" . }}{{- end}} {{- end -}} -{{- define "terraform.max-key-length" -}} -{{- $max := 0 -}} -{{- range $key, $value := . -}} -{{- if gt (len $key) $max -}} -{{- $max = (len $key) -}} -{{- end -}} -{{- end -}} -{{ $max }} -{{- end -}} - -{{/* -Generate key for Terraform object. Default behaviour is to print: - -{{ name }} = {{ value }} - -If this is key for list, prints only value. - -This template takes care about indentation using Indent key - -Example usage: - -{{- include "terraform.generate-object" (dict "Name" "my_key" "Value" "my_value" "Indent" 8 "List" true) }} -*/}} -{{- define "terraform.generate-key" -}} -{{- $indent := int .Indent -}} -{{- $name := .Name -}} -{{- $keyLength := int .KeyLength -}} -{{- $format := printf "%%-%ss" (toString $keyLength) -}} -{{- $value := .Value -}} -{{- if and ( eq (kindOf $value) "string") (not .SkipEscaping) -}} -{{- $value = printf "\"%s\"" $value -}} -{{- end -}} -{{- if .SkipPadding -}} -{{- $format = "%s" -}} -{{- end -}} -{{ indent (int $indent) "" }}{{ if not .SkipName }}{{ printf $format (toString $name) }} {{ if not .SkipEqual }}= {{ end }}{{ end }}{{ (toString $value) }}{{ if .AddComma }},{{ end }} -{{- end -}} - {{/* get configuration variable name for sources confg map diff --git a/tests/helm/testdata/goldenfile/terraform/all_fields.output.yaml b/tests/helm/testdata/goldenfile/terraform/all_fields.output.yaml index a6f7e0812..a9a5cdb1d 100644 --- a/tests/helm/testdata/goldenfile/terraform/all_fields.output.yaml +++ b/tests/helm/testdata/goldenfile/terraform/all_fields.output.yaml @@ -332,9 +332,8 @@ data: resources.tf: | resource "sumologic_collector" "collector" { name = var.collector_name - description = "Sumo Logic Kubernetes Collection\nversion: %CURRENT_CHART_VERSION%" - fields = { - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { @@ -816,6 +815,7 @@ data: } terraform.tfvars.json: | { + "collector_fields": {}, "fields": [ "cluster", "container", @@ -849,3 +849,13 @@ data: description = "Log fields to create." type = list(string) } + + variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) + } + + variable "chart_version" { + description = "The Helm Chart version." + type = string + } diff --git a/tests/helm/testdata/goldenfile/terraform/collector_fields.output.yaml b/tests/helm/testdata/goldenfile/terraform/collector_fields.output.yaml index f8af10b24..290815e6c 100644 --- a/tests/helm/testdata/goldenfile/terraform/collector_fields.output.yaml +++ b/tests/helm/testdata/goldenfile/terraform/collector_fields.output.yaml @@ -332,11 +332,8 @@ data: resources.tf: | resource "sumologic_collector" "collector" { name = var.collector_name - description = "Sumo Logic Kubernetes Collection\nversion: %CURRENT_CHART_VERSION%" - fields = { - here_is_very_long_field_name = "another_value" - test_fields = "test_value" - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { @@ -718,6 +715,10 @@ data: } terraform.tfvars.json: | { + "collector_fields": { + "here_is_very_long_field_name": "another_value", + "test_fields": "test_value" + }, "fields": [ "cluster", "container", @@ -751,3 +752,13 @@ data: description = "Log fields to create." type = list(string) } + + variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) + } + + variable "chart_version" { + description = "The Helm Chart version." + type = string + } diff --git a/tests/helm/testdata/goldenfile/terraform/conditional_sources.output.yaml b/tests/helm/testdata/goldenfile/terraform/conditional_sources.output.yaml index 29318e7c5..df7445b91 100644 --- a/tests/helm/testdata/goldenfile/terraform/conditional_sources.output.yaml +++ b/tests/helm/testdata/goldenfile/terraform/conditional_sources.output.yaml @@ -332,9 +332,8 @@ data: resources.tf: | resource "sumologic_collector" "collector" { name = var.collector_name - description = "Sumo Logic Kubernetes Collection\nversion: %CURRENT_CHART_VERSION%" - fields = { - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { @@ -515,6 +514,7 @@ data: } terraform.tfvars.json: | { + "collector_fields": {}, "fields": [ "cluster", "container", @@ -548,3 +548,13 @@ data: description = "Log fields to create." type = list(string) } + + variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) + } + + variable "chart_version" { + description = "The Helm Chart version." + type = string + } diff --git a/tests/helm/testdata/goldenfile/terraform/custom.output.yaml b/tests/helm/testdata/goldenfile/terraform/custom.output.yaml index 29318e7c5..df7445b91 100644 --- a/tests/helm/testdata/goldenfile/terraform/custom.output.yaml +++ b/tests/helm/testdata/goldenfile/terraform/custom.output.yaml @@ -332,9 +332,8 @@ data: resources.tf: | resource "sumologic_collector" "collector" { name = var.collector_name - description = "Sumo Logic Kubernetes Collection\nversion: %CURRENT_CHART_VERSION%" - fields = { - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { @@ -515,6 +514,7 @@ data: } terraform.tfvars.json: | { + "collector_fields": {}, "fields": [ "cluster", "container", @@ -548,3 +548,13 @@ data: description = "Log fields to create." type = list(string) } + + variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) + } + + variable "chart_version" { + description = "The Helm Chart version." + type = string + } diff --git a/tests/helm/testdata/goldenfile/terraform/default.output.yaml b/tests/helm/testdata/goldenfile/terraform/default.output.yaml index 073933919..0382da981 100644 --- a/tests/helm/testdata/goldenfile/terraform/default.output.yaml +++ b/tests/helm/testdata/goldenfile/terraform/default.output.yaml @@ -332,9 +332,8 @@ data: resources.tf: | resource "sumologic_collector" "collector" { name = var.collector_name - description = "Sumo Logic Kubernetes Collection\nversion: %CURRENT_CHART_VERSION%" - fields = { - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { @@ -716,6 +715,7 @@ data: } terraform.tfvars.json: | { + "collector_fields": {}, "fields": [ "cluster", "container", @@ -749,3 +749,13 @@ data: description = "Log fields to create." type = list(string) } + + variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) + } + + variable "chart_version" { + description = "The Helm Chart version." + type = string + } diff --git a/tests/helm/testdata/goldenfile/terraform/disable_default_metrics.output.yaml b/tests/helm/testdata/goldenfile/terraform/disable_default_metrics.output.yaml index 685022dde..286743398 100644 --- a/tests/helm/testdata/goldenfile/terraform/disable_default_metrics.output.yaml +++ b/tests/helm/testdata/goldenfile/terraform/disable_default_metrics.output.yaml @@ -332,9 +332,8 @@ data: resources.tf: | resource "sumologic_collector" "collector" { name = var.collector_name - description = "Sumo Logic Kubernetes Collection\nversion: %CURRENT_CHART_VERSION%" - fields = { - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { @@ -706,6 +705,7 @@ data: } terraform.tfvars.json: | { + "collector_fields": {}, "fields": [ "cluster", "container", @@ -739,3 +739,13 @@ data: description = "Log fields to create." type = list(string) } + + variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) + } + + variable "chart_version" { + description = "The Helm Chart version." + type = string + } diff --git a/tests/helm/testdata/goldenfile/terraform/disabled_dashboards.output.yaml b/tests/helm/testdata/goldenfile/terraform/disabled_dashboards.output.yaml index 08aac4182..b703cef4b 100644 --- a/tests/helm/testdata/goldenfile/terraform/disabled_dashboards.output.yaml +++ b/tests/helm/testdata/goldenfile/terraform/disabled_dashboards.output.yaml @@ -332,9 +332,8 @@ data: resources.tf: | resource "sumologic_collector" "collector" { name = var.collector_name - description = "Sumo Logic Kubernetes Collection\nversion: %CURRENT_CHART_VERSION%" - fields = { - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { @@ -718,6 +717,7 @@ data: } terraform.tfvars.json: | { + "collector_fields": {}, "fields": [ "cluster", "container", @@ -751,3 +751,13 @@ data: description = "Log fields to create." type = list(string) } + + variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) + } + + variable "chart_version" { + description = "The Helm Chart version." + type = string + } diff --git a/tests/helm/testdata/goldenfile/terraform/disabled_monitors.output.yaml b/tests/helm/testdata/goldenfile/terraform/disabled_monitors.output.yaml index f9f690eb6..629b3cb2f 100644 --- a/tests/helm/testdata/goldenfile/terraform/disabled_monitors.output.yaml +++ b/tests/helm/testdata/goldenfile/terraform/disabled_monitors.output.yaml @@ -332,9 +332,8 @@ data: resources.tf: | resource "sumologic_collector" "collector" { name = var.collector_name - description = "Sumo Logic Kubernetes Collection\nversion: %CURRENT_CHART_VERSION%" - fields = { - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { @@ -718,6 +717,7 @@ data: } terraform.tfvars.json: | { + "collector_fields": {}, "fields": [ "cluster", "container", @@ -751,3 +751,13 @@ data: description = "Log fields to create." type = list(string) } + + variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) + } + + variable "chart_version" { + description = "The Helm Chart version." + type = string + } diff --git a/tests/helm/testdata/goldenfile/terraform/logs_fields.output.yaml b/tests/helm/testdata/goldenfile/terraform/logs_fields.output.yaml index 60acf37e3..8e75ae360 100644 --- a/tests/helm/testdata/goldenfile/terraform/logs_fields.output.yaml +++ b/tests/helm/testdata/goldenfile/terraform/logs_fields.output.yaml @@ -332,9 +332,8 @@ data: resources.tf: | resource "sumologic_collector" "collector" { name = var.collector_name - description = "Sumo Logic Kubernetes Collection\nversion: %CURRENT_CHART_VERSION%" - fields = { - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { @@ -716,6 +715,7 @@ data: } terraform.tfvars.json: | { + "collector_fields": {}, "fields": [ "cluster", "container", @@ -751,3 +751,13 @@ data: description = "Log fields to create." type = list(string) } + + variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) + } + + variable "chart_version" { + description = "The Helm Chart version." + type = string + } diff --git a/tests/helm/testdata/goldenfile/terraform/monitors_with_email_notifications.output.yaml b/tests/helm/testdata/goldenfile/terraform/monitors_with_email_notifications.output.yaml index 385cc68a8..ccbc7288d 100644 --- a/tests/helm/testdata/goldenfile/terraform/monitors_with_email_notifications.output.yaml +++ b/tests/helm/testdata/goldenfile/terraform/monitors_with_email_notifications.output.yaml @@ -338,9 +338,8 @@ data: resources.tf: | resource "sumologic_collector" "collector" { name = var.collector_name - description = "Sumo Logic Kubernetes Collection\nversion: %CURRENT_CHART_VERSION%" - fields = { - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { @@ -722,6 +721,7 @@ data: } terraform.tfvars.json: | { + "collector_fields": {}, "fields": [ "cluster", "container", @@ -755,3 +755,13 @@ data: description = "Log fields to create." type = list(string) } + + variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) + } + + variable "chart_version" { + description = "The Helm Chart version." + type = string + } diff --git a/tests/helm/testdata/goldenfile/terraform/monitors_with_single_email.output.yaml b/tests/helm/testdata/goldenfile/terraform/monitors_with_single_email.output.yaml index 2bf539a27..7ec2f8c92 100644 --- a/tests/helm/testdata/goldenfile/terraform/monitors_with_single_email.output.yaml +++ b/tests/helm/testdata/goldenfile/terraform/monitors_with_single_email.output.yaml @@ -338,9 +338,8 @@ data: resources.tf: | resource "sumologic_collector" "collector" { name = var.collector_name - description = "Sumo Logic Kubernetes Collection\nversion: %CURRENT_CHART_VERSION%" - fields = { - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { @@ -722,6 +721,7 @@ data: } terraform.tfvars.json: | { + "collector_fields": {}, "fields": [ "cluster", "container", @@ -755,3 +755,13 @@ data: description = "Log fields to create." type = list(string) } + + variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) + } + + variable "chart_version" { + description = "The Helm Chart version." + type = string + } diff --git a/tests/helm/testdata/goldenfile/terraform/strip_extrapolation.output.yaml b/tests/helm/testdata/goldenfile/terraform/strip_extrapolation.output.yaml index c441bf9b3..2e4ac756f 100644 --- a/tests/helm/testdata/goldenfile/terraform/strip_extrapolation.output.yaml +++ b/tests/helm/testdata/goldenfile/terraform/strip_extrapolation.output.yaml @@ -333,9 +333,8 @@ data: resources.tf: | resource "sumologic_collector" "collector" { name = var.collector_name - description = "Sumo Logic Kubernetes Collection\nversion: %CURRENT_CHART_VERSION%" - fields = { - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { @@ -717,6 +716,7 @@ data: } terraform.tfvars.json: | { + "collector_fields": {}, "fields": [ "cluster", "container", @@ -750,3 +750,13 @@ data: description = "Log fields to create." type = list(string) } + + variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) + } + + variable "chart_version" { + description = "The Helm Chart version." + type = string + } diff --git a/tests/helm/testdata/goldenfile/terraform/traces.output.yaml b/tests/helm/testdata/goldenfile/terraform/traces.output.yaml index bd2322979..6ca58e2f4 100644 --- a/tests/helm/testdata/goldenfile/terraform/traces.output.yaml +++ b/tests/helm/testdata/goldenfile/terraform/traces.output.yaml @@ -332,9 +332,8 @@ data: resources.tf: | resource "sumologic_collector" "collector" { name = var.collector_name - description = "Sumo Logic Kubernetes Collection\nversion: %CURRENT_CHART_VERSION%" - fields = { - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { @@ -556,6 +555,7 @@ data: } terraform.tfvars.json: | { + "collector_fields": {}, "fields": [ "cluster", "container", @@ -589,3 +589,13 @@ data: description = "Log fields to create." type = list(string) } + + variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) + } + + variable "chart_version" { + description = "The Helm Chart version." + type = string + } diff --git a/tests/helm/testdata/goldenfile/terraform/tracing-metrics-disabled.output.yaml b/tests/helm/testdata/goldenfile/terraform/tracing-metrics-disabled.output.yaml index 073933919..0382da981 100644 --- a/tests/helm/testdata/goldenfile/terraform/tracing-metrics-disabled.output.yaml +++ b/tests/helm/testdata/goldenfile/terraform/tracing-metrics-disabled.output.yaml @@ -332,9 +332,8 @@ data: resources.tf: | resource "sumologic_collector" "collector" { name = var.collector_name - description = "Sumo Logic Kubernetes Collection\nversion: %CURRENT_CHART_VERSION%" - fields = { - } + description = format("Sumo Logic Kubernetes Collection\nversion: %s", var.chart_version) + fields = var.collector_fields } resource "kubernetes_secret" "sumologic_collection_secret" { @@ -716,6 +715,7 @@ data: } terraform.tfvars.json: | { + "collector_fields": {}, "fields": [ "cluster", "container", @@ -749,3 +749,13 @@ data: description = "Log fields to create." type = list(string) } + + variable "collector_fields" { + description = "Fields to set on the collector." + type = map(string) + } + + variable "chart_version" { + description = "The Helm Chart version." + type = string + }