mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 10:30:59 +00:00
Allow patch removal of emptyDir {}
This commit is contained in:
@@ -32,45 +32,57 @@ func (p *plugin) Config(
|
||||
return fmt.Errorf("empty file path and empty patch content")
|
||||
}
|
||||
if len(p.Paths) != 0 {
|
||||
for _, onePath := range p.Paths {
|
||||
// The following oddly attempts to interpret a path string as an
|
||||
// actual patch (instead of as a path to a file containing a patch).
|
||||
// All tests pass if this code is commented out. This code should
|
||||
// be deleted; the user should use the Patches field which
|
||||
// exists for this purpose (inline patch declaration).
|
||||
res, err := h.ResmapFactory().RF().SliceFromBytes([]byte(onePath))
|
||||
if err == nil {
|
||||
p.loadedPatches = append(p.loadedPatches, res...)
|
||||
continue
|
||||
}
|
||||
res, err = h.ResmapFactory().RF().SliceFromPatches(
|
||||
h.Loader(), []types.PatchStrategicMerge{onePath})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.loadedPatches = append(p.loadedPatches, res...)
|
||||
}
|
||||
}
|
||||
if p.Patches != "" {
|
||||
res, err := h.ResmapFactory().RF().SliceFromBytes([]byte(p.Patches))
|
||||
patches, err := loadFromPaths(h, p.Paths)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.loadedPatches = append(p.loadedPatches, res...)
|
||||
p.loadedPatches = append(p.loadedPatches, patches...)
|
||||
}
|
||||
if p.Patches != "" {
|
||||
patches, err := h.ResmapFactory().RF().SliceFromBytes([]byte(p.Patches))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.loadedPatches = append(p.loadedPatches, patches...)
|
||||
}
|
||||
|
||||
if len(p.loadedPatches) == 0 {
|
||||
return fmt.Errorf(
|
||||
"patch appears to be empty; files=%v, Patch=%s", p.Paths, p.Patches)
|
||||
}
|
||||
// Merge the patches, looking for conflicts.
|
||||
_, err = h.ResmapFactory().ConflatePatches(p.loadedPatches)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO(#3723): Delete conflict detection.
|
||||
// Since #1500 closed, the conflict detector in use doesn't do
|
||||
// anything useful. The resmap returned by this method hasn't
|
||||
// been used for many releases. Leaving code as a comment to
|
||||
// aid in deletion (fixing #3723).
|
||||
// _, err = h.ResmapFactory().ConflatePatches(p.loadedPatches)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadFromPaths(
|
||||
h *resmap.PluginHelpers,
|
||||
paths []types.PatchStrategicMerge) (
|
||||
result []*resource.Resource, err error) {
|
||||
var patches []*resource.Resource
|
||||
for _, path := range paths {
|
||||
// For legacy reasons, attempt to treat the path string as
|
||||
// actual patch content.
|
||||
patches, err = h.ResmapFactory().RF().SliceFromBytes([]byte(path))
|
||||
if err != nil {
|
||||
// Failing that, treat it as a file path.
|
||||
patches, err = h.ResmapFactory().RF().SliceFromPatches(
|
||||
h.Loader(), []types.PatchStrategicMerge{path})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
result = append(result, patches...)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p *plugin) Transform(m resmap.ResMap) error {
|
||||
for _, patch := range p.loadedPatches {
|
||||
target, err := m.GetById(patch.OrgId())
|
||||
|
||||
@@ -562,6 +562,7 @@ func TestStrategicMergeTransformerNoSchemaMultiPatchesNoConflict(t *testing.T) {
|
||||
th := kusttest_test.MakeEnhancedHarness(t).
|
||||
PrepBuiltin("PatchStrategicMergeTransformer")
|
||||
defer th.Reset()
|
||||
// This patch wants to delete "B".
|
||||
th.WriteF("patch1.yaml", `
|
||||
apiVersion: example.com/v1
|
||||
kind: Foo
|
||||
@@ -603,10 +604,6 @@ spec:
|
||||
`)
|
||||
assert.NoError(t, err)
|
||||
th.AssertActualEqualsExpectedNoIdAnnotations(
|
||||
// In kyaml/yaml.merge2, the empty "B: " is dropped
|
||||
// when patch1 and patch2 are merged, so the patch
|
||||
// applied is effectively only patch2.yaml.
|
||||
// So it cannot delete the "B: Y"
|
||||
resMap, `
|
||||
apiVersion: example.com/v1
|
||||
kind: Foo
|
||||
@@ -615,7 +612,6 @@ metadata:
|
||||
spec:
|
||||
bar:
|
||||
A: X
|
||||
B: "Y"
|
||||
C: Z
|
||||
D: W
|
||||
baz:
|
||||
@@ -639,8 +635,7 @@ spec:
|
||||
`)
|
||||
assert.NoError(t, err)
|
||||
th.AssertActualEqualsExpectedNoIdAnnotations(
|
||||
// This time only patch2 was applied. Same answer on the kyaml
|
||||
// path, but different answer on apimachinery path (B becomes "true"?)
|
||||
// This time only patch2 is applied.
|
||||
resMap, `
|
||||
apiVersion: example.com/v1
|
||||
kind: Foo
|
||||
|
||||
Reference in New Issue
Block a user