Skip to content

Commit

Permalink
Merge pull request #1396 from Liujingfang1/delete
Browse files Browse the repository at this point in the history
support strategic merge patch with $patch: delete
  • Loading branch information
k8s-ci-robot authored Jul 25, 2019
2 parents 99a21b0 + fc78917 commit e646bba
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
6 changes: 5 additions & 1 deletion k8sdeps/kunstruct/kunstruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,11 @@ func (fs *UnstructAdapter) Patch(patch ifc.Kunstructured) error {
}
}
fs.SetMap(merged)
fs.SetName(saveName)
if len(fs.Map()) != 0 {
// if the patch deletes the object
// don't reset the name
fs.SetName(saveName)
}
return nil
}

Expand Down
8 changes: 8 additions & 0 deletions plugin/builtin/PatchStrategicMergeTransformer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ func (p *plugin) Transform(m resmap.ResMap) error {
if err != nil {
return err
}
// remove the resource from resmap
// when the patch is to $patch: delete that target
if len(target.Map()) == 0 {
err = m.Remove(target.CurId())
if err != nil {
return err
}
}
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -1198,3 +1198,32 @@ func TestMultipleNamespaces(t *testing.T) {
}
}
}

func TestPatchStrategicMergeTransformerPatchDelete(t *testing.T) {
tc := plugins_test.NewEnvForTest(t).Set()
defer tc.Reset()

tc.BuildGoPlugin(
"builtin", "", "PatchStrategicMergeTransformer")

th := kusttest_test.NewKustTestPluginHarness(t, "/app")

th.WriteF("/app/patch.yaml", `
apiVersion: apps/v1
metadata:
name: myDeploy
kind: Deployment
$patch: delete
`)

rm := th.LoadAndRunTransformer(`
apiVersion: builtin
kind: PatchStrategicMergeTransformer
metadata:
name: notImportantHere
paths:
- patch.yaml
`, target)

th.AssertActualEqualsExpected(rm, ``)
}

0 comments on commit e646bba

Please sign in to comment.