diff --git a/cmd/config/internal/commands/source.go b/cmd/config/internal/commands/source.go index 7873956ce..d731aa5c8 100644 --- a/cmd/config/internal/commands/source.go +++ b/cmd/config/internal/commands/source.go @@ -77,6 +77,6 @@ func (r *SourceRunner) runE(c *cobra.Command, args []string) error { inputs = []kio.Reader{&kio.ByteReader{Reader: c.InOrStdin()}} } - err := kio.Pipeline{Inputs: inputs, Outputs: outputs}.Execute() + err := kio.Pipeline{Inputs: inputs, Outputs: outputs, ContinueIfInputEmpty: true}.Execute() return handleError(c, err) } diff --git a/kyaml/kio/kio.go b/kyaml/kio/kio.go index 32bdebb32..c5165ab04 100644 --- a/kyaml/kio/kio.go +++ b/kyaml/kio/kio.go @@ -75,6 +75,11 @@ type Pipeline struct { // Outputs are where the transformed Resource Configuration is written. Outputs []Writer `yaml:"outputs,omitempty"` + + // ContinueIfInputEmpty indicates should pipeline continue when the + // the input result is empty. This is useful when we want to run outputs + // even the input is empty. + ContinueIfInputEmpty bool } // Execute executes each step in the sequence, returning immediately after encountering @@ -99,7 +104,7 @@ func (p Pipeline) ExecuteWithCallback(callback PipelineExecuteCallbackFunc) erro } result = append(result, nodes...) } - if len(result) == 0 { + if len(result) == 0 && !p.ContinueIfInputEmpty { // no inputs to operate on return nil } diff --git a/kyaml/kio/kio_test.go b/kyaml/kio/kio_test.go index d51301d9b..ad4cd70bb 100644 --- a/kyaml/kio/kio_test.go +++ b/kyaml/kio/kio_test.go @@ -4,7 +4,9 @@ package kio_test import ( + "bytes" "reflect" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -75,3 +77,34 @@ func TestPipelineWithCallback(t *testing.T) { ) } } + +func TestContinueIfInputEmpty(t *testing.T) { + actual := &bytes.Buffer{} + output := ByteWriter{ + Sort: true, + WrappingKind: ResourceListKind, + WrappingAPIVersion: ResourceListAPIVersion, + } + output.Writer = actual + + p := Pipeline{ + Outputs: []Writer{output}, + ContinueIfInputEmpty: true, + } + + err := p.Execute() + if err != nil { + t.Fatal(err) + } + + expected := ` +apiVersion: config.kubernetes.io/v1alpha1 +kind: ResourceList +items: [] +` + + if !assert.Equal(t, + strings.TrimSpace(expected), strings.TrimSpace(actual.String())) { + t.FailNow() + } +}