Skip to content

Commit dd08aec

Browse files
authored
fix: Don't panic on multiple $patch: delete strategic merge patches in a single patch file (#5859)
* chore: add test for multiple $patch: delete patches not panicking * fix: don't panic on multiple deletion SM patches
1 parent 2e80ceb commit dd08aec

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

api/krusty/multiplepatch_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,72 @@ metadata:
12821282
}
12831283
}
12841284

1285+
func TestSinglePatchWithMultiplePatchDeleteDirectives(t *testing.T) {
1286+
th := kusttest_test.MakeHarness(t)
1287+
makeCommonFilesForMultiplePatchTests(th)
1288+
th.WriteF("overlay/staging/deployment-patch1.yaml", `
1289+
$patch: delete
1290+
apiVersion: apps/v1
1291+
kind: Deployment
1292+
metadata:
1293+
name: nginx
1294+
---
1295+
$patch: delete
1296+
apiVersion: v1
1297+
kind: Service
1298+
metadata:
1299+
name: nginx
1300+
`)
1301+
th.WriteF("overlay/staging/deployment-patch2.yaml", `
1302+
apiVersion: v1
1303+
kind: ConfigMap
1304+
metadata:
1305+
name: configmap-in-base
1306+
data:
1307+
foo2: bar2
1308+
`)
1309+
th.WriteK("overlay/staging", `
1310+
namePrefix: staging-
1311+
commonLabels:
1312+
env: staging
1313+
patches:
1314+
- path: deployment-patch1.yaml
1315+
- path: deployment-patch2.yaml
1316+
resources:
1317+
- ../../base
1318+
configMapGenerator:
1319+
- name: configmap-in-overlay
1320+
literals:
1321+
- hello=world
1322+
`)
1323+
m := th.Run("overlay/staging", th.MakeDefaultOptions())
1324+
th.AssertActualEqualsExpected(m, `
1325+
apiVersion: v1
1326+
data:
1327+
foo: bar
1328+
foo2: bar2
1329+
kind: ConfigMap
1330+
metadata:
1331+
annotations:
1332+
note: This is a test annotation
1333+
labels:
1334+
app: mynginx
1335+
env: staging
1336+
org: example.com
1337+
team: foo
1338+
name: staging-team-foo-configmap-in-base-8cmgkm9f44
1339+
---
1340+
apiVersion: v1
1341+
data:
1342+
hello: world
1343+
kind: ConfigMap
1344+
metadata:
1345+
labels:
1346+
env: staging
1347+
name: staging-configmap-in-overlay-dc6fm46dhm
1348+
`)
1349+
}
1350+
12851351
func TestMultiplePatchesBothWithPatchDeleteDirective(t *testing.T) {
12861352
th := kusttest_test.MakeHarness(t)
12871353
makeCommonFilesForMultiplePatchTests(th)

api/resmap/reswrangler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ func (m *resWrangler) GetMatchingResourcesByAnyId(
181181
matches IdMatcher) []*resource.Resource {
182182
var result []*resource.Resource
183183
for _, r := range m.rList {
184+
if r.RNode.IsNilOrEmpty() {
185+
continue
186+
}
187+
184188
for _, id := range append(r.PrevIds(), r.CurId()) {
185189
if matches(id) {
186190
result = append(result, r)

0 commit comments

Comments
 (0)