Skip to content

Commit

Permalink
mark all data values coming from cmd as "@overlay/match missing_ok=True"
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Kalinin committed May 25, 2021
1 parent b3f5da9 commit 706e81c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 18 deletions.
53 changes: 53 additions & 0 deletions pkg/cmd/template/cmd_data_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,59 @@ another:
assert.Equal(t, expectedYAMLTplData, string(file.Bytes()))
}

func TestDataValuesWithFlagsWithoutDataValuesOverlay(t *testing.T) {
yamlTplData := []byte(`
#@ load("@ytt:data", "data")
data_int: #@ data.values.int
data_str: #@ data.values.str
values: #@ data.values`)

expectedYAMLTplData := `data_int: 124
data_str: str
values:
int: 124
another:
nested:
map: 567
str: str
boolean: true
nested:
value: str
`

// Only some values are prespecified by the overlay
yamlData := []byte(`
#@data/values
---
int: 123
another:
nested:
map: {"a": 123}`)

filesToProcess := files.NewSortedFiles([]*files.File{
files.MustNewFileFromSource(files.NewBytesSource("tpl.yml", yamlTplData)),
files.MustNewFileFromSource(files.NewBytesSource("data.yml", yamlData)),
})

ui := ui.NewTTY(false)
opts := cmdtpl.NewOptions()

opts.DataValuesFlags = cmdtpl.DataValuesFlags{
EnvFromStrings: []string{"DVS"},
EnvironFunc: func() []string { return []string{"DVS_str=str"} },
KVsFromYAML: []string{"int=124", "boolean=true", "nested.value=\"str\"", "another.nested.map=567"},
}

out := opts.RunWithFiles(cmdtpl.Input{Files: filesToProcess}, ui)
require.NoError(t, out.Err)
require.Len(t, out.Files, 1, "unexpected number of output files")

file := out.Files[0]

assert.Equal(t, "tpl.yml", file.RelativePath())
assert.Equal(t, expectedYAMLTplData, string(file.Bytes()))
}

func TestDataValuesWithFlagsMarkedMissingOk(t *testing.T) {
yamlTplData := []byte(`
#@ load("@ytt:data", "data")
Expand Down
40 changes: 22 additions & 18 deletions pkg/cmd/template/data_values_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,6 @@ func (DataValuesFlags) libraryRefAndKey(key string) (string, string, error) {
}

func (s *DataValuesFlags) buildOverlay(keyPieces []string, value interface{}, desc string) *yamlmeta.Document {
const (
missingOkSuffix = "+"
)

resultMap := &yamlmeta.Map{}
currMap := resultMap
var lastMapItem *yamlmeta.MapItem
Expand All @@ -300,23 +296,26 @@ func (s *DataValuesFlags) buildOverlay(keyPieces []string, value interface{}, de
pos.SetFile(fmt.Sprintf("key '%s' (%s)", strings.Join(keyPieces, dvsMapKeySep), desc))

for _, piece := range keyPieces {
newMap := &yamlmeta.Map{}
nodeAnns := template.NodeAnnotations{}

if strings.HasSuffix(piece, missingOkSuffix) {
// In previous versions of ytt '+' key suffix was meant to indicate
// "@overlay/match missing_ok=True". It's no longer needed as all keys
// are considered as such. Data values schemas should be enough to
// provide key checking/validations.
// (Stripped for backwards compatibility.)
if strings.HasSuffix(piece, "+") {
piece = piece[:len(piece)-1]
nodeAnns = template.NodeAnnotations{
yttoverlay.AnnotationMatch: template.NodeAnnotation{
Kwargs: []starlark.Tuple{{
starlark.String(yttoverlay.MatchAnnotationKwargMissingOK),
starlark.Bool(true),
}},
},
}
}

newMap := &yamlmeta.Map{}

lastMapItem = &yamlmeta.MapItem{Key: piece, Value: newMap, Position: pos}
lastMapItem.SetAnnotations(nodeAnns)
lastMapItem.SetAnnotations(template.NodeAnnotations{
yttoverlay.AnnotationMatch: template.NodeAnnotation{
Kwargs: []starlark.Tuple{{
starlark.String(yttoverlay.MatchAnnotationKwargMissingOK),
starlark.Bool(true),
}},
},
})

currMap.Items = append(currMap.Items, lastMapItem)
currMap = newMap
Expand All @@ -327,7 +326,12 @@ func (s *DataValuesFlags) buildOverlay(keyPieces []string, value interface{}, de
// Explicitly replace entire value at given key
// (this allows to specify non-scalar data values)
existingAnns := template.NewAnnotations(lastMapItem)
existingAnns[yttoverlay.AnnotationReplace] = template.NodeAnnotation{}
existingAnns[yttoverlay.AnnotationReplace] = template.NodeAnnotation{
Kwargs: []starlark.Tuple{{
starlark.String(yttoverlay.ReplaceAnnotationKwargOrAdd),
starlark.Bool(true),
}},
}
lastMapItem.SetAnnotations(existingAnns)

return &yamlmeta.Document{Value: resultMap, Position: pos}
Expand Down

0 comments on commit 706e81c

Please sign in to comment.