mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 18:10:59 +00:00
Merge pull request #3644 from natasha41575/AllowGeneralNameChanges
allow general name and kind changes via an options field in patches
This commit is contained in:
@@ -21,6 +21,7 @@ type PatchTransformerPlugin struct {
|
|||||||
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
||||||
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
|
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
|
||||||
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
|
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
|
||||||
|
Options map[string]bool `json:"options,omitempty" yaml:"options,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PatchTransformerPlugin) Config(
|
func (p *PatchTransformerPlugin) Config(
|
||||||
@@ -60,6 +61,12 @@ func (p *PatchTransformerPlugin) Config(
|
|||||||
}
|
}
|
||||||
if errSM == nil {
|
if errSM == nil {
|
||||||
p.loadedPatch = patchSM
|
p.loadedPatch = patchSM
|
||||||
|
if p.Options["allowNameChange"] {
|
||||||
|
p.loadedPatch.SetAllowNameChange("true")
|
||||||
|
}
|
||||||
|
if p.Options["allowKindChange"] {
|
||||||
|
p.loadedPatch.SetAllowKindChange("true")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
p.decodedPatch = patchJson
|
p.decodedPatch = patchJson
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -352,6 +352,7 @@ func TestResolveVarsWithNoambiguation(t *testing.T) {
|
|||||||
"metadata": map[string]interface{}{
|
"metadata": map[string]interface{}{
|
||||||
"name": "sub-backendOne",
|
"name": "sub-backendOne",
|
||||||
"annotations": map[string]interface{}{
|
"annotations": map[string]interface{}{
|
||||||
|
"config.kubernetes.io/previousKinds": "Service",
|
||||||
"config.kubernetes.io/previousNames": "backendOne",
|
"config.kubernetes.io/previousNames": "backendOne",
|
||||||
"config.kubernetes.io/previousNamespaces": "default",
|
"config.kubernetes.io/previousNamespaces": "default",
|
||||||
"config.kubernetes.io/prefixes": "sub-",
|
"config.kubernetes.io/prefixes": "sub-",
|
||||||
|
|||||||
@@ -201,14 +201,16 @@ var transformerConfigurators = map[builtinhelpers.BuiltinPluginType]func(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var c struct {
|
var c struct {
|
||||||
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
||||||
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
|
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
|
||||||
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
|
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
|
||||||
|
Options map[string]bool `json:"options,omitempty" yaml:"options,omitempty"`
|
||||||
}
|
}
|
||||||
for _, pc := range kt.kustomization.Patches {
|
for _, pc := range kt.kustomization.Patches {
|
||||||
c.Target = pc.Target
|
c.Target = pc.Target
|
||||||
c.Patch = pc.Patch
|
c.Patch = pc.Patch
|
||||||
c.Path = pc.Path
|
c.Path = pc.Path
|
||||||
|
c.Options = pc.Options
|
||||||
p := f()
|
p := f()
|
||||||
err = kt.configureBuiltinPlugin(p, c, bpt)
|
err = kt.configureBuiltinPlugin(p, c, bpt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import (
|
|||||||
// GVKN shouldn't change with default options
|
// GVKN shouldn't change with default options
|
||||||
func TestKeepOriginalGVKN(t *testing.T) {
|
func TestKeepOriginalGVKN(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
|
||||||
th.WriteF("deployment.yaml", `
|
th.WriteF("deployment.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -27,14 +26,12 @@ spec:
|
|||||||
- name: nginx
|
- name: nginx
|
||||||
image: nginx
|
image: nginx
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteF("patch.yaml", `
|
th.WriteF("patch.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: StatefulSet
|
kind: StatefulSet
|
||||||
metadata:
|
metadata:
|
||||||
name: new-name
|
name: new-name
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteK(".", `
|
th.WriteK(".", `
|
||||||
resources:
|
resources:
|
||||||
- deployment.yaml
|
- deployment.yaml
|
||||||
@@ -44,7 +41,6 @@ patches:
|
|||||||
target:
|
target:
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
`)
|
`)
|
||||||
|
|
||||||
m := th.Run(".", th.MakeDefaultOptions())
|
m := th.Run(".", th.MakeDefaultOptions())
|
||||||
th.AssertActualEqualsExpected(m, `
|
th.AssertActualEqualsExpected(m, `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
@@ -64,7 +60,6 @@ spec:
|
|||||||
// These tests document behavior that will change
|
// These tests document behavior that will change
|
||||||
func TestChangeName(t *testing.T) {
|
func TestChangeName(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
|
||||||
th.WriteF("deployment.yaml", `
|
th.WriteF("deployment.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -77,14 +72,12 @@ spec:
|
|||||||
- name: nginx
|
- name: nginx
|
||||||
image: nginx
|
image: nginx
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteF("patch.yaml", `
|
th.WriteF("patch.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: new-name
|
name: new-name
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteK(".", `
|
th.WriteK(".", `
|
||||||
resources:
|
resources:
|
||||||
- deployment.yaml
|
- deployment.yaml
|
||||||
@@ -93,18 +86,16 @@ patches:
|
|||||||
- path: patch.yaml
|
- path: patch.yaml
|
||||||
target:
|
target:
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
|
options:
|
||||||
|
allowNameChange: true
|
||||||
`)
|
`)
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
options := th.MakeDefaultOptions()
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// name should become `new-name`
|
|
||||||
m := th.Run(".", options)
|
m := th.Run(".", options)
|
||||||
th.AssertActualEqualsExpected(m, `
|
th.AssertActualEqualsExpected(m, `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: old-name
|
name: new-name
|
||||||
spec:
|
spec:
|
||||||
template:
|
template:
|
||||||
spec:
|
spec:
|
||||||
@@ -116,7 +107,6 @@ spec:
|
|||||||
|
|
||||||
func TestChangeKind(t *testing.T) {
|
func TestChangeKind(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
|
||||||
th.WriteF("deployment.yaml", `
|
th.WriteF("deployment.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -129,32 +119,27 @@ spec:
|
|||||||
- name: nginx
|
- name: nginx
|
||||||
image: nginx
|
image: nginx
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteF("patch.yaml", `
|
th.WriteF("patch.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: StatefulSet
|
kind: StatefulSet
|
||||||
metadata:
|
metadata:
|
||||||
name: old-name
|
name: old-name
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteK(".", `
|
th.WriteK(".", `
|
||||||
resources:
|
resources:
|
||||||
- deployment.yaml
|
- deployment.yaml
|
||||||
|
|
||||||
patches:
|
patches:
|
||||||
- path: patch.yaml
|
- path: patch.yaml
|
||||||
target:
|
target:
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
|
options:
|
||||||
|
allowKindChange: true
|
||||||
`)
|
`)
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
options := th.MakeDefaultOptions()
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// kind should become `StatefulSet`
|
|
||||||
m := th.Run(".", options)
|
m := th.Run(".", options)
|
||||||
th.AssertActualEqualsExpected(m, `
|
th.AssertActualEqualsExpected(m, `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: StatefulSet
|
||||||
metadata:
|
metadata:
|
||||||
name: old-name
|
name: old-name
|
||||||
spec:
|
spec:
|
||||||
@@ -168,7 +153,6 @@ spec:
|
|||||||
|
|
||||||
func TestChangeNameAndKind(t *testing.T) {
|
func TestChangeNameAndKind(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
|
||||||
th.WriteF("deployment.yaml", `
|
th.WriteF("deployment.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -181,35 +165,30 @@ spec:
|
|||||||
- name: nginx
|
- name: nginx
|
||||||
image: nginx
|
image: nginx
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteF("patch.yaml", `
|
th.WriteF("patch.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: StatefulSet
|
kind: StatefulSet
|
||||||
metadata:
|
metadata:
|
||||||
name: new-name
|
name: new-name
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteK(".", `
|
th.WriteK(".", `
|
||||||
resources:
|
resources:
|
||||||
- deployment.yaml
|
- deployment.yaml
|
||||||
|
|
||||||
patches:
|
patches:
|
||||||
- path: patch.yaml
|
- path: patch.yaml
|
||||||
target:
|
target:
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
|
options:
|
||||||
|
allowNameChange: true
|
||||||
|
allowKindChange: true
|
||||||
`)
|
`)
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
options := th.MakeDefaultOptions()
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// kind should become `StatefulSet`
|
|
||||||
// name should become `new-name`
|
|
||||||
m := th.Run(".", options)
|
m := th.Run(".", options)
|
||||||
th.AssertActualEqualsExpected(m, `
|
th.AssertActualEqualsExpected(m, `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: StatefulSet
|
||||||
metadata:
|
metadata:
|
||||||
name: old-name
|
name: new-name
|
||||||
spec:
|
spec:
|
||||||
template:
|
template:
|
||||||
spec:
|
spec:
|
||||||
@@ -219,12 +198,10 @@ spec:
|
|||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://github.com/kubernetes-sigs/kustomize/issues/3280
|
|
||||||
// Should be able to refer to a resource with either its
|
// Should be able to refer to a resource with either its
|
||||||
// original GVKN or its current one
|
// original GVKN or its current one
|
||||||
func TestPatchOriginalName(t *testing.T) {
|
func TestPatchOriginalName(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
|
||||||
th.WriteF("base/deployment.yaml", `
|
th.WriteF("base/deployment.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -246,13 +223,13 @@ metadata:
|
|||||||
th.WriteK("base", `
|
th.WriteK("base", `
|
||||||
resources:
|
resources:
|
||||||
- deployment.yaml
|
- deployment.yaml
|
||||||
|
|
||||||
patches:
|
patches:
|
||||||
- path: patch.yaml
|
- path: patch.yaml
|
||||||
target:
|
target:
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
|
options:
|
||||||
|
allowNameChange: true
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteK("overlay", `
|
th.WriteK("overlay", `
|
||||||
resources:
|
resources:
|
||||||
- ../base
|
- ../base
|
||||||
@@ -267,17 +244,13 @@ metadata:
|
|||||||
spec:
|
spec:
|
||||||
replicas: 999
|
replicas: 999
|
||||||
`)
|
`)
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
options := th.MakeDefaultOptions()
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// name should become `new-name`
|
|
||||||
m := th.Run("overlay", options)
|
m := th.Run("overlay", options)
|
||||||
th.AssertActualEqualsExpected(m, `
|
th.AssertActualEqualsExpected(m, `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: old-name
|
name: new-name
|
||||||
spec:
|
spec:
|
||||||
replicas: 999
|
replicas: 999
|
||||||
template:
|
template:
|
||||||
@@ -290,7 +263,6 @@ spec:
|
|||||||
|
|
||||||
func TestPatchNewName(t *testing.T) {
|
func TestPatchNewName(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
|
||||||
th.WriteF("base/deployment.yaml", `
|
th.WriteF("base/deployment.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -312,13 +284,13 @@ metadata:
|
|||||||
th.WriteK("base", `
|
th.WriteK("base", `
|
||||||
resources:
|
resources:
|
||||||
- deployment.yaml
|
- deployment.yaml
|
||||||
|
|
||||||
patches:
|
patches:
|
||||||
- path: patch.yaml
|
- path: patch.yaml
|
||||||
target:
|
target:
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
|
options:
|
||||||
|
allowNameChange: true
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteK("overlay", `
|
th.WriteK("overlay", `
|
||||||
resources:
|
resources:
|
||||||
- ../base
|
- ../base
|
||||||
@@ -333,18 +305,25 @@ metadata:
|
|||||||
spec:
|
spec:
|
||||||
replicas: 999
|
replicas: 999
|
||||||
`)
|
`)
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
options := th.MakeDefaultOptions()
|
||||||
options.AllowResourceIdChanges = true
|
m := th.Run("overlay", options)
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
// depPatch cannot find target with the name `new-name`
|
apiVersion: v1
|
||||||
// because base/patch.yaml can't yet edit the name
|
kind: Deployment
|
||||||
assert.Error(t, th.RunWithErr("overlay", options))
|
metadata:
|
||||||
|
name: new-name
|
||||||
|
spec:
|
||||||
|
replicas: 999
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx
|
||||||
|
name: nginx
|
||||||
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPatchOriginalNameAndKind(t *testing.T) {
|
func TestPatchOriginalNameAndKind(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
|
||||||
th.WriteF("base/deployment.yaml", `
|
th.WriteF("base/deployment.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -366,13 +345,14 @@ metadata:
|
|||||||
th.WriteK("base", `
|
th.WriteK("base", `
|
||||||
resources:
|
resources:
|
||||||
- deployment.yaml
|
- deployment.yaml
|
||||||
|
|
||||||
patches:
|
patches:
|
||||||
- path: patch.yaml
|
- path: patch.yaml
|
||||||
target:
|
target:
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
|
options:
|
||||||
|
allowNameChange: true
|
||||||
|
allowKindChange: true
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteK("overlay", `
|
th.WriteK("overlay", `
|
||||||
resources:
|
resources:
|
||||||
- ../base
|
- ../base
|
||||||
@@ -387,18 +367,13 @@ metadata:
|
|||||||
spec:
|
spec:
|
||||||
replicas: 999
|
replicas: 999
|
||||||
`)
|
`)
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
options := th.MakeDefaultOptions()
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// kind should become `StatefulSet`
|
|
||||||
// name should become `new-name`
|
|
||||||
m := th.Run("overlay", options)
|
m := th.Run("overlay", options)
|
||||||
th.AssertActualEqualsExpected(m, `
|
th.AssertActualEqualsExpected(m, `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: StatefulSet
|
||||||
metadata:
|
metadata:
|
||||||
name: old-name
|
name: new-name
|
||||||
spec:
|
spec:
|
||||||
replicas: 999
|
replicas: 999
|
||||||
template:
|
template:
|
||||||
@@ -411,7 +386,6 @@ spec:
|
|||||||
|
|
||||||
func TestPatchNewNameAndKind(t *testing.T) {
|
func TestPatchNewNameAndKind(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
|
||||||
th.WriteF("base/deployment.yaml", `
|
th.WriteF("base/deployment.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -433,13 +407,14 @@ metadata:
|
|||||||
th.WriteK("base", `
|
th.WriteK("base", `
|
||||||
resources:
|
resources:
|
||||||
- deployment.yaml
|
- deployment.yaml
|
||||||
|
|
||||||
patches:
|
patches:
|
||||||
- path: patch.yaml
|
- path: patch.yaml
|
||||||
target:
|
target:
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
|
options:
|
||||||
|
allowNameChange: true
|
||||||
|
allowKindChange: true
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteK("overlay", `
|
th.WriteK("overlay", `
|
||||||
resources:
|
resources:
|
||||||
- ../base
|
- ../base
|
||||||
@@ -454,20 +429,27 @@ metadata:
|
|||||||
spec:
|
spec:
|
||||||
replicas: 999
|
replicas: 999
|
||||||
`)
|
`)
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
options := th.MakeDefaultOptions()
|
||||||
options.AllowResourceIdChanges = true
|
m := th.Run("overlay", options)
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
// depPatch cannot find target with kind `StatefulSet` and name `new-name`
|
apiVersion: v1
|
||||||
// because base/patch.yaml can't yet edit the kind or name
|
kind: StatefulSet
|
||||||
assert.Error(t, th.RunWithErr("overlay", options))
|
metadata:
|
||||||
|
name: new-name
|
||||||
|
spec:
|
||||||
|
replicas: 999
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx
|
||||||
|
name: nginx
|
||||||
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use original name, but new kind
|
// Use original name, but new kind
|
||||||
// Should fail, even after #3280 is done, because this ID is invalid
|
// Should fail, even after #3280 is done, because this ID is invalid
|
||||||
func TestPatchOriginalNameAndNewKind(t *testing.T) {
|
func TestPatchOriginalNameAndNewKind(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
|
||||||
th.WriteF("base/deployment.yaml", `
|
th.WriteF("base/deployment.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -489,13 +471,14 @@ metadata:
|
|||||||
th.WriteK("base", `
|
th.WriteK("base", `
|
||||||
resources:
|
resources:
|
||||||
- deployment.yaml
|
- deployment.yaml
|
||||||
|
|
||||||
patches:
|
patches:
|
||||||
- path: patch.yaml
|
- path: patch.yaml
|
||||||
target:
|
target:
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
|
options:
|
||||||
|
allowNameChange: true
|
||||||
|
allowKindChange: true
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteK("overlay", `
|
th.WriteK("overlay", `
|
||||||
resources:
|
resources:
|
||||||
- ../base
|
- ../base
|
||||||
@@ -510,10 +493,7 @@ metadata:
|
|||||||
spec:
|
spec:
|
||||||
replicas: 999
|
replicas: 999
|
||||||
`)
|
`)
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
options := th.MakeDefaultOptions()
|
||||||
options.AllowResourceIdChanges = true
|
|
||||||
|
|
||||||
// depPatch cannot find target with kind `Deployment` and name `new-name`
|
// depPatch cannot find target with kind `Deployment` and name `new-name`
|
||||||
// because the resource never had this GVKN
|
// because the resource never had this GVKN
|
||||||
assert.Error(t, th.RunWithErr("overlay", options))
|
assert.Error(t, th.RunWithErr("overlay", options))
|
||||||
@@ -552,12 +532,11 @@ spec:
|
|||||||
|
|
||||||
func TestBaseReuseNameAndKindConflict(t *testing.T) {
|
func TestBaseReuseNameAndKindConflict(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteK("shared", `
|
||||||
th.WriteK("/app/shared", `
|
|
||||||
resources:
|
resources:
|
||||||
- deployment.yaml
|
- deployment.yaml
|
||||||
`)
|
`)
|
||||||
th.WriteF("/app/shared/deployment.yaml", `
|
th.WriteF("shared/deployment.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@@ -570,53 +549,139 @@ spec:
|
|||||||
image: nginx
|
image: nginx
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteK("/app/component1/base", `
|
th.WriteK("component1/base", `
|
||||||
resources:
|
resources:
|
||||||
- ../../shared
|
- ../../shared
|
||||||
`)
|
`)
|
||||||
th.WriteK("/app/component1/overlay", `
|
th.WriteK("component1/overlay", `
|
||||||
resources:
|
resources:
|
||||||
- ../base
|
- ../base
|
||||||
|
|
||||||
namePrefix: overlay-
|
namePrefix: overlay-
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteK("/app/component2/base", `
|
th.WriteK("component2/base", `
|
||||||
resources:
|
resources:
|
||||||
- ../../shared
|
- ../../shared
|
||||||
|
|
||||||
patches:
|
patches:
|
||||||
- path: patch.yaml
|
- path: patch.yaml
|
||||||
target:
|
target:
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
name: my-deploy
|
name: my-deploy
|
||||||
|
options:
|
||||||
|
allowNameChange: true
|
||||||
|
allowKindChange: true
|
||||||
`)
|
`)
|
||||||
th.WriteF("/app/component2/base/patch.yaml", `
|
th.WriteF("component2/base/patch.yaml", `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: StatefulSet
|
kind: StatefulSet
|
||||||
metadata:
|
metadata:
|
||||||
name: my-stateful-set
|
name: my-stateful-set
|
||||||
`)
|
`)
|
||||||
th.WriteK("/app/component2/overlay", `
|
th.WriteK("component2/overlay", `
|
||||||
resources:
|
resources:
|
||||||
- ../base
|
- ../base
|
||||||
|
|
||||||
namePrefix: overlay-
|
namePrefix: overlay-
|
||||||
`)
|
`)
|
||||||
|
|
||||||
th.WriteK("/app", `
|
th.WriteK(".", `
|
||||||
resources:
|
resources:
|
||||||
- component1/overlay
|
- component1/overlay
|
||||||
- component2/overlay
|
- component2/overlay
|
||||||
`)
|
`)
|
||||||
|
|
||||||
options := th.MakeDefaultOptions()
|
options := th.MakeDefaultOptions()
|
||||||
options.AllowResourceIdChanges = true
|
m := th.Run(".", options)
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
// Error occurs when app/component2/base tries to load the shared resources
|
apiVersion: v1
|
||||||
// because the kind is not (yet) allowed to change yet
|
kind: Deployment
|
||||||
// so it loads a second Deployment with the name my-deploy
|
metadata:
|
||||||
// instead of a StatefulSet as specified by the patch.
|
name: overlay-my-deploy
|
||||||
// Will be fixed by #3280.
|
spec:
|
||||||
assert.Error(t, th.RunWithErr("overlay", options))
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx
|
||||||
|
name: nginx
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: StatefulSet
|
||||||
|
metadata:
|
||||||
|
name: overlay-my-stateful-set
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx
|
||||||
|
name: nginx
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNameReferenceAfterGvknChange(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteF("configMap.yaml", `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: old-name
|
||||||
|
`)
|
||||||
|
th.WriteF("patch.yaml", `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: new-name
|
||||||
|
`)
|
||||||
|
th.WriteF("deployment.yaml", `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deploy
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- env:
|
||||||
|
- valueFrom:
|
||||||
|
configMapKeyRef:
|
||||||
|
name: old-name
|
||||||
|
key: somekey
|
||||||
|
envFrom:
|
||||||
|
- configMapRef:
|
||||||
|
name: old-name
|
||||||
|
key: somekey
|
||||||
|
`)
|
||||||
|
th.WriteK(".", `
|
||||||
|
resources:
|
||||||
|
- configMap.yaml
|
||||||
|
- deployment.yaml
|
||||||
|
patches:
|
||||||
|
- path: patch.yaml
|
||||||
|
target:
|
||||||
|
kind: ConfigMap
|
||||||
|
options:
|
||||||
|
allowNameChange: true
|
||||||
|
`)
|
||||||
|
m := th.Run(".", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: new-name
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: deploy
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- env:
|
||||||
|
- valueFrom:
|
||||||
|
configMapKeyRef:
|
||||||
|
key: somekey
|
||||||
|
name: new-name
|
||||||
|
envFrom:
|
||||||
|
- configMapRef:
|
||||||
|
key: somekey
|
||||||
|
name: new-name
|
||||||
|
`)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,21 +33,16 @@ type Options struct {
|
|||||||
|
|
||||||
// Options related to kustomize plugins.
|
// Options related to kustomize plugins.
|
||||||
PluginConfig *types.PluginConfig
|
PluginConfig *types.PluginConfig
|
||||||
|
|
||||||
// When true, allow name and kind changing via a patch
|
|
||||||
// When false, patch name/kind don't overwrite target name/kind
|
|
||||||
AllowResourceIdChanges bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeDefaultOptions returns a default instance of Options.
|
// MakeDefaultOptions returns a default instance of Options.
|
||||||
func MakeDefaultOptions() *Options {
|
func MakeDefaultOptions() *Options {
|
||||||
return &Options{
|
return &Options{
|
||||||
DoLegacyResourceSort: false,
|
DoLegacyResourceSort: false,
|
||||||
AddManagedbyLabel: false,
|
AddManagedbyLabel: false,
|
||||||
LoadRestrictions: types.LoadRestrictionsRootOnly,
|
LoadRestrictions: types.LoadRestrictionsRootOnly,
|
||||||
DoPrune: false,
|
DoPrune: false,
|
||||||
PluginConfig: konfig.DisabledPluginConfig(),
|
PluginConfig: konfig.DisabledPluginConfig(),
|
||||||
AllowResourceIdChanges: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -596,6 +596,7 @@ func (m *resWrangler) ApplySmPatch(
|
|||||||
patchCopy := patch.DeepCopy()
|
patchCopy := patch.DeepCopy()
|
||||||
patchCopy.CopyMergeMetaDataFieldsFrom(patch)
|
patchCopy.CopyMergeMetaDataFieldsFrom(patch)
|
||||||
patchCopy.SetGvk(res.GetGvk())
|
patchCopy.SetGvk(res.GetGvk())
|
||||||
|
patchCopy.SetKind(patch.GetKind())
|
||||||
err := res.ApplySmPatch(patchCopy)
|
err := res.ApplySmPatch(patchCopy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Check for an error string from UnmarshalJSON that's indicative
|
// Check for an error string from UnmarshalJSON that's indicative
|
||||||
|
|||||||
@@ -339,6 +339,7 @@ func TestGetMatchingResourcesByAnyId(t *testing.T) {
|
|||||||
"metadata": map[string]interface{}{
|
"metadata": map[string]interface{}{
|
||||||
"name": "new-alice",
|
"name": "new-alice",
|
||||||
"annotations": map[string]interface{}{
|
"annotations": map[string]interface{}{
|
||||||
|
"config.kubernetes.io/previousKinds": "ConfigMap",
|
||||||
"config.kubernetes.io/previousNames": "alice",
|
"config.kubernetes.io/previousNames": "alice",
|
||||||
"config.kubernetes.io/previousNamespaces": "default",
|
"config.kubernetes.io/previousNamespaces": "default",
|
||||||
},
|
},
|
||||||
@@ -351,6 +352,7 @@ func TestGetMatchingResourcesByAnyId(t *testing.T) {
|
|||||||
"metadata": map[string]interface{}{
|
"metadata": map[string]interface{}{
|
||||||
"name": "new-bob",
|
"name": "new-bob",
|
||||||
"annotations": map[string]interface{}{
|
"annotations": map[string]interface{}{
|
||||||
|
"config.kubernetes.io/previousKinds": "ConfigMap,ConfigMap",
|
||||||
"config.kubernetes.io/previousNames": "bob,bob2",
|
"config.kubernetes.io/previousNames": "bob,bob2",
|
||||||
"config.kubernetes.io/previousNamespaces": "default,default",
|
"config.kubernetes.io/previousNamespaces": "default,default",
|
||||||
},
|
},
|
||||||
@@ -364,6 +366,7 @@ func TestGetMatchingResourcesByAnyId(t *testing.T) {
|
|||||||
"name": "new-bob",
|
"name": "new-bob",
|
||||||
"namespace": "new-happy",
|
"namespace": "new-happy",
|
||||||
"annotations": map[string]interface{}{
|
"annotations": map[string]interface{}{
|
||||||
|
"config.kubernetes.io/previousKinds": "ConfigMap",
|
||||||
"config.kubernetes.io/previousNames": "bob",
|
"config.kubernetes.io/previousNames": "bob",
|
||||||
"config.kubernetes.io/previousNamespaces": "happy",
|
"config.kubernetes.io/previousNamespaces": "happy",
|
||||||
},
|
},
|
||||||
@@ -377,6 +380,7 @@ func TestGetMatchingResourcesByAnyId(t *testing.T) {
|
|||||||
"name": "charlie",
|
"name": "charlie",
|
||||||
"namespace": "happy",
|
"namespace": "happy",
|
||||||
"annotations": map[string]interface{}{
|
"annotations": map[string]interface{}{
|
||||||
|
"config.kubernetes.io/previousKinds": "ConfigMap",
|
||||||
"config.kubernetes.io/previousNames": "charlie",
|
"config.kubernetes.io/previousNames": "charlie",
|
||||||
"config.kubernetes.io/previousNamespaces": "default",
|
"config.kubernetes.io/previousNamespaces": "default",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -41,7 +41,8 @@ func (rf *Factory) FromMapWithName(n string, m map[string]interface{}) *Resource
|
|||||||
|
|
||||||
// FromMapWithNamespaceAndName returns a new instance with the given "original" namespace.
|
// FromMapWithNamespaceAndName returns a new instance with the given "original" namespace.
|
||||||
func (rf *Factory) FromMapWithNamespaceAndName(ns string, n string, m map[string]interface{}) *Resource {
|
func (rf *Factory) FromMapWithNamespaceAndName(ns string, n string, m map[string]interface{}) *Resource {
|
||||||
return rf.makeOne(rf.kf.FromMap(m), nil).setPreviousNamespaceAndName(ns, n)
|
r := rf.makeOne(rf.kf.FromMap(m), nil)
|
||||||
|
return r.setPreviousId(ns, n, r.GetKind())
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromMapAndOption returns a new instance of Resource with given options.
|
// FromMapAndOption returns a new instance of Resource with given options.
|
||||||
@@ -157,7 +158,7 @@ func (rf *Factory) SliceFromBytesWithNames(names []string, in []byte) ([]*Resour
|
|||||||
return nil, fmt.Errorf("number of names doesn't match number of resources")
|
return nil, fmt.Errorf("number of names doesn't match number of resources")
|
||||||
}
|
}
|
||||||
for i, res := range result {
|
for i, res := range result {
|
||||||
res.setPreviousNamespaceAndName(resid.DefaultNamespace, names[i])
|
res.setPreviousId(resid.DefaultNamespace, names[i], res.GetKind())
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,17 +33,26 @@ type Resource struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
buildAnnotationPreviousKinds = konfig.ConfigAnnoDomain + "/previousKinds"
|
||||||
buildAnnotationPreviousNames = konfig.ConfigAnnoDomain + "/previousNames"
|
buildAnnotationPreviousNames = konfig.ConfigAnnoDomain + "/previousNames"
|
||||||
buildAnnotationPrefixes = konfig.ConfigAnnoDomain + "/prefixes"
|
buildAnnotationPrefixes = konfig.ConfigAnnoDomain + "/prefixes"
|
||||||
buildAnnotationSuffixes = konfig.ConfigAnnoDomain + "/suffixes"
|
buildAnnotationSuffixes = konfig.ConfigAnnoDomain + "/suffixes"
|
||||||
buildAnnotationPreviousNamespaces = konfig.ConfigAnnoDomain + "/previousNamespaces"
|
buildAnnotationPreviousNamespaces = konfig.ConfigAnnoDomain + "/previousNamespaces"
|
||||||
|
|
||||||
|
// the following are only for patches, to specify whether they can change names
|
||||||
|
// and kinds of their targets
|
||||||
|
buildAnnotationAllowNameChange = konfig.ConfigAnnoDomain + "/allowNameChange"
|
||||||
|
buildAnnotationAllowKindChange = konfig.ConfigAnnoDomain + "/allowKindChange"
|
||||||
)
|
)
|
||||||
|
|
||||||
var buildAnnotations = []string{
|
var buildAnnotations = []string{
|
||||||
|
buildAnnotationPreviousKinds,
|
||||||
buildAnnotationPreviousNames,
|
buildAnnotationPreviousNames,
|
||||||
buildAnnotationPrefixes,
|
buildAnnotationPrefixes,
|
||||||
buildAnnotationSuffixes,
|
buildAnnotationSuffixes,
|
||||||
buildAnnotationPreviousNamespaces,
|
buildAnnotationPreviousNamespaces,
|
||||||
|
buildAnnotationAllowNameChange,
|
||||||
|
buildAnnotationAllowKindChange,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) ResetPrimaryData(incoming *Resource) {
|
func (r *Resource) ResetPrimaryData(incoming *Resource) {
|
||||||
@@ -157,6 +166,12 @@ func (r *Resource) SetNamespace(n string) {
|
|||||||
r.kunStr.SetNamespace(n)
|
r.kunStr.SetNamespace(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Resource) SetKind(k string) {
|
||||||
|
gvk := r.GetGvk()
|
||||||
|
gvk.Kind = k
|
||||||
|
r.SetGvk(gvk)
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Resource) UnmarshalJSON(s []byte) error {
|
func (r *Resource) UnmarshalJSON(s []byte) error {
|
||||||
return r.kunStr.UnmarshalJSON(s)
|
return r.kunStr.UnmarshalJSON(s)
|
||||||
}
|
}
|
||||||
@@ -351,12 +366,41 @@ func (r *Resource) RemoveBuildAnnotations() {
|
|||||||
r.SetAnnotations(annotations)
|
r.SetAnnotations(annotations)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) setPreviousNamespaceAndName(ns string, n string) *Resource {
|
func (r *Resource) setPreviousId(ns string, n string, k string) *Resource {
|
||||||
r.appendCsvAnnotation(buildAnnotationPreviousNames, n)
|
r.appendCsvAnnotation(buildAnnotationPreviousNames, n)
|
||||||
r.appendCsvAnnotation(buildAnnotationPreviousNamespaces, ns)
|
r.appendCsvAnnotation(buildAnnotationPreviousNamespaces, ns)
|
||||||
|
r.appendCsvAnnotation(buildAnnotationPreviousKinds, k)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Resource) SetAllowNameChange(value string) {
|
||||||
|
annotations := r.GetAnnotations()
|
||||||
|
annotations[buildAnnotationAllowNameChange] = value
|
||||||
|
r.SetAnnotations(annotations)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resource) NameChangeAllowed() bool {
|
||||||
|
annotations := r.GetAnnotations()
|
||||||
|
if allowed, set := annotations[buildAnnotationAllowNameChange]; set && allowed == "true" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resource) SetAllowKindChange(value string) {
|
||||||
|
annotations := r.GetAnnotations()
|
||||||
|
annotations[buildAnnotationAllowKindChange] = value
|
||||||
|
r.SetAnnotations(annotations)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resource) KindChangeAllowed() bool {
|
||||||
|
annotations := r.GetAnnotations()
|
||||||
|
if allowed, set := annotations[buildAnnotationAllowKindChange]; set && allowed == "true" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// String returns resource as JSON.
|
// String returns resource as JSON.
|
||||||
func (r *Resource) String() string {
|
func (r *Resource) String() string {
|
||||||
bs, err := r.MarshalJSON()
|
bs, err := r.MarshalJSON()
|
||||||
@@ -430,14 +474,19 @@ func (r *Resource) PrevIds() []resid.ResId {
|
|||||||
// pairs on one annotation so there is no chance of error
|
// pairs on one annotation so there is no chance of error
|
||||||
names := r.getCsvAnnotation(buildAnnotationPreviousNames)
|
names := r.getCsvAnnotation(buildAnnotationPreviousNames)
|
||||||
ns := r.getCsvAnnotation(buildAnnotationPreviousNamespaces)
|
ns := r.getCsvAnnotation(buildAnnotationPreviousNamespaces)
|
||||||
if len(names) != len(ns) {
|
kinds := r.getCsvAnnotation(buildAnnotationPreviousKinds)
|
||||||
|
if len(names) != len(ns) || len(names) != len(kinds) {
|
||||||
panic(errors.New(
|
panic(errors.New(
|
||||||
"number of previous names not equal to " +
|
"number of previous names, " +
|
||||||
"number of previous namespaces"))
|
"number of previous namespaces, " +
|
||||||
|
"number of previous kinds not equal"))
|
||||||
}
|
}
|
||||||
for i := range names {
|
for i := range names {
|
||||||
|
k := kinds[i]
|
||||||
|
gvk := r.GetGvk()
|
||||||
|
gvk.Kind = k
|
||||||
ids = append(ids, resid.NewResIdWithNamespace(
|
ids = append(ids, resid.NewResIdWithNamespace(
|
||||||
r.GetGvk(), names[i], ns[i]))
|
gvk, names[i], ns[i]))
|
||||||
}
|
}
|
||||||
return ids
|
return ids
|
||||||
}
|
}
|
||||||
@@ -445,7 +494,7 @@ func (r *Resource) PrevIds() []resid.ResId {
|
|||||||
// StorePreviousId stores the resource's current ID via build annotations.
|
// StorePreviousId stores the resource's current ID via build annotations.
|
||||||
func (r *Resource) StorePreviousId() {
|
func (r *Resource) StorePreviousId() {
|
||||||
id := r.CurId()
|
id := r.CurId()
|
||||||
r.setPreviousNamespaceAndName(id.EffectiveNamespace(), id.Name)
|
r.setPreviousId(id.EffectiveNamespace(), id.Name, id.Kind)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CurId returns a ResId for the resource using the
|
// CurId returns a ResId for the resource using the
|
||||||
@@ -482,7 +531,10 @@ func (r *Resource) ApplySmPatch(patch *Resource) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
n, ns := r.GetName(), r.GetNamespace()
|
n, ns, k := r.GetName(), r.GetNamespace(), r.GetKind()
|
||||||
|
if patch.NameChangeAllowed() || patch.KindChangeAllowed() {
|
||||||
|
r.StorePreviousId()
|
||||||
|
}
|
||||||
err = r.ApplyFilter(patchstrategicmerge.Filter{
|
err = r.ApplyFilter(patchstrategicmerge.Filter{
|
||||||
Patch: node,
|
Patch: node,
|
||||||
})
|
})
|
||||||
@@ -493,11 +545,17 @@ func (r *Resource) ApplySmPatch(patch *Resource) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !empty {
|
if empty {
|
||||||
r.SetName(n)
|
return nil
|
||||||
r.SetNamespace(ns)
|
|
||||||
}
|
}
|
||||||
return err
|
if !patch.KindChangeAllowed() {
|
||||||
|
r.SetKind(k)
|
||||||
|
}
|
||||||
|
if !patch.NameChangeAllowed() {
|
||||||
|
r.SetName(n)
|
||||||
|
}
|
||||||
|
r.SetNamespace(ns)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) ApplyFilter(f kio.Filter) error {
|
func (r *Resource) ApplyFilter(f kio.Filter) error {
|
||||||
|
|||||||
@@ -714,6 +714,7 @@ metadata:
|
|||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
|
config.kubernetes.io/previousKinds: Secret
|
||||||
config.kubernetes.io/previousNames: oldName
|
config.kubernetes.io/previousNames: oldName
|
||||||
config.kubernetes.io/previousNamespaces: default
|
config.kubernetes.io/previousNamespaces: default
|
||||||
name: newName
|
name: newName
|
||||||
@@ -725,6 +726,7 @@ metadata:
|
|||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
|
config.kubernetes.io/previousKinds: Secret
|
||||||
config.kubernetes.io/previousNames: oldName
|
config.kubernetes.io/previousNames: oldName
|
||||||
config.kubernetes.io/previousNamespaces: default
|
config.kubernetes.io/previousNamespaces: default
|
||||||
name: oldName2
|
name: oldName2
|
||||||
@@ -735,6 +737,7 @@ metadata:
|
|||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
|
config.kubernetes.io/previousKinds: Secret,Secret
|
||||||
config.kubernetes.io/previousNames: oldName,oldName2
|
config.kubernetes.io/previousNames: oldName,oldName2
|
||||||
config.kubernetes.io/previousNamespaces: default,default
|
config.kubernetes.io/previousNamespaces: default,default
|
||||||
name: newName
|
name: newName
|
||||||
@@ -746,6 +749,7 @@ metadata:
|
|||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
|
config.kubernetes.io/previousKinds: Secret
|
||||||
config.kubernetes.io/previousNames: oldName
|
config.kubernetes.io/previousNames: oldName
|
||||||
config.kubernetes.io/previousNamespaces: default
|
config.kubernetes.io/previousNamespaces: default
|
||||||
name: oldName2
|
name: oldName2
|
||||||
@@ -757,6 +761,7 @@ metadata:
|
|||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
|
config.kubernetes.io/previousKinds: Secret,Secret
|
||||||
config.kubernetes.io/previousNames: oldName,oldName2
|
config.kubernetes.io/previousNames: oldName,oldName2
|
||||||
config.kubernetes.io/previousNamespaces: default,oldNamespace
|
config.kubernetes.io/previousNamespaces: default,oldNamespace
|
||||||
name: newName
|
name: newName
|
||||||
@@ -806,6 +811,7 @@ metadata:
|
|||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
|
config.kubernetes.io/previousKinds: Secret
|
||||||
config.kubernetes.io/previousNames: oldName
|
config.kubernetes.io/previousNames: oldName
|
||||||
config.kubernetes.io/previousNamespaces: default
|
config.kubernetes.io/previousNamespaces: default
|
||||||
name: newName
|
name: newName
|
||||||
@@ -824,6 +830,7 @@ metadata:
|
|||||||
kind: Secret
|
kind: Secret
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
|
config.kubernetes.io/previousKinds: Secret,Secret
|
||||||
config.kubernetes.io/previousNames: oldName,oldName2
|
config.kubernetes.io/previousNames: oldName,oldName2
|
||||||
config.kubernetes.io/previousNamespaces: default,oldNamespace
|
config.kubernetes.io/previousNamespaces: default,oldNamespace
|
||||||
name: newName
|
name: newName
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
package types
|
package types
|
||||||
|
|
||||||
|
import "reflect"
|
||||||
|
|
||||||
// Patch represent either a Strategic Merge Patch or a JSON patch
|
// Patch represent either a Strategic Merge Patch or a JSON patch
|
||||||
// and its targets.
|
// and its targets.
|
||||||
// The content of the patch can either be from a file
|
// The content of the patch can either be from a file
|
||||||
@@ -16,6 +18,9 @@ type Patch struct {
|
|||||||
|
|
||||||
// Target points to the resources that the patch is applied to
|
// Target points to the resources that the patch is applied to
|
||||||
Target *Selector `json:"target,omitempty" yaml:"target,omitempty"`
|
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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equals return true if p equals o.
|
// Equals return true if p equals o.
|
||||||
@@ -24,5 +29,6 @@ func (p *Patch) Equals(o Patch) bool {
|
|||||||
(p.Target != nil && o.Target != nil && *p.Target == *o.Target)
|
(p.Target != nil && o.Target != nil && *p.Target == *o.Target)
|
||||||
return p.Path == o.Path &&
|
return p.Path == o.Path &&
|
||||||
p.Patch == o.Patch &&
|
p.Patch == o.Patch &&
|
||||||
targetEqual
|
targetEqual &&
|
||||||
|
reflect.DeepEqual(p.Options, o.Options)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,9 +22,8 @@ var theArgs struct {
|
|||||||
var theFlags struct {
|
var theFlags struct {
|
||||||
outputPath string
|
outputPath string
|
||||||
enable struct {
|
enable struct {
|
||||||
resourceIdChanges bool
|
plugins bool
|
||||||
plugins bool
|
managedByLabel bool
|
||||||
managedByLabel bool
|
|
||||||
}
|
}
|
||||||
loadRestrictor string
|
loadRestrictor string
|
||||||
reorderOutput string
|
reorderOutput string
|
||||||
@@ -104,7 +103,6 @@ func NewCmdBuild(
|
|||||||
AddFlagEnablePlugins(cmd.Flags())
|
AddFlagEnablePlugins(cmd.Flags())
|
||||||
AddFlagReorderOutput(cmd.Flags())
|
AddFlagReorderOutput(cmd.Flags())
|
||||||
AddFlagEnableManagedbyLabel(cmd.Flags())
|
AddFlagEnableManagedbyLabel(cmd.Flags())
|
||||||
AddFlagAllowResourceIdChanges(cmd.Flags())
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,6 +138,5 @@ func HonorKustomizeFlags(kOpts *krusty.Options) *krusty.Options {
|
|||||||
kOpts.PluginConfig = c
|
kOpts.PluginConfig = c
|
||||||
}
|
}
|
||||||
kOpts.AddManagedbyLabel = isManagedByLabelEnabled()
|
kOpts.AddManagedbyLabel = isManagedByLabelEnabled()
|
||||||
kOpts.AllowResourceIdChanges = theFlags.enable.resourceIdChanges
|
|
||||||
return kOpts
|
return kOpts
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package build
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/spf13/pflag"
|
|
||||||
)
|
|
||||||
|
|
||||||
func AddFlagAllowResourceIdChanges(set *pflag.FlagSet) {
|
|
||||||
set.BoolVar(
|
|
||||||
&theFlags.enable.resourceIdChanges,
|
|
||||||
"allow-id-changes",
|
|
||||||
false,
|
|
||||||
`enable changes to a resourceId`)
|
|
||||||
}
|
|
||||||
@@ -85,7 +85,7 @@ func (o *removePatchOptions) RunRemovePatch(fSys filesys.FileSystem) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(patches) == len(m.Patches) {
|
if len(patches) == len(m.Patches) {
|
||||||
log.Printf("patch %s doesn't exist in kustomization file", o.Patch)
|
log.Printf("patch %s doesn't exist in kustomization file", o.Patch.Patch)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
m.Patches = patches
|
m.Patches = patches
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ type plugin struct {
|
|||||||
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
||||||
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
|
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
|
||||||
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
|
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
|
||||||
|
Options map[string]bool `json:"options,omitempty" yaml:"options,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//noinspection GoUnusedGlobalVariable
|
//noinspection GoUnusedGlobalVariable
|
||||||
@@ -64,6 +65,12 @@ func (p *plugin) Config(
|
|||||||
}
|
}
|
||||||
if errSM == nil {
|
if errSM == nil {
|
||||||
p.loadedPatch = patchSM
|
p.loadedPatch = patchSM
|
||||||
|
if p.Options["allowNameChange"] {
|
||||||
|
p.loadedPatch.SetAllowNameChange("true")
|
||||||
|
}
|
||||||
|
if p.Options["allowKindChange"] {
|
||||||
|
p.loadedPatch.SetAllowKindChange("true")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
p.decodedPatch = patchJson
|
p.decodedPatch = patchJson
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -296,7 +296,6 @@ spec:
|
|||||||
- proxy
|
- proxy
|
||||||
- sidecar
|
- sidecar
|
||||||
`)
|
`)
|
||||||
|
|
||||||
rm := th.LoadAndRunTransformer(`
|
rm := th.LoadAndRunTransformer(`
|
||||||
apiVersion: builtin
|
apiVersion: builtin
|
||||||
kind: PatchTransformer
|
kind: PatchTransformer
|
||||||
@@ -364,6 +363,8 @@ spec:
|
|||||||
- sidecar
|
- sidecar
|
||||||
image: docker.io/istio/proxyv2
|
image: docker.io/istio/proxyv2
|
||||||
name: istio-proxy
|
name: istio-proxy
|
||||||
|
- image: nginx
|
||||||
|
name: nginx
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ kind: Service
|
|||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/prefixes: baked-
|
config.kubernetes.io/prefixes: baked-
|
||||||
|
config.kubernetes.io/previousKinds: Service
|
||||||
config.kubernetes.io/previousNames: apple
|
config.kubernetes.io/previousNames: apple
|
||||||
config.kubernetes.io/previousNamespaces: default
|
config.kubernetes.io/previousNamespaces: default
|
||||||
config.kubernetes.io/suffixes: -pie
|
config.kubernetes.io/suffixes: -pie
|
||||||
@@ -87,6 +88,7 @@ kind: ConfigMap
|
|||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/prefixes: baked-
|
config.kubernetes.io/prefixes: baked-
|
||||||
|
config.kubernetes.io/previousKinds: ConfigMap
|
||||||
config.kubernetes.io/previousNames: cm
|
config.kubernetes.io/previousNames: cm
|
||||||
config.kubernetes.io/previousNamespaces: default
|
config.kubernetes.io/previousNamespaces: default
|
||||||
config.kubernetes.io/suffixes: -pie
|
config.kubernetes.io/suffixes: -pie
|
||||||
@@ -138,6 +140,7 @@ kind: Deployment
|
|||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/prefixes: test-
|
config.kubernetes.io/prefixes: test-
|
||||||
|
config.kubernetes.io/previousKinds: Deployment
|
||||||
config.kubernetes.io/previousNames: deployment
|
config.kubernetes.io/previousNames: deployment
|
||||||
config.kubernetes.io/previousNamespaces: default
|
config.kubernetes.io/previousNamespaces: default
|
||||||
name: test-deployment
|
name: test-deployment
|
||||||
|
|||||||
Reference in New Issue
Block a user