Switch namespace and patch transformers to kyaml.

This commit is contained in:
jregan
2020-06-27 14:24:12 -07:00
parent 42d1f7b792
commit d3a7335bbc
18 changed files with 234 additions and 81 deletions

View File

@@ -6,6 +6,7 @@ package main
import (
"fmt"
"strings"
"sigs.k8s.io/kustomize/api/filters/namespace"
"sigs.k8s.io/kustomize/api/resid"
@@ -35,6 +36,11 @@ func (p *plugin) Config(
_ *resmap.PluginHelpers, c []byte) (err error) {
p.Namespace = ""
p.FieldSpecs = nil
if !strings.Contains(string(c), "yamlSupport") {
// If not explicitly denied,
// activate kyaml-based transformation.
p.YAMLSupport = true
}
return yaml.Unmarshal(c, p)
}

View File

@@ -6,6 +6,7 @@ package main
import (
"fmt"
"strings"
jsonpatch "github.com/evanphx/json-patch"
"github.com/pkg/errors"
@@ -38,6 +39,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 p.Target.Name == "" {
return fmt.Errorf("must specify the target name")
}

View File

@@ -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
}

View File

@@ -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, ``)
}

View File

@@ -38,6 +38,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
}
p.Patch = strings.TrimSpace(p.Patch)
if p.Patch == "" && p.Path == "" {
return fmt.Errorf(