Skip to content

Commit d94d556

Browse files
committed
feat: add PatchArgs API type to populate patch options
This commit converts the Options section of a patch into an object instead of map. This allows better clarification of the available options.
1 parent 7558804 commit d94d556

File tree

5 files changed

+76
-14
lines changed

5 files changed

+76
-14
lines changed

api/internal/target/kusttarget_configplugin.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,10 @@ var transformerConfigurators = map[builtinhelpers.BuiltinPluginType]func(
250250
return
251251
}
252252
var c struct {
253-
Path string `json:"path,omitempty" yaml:"path,omitempty"`
254-
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
255-
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
256-
Options map[string]bool `json:"options,omitempty" yaml:"options,omitempty"`
253+
Path string `json:"path,omitempty" yaml:"path,omitempty"`
254+
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
255+
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
256+
Options *types.PatchArgs `json:"options,omitempty" yaml:"options,omitempty"`
257257
}
258258
for _, pc := range kt.kustomization.Patches {
259259
c.Target = pc.Target

api/types/patch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
package types
55

6-
import "reflect"
7-
86
// Patch represent either a Strategic Merge Patch or a JSON patch
97
// and its targets.
108
// The content of the patch can either be from a file
@@ -20,15 +18,17 @@ type Patch struct {
2018
Target *Selector `json:"target,omitempty" yaml:"target,omitempty"`
2119

2220
// Options is a list of options for the patch
23-
Options map[string]bool `json:"options,omitempty" yaml:"options,omitempty"`
21+
Options *PatchArgs `json:"options,omitempty" yaml:"options,omitempty"`
2422
}
2523

2624
// Equals return true if p equals o.
2725
func (p *Patch) Equals(o Patch) bool {
2826
targetEqual := (p.Target == o.Target) ||
2927
(p.Target != nil && o.Target != nil && *p.Target == *o.Target)
28+
optionsEqual := (p.Options == o.Options) ||
29+
(p.Options != nil && o.Options != nil && *p.Options == *o.Options)
3030
return p.Path == o.Path &&
3131
p.Patch == o.Patch &&
3232
targetEqual &&
33-
reflect.DeepEqual(p.Options, o.Options)
33+
optionsEqual
3434
}

api/types/patch_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@ func TestPatchEquals(t *testing.T) {
101101
},
102102
expect: true,
103103
},
104+
{
105+
name: "same options",
106+
patch1: Patch{
107+
Path: "foo",
108+
Patch: "bar",
109+
Target: &selector,
110+
Options: &PatchArgs{
111+
AllowNameChange: true,
112+
AllowNoTargetMatch: true,
113+
},
114+
},
115+
patch2: Patch{
116+
Path: "foo",
117+
Patch: "bar",
118+
Target: &selector,
119+
Options: &PatchArgs{
120+
AllowNameChange: true,
121+
AllowNoTargetMatch: true,
122+
},
123+
},
124+
expect: true,
125+
},
104126
{
105127
name: "one nil target",
106128
patch1: Patch{
@@ -124,6 +146,28 @@ func TestPatchEquals(t *testing.T) {
124146
},
125147
expect: false,
126148
},
149+
{
150+
name: "different options",
151+
patch1: Patch{
152+
Path: "foo",
153+
Patch: "bar",
154+
Target: &selector,
155+
Options: &PatchArgs{
156+
AllowNameChange: false,
157+
AllowNoTargetMatch: true,
158+
},
159+
},
160+
patch2: Patch{
161+
Path: "foo",
162+
Patch: "bar",
163+
Target: &selector,
164+
Options: &PatchArgs{
165+
AllowNameChange: true,
166+
AllowNoTargetMatch: true,
167+
},
168+
},
169+
expect: false,
170+
},
127171
}
128172

129173
for _, tc := range testcases {

api/types/patchargs.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2019 The Kubernetes Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package types
5+
6+
// PatchArgs represent set of options on resources of a patch.
7+
type PatchArgs struct {
8+
// AllowNameChange allows name changes to the resource.
9+
AllowNameChange bool `json:"allowNameChange,omitempty" yaml:"allowNameChange,omitempty"`
10+
11+
// AllowKindChange allows kind changes to the resource.
12+
AllowKindChange bool `json:"allowKindChange,omitempty" yaml:"allowKindChange,omitempty"`
13+
14+
// AllowNoTargetMatch allows files rendering in case of no target (`api/types/selector`) match.
15+
AllowNoTargetMatch bool `json:"allowNoTargetMatch,omitempty" yaml:"allowNoTargetMatch,omitempty"`
16+
}

kustomize/commands/edit/fix/convert.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func constructTargets(file string, node *kyaml.RNode, fieldPaths []string,
241241
}
242242

243243
if patch, ok := patchTarget[file]; ok {
244-
if !patch.Options["allowNameChange"] || !patch.Options["allowKindChange"] {
244+
if patch.Options == nil || (!patch.Options.AllowNameChange || !patch.Options.AllowKindChange) {
245245
return writePatchTargets(patch, node, fieldPaths, options)
246246
}
247247
}
@@ -299,11 +299,13 @@ func writePatchTargets(patch types.Patch, node *kyaml.RNode, fieldPaths []string
299299
if options[i].String() != "" {
300300
target.Options = options[i]
301301
}
302-
if patch.Options["allowNameChange"] {
303-
target.Select.ResId.Name = node.GetName()
304-
}
305-
if patch.Options["allowKindChange"] {
306-
target.Select.ResId.Kind = node.GetKind()
302+
if patch.Options != nil {
303+
if patch.Options.AllowNameChange {
304+
target.Select.ResId.Name = node.GetName()
305+
}
306+
if patch.Options.AllowKindChange {
307+
target.Select.ResId.Kind = node.GetKind()
308+
}
307309
}
308310
if node.GetNamespace() != "" {
309311
target.Select.ResId.Namespace = node.GetNamespace()

0 commit comments

Comments
 (0)