Skip to content

Commit

Permalink
chore(setup): pass collector fields as variable
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm committed Jun 17, 2024
1 parent 2aa959e commit ad0af09
Show file tree
Hide file tree
Showing 18 changed files with 199 additions and 90 deletions.
9 changes: 2 additions & 7 deletions deploy/helm/sumologic/conf/setup/resources.tf
Original file line number Diff line number Diff line change
@@ -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" {
Expand Down
3 changes: 2 additions & 1 deletion deploy/helm/sumologic/conf/setup/terraform.tfvars.json
Original file line number Diff line number Diff line change
@@ -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 }}
10 changes: 10 additions & 0 deletions deploy/helm/sumologic/conf/setup/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
38 changes: 0 additions & 38 deletions deploy/helm/sumologic/templates/_helpers/_setup.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions tests/helm/testdata/goldenfile/terraform/all_fields.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -816,6 +815,7 @@ data:
}
terraform.tfvars.json: |
{
"collector_fields": {},
"fields": [
"cluster",
"container",
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -515,6 +514,7 @@ data:
}
terraform.tfvars.json: |
{
"collector_fields": {},
"fields": [
"cluster",
"container",
Expand Down Expand Up @@ -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
}
16 changes: 13 additions & 3 deletions tests/helm/testdata/goldenfile/terraform/custom.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -515,6 +514,7 @@ data:
}
terraform.tfvars.json: |
{
"collector_fields": {},
"fields": [
"cluster",
"container",
Expand Down Expand Up @@ -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
}
16 changes: 13 additions & 3 deletions tests/helm/testdata/goldenfile/terraform/default.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -716,6 +715,7 @@ data:
}
terraform.tfvars.json: |
{
"collector_fields": {},
"fields": [
"cluster",
"container",
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -706,6 +705,7 @@ data:
}
terraform.tfvars.json: |
{
"collector_fields": {},
"fields": [
"cluster",
"container",
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -718,6 +717,7 @@ data:
}
terraform.tfvars.json: |
{
"collector_fields": {},
"fields": [
"cluster",
"container",
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -718,6 +717,7 @@ data:
}
terraform.tfvars.json: |
{
"collector_fields": {},
"fields": [
"cluster",
"container",
Expand Down Expand Up @@ -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
}
16 changes: 13 additions & 3 deletions tests/helm/testdata/goldenfile/terraform/logs_fields.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -716,6 +715,7 @@ data:
}
terraform.tfvars.json: |
{
"collector_fields": {},
"fields": [
"cluster",
"container",
Expand Down Expand Up @@ -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
}
Loading

0 comments on commit ad0af09

Please sign in to comment.