Skip to content

Commit

Permalink
feat(pkger): make envRef default values support more types
Browse files Browse the repository at this point in the history
references: #18237
  • Loading branch information
jsteenb2 committed Jul 28, 2020
1 parent 902cdeb commit a745bf0
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 53 deletions.
8 changes: 4 additions & 4 deletions pkger/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,10 @@ type SummaryLabelMapping struct {
// SummaryReference informs the consumer of required references for
// this resource.
type SummaryReference struct {
Field string `json:"resourceField"`
EnvRefKey string `json:"envRefKey"`
Value string `json:"value"`
DefaultValue string `json:"defaultValue"`
Field string `json:"resourceField"`
EnvRefKey string `json:"envRefKey"`
Value string `json:"value"`
DefaultValue interface{} `json:"defaultValue"`
}

// SummaryTask provides a summary of a task.
Expand Down
2 changes: 1 addition & 1 deletion pkger/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1772,7 +1772,7 @@ func ifaceToReference(i interface{}) *references {
switch f {
case fieldReferencesEnv:
ref.EnvRef = keyRes.stringShort(fieldKey)
ref.defaultVal = keyRes.stringShort(fieldDefault)
ref.defaultVal = keyRes[fieldDefault]
case fieldReferencesSecret:
ref.Secret = keyRes.stringShort(fieldKey)
}
Expand Down
22 changes: 10 additions & 12 deletions pkger/parser_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2050,10 +2050,11 @@ const (
)

type references struct {
EnvRef string
Secret string

val interface{}
defaultVal string
EnvRef string
Secret string
defaultVal interface{}
}

func (r *references) hasValue() bool {
Expand All @@ -2072,18 +2073,15 @@ func (r *references) String() string {
return v
}
if r.EnvRef != "" {
return r.defaultEnvValue()
s, _ := ifaceToStr(r.defaultVal)
if s != "" {
return s
}
return "env-" + r.EnvRef
}
return ""
}

func (r *references) defaultEnvValue() string {
if r.defaultVal != "" {
return r.defaultVal
}
return "env-" + r.EnvRef
}

func (r *references) StringVal() string {
if r.val != nil {
s, _ := r.val.(string)
Expand All @@ -2107,7 +2105,7 @@ func convertRefToRefSummary(field string, ref *references) SummaryReference {
Field: field,
EnvRefKey: ref.EnvRef,
Value: ref.StringVal(),
DefaultValue: ref.defaultEnvValue(),
DefaultValue: ref.defaultVal,
}
}

Expand Down
60 changes: 24 additions & 36 deletions pkger/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ func TestParse(t *testing.T) {
DefaultValue: "spectacles",
},
{
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
DefaultValue: "env-label-meta-name",
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
},
}
assert.Equal(t, expectedEnvRefs, actual[0].EnvReferences)
Expand Down Expand Up @@ -215,14 +214,12 @@ spec:

expected := sumLabelGen("env-meta-name", "env-spec-name", "", "",
SummaryReference{
Field: "metadata.name",
EnvRefKey: "meta-name",
DefaultValue: "env-meta-name",
Field: "metadata.name",
EnvRefKey: "meta-name",
},
SummaryReference{
Field: "spec.name",
EnvRefKey: "spec-name",
DefaultValue: "env-spec-name",
Field: "spec.name",
EnvRefKey: "spec-name",
},
)
assert.Contains(t, actual, expected)
Expand Down Expand Up @@ -598,9 +595,8 @@ spec:
DefaultValue: "spectacles",
},
{
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
DefaultValue: "env-label-meta-name",
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
},
}
assert.Equal(t, expectedEnvRefs, actual[0].EnvReferences)
Expand Down Expand Up @@ -2309,9 +2305,8 @@ spec:
DefaultValue: "spectacles",
},
{
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
DefaultValue: "env-label-meta-name",
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
},
}
assert.Equal(t, expected, actual[0].EnvReferences)
Expand Down Expand Up @@ -2571,9 +2566,8 @@ spec:
DefaultValue: "spectacles",
},
{
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
DefaultValue: "env-label-meta-name",
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
},
}
assert.Equal(t, expectedEnvRefs, actual[0].EnvReferences)
Expand Down Expand Up @@ -2896,14 +2890,12 @@ spec:
DefaultValue: "spectacles",
},
{
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
DefaultValue: "env-label-meta-name",
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
},
{
Field: "spec.endpointName",
EnvRefKey: "endpoint-meta-name",
DefaultValue: "env-endpoint-meta-name",
Field: "spec.endpointName",
EnvRefKey: "endpoint-meta-name",
},
}
assert.Equal(t, expectedEnvRefs, actual[0].EnvReferences)
Expand Down Expand Up @@ -3238,9 +3230,8 @@ spec:
DefaultValue: "spectacles",
},
{
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
DefaultValue: "env-label-meta-name",
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
},
}
assert.Equal(t, expectedEnvRefs, actual[0].EnvReferences)
Expand Down Expand Up @@ -3457,9 +3448,8 @@ spec:
DefaultValue: "spectacles",
},
{
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
DefaultValue: "env-label-meta-name",
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
},
}
assert.Equal(t, expectedEnvRefs, actual[0].EnvReferences)
Expand Down Expand Up @@ -3588,19 +3578,17 @@ spec:
DefaultValue: "spectacles",
},
{
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
DefaultValue: "env-label-meta-name",
Field: "spec.associations[0].name",
EnvRefKey: "label-meta-name",
},
{
Field: "spec.selected[0]",
EnvRefKey: "the-selected",
DefaultValue: "second val",
},
{
Field: "spec.selected[1]",
EnvRefKey: "the-2nd",
DefaultValue: "env-the-2nd",
Field: "spec.selected[1]",
EnvRefKey: "the-2nd",
},
}
assert.Equal(t, expectedEnvRefs, actual[0].EnvReferences)
Expand Down
35 changes: 35 additions & 0 deletions pkger/testdata/dashboard_params.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: influxdata.com/v2alpha1
kind: Dashboard
metadata:
name: dash-1
spec:
charts:
- kind: Single_Stat
name: single stat
xPos: 1
yPos: 2
width: 6
height: 3
queries:
- query: |
option params = {
bucket: "foo",
start: -1d,
stop: now(),
name: "max",
floatVal: 1.0,
minVal: 10
}
from(bucket: params.bucket)
|> range(start: params.start, end: params.stop)
|> filter(fn: (r) => r._measurement == "processes")
|> filter(fn: (r) => r.floater == params.floatVal)
|> filter(fn: (r) => r._value > params.minVal)
|> aggregateWindow(every: v.windowPeriod, fn: max)
|> yield(name: params.name)
colors:
- name: laser
type: text
hex: "#8F8AF4"
value: 3

0 comments on commit a745bf0

Please sign in to comment.