mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
update edit fix to convert the old patches to patchesStrategicMerge
This commit is contained in:
@@ -61,6 +61,7 @@ func determineFieldOrder() []string {
|
|||||||
"CommonAnnotations",
|
"CommonAnnotations",
|
||||||
"PatchesStrategicMerge",
|
"PatchesStrategicMerge",
|
||||||
"PatchesJson6902",
|
"PatchesJson6902",
|
||||||
|
"Patches",
|
||||||
"ConfigMapGenerator",
|
"ConfigMapGenerator",
|
||||||
"SecretGenerator",
|
"SecretGenerator",
|
||||||
"GeneratorOptions",
|
"GeneratorOptions",
|
||||||
@@ -73,9 +74,7 @@ func determineFieldOrder() []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add deprecated fields here.
|
// Add deprecated fields here.
|
||||||
deprecated := map[string]bool{
|
deprecated := map[string]bool{}
|
||||||
"Patches": true,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Account for the inlined TypeMeta fields.
|
// Account for the inlined TypeMeta fields.
|
||||||
var result []string
|
var result []string
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ func TestFieldOrder(t *testing.T) {
|
|||||||
"CommonAnnotations",
|
"CommonAnnotations",
|
||||||
"PatchesStrategicMerge",
|
"PatchesStrategicMerge",
|
||||||
"PatchesJson6902",
|
"PatchesJson6902",
|
||||||
|
"Patches",
|
||||||
"ConfigMapGenerator",
|
"ConfigMapGenerator",
|
||||||
"SecretGenerator",
|
"SecretGenerator",
|
||||||
"GeneratorOptions",
|
"GeneratorOptions",
|
||||||
@@ -264,3 +265,85 @@ generatorOptions:
|
|||||||
string(expected), string(bytes))
|
string(expected), string(bytes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFixPatchesField(t *testing.T) {
|
||||||
|
kustomizationContentWithComments := []byte(`
|
||||||
|
patches:
|
||||||
|
- patch1.yaml
|
||||||
|
- patch2.yaml
|
||||||
|
`)
|
||||||
|
|
||||||
|
expected := []byte(`
|
||||||
|
patchesStrategicMerge:
|
||||||
|
- patch1.yaml
|
||||||
|
- patch2.yaml
|
||||||
|
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
|
kind: Kustomization
|
||||||
|
`)
|
||||||
|
fSys := fs.MakeFakeFS()
|
||||||
|
fSys.WriteTestKustomizationWith(kustomizationContentWithComments)
|
||||||
|
mf, err := NewKustomizationFile(fSys)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected Error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
kustomization, err := mf.Read()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected Error: %v", err)
|
||||||
|
}
|
||||||
|
if err = mf.Write(kustomization); err != nil {
|
||||||
|
t.Fatalf("Unexpected Error: %v", err)
|
||||||
|
}
|
||||||
|
bytes, _ := fSys.ReadFile(mf.path)
|
||||||
|
|
||||||
|
if string(expected) != string(bytes) {
|
||||||
|
t.Fatalf(
|
||||||
|
"expected =\n%s\n\nactual =\n%s\n",
|
||||||
|
string(expected), string(bytes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFixPatchesFieldForExtendedPatch(t *testing.T) {
|
||||||
|
kustomizationContentWithComments := []byte(`
|
||||||
|
patches:
|
||||||
|
- path: patch1.yaml
|
||||||
|
target:
|
||||||
|
kind: Deployment
|
||||||
|
- path: patch2.yaml
|
||||||
|
target:
|
||||||
|
kind: Service
|
||||||
|
`)
|
||||||
|
|
||||||
|
expected := []byte(`
|
||||||
|
patches:
|
||||||
|
- path: patch1.yaml
|
||||||
|
target:
|
||||||
|
kind: Deployment
|
||||||
|
- path: patch2.yaml
|
||||||
|
target:
|
||||||
|
kind: Service
|
||||||
|
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
|
kind: Kustomization
|
||||||
|
`)
|
||||||
|
fSys := fs.MakeFakeFS()
|
||||||
|
fSys.WriteTestKustomizationWith(kustomizationContentWithComments)
|
||||||
|
mf, err := NewKustomizationFile(fSys)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected Error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
kustomization, err := mf.Read()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected Error: %v", err)
|
||||||
|
}
|
||||||
|
if err = mf.Write(kustomization); err != nil {
|
||||||
|
t.Fatalf("Unexpected Error: %v", err)
|
||||||
|
}
|
||||||
|
bytes, _ := fSys.ReadFile(mf.path)
|
||||||
|
|
||||||
|
if string(expected) != string(bytes) {
|
||||||
|
t.Fatalf(
|
||||||
|
"expected =\n%s\n\nactual =\n%s\n",
|
||||||
|
string(expected), string(bytes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -923,6 +923,107 @@ spec:
|
|||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExtendedPatchWithoutTarget(t *testing.T) {
|
||||||
|
th := kusttest_test.NewKustTestHarness(t, "/app/base")
|
||||||
|
makeCommonFileForExtendedPatchTest(th)
|
||||||
|
th.WriteK("/app/base", `
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
- service.yaml
|
||||||
|
patches:
|
||||||
|
- path: patch.yaml
|
||||||
|
`)
|
||||||
|
th.WriteF("/app/base/patch.yaml", `
|
||||||
|
apiVersion: apps/v1beta2
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: busybox
|
||||||
|
annotations:
|
||||||
|
new-key: new-value
|
||||||
|
`)
|
||||||
|
m, err := th.MakeKustTarget().MakeCustomizedResMap()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Err: %v", err)
|
||||||
|
}
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: apps/v1beta2
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
name: nginx
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx
|
||||||
|
name: nginx
|
||||||
|
volumeMounts:
|
||||||
|
- mountPath: /tmp/ps
|
||||||
|
name: nginx-persistent-storage
|
||||||
|
volumes:
|
||||||
|
- emptyDir: {}
|
||||||
|
name: nginx-persistent-storage
|
||||||
|
- configMap:
|
||||||
|
name: configmap-in-base
|
||||||
|
name: configmap-in-base
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1beta2
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
annotations:
|
||||||
|
new-key: new-value
|
||||||
|
labels:
|
||||||
|
app: busybox
|
||||||
|
name: busybox
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: busybox
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: busybox
|
||||||
|
name: busybox
|
||||||
|
volumeMounts:
|
||||||
|
- mountPath: /tmp/ps
|
||||||
|
name: busybox-persistent-storage
|
||||||
|
volumes:
|
||||||
|
- emptyDir: {}
|
||||||
|
name: busybox-persistent-storage
|
||||||
|
- configMap:
|
||||||
|
name: configmap-in-base
|
||||||
|
name: configmap-in-base
|
||||||
|
---
|
||||||
|
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
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
func TestExtendedPatchNoMatchMultiplePatch(t *testing.T) {
|
func TestExtendedPatchNoMatchMultiplePatch(t *testing.T) {
|
||||||
th := kusttest_test.NewKustTestHarness(t, "/app/base")
|
th := kusttest_test.NewKustTestHarness(t, "/app/base")
|
||||||
makeCommonFileForExtendedPatchTest(th)
|
makeCommonFileForExtendedPatchTest(th)
|
||||||
|
|||||||
@@ -200,7 +200,7 @@ func (kt *KustTarget) configureBuiltinPatchTransformer(
|
|||||||
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
|
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
|
||||||
}
|
}
|
||||||
for _, patch := range kt.kustomization.Patches {
|
for _, patch := range kt.kustomization.Patches {
|
||||||
c.Target = &patch.Target
|
c.Target = patch.Target
|
||||||
c.Patch = patch.Patch
|
c.Patch = patch.Patch
|
||||||
c.Path = patch.Path
|
c.Path = patch.Path
|
||||||
p := builtin.NewPatchTransformerPlugin()
|
p := builtin.NewPatchTransformerPlugin()
|
||||||
|
|||||||
55
pkg/types/fix.go
Normal file
55
pkg/types/fix.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
// Package types holds struct definitions that should find a better home.
|
||||||
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"sigs.k8s.io/yaml"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FixKustomizationPreUnmarshalling modies the raw data
|
||||||
|
// before marshalling - e.g. changes old field names to
|
||||||
|
// new field names.
|
||||||
|
func FixKustomizationPreUnmarshalling(data []byte) []byte {
|
||||||
|
deprecateFieldsMap := map[string]string{
|
||||||
|
"imageTags:": "images:",
|
||||||
|
}
|
||||||
|
for oldname, newname := range deprecateFieldsMap {
|
||||||
|
pattern := regexp.MustCompile(oldname)
|
||||||
|
data = pattern.ReplaceAll(data, []byte(newname))
|
||||||
|
}
|
||||||
|
|
||||||
|
if useLegacyPatch(data) {
|
||||||
|
pattern := regexp.MustCompile("patches:")
|
||||||
|
data = pattern.ReplaceAll(data, []byte("patchesStrategicMerge:"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
func useLegacyPatch(data []byte) bool {
|
||||||
|
found := false
|
||||||
|
|
||||||
|
var object map[string]interface{}
|
||||||
|
err := yaml.Unmarshal(data, &object)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("invalid content from %s\n", string(data))
|
||||||
|
}
|
||||||
|
if rawPatches, ok := object["patches"]; ok {
|
||||||
|
patches, ok := rawPatches.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
log.Fatalf("invalid patches from %v\n", rawPatches)
|
||||||
|
}
|
||||||
|
for _, p := range patches {
|
||||||
|
_, ok := p.(string)
|
||||||
|
if ok {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return found
|
||||||
|
}
|
||||||
@@ -5,8 +5,6 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/gvk"
|
"sigs.k8s.io/kustomize/v3/pkg/gvk"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/image"
|
"sigs.k8s.io/kustomize/v3/pkg/image"
|
||||||
)
|
)
|
||||||
@@ -195,20 +193,6 @@ func (k *Kustomization) EnforceFields() []string {
|
|||||||
return errs
|
return errs
|
||||||
}
|
}
|
||||||
|
|
||||||
// FixKustomizationPreUnmarshalling modies the raw data
|
|
||||||
// before marshalling - e.g. changes old field names to
|
|
||||||
// new field names.
|
|
||||||
func FixKustomizationPreUnmarshalling(data []byte) []byte {
|
|
||||||
deprecateFieldsMap := map[string]string{
|
|
||||||
"imageTags:": "images:",
|
|
||||||
}
|
|
||||||
for oldname, newname := range deprecateFieldsMap {
|
|
||||||
pattern := regexp.MustCompile(oldname)
|
|
||||||
data = pattern.ReplaceAll(data, []byte(newname))
|
|
||||||
}
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
// GeneratorArgs contains arguments common to generators.
|
// GeneratorArgs contains arguments common to generators.
|
||||||
type GeneratorArgs struct {
|
type GeneratorArgs struct {
|
||||||
// Namespace for the configmap, optional
|
// Namespace for the configmap, optional
|
||||||
@@ -385,7 +369,7 @@ type Patch struct {
|
|||||||
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
|
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
|
||||||
|
|
||||||
// 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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Selector specifies a set of resources.
|
// Selector specifies a set of resources.
|
||||||
|
|||||||
@@ -87,6 +87,7 @@ func (p *PatchTransformerPlugin) Transform(m resmap.ResMap) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Target == nil {
|
if p.Target == nil {
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ func (p *plugin) Transform(m resmap.ResMap) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Target == nil {
|
if p.Target == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user