From d46d2e8edffbe6edf34fa6b9aef4e0329c2e7337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= Date: Tue, 3 Nov 2020 16:55:06 +0100 Subject: [PATCH] Create fields automatically in setup job --- .../sumologic/conf/setup/fields/fields.tf | 47 +++++++++++++++++++ .../helm/sumologic/conf/setup/fields/main.tf | 5 ++ deploy/helm/sumologic/conf/setup/setup.sh | 29 ++++++++++-- deploy/helm/sumologic/templates/NOTES.txt | 6 ++- 4 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 deploy/helm/sumologic/conf/setup/fields/fields.tf create mode 100644 deploy/helm/sumologic/conf/setup/fields/main.tf mode change 100644 => 100755 deploy/helm/sumologic/conf/setup/setup.sh diff --git a/deploy/helm/sumologic/conf/setup/fields/fields.tf b/deploy/helm/sumologic/conf/setup/fields/fields.tf new file mode 100644 index 0000000000..b2c24330d3 --- /dev/null +++ b/deploy/helm/sumologic/conf/setup/fields/fields.tf @@ -0,0 +1,47 @@ +resource "sumologic_field" "cluster" { + field_name = "cluster" + data_type = "String" + state = "Enabled" +} + +resource "sumologic_field" "container" { + field_name = "container" + data_type = "String" + state = "Enabled" +} + +resource "sumologic_field" "deployment" { + field_name = "deployment" + data_type = "String" + state = "Enabled" +} + +resource "sumologic_field" "host" { + field_name = "host" + data_type = "String" + state = "Enabled" +} + +resource "sumologic_field" "namespace" { + field_name = "namespace" + data_type = "String" + state = "Enabled" +} + +resource "sumologic_field" "node" { + field_name = "node" + data_type = "String" + state = "Enabled" +} + +resource "sumologic_field" "pod" { + field_name = "pod" + data_type = "String" + state = "Enabled" +} + +resource "sumologic_field" "service" { + field_name = "service" + data_type = "String" + state = "Enabled" +} diff --git a/deploy/helm/sumologic/conf/setup/fields/main.tf b/deploy/helm/sumologic/conf/setup/fields/main.tf new file mode 100644 index 0000000000..092e8f7759 --- /dev/null +++ b/deploy/helm/sumologic/conf/setup/fields/main.tf @@ -0,0 +1,5 @@ +terraform { + required_providers { + sumologic = "~> 2.3.0" + } +} diff --git a/deploy/helm/sumologic/conf/setup/setup.sh b/deploy/helm/sumologic/conf/setup/setup.sh old mode 100644 new mode 100755 index 7b7aba05b3..59627fa793 --- a/deploy/helm/sumologic/conf/setup/setup.sh +++ b/deploy/helm/sumologic/conf/setup/setup.sh @@ -1,15 +1,25 @@ #!/bin/bash -cp /etc/terraform/{locals,main,providers,resources,variables}.tf /terraform -cd /terraform # Fix URL to remove "v1" or "v1/" export SUMOLOGIC_BASE_URL=${SUMOLOGIC_BASE_URL%v1*} - # Support proxy for terraform export HTTP_PROXY=${HTTP_PROXY:=""} export HTTPS_PROXY=${HTTPS_PROXY:=""} export NO_PROXY=${NO_PROXY:=""} +function remaining_fields() { + readonly RESPONSE="$(curl -XGET -s \ + -u "${SUMOLOGIC_ACCESSID}:${SUMOLOGIC_ACCESSKEY}" \ + "${SUMOLOGIC_BASE_URL}"v1/fields/quota)" + + echo "${RESPONSE}" | jq '.remaining' +} + +cp /etc/terraform/{locals,main,providers,resources,variables}.tf /terraform/ +mkdir /terraform/fields/ +cp /etc/terraform/fields/{main,fields}.tf /terraform/fields/ +cd /terraform + COLLECTOR_NAME="{{- if .Values.sumologic.collectorName }}{{ .Values.sumologic.collectorName }}{{- else}}{{ .Values.sumologic.clusterName }}{{- end}}" terraform init @@ -32,6 +42,19 @@ terraform import kubernetes_secret.sumologic_collection_secret {{ .Release.Names terraform apply -auto-approve +# Fields +# +# Check if we have at least 10 fields remaining after we would +# create 8 additional fields for the collection +readonly REMAINING=$(remaining_fields) +if [[ $(( REMAINING - 8 )) -ge -10 ]] ; then + echo "Creating fields..." + ( cd fields && terraform destroy -auto-approve ) +else + printf "Couldn't automatically create fields\n" + printf "There's only %s remaining and collection requires at least 8\n" "${REMAINING}" +fi + # Cleanup env variables export SUMOLOGIC_BASE_URL= export SUMOLOGIC_ACCESSKEY= diff --git a/deploy/helm/sumologic/templates/NOTES.txt b/deploy/helm/sumologic/templates/NOTES.txt index 6fb7489259..922752a9d5 100644 --- a/deploy/helm/sumologic/templates/NOTES.txt +++ b/deploy/helm/sumologic/templates/NOTES.txt @@ -11,4 +11,8 @@ Check the release status by running: {{- if eq .Values.fluentd.persistence.enabled false }} WARNING: File persistence for fluentd is disabled. This might lead to loss of data in case of memory buffer overflow. We recommend turning this property on for production environments by setting fluentd.persistence.enabled=true -{{- end }} \ No newline at end of file +{{- end }} + +We've tried to automatically create fields. In an unlikely scenario that this +fails please refer to the following to create them manually: +https://github.com/SumoLogic/sumologic-kubernetes-collection/blob/2b3ca63/deploy/docs/Installation_with_Helm.md#prerequisite