Skip to content

Detect no matching resources #5917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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.

61 changes: 49 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.

8 changes: 4 additions & 4 deletions api/internal/target/kusttarget_configplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ var transformerConfigurators = map[builtinhelpers.BuiltinPluginType]func(
return
}
var c struct {
Path string `json:"path,omitempty" yaml:"path,omitempty"`
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
Options map[string]bool `json:"options,omitempty" yaml:"options,omitempty"`
Path string `json:"path,omitempty" yaml:"path,omitempty"`
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
Options *types.PatchArgs `json:"options,omitempty" yaml:"options,omitempty"`
}
for _, pc := range kt.kustomization.Patches {
c.Target = pc.Target
Expand Down
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
`)
}
8 changes: 4 additions & 4 deletions api/types/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

package types

import "reflect"

// Patch represent either a Strategic Merge Patch or a JSON patch
// and its targets.
// The content of the patch can either be from a file
Expand All @@ -20,15 +18,17 @@ type Patch struct {
Target *Selector `json:"target,omitempty" yaml:"target,omitempty"`

// Options is a list of options for the patch
Options map[string]bool `json:"options,omitempty" yaml:"options,omitempty"`
Options *PatchArgs `json:"options,omitempty" yaml:"options,omitempty"`
}

// Equals return true if p equals o.
func (p *Patch) Equals(o Patch) bool {
targetEqual := (p.Target == o.Target) ||
(p.Target != nil && o.Target != nil && *p.Target == *o.Target)
optionsEqual := (p.Options == o.Options) ||
(p.Options != nil && o.Options != nil && *p.Options == *o.Options)
return p.Path == o.Path &&
p.Patch == o.Patch &&
targetEqual &&
reflect.DeepEqual(p.Options, o.Options)
optionsEqual
}
44 changes: 44 additions & 0 deletions api/types/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ func TestPatchEquals(t *testing.T) {
},
expect: true,
},
{
name: "same options",
patch1: Patch{
Path: "foo",
Patch: "bar",
Target: &selector,
Options: &PatchArgs{
AllowNameChange: true,
AllowNoTargetMatch: true,
},
},
patch2: Patch{
Path: "foo",
Patch: "bar",
Target: &selector,
Options: &PatchArgs{
AllowNameChange: true,
AllowNoTargetMatch: true,
},
},
expect: true,
},
{
name: "one nil target",
patch1: Patch{
Expand All @@ -124,6 +146,28 @@ func TestPatchEquals(t *testing.T) {
},
expect: false,
},
{
name: "different options",
patch1: Patch{
Path: "foo",
Patch: "bar",
Target: &selector,
Options: &PatchArgs{
AllowNameChange: false,
AllowNoTargetMatch: true,
},
},
patch2: Patch{
Path: "foo",
Patch: "bar",
Target: &selector,
Options: &PatchArgs{
AllowNameChange: true,
AllowNoTargetMatch: true,
},
},
expect: false,
},
}

for _, tc := range testcases {
Expand Down
Loading