diff --git a/api/internal/target/kusttarget.go b/api/internal/target/kusttarget.go index 0fe3c6daf5..b05743e6f3 100644 --- a/api/internal/target/kusttarget.go +++ b/api/internal/target/kusttarget.go @@ -235,7 +235,18 @@ func (kt *KustTarget) runGenerators( func (kt *KustTarget) configureExternalGenerators() ([]resmap.Generator, error) { ra := accumulator.MakeEmptyAccumulator() - ra, err := kt.accumulateResources(ra, kt.kustomization.Generators) + var generatorPaths []string + for _, p := range kt.kustomization.Generators { + // handle inline generators + rm, err := kt.rFactory.NewResMapFromBytes([]byte(p)) + if err != nil { + // not an inline config + generatorPaths = append(generatorPaths, p) + continue + } + ra.AppendAll(rm) + } + ra, err := kt.accumulateResources(ra, generatorPaths) if err != nil { return nil, err } @@ -260,7 +271,18 @@ func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator) error { func (kt *KustTarget) configureExternalTransformers(transformers []string) ([]resmap.Transformer, error) { ra := accumulator.MakeEmptyAccumulator() - ra, err := kt.accumulateResources(ra, transformers) + var transformerPaths []string + for _, p := range transformers { + // handle inline transformers + rm, err := kt.rFactory.NewResMapFromBytes([]byte(p)) + if err != nil { + // not an inline config + transformerPaths = append(transformerPaths, p) + continue + } + ra.AppendAll(rm) + } + ra, err := kt.accumulateResources(ra, transformerPaths) if err != nil { return nil, err diff --git a/api/krusty/inlinetransformer_test.go b/api/krusty/inlinetransformer_test.go new file mode 100644 index 0000000000..f204e5a1c1 --- /dev/null +++ b/api/krusty/inlinetransformer_test.go @@ -0,0 +1,79 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package krusty_test + +import ( + "testing" + + kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" +) + +func TestInlineTransformer(t *testing.T) { + th := kusttest_test.MakeEnhancedHarness(t) + defer th.Reset() + + th.WriteF("/app/resource.yaml", ` +apiVersion: apps/v1 +kind: ConfigMap +metadata: + name: whatever +data: {} +`) + th.WriteK("/app", ` +resources: +- resource.yaml +transformers: +- |- + apiVersion: builtin + kind: NamespaceTransformer + metadata: + name: not-important-to-example + namespace: test + fieldSpecs: + - path: metadata/namespace + create: true +`) + + expected := ` +apiVersion: apps/v1 +data: {} +kind: ConfigMap +metadata: + name: whatever + namespace: test +` + + m := th.Run("/app", th.MakeDefaultOptions()) + th.AssertActualEqualsExpected(m, expected) +} + +func TestInlineGenerator(t *testing.T) { + th := kusttest_test.MakeEnhancedHarness(t) + defer th.Reset() + + th.WriteK("/app", ` +generators: +- |- + apiVersion: builtin + kind: ConfigMapGenerator + metadata: + name: mymap + literals: + - FRUIT=apple + - VEGETABLE=carrot +`) + + expected := ` +apiVersion: v1 +data: + FRUIT: apple + VEGETABLE: carrot +kind: ConfigMap +metadata: + name: mymap-kfd8tf729k +` + + m := th.Run("/app", th.MakeDefaultOptions()) + th.AssertActualEqualsExpected(m, expected) +}