From cd04731efef9f42365df44755c1c4060343445cd Mon Sep 17 00:00:00 2001 From: Johnny Steenbergen Date: Mon, 22 Jun 2020 14:49:14 -0700 Subject: [PATCH] feat(pkger): add env default vaules for template references --- CHANGELOG.md | 1 + pkger/parser.go | 1 + pkger/parser_models.go | 11 +++++-- pkger/parser_test.go | 32 ++++++++++---------- pkger/testdata/bucket_ref.yml | 2 ++ pkger/testdata/checks_ref.yml | 2 ++ pkger/testdata/dashboard_ref.yml | 2 ++ pkger/testdata/notification_endpoint_ref.yml | 2 ++ pkger/testdata/notification_rule_ref.yml | 2 ++ pkger/testdata/task_ref.yml | 2 ++ pkger/testdata/telegraf_ref.yml | 2 ++ pkger/testdata/variable_ref.yml | 2 ++ 12 files changed, 42 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf71d921cdb..fd47f609a02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ 1. [18600](https://github.com/influxdata/influxdb/pull/18600): Extend influx apply with resource filter capabilities 1. [18601](https://github.com/influxdata/influxdb/pull/18601): Provide active config running influx config without args 1. [18606](https://github.com/influxdata/influxdb/pull/18606): Enable influxd binary to look for a config file on startup +1. [18647](https://github.com/influxdata/influxdb/pull/18647): Add support for env ref default values to the template parser ### Bug Fixes diff --git a/pkger/parser.go b/pkger/parser.go index 72cf30da54f..adeb35cab20 100644 --- a/pkger/parser.go +++ b/pkger/parser.go @@ -1639,6 +1639,7 @@ func (r Resource) references(key string) *references { switch f { case fieldReferencesEnv: ref.EnvRef = keyRes.stringShort(fieldKey) + ref.defaultVal = keyRes.stringShort(fieldDefault) case fieldReferencesSecret: ref.Secret = keyRes.stringShort(fieldKey) } diff --git a/pkger/parser_models.go b/pkger/parser_models.go index 816af9d37a2..da61e00a523 100644 --- a/pkger/parser_models.go +++ b/pkger/parser_models.go @@ -50,6 +50,7 @@ func summarizeCommonReferences(ident identity, labels sortedLabels) []SummaryRef const ( fieldAPIVersion = "apiVersion" fieldAssociations = "associations" + fieldDefault = "default" fieldDescription = "description" fieldEvery = "every" fieldKey = "key" @@ -1992,9 +1993,10 @@ const ( ) type references struct { - val interface{} - EnvRef string - Secret string + val interface{} + defaultVal string + EnvRef string + Secret string } func (r *references) hasValue() bool { @@ -2019,6 +2021,9 @@ func (r *references) String() string { } func (r *references) defaultEnvValue() string { + if r.defaultVal != "" { + return r.defaultVal + } return "env-" + r.EnvRef } diff --git a/pkger/parser_test.go b/pkger/parser_test.go index ded5c1b378a..9f3c65b1c9f 100644 --- a/pkger/parser_test.go +++ b/pkger/parser_test.go @@ -60,12 +60,12 @@ func TestParse(t *testing.T) { { Field: "metadata.name", EnvRefKey: "meta-name", - DefaultValue: "env-meta-name", + DefaultValue: "meta", }, { Field: "spec.name", EnvRefKey: "spec-name", - DefaultValue: "env-spec-name", + DefaultValue: "spectacles", }, { Field: "spec.associations[0].name", @@ -582,12 +582,12 @@ spec: { Field: "metadata.name", EnvRefKey: "meta-name", - DefaultValue: "env-meta-name", + DefaultValue: "meta", }, { Field: "spec.name", EnvRefKey: "spec-name", - DefaultValue: "env-spec-name", + DefaultValue: "spectacles", }, { Field: "spec.associations[0].name", @@ -2281,12 +2281,12 @@ spec: { Field: "metadata.name", EnvRefKey: "meta-name", - DefaultValue: "env-meta-name", + DefaultValue: "meta", }, { Field: "spec.name", EnvRefKey: "spec-name", - DefaultValue: "env-spec-name", + DefaultValue: "spectacles", }, { Field: "spec.associations[0].name", @@ -2528,12 +2528,12 @@ spec: { Field: "metadata.name", EnvRefKey: "meta-name", - DefaultValue: "env-meta-name", + DefaultValue: "meta", }, { Field: "spec.name", EnvRefKey: "spec-name", - DefaultValue: "env-spec-name", + DefaultValue: "spectacles", }, { Field: "spec.associations[0].name", @@ -2852,12 +2852,12 @@ spec: { Field: "metadata.name", EnvRefKey: "meta-name", - DefaultValue: "env-meta-name", + DefaultValue: "meta", }, { Field: "spec.name", EnvRefKey: "spec-name", - DefaultValue: "env-spec-name", + DefaultValue: "spectacles", }, { Field: "spec.associations[0].name", @@ -3189,12 +3189,12 @@ spec: { Field: "metadata.name", EnvRefKey: "meta-name", - DefaultValue: "env-meta-name", + DefaultValue: "meta", }, { Field: "spec.name", EnvRefKey: "spec-name", - DefaultValue: "env-spec-name", + DefaultValue: "spectacles", }, { Field: "spec.associations[0].name", @@ -3407,12 +3407,12 @@ spec: { Field: "metadata.name", EnvRefKey: "meta-name", - DefaultValue: "env-meta-name", + DefaultValue: "meta", }, { Field: "spec.name", EnvRefKey: "spec-name", - DefaultValue: "env-spec-name", + DefaultValue: "spectacles", }, { Field: "spec.associations[0].name", @@ -3527,12 +3527,12 @@ spec: { Field: "metadata.name", EnvRefKey: "meta-name", - DefaultValue: "env-meta-name", + DefaultValue: "meta", }, { Field: "spec.name", EnvRefKey: "spec-name", - DefaultValue: "env-spec-name", + DefaultValue: "spectacles", }, { Field: "spec.associations[0].name", diff --git a/pkger/testdata/bucket_ref.yml b/pkger/testdata/bucket_ref.yml index f27a672735f..6870fc65172 100644 --- a/pkger/testdata/bucket_ref.yml +++ b/pkger/testdata/bucket_ref.yml @@ -4,10 +4,12 @@ metadata: name: envRef: key: meta-name + default: meta spec: name: envRef: key: spec-name + default: spectacles associations: - kind: Label name: diff --git a/pkger/testdata/checks_ref.yml b/pkger/testdata/checks_ref.yml index 8ace9cb73cb..4e1590655ab 100644 --- a/pkger/testdata/checks_ref.yml +++ b/pkger/testdata/checks_ref.yml @@ -4,10 +4,12 @@ metadata: name: envRef: key: meta-name + default: meta spec: name: envRef: key: spec-name + default: spectacles every: 1m offset: 15s query: | diff --git a/pkger/testdata/dashboard_ref.yml b/pkger/testdata/dashboard_ref.yml index 77ed973ce5a..76cafe52748 100644 --- a/pkger/testdata/dashboard_ref.yml +++ b/pkger/testdata/dashboard_ref.yml @@ -4,10 +4,12 @@ metadata: name: envRef: key: meta-name + default: meta spec: name: envRef: key: spec-name + default: spectacles associations: - kind: Label name: diff --git a/pkger/testdata/notification_endpoint_ref.yml b/pkger/testdata/notification_endpoint_ref.yml index da813a4742d..2cd6837349a 100644 --- a/pkger/testdata/notification_endpoint_ref.yml +++ b/pkger/testdata/notification_endpoint_ref.yml @@ -4,10 +4,12 @@ metadata: name: envRef: key: meta-name + default: meta spec: name: envRef: key: spec-name + default: spectacles url: https://hooks.slack.com/services/bip/piddy/boppidy token: tokenval associations: diff --git a/pkger/testdata/notification_rule_ref.yml b/pkger/testdata/notification_rule_ref.yml index fc82e9b4402..c32773c56ee 100644 --- a/pkger/testdata/notification_rule_ref.yml +++ b/pkger/testdata/notification_rule_ref.yml @@ -4,10 +4,12 @@ metadata: name: envRef: key: meta-name + default: meta spec: name: envRef: key: spec-name + default: spectacles endpointName: envRef: key: endpoint-meta-name diff --git a/pkger/testdata/task_ref.yml b/pkger/testdata/task_ref.yml index 45ccd834dc1..ce8f0c62736 100644 --- a/pkger/testdata/task_ref.yml +++ b/pkger/testdata/task_ref.yml @@ -4,10 +4,12 @@ metadata: name: envRef: key: meta-name + default: meta spec: name: envRef: key: spec-name + default: spectacles every: 10m offset: 15s query: > diff --git a/pkger/testdata/telegraf_ref.yml b/pkger/testdata/telegraf_ref.yml index f7a14f80843..ffad1dda567 100644 --- a/pkger/testdata/telegraf_ref.yml +++ b/pkger/testdata/telegraf_ref.yml @@ -4,10 +4,12 @@ metadata: name: envRef: key: meta-name + default: meta spec: name: envRef: key: spec-name + default: spectacles config: | # Configuration for telegraf agent [agent] diff --git a/pkger/testdata/variable_ref.yml b/pkger/testdata/variable_ref.yml index 798f330497f..199ca4f3375 100644 --- a/pkger/testdata/variable_ref.yml +++ b/pkger/testdata/variable_ref.yml @@ -4,10 +4,12 @@ metadata: name: envRef: key: meta-name + default: meta spec: name: envRef: key: spec-name + default: spectacles type: constant values: - first val