mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
Switch namespace and patch transformers to kyaml.
This commit is contained in:
@@ -6,6 +6,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/patchstrategicmerge"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
@@ -34,6 +35,11 @@ func (p *plugin) Config(
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !strings.Contains(string(c), "yamlSupport") {
|
||||
// If not explicitly denied,
|
||||
// activate kyaml-based transformation.
|
||||
p.YAMLSupport = true
|
||||
}
|
||||
if len(p.Paths) == 0 && p.Patches == "" {
|
||||
return fmt.Errorf("empty file path and empty patch content")
|
||||
}
|
||||
@@ -79,17 +85,6 @@ func (p *plugin) Transform(m resmap.ResMap) error {
|
||||
}
|
||||
if !p.YAMLSupport {
|
||||
err = target.Patch(patch.Copy())
|
||||
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
|
||||
}
|
||||
}
|
||||
} else {
|
||||
patchCopy := patch.DeepCopy()
|
||||
patchCopy.SetName(target.GetName())
|
||||
@@ -103,6 +98,19 @@ func (p *plugin) Transform(m resmap.ResMap) error {
|
||||
Patch: node,
|
||||
}, target)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(target.Map()) == 0 {
|
||||
// This means all fields have been removed from the object.
|
||||
// This can happen if a patch required deletion of the
|
||||
// entire resource (not just a part of it). This means
|
||||
// the overall resmap must shrink by one.
|
||||
err = m.Remove(target.CurId())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1208,9 +1208,89 @@ func TestMultipleNamespaces(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// We don't run this for the kyaml implementation since we don't support
|
||||
// the strategic merge patch directives (yet).
|
||||
func TestPatchStrategicMergeTransformerPatchDelete(t *testing.T) {
|
||||
func TestPatchStrategicMergeTransformerPatchDelete1(t *testing.T) {
|
||||
th := kusttest_test.MakeEnhancedHarness(t).
|
||||
PrepBuiltin("PatchStrategicMergeTransformer")
|
||||
defer th.Reset()
|
||||
|
||||
th.WriteF("patch.yaml", `
|
||||
apiVersion: apps/v1
|
||||
metadata:
|
||||
name: myDeploy
|
||||
kind: Deployment
|
||||
spec:
|
||||
replica: 2
|
||||
template:
|
||||
$patch: delete
|
||||
metadata:
|
||||
labels:
|
||||
old-label: old-value
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx
|
||||
`)
|
||||
|
||||
rm := th.LoadAndRunTransformer(`
|
||||
apiVersion: builtin
|
||||
kind: PatchStrategicMergeTransformer
|
||||
metadata:
|
||||
name: notImportantHere
|
||||
paths:
|
||||
- patch.yaml
|
||||
`, target)
|
||||
|
||||
th.AssertActualEqualsExpected(rm, `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: myDeploy
|
||||
spec:
|
||||
replica: 2
|
||||
`)
|
||||
}
|
||||
|
||||
func TestPatchStrategicMergeTransformerPatchDelete2(t *testing.T) {
|
||||
th := kusttest_test.MakeEnhancedHarness(t).
|
||||
PrepBuiltin("PatchStrategicMergeTransformer")
|
||||
defer th.Reset()
|
||||
|
||||
th.WriteF("patch.yaml", `
|
||||
apiVersion: apps/v1
|
||||
metadata:
|
||||
name: myDeploy
|
||||
kind: Deployment
|
||||
spec:
|
||||
$patch: delete
|
||||
replica: 2
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
old-label: old-value
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx
|
||||
`)
|
||||
|
||||
rm := th.LoadAndRunTransformer(`
|
||||
apiVersion: builtin
|
||||
kind: PatchStrategicMergeTransformer
|
||||
metadata:
|
||||
name: notImportantHere
|
||||
paths:
|
||||
- patch.yaml
|
||||
`, target)
|
||||
|
||||
th.AssertActualEqualsExpected(rm, `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: myDeploy
|
||||
`)
|
||||
}
|
||||
|
||||
func TestPatchStrategicMergeTransformerPatchDelete3(t *testing.T) {
|
||||
th := kusttest_test.MakeEnhancedHarness(t).
|
||||
PrepBuiltin("PatchStrategicMergeTransformer")
|
||||
defer th.Reset()
|
||||
@@ -1234,3 +1314,4 @@ paths:
|
||||
|
||||
th.AssertActualEqualsExpected(rm, ``)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user