Skip to content

Commit 311dbbf

Browse files
authored
Merge pull request #5990 from ralgozino/fix/allow-empty-strategicmerge-patches-files
fix: allow empty patches files
2 parents 77b3446 + 24ea1b9 commit 311dbbf

File tree

3 files changed

+114
-2
lines changed

3 files changed

+114
-2
lines changed

api/internal/builtins/PatchTransformer.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/krusty/multiplepatch_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package krusty_test
55

66
import (
7+
"strings"
78
"testing"
89

910
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
@@ -1766,3 +1767,108 @@ metadata:
17661767
name: fluentd-sa-abc
17671768
`)
17681769
}
1770+
1771+
// TestEmptyPatchFilesShouldBeIgnored verifies that empty patch files are ignored.
1772+
// Tests three cases:
1773+
// 1. Completely empty file
1774+
// 2. File with only comments
1775+
// 3. File with whitespace and comments
1776+
func TestEmptyPatchFilesShouldBeIgnored(t *testing.T) {
1777+
th := kusttest_test.MakeHarness(t)
1778+
1779+
// Write a basic resource
1780+
th.WriteF("deployment.yaml", `
1781+
apiVersion: apps/v1
1782+
kind: Deployment
1783+
metadata:
1784+
name: nginx
1785+
spec:
1786+
template:
1787+
spec:
1788+
containers:
1789+
- name: nginx
1790+
image: nginx
1791+
`)
1792+
1793+
// Create empty patch files of different types
1794+
th.WriteF("empty.yaml", ``)
1795+
th.WriteF("comments-only.yaml", `
1796+
# This is a comment
1797+
# Another comment
1798+
`)
1799+
th.WriteF("whitespace.yaml", `
1800+
1801+
# Comments with whitespace
1802+
1803+
# Indented comment
1804+
1805+
`)
1806+
1807+
// Reference empty patches in kustomization
1808+
th.WriteK(".", `
1809+
resources:
1810+
- deployment.yaml
1811+
patches:
1812+
- path: empty.yaml
1813+
- path: comments-only.yaml
1814+
- path: whitespace.yaml
1815+
`)
1816+
1817+
// Empty patches should be ignored, output should be unchanged
1818+
m := th.Run(".", th.MakeDefaultOptions())
1819+
th.AssertActualEqualsExpected(m, `
1820+
apiVersion: apps/v1
1821+
kind: Deployment
1822+
metadata:
1823+
name: nginx
1824+
spec:
1825+
template:
1826+
spec:
1827+
containers:
1828+
- image: nginx
1829+
name: nginx
1830+
`)
1831+
}
1832+
1833+
// TestEmptyPatchesStrategicMergeFails verifies that empty patch files are
1834+
// handled correctly with the deprecated patchesStrategicMerge field
1835+
func TestEmptyPatchesStrategicMergeFails(t *testing.T) {
1836+
th := kusttest_test.MakeHarness(t)
1837+
1838+
// Create a basic resource
1839+
th.WriteF("resource.yaml", `
1840+
apiVersion: v1
1841+
kind: ConfigMap
1842+
metadata:
1843+
name: dummy
1844+
data:
1845+
dummy: value
1846+
`)
1847+
1848+
// Create an empty patch file
1849+
th.WriteF("empty-patch.yaml", ``)
1850+
1851+
// Create a patch file with only comments
1852+
th.WriteF("comments-patch.yaml", `
1853+
# This is just a comment
1854+
# Another comment
1855+
`)
1856+
1857+
// Create kustomization using patchesStrategicMerge
1858+
th.WriteK(".", `
1859+
resources:
1860+
- resource.yaml
1861+
patchesStrategicMerge:
1862+
- empty-patch.yaml
1863+
- comments-patch.yaml
1864+
`)
1865+
1866+
// This fails with message
1867+
err := th.RunWithErr(".", th.MakeDefaultOptions())
1868+
if err == nil {
1869+
t.Fatalf("expected error for empty patchesStrategicMerge files but got none")
1870+
}
1871+
if !strings.Contains(err.Error(), "patch appears to be empty") {
1872+
t.Fatalf("unexpected error: %v", err)
1873+
}
1874+
}

plugin/builtin/patchtransformer/PatchTransformer.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ func (p *plugin) Transform(m resmap.ResMap) error {
9494
if p.smPatches != nil {
9595
return p.transformStrategicMerge(m)
9696
}
97-
return p.transformJson6902(m)
97+
if p.jsonPatches != nil {
98+
return p.transformJson6902(m)
99+
}
100+
return nil
98101
}
99102

100103
// transformStrategicMerge applies each loaded strategic merge patch

0 commit comments

Comments
 (0)