Option to continue pipeline processing when filter returns empty result

This commit is contained in:
Katrina Verey
2020-10-16 11:28:07 -07:00
parent 7e04be9ec6
commit 44619d5ca2
2 changed files with 49 additions and 1 deletions

View File

@@ -75,6 +75,16 @@ type Pipeline struct {
// Outputs are where the transformed Resource Configuration is written.
Outputs []Writer `yaml:"outputs,omitempty"`
// ContinueOnEmptyResult configures what happens when a filter in the pipeline
// returns an empty result.
// If it is false (default), subsequent filters will be skipped and the result
// will be returned immediately. This is useful as an optimization when you
// know that subsequent filters will not alter the empty result.
// If it is true, the empty result will be provided as input to the next
// filter in the list. This is useful when subsequent functions in the
// pipeline may generate new resources.
ContinueOnEmptyResult bool `yaml:"continueOnEmptyResult,omitempty"`
}
// Execute executes each step in the sequence, returning immediately after encountering
@@ -111,7 +121,7 @@ func (p Pipeline) ExecuteWithCallback(callback PipelineExecuteCallbackFunc) erro
// TODO (issue 2872): This len(result) == 0 should be removed and empty result list should be
// handled by outputs. However currently some writer like LocalPackageReadWriter
// will clear the output directory and which will cause unpredictable results
if len(result) == 0 || err != nil {
if len(result) == 0 && !p.ContinueOnEmptyResult || err != nil {
return errors.Wrap(err)
}
}

View File

@@ -149,3 +149,41 @@ items:
t.FailNow()
}
}
func TestContinueOnEmptyBehavior(t *testing.T) {
cases := map[string]struct {
continueOnEmptyResult bool
expected string
}{
"quit on empty": {continueOnEmptyResult: false, expected: ""},
"continue on empty": {continueOnEmptyResult: true, expected: "foo: bar"},
}
for _, tc := range cases {
actual := &bytes.Buffer{}
output := ByteWriter{Writer: actual}
generatorFunc := FilterFunc(func(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
nodes = append(nodes, yaml.NewMapRNode(&map[string]string{
"foo": "bar",
}))
return nodes, nil
})
emptyFunc := FilterFunc(func(nodes []*yaml.RNode) ([]*yaml.RNode, error) { return nodes, nil })
p := Pipeline{
Outputs: []Writer{output},
Filters: []Filter{emptyFunc, generatorFunc},
ContinueOnEmptyResult: tc.continueOnEmptyResult,
}
err := p.Execute()
if err != nil {
t.Fatal(err)
}
if !assert.Equal(t,
tc.expected, strings.TrimSpace(actual.String())) {
t.Fail()
}
}
}