Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/internal/builtins/PatchJson6902Transformer.go

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

52 changes: 40 additions & 12 deletions api/internal/builtins/PatchTransformer.go

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

151 changes: 151 additions & 0 deletions api/krusty/extendedpatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package krusty_test
import (
"testing"

"github.com/stretchr/testify/assert"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
)

Expand Down Expand Up @@ -816,6 +817,32 @@ patches:
th.WriteF("base/patch.yaml", `
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
annotations:
new-key: new-value
`)
err := th.RunWithErr("base", th.MakeDefaultOptions())
assert.Contains(t, err.Error(), "patches target not found for [noKind].[noVer].[noGrp]/no-match.[noNs]")
}

func TestExtendedPatchAllowNoMatch(t *testing.T) {
th := kusttest_test.MakeHarness(t)
makeCommonFileForExtendedPatchTest(th)
th.WriteK("base", `
resources:
- deployment.yaml
- service.yaml
patches:
- path: patch.yaml
target:
name: no-match
options:
allowNoTargetMatch: true
`)
th.WriteF("base/patch.yaml", `
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
annotations:
Expand Down Expand Up @@ -1016,6 +1043,38 @@ patches:
th.WriteF("base/patch.yaml", `
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
annotations:
new-key: new-value
`)
err := th.RunWithErr("base", th.MakeDefaultOptions())
assert.Contains(t, err.Error(), "patches target not found for [noKind].[noVer].[noGrp]/no-match.[noNs]")
}

func TestExtendedPatchAllowNoMatchMultiplePatch(t *testing.T) {
th := kusttest_test.MakeHarness(t)
makeCommonFileForExtendedPatchTest(th)
th.WriteK("base", `
resources:
- deployment.yaml
- service.yaml
patches:
- path: patch.yaml
target:
name: no-match
options:
allowNoTargetMatch: true
- path: patch.yaml
target:
name: busybox
kind: Job
options:
allowNoTargetMatch: true
`)
th.WriteF("base/patch.yaml", `
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
annotations:
Expand Down Expand Up @@ -1213,3 +1272,95 @@ spec:
app: busybox
`)
}

func TestTargetMissingPatchJson6902Error(t *testing.T) {
th := kusttest_test.MakeHarness(t)
makeCommonFileForExtendedPatchTest(th)
th.WriteK("base", `
resources:
- service.yaml
patchesJson6902:
- target:
kind: Service
name: busybox
version: v2
path: patch.yaml
`)
th.WriteF("base/patch.yaml", `
- op: add
path: /metadata/labels/release
value: this-label-will-not-go-through
`)
err := th.RunWithErr("base", th.MakeDefaultOptions())
assert.Contains(t, err.Error(), "patchesJson6902 target not found for Service.v2.[noGrp]/busybox.[noNs]")
}

func TestTargetMissingJsonPatchError(t *testing.T) {
th := kusttest_test.MakeHarness(t)
makeCommonFileForExtendedPatchTest(th)
th.WriteK("base", `
resources:
- service.yaml
patches:
- target:
kind: Service
name: busybox
version: v2
path: patch.yaml
`)
th.WriteF("base/patch.yaml", `
- op: add
path: /metadata/labels/release
value: this-label-will-not-go-through
`)
err := th.RunWithErr("base", th.MakeDefaultOptions())
assert.Contains(t, err.Error(), "patches target not found for Service.v2.[noGrp]/busybox.[noNs]")
}

func TestAllowNoTargetMatchPatchTransformerOptions(t *testing.T) {
th := kusttest_test.MakeHarness(t)
makeCommonFileForExtendedPatchTest(th)
th.WriteK("base", `
resources:
- service.yaml
patches:
- target:
kind: Service
name: busybox
version: v2
path: patch.yaml
options:
allowNoTargetMatch: true
`)
th.WriteF("base/patch.yaml", `
- op: add
path: /metadata/labels/release
value: this-label-will-not-go-through
`)
m := th.Run("base", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, `
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: nginx
spec:
ports:
- port: 80
selector:
app: nginx
---
apiVersion: v1
kind: Service
metadata:
labels:
app: busybox
name: busybox
spec:
ports:
- port: 8080
selector:
app: busybox
`)
}
15 changes: 8 additions & 7 deletions api/types/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,19 @@ func TestPatchEquals(t *testing.T) {
Patch: "bar",
Target: &selector,
Options: &PatchArgs{
AllowNameChange: true,
AllowKindChange: true,
AllowNameChange: true,
AllowKindChange: true,
AllowNoTargetMatch: false,
},
},
patch2: Patch{
Path: "foo",
Patch: "bar",
Target: &selector,
Options: &PatchArgs{
AllowNameChange: true,
AllowKindChange: true,
AllowNameChange: true,
AllowKindChange: true,
AllowNoTargetMatch: false,
},
},
expect: true,
Expand Down Expand Up @@ -153,7 +155,6 @@ func TestPatchEquals(t *testing.T) {
Patch: "bar",
Target: &selector,
Options: &PatchArgs{
AllowNameChange: false,
AllowKindChange: true,
},
},
Expand All @@ -162,8 +163,8 @@ func TestPatchEquals(t *testing.T) {
Patch: "bar",
Target: &selector,
Options: &PatchArgs{
AllowNameChange: true,
AllowKindChange: true,
AllowKindChange: true,
AllowNoTargetMatch: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is changing an existing test. Perhaps it would be better to keep it as-is, making only the necessary changes to pass with the current API changes, and add a new test to cover the new feature?

I'm also a bit confused about the removal of line 156.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my bad, I thought it wouldn't make a difference if I change a bit the test.
I'll revert this change.

},
},
expect: false,
Expand Down
3 changes: 3 additions & 0 deletions api/types/patchargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ type PatchArgs struct {

// AllowKindChange allows kind changes to the resource.
AllowKindChange bool `json:"allowKindChange,omitempty" yaml:"allowKindChange,omitempty"`

// AllowNoTargetMatch allows files rendering in case of no target (`api/types/selector`) match.
AllowNoTargetMatch bool `json:"allowNoTargetMatch,omitempty" yaml:"allowNoTargetMatch,omitempty"`
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we include some tests for this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func (p *plugin) Transform(m resmap.ResMap) error {
if err != nil {
return err
}

if len(resources) == 0 {
return fmt.Errorf("patchesJson6902 target not found for %s", p.Target.ResId)
}

for _, res := range resources {
internalAnnotations := kioutil.GetInternalAnnotations(&res.RNode)

Expand Down
Loading
Loading