Skip to content

Commit

Permalink
chore(setup): pass fields via Terraform variable
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm committed Jun 17, 2024
1 parent 79caa41 commit 2aa959e
Show file tree
Hide file tree
Showing 28 changed files with 431 additions and 1,790 deletions.
6 changes: 5 additions & 1 deletion deploy/helm/sumologic/conf/cleanup/cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export NO_PROXY="${NO_PROXY:=""}"

readonly SUMOLOGIC_COLLECTOR_NAME="${SUMOLOGIC_COLLECTOR_NAME:?}"

cp /etc/terraform/*.tf /terraform/
# Set variables for terraform
export TF_VAR_collector_name="${SUMOLOGIC_COLLECTOR_NAME}"
export TF_VAR_chart_version="${CHART_VERSION:?}"

cp /etc/terraform/* /terraform/
cd /terraform || exit 1

# Fall back to init -upgrade to prevent:
Expand Down
24 changes: 3 additions & 21 deletions deploy/helm/sumologic/conf/setup/fields.tf
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
{{- range $value := .Values.sumologic.logs.fields }}
resource "sumologic_field" {{ $value | quote }} {
count = var.create_fields ? 1 : 0
resource "sumologic_field" "collection_field" {
for_each = var.create_fields ? toset(var.fields) : toset([])

lifecycle {
# ignore changes for name and type, as older installations have been case sensitive
# see: https://github.com/SumoLogic/sumologic-kubernetes-collection/issues/2865
ignore_changes = [field_name, data_type]
}

field_name = {{ $value | quote }}
field_name = "${ each.key }"
data_type = "String"
state = "Enabled"
}
{{- end }}

{{- range $value := .Values.sumologic.logs.additionalFields }}
resource "sumologic_field" {{ $value | quote }} {
count = var.create_fields ? 1 : 0

lifecycle {
# ignore changes for name and type, as older installations have been case sensitive
# see: https://github.com/SumoLogic/sumologic-kubernetes-collection/issues/2865
ignore_changes = [field_name, data_type]
}

field_name = {{ $value | quote }}
data_type = "String"
state = "Enabled"
}
{{- end }}
8 changes: 5 additions & 3 deletions deploy/helm/sumologic/conf/setup/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ readonly SUMOLOGIC_COLLECTOR_NAME="${SUMOLOGIC_COLLECTOR_NAME:?}"

# Set variables for terraform
export TF_VAR_collector_name="${SUMOLOGIC_COLLECTOR_NAME}"
export TF_VAR_chart_version="${CHART_VERSION:?}"

# Let's compare the variables ignoring the case with help of ${VARIABLE,,} which makes the string lowercased
# so that we don't have to deal with True vs true vs TRUE
Expand Down Expand Up @@ -80,7 +81,7 @@ function should_create_fields() {
local REMAINING
REMAINING=$(jq -e '.remaining' <<< "${RESPONSE}")
readonly REMAINING
if [[ $(( REMAINING - {{ len .Values.sumologic.logs.fields }} )) -ge 10 ]] ; then
if [[ $(( REMAINING - ${#FIELDS} )) -ge 10 ]] ; then
return 0
else
return 1
Expand All @@ -90,6 +91,8 @@ function should_create_fields() {
cp /etc/terraform/* /terraform/
cd /terraform || exit 1

declare -ra FIELDS=($(jq -r '.fields[]' terraform.tfvars.json))

# Fall back to init -upgrade to prevent:
# Error: Inconsistent dependency lock file
terraform init -input=false -get=false || terraform init -input=false -upgrade
Expand All @@ -103,7 +106,6 @@ if should_create_fields ; then
"${SUMOLOGIC_BASE_URL}"v1/fields | jq '.data[]' )"
readonly FIELDS_RESPONSE

declare -ra FIELDS=({{ include "helm-toolkit.utils.joinListWithSpaces" .Values.sumologic.logs.fields }})
for FIELD in "${FIELDS[@]}" ; do
FIELD_ID=$( echo "${FIELDS_RESPONSE}" | jq -r "select(.fieldName | ascii_downcase == \"${FIELD}\") | .fieldId" )
# Don't try to import non existing fields
Expand All @@ -113,7 +115,7 @@ if should_create_fields ; then

terraform import \
-var="create_fields=1" \
sumologic_field."${FIELD}"[0] "${FIELD_ID}"
sumologic_field.collection_field[\"${FIELD}\"] "${FIELD_ID}"
done
else
readonly CREATE_FIELDS=0
Expand Down
3 changes: 3 additions & 0 deletions deploy/helm/sumologic/conf/setup/terraform.tfvars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{- $fields := concat .Values.sumologic.logs.fields .Values.sumologic.logs.additionalFields -}}
{{- $variables := dict "fields" $fields -}}
{{- toPrettyJson $variables | nindent 2 }}
5 changes: 5 additions & 0 deletions deploy/helm/sumologic/conf/setup/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ variable "create_fields" {
type = bool
default = true
}

variable "fields" {
description = "Log fields to create."
type = list(string)
}
17 changes: 0 additions & 17 deletions deploy/helm/sumologic/templates/_helpers/_common.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -417,23 +417,6 @@ Example:
{{ end }}
{{/*
Generate a space separated list of quoted values:
Example:
{{ include "helm-toolkit.utils.joinListWithSpaces" .Values.sumologic.logs.fields }}
*/}}
{{- define "helm-toolkit.utils.joinListWithSpaces" -}}
{{- $local := dict "first" true -}}
{{- range $k, $v := . -}}
{{- if not $local.first }} {{ end -}}
{{- $v | quote -}}
{{- $_ := set $local "first" false -}}
{{- end -}}
{{- end -}}
{{/*
Returns kubernetes minor version as integer (without additional chars like +)
Expand Down
2 changes: 2 additions & 0 deletions deploy/helm/sumologic/templates/cleanup/job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ spec:
{{- end }}
- name: SUMOLOGIC_COLLECTOR_NAME
value: {{ include "terraform.collector.name" . }}
- name: CHART_VERSION
value: "{{ .Chart.Version }}"
{{- include "proxy-env-variables" . | nindent 10 }}
securityContext:
runAsUser: 999
Expand Down
2 changes: 2 additions & 0 deletions deploy/helm/sumologic/templates/setup/job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ spec:
{{- end }}
- name: SUMOLOGIC_COLLECTOR_NAME
value: {{ include "terraform.collector.name" . }}
- name: CHART_VERSION
value: "{{ .Chart.Version }}"
{{- include "proxy-env-variables" . | nindent 8 -}}
{{- if .Values.sumologic.setup.debug }}
- name: DEBUG_MODE
Expand Down
2 changes: 2 additions & 0 deletions tests/helm/testdata/goldenfile/cleanup/basic.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ spec:
value:
- name: SUMOLOGIC_COLLECTOR_NAME
value: kubernetes
- name: CHART_VERSION
value: "%CURRENT_CHART_VERSION%"

- name: NO_PROXY
value: kubernetes.default.svc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ spec:
value: http://RELEASE-NAME-sumologic-mock.sumologic.svc.cluster.local.:3000/terraform/api/
- name: SUMOLOGIC_COLLECTOR_NAME
value: kubernetes
- name: CHART_VERSION
value: "%CURRENT_CHART_VERSION%"

- name: NO_PROXY
value: kubernetes.default.svc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ spec:
value: http://RELEASE-NAME-sumologic-mock.sumologic.svc.cluster.local.:3000/terraform/api/
- name: SUMOLOGIC_COLLECTOR_NAME
value: kubernetes
- name: CHART_VERSION
value: "%CURRENT_CHART_VERSION%"

- name: NO_PROXY
value: kubernetes.default.svc
Expand Down
2 changes: 2 additions & 0 deletions tests/helm/testdata/goldenfile/setup/basic.output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ spec:
value:
- name: SUMOLOGIC_COLLECTOR_NAME
value: kubernetes
- name: CHART_VERSION
value: "%CURRENT_CHART_VERSION%"

- name: NO_PROXY
value: kubernetes.default.svc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ spec:
value: http://RELEASE-NAME-sumologic-mock.sumologic.svc.cluster.local.:3000/terraform/api/
- name: SUMOLOGIC_COLLECTOR_NAME
value: kubernetes
- name: CHART_VERSION
value: "%CURRENT_CHART_VERSION%"

- name: NO_PROXY
value: kubernetes.default.svc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ spec:
value: http://RELEASE-NAME-sumologic-mock.sumologic.svc.cluster.local.:3000/terraform/api/
- name: SUMOLOGIC_COLLECTOR_NAME
value: kubernetes
- name: CHART_VERSION
value: "%CURRENT_CHART_VERSION%"

- name: NO_PROXY
value: kubernetes.default.svc
Expand Down
Loading

0 comments on commit 2aa959e

Please sign in to comment.