diff --git a/api/types/fix.go b/api/types/fix.go index 71548bea5..de70467ed 100644 --- a/api/types/fix.go +++ b/api/types/fix.go @@ -9,14 +9,14 @@ import ( "sigs.k8s.io/yaml" ) -// FixKustomizationPreUnmarshalling modies the raw data +// FixKustomizationPreUnmarshalling modifies the raw data // before marshalling - e.g. changes old field names to // new field names. func FixKustomizationPreUnmarshalling(data []byte) ([]byte, error) { - deprecateFieldsMap := map[string]string{ + deprecatedFieldsMap := map[string]string{ "imageTags:": "images:", } - for oldname, newname := range deprecateFieldsMap { + for oldname, newname := range deprecatedFieldsMap { pattern := regexp.MustCompile(oldname) data = pattern.ReplaceAll(data, []byte(newname)) } diff --git a/api/types/kustomization.go b/api/types/kustomization.go index 8cc7299b7..c32832b2c 100644 --- a/api/types/kustomization.go +++ b/api/types/kustomization.go @@ -144,6 +144,7 @@ type Kustomization struct { // moving content of deprecated fields to newer // fields. func (k *Kustomization) FixKustomizationPostUnmarshalling() { + if k.Kind == "" { k.Kind = KustomizationKind } @@ -158,6 +159,17 @@ func (k *Kustomization) FixKustomizationPostUnmarshalling() { k.Bases = nil } +// FixKustomizationPreMarshalling fixes things +// that should occur after the kustomization file +// has been processed. +func (k *Kustomization) FixKustomizationPreMarshalling() { + // PatchesJson6902 should be under the Patches field. + for _, patch := range k.PatchesJson6902 { + k.Patches = append(k.Patches, patch.ToPatch()) + } + k.PatchesJson6902 = nil +} + func (k *Kustomization) EnforceFields() []string { var errs []string if k.Kind != "" && k.Kind != KustomizationKind && k.Kind != ComponentKind { diff --git a/api/types/patchjson6902.go b/api/types/patchjson6902.go index 1a189aa6d..4c90ce201 100644 --- a/api/types/patchjson6902.go +++ b/api/types/patchjson6902.go @@ -19,3 +19,9 @@ type PatchJson6902 struct { // inline patch string Patch string `json:"patch,omitempty" yaml:"patch,omitempty"` } + +// ToPatch converts a PatchJson6902 to its superset Patch. +func (patch *PatchJson6902) ToPatch() Patch { + selector := patch.Target.ToSelector() + return Patch{Path: patch.Path, Patch: patch.Patch, Target: &selector} +} diff --git a/api/types/patchtarget.go b/api/types/patchtarget.go index ee8c6a691..85f804574 100644 --- a/api/types/patchtarget.go +++ b/api/types/patchtarget.go @@ -13,3 +13,8 @@ type PatchTarget struct { Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` Name string `json:"name" yaml:"name"` } + +// ToSelector converts a PatchTarget to a Selector. +func (target *PatchTarget) ToSelector() Selector { + return Selector{Name: target.Name, Namespace: target.Namespace, Gvk: target.Gvk} +} diff --git a/kustomize/go.mod b/kustomize/go.mod index 74cc82e42..fc49b66c4 100644 --- a/kustomize/go.mod +++ b/kustomize/go.mod @@ -3,6 +3,7 @@ module sigs.k8s.io/kustomize/kustomize/v3 go 1.14 require ( + github.com/google/go-cmp v0.3.0 github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.0.0 github.com/spf13/pflag v1.0.5 diff --git a/kustomize/go.sum b/kustomize/go.sum index 214137f73..230ea3a57 100644 --- a/kustomize/go.sum +++ b/kustomize/go.sum @@ -239,6 +239,7 @@ github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k= github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI= github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= diff --git a/kustomize/internal/commands/kustfile/kustomizationfile.go b/kustomize/internal/commands/kustfile/kustomizationfile.go index f347e368a..900d748e2 100644 --- a/kustomize/internal/commands/kustfile/kustomizationfile.go +++ b/kustomize/internal/commands/kustfile/kustomizationfile.go @@ -170,6 +170,7 @@ func (mf *kustomizationFile) Write(kustomization *types.Kustomization) error { if kustomization == nil { return errors.New("util: kustomization file arg is nil") } + kustomization.FixKustomizationPreMarshalling() data, err := mf.marshal(kustomization) if err != nil { return err diff --git a/kustomize/internal/commands/kustfile/kustomizationfile_test.go b/kustomize/internal/commands/kustfile/kustomizationfile_test.go index 2f9e9e69b..97bcc7655 100644 --- a/kustomize/internal/commands/kustfile/kustomizationfile_test.go +++ b/kustomize/internal/commands/kustfile/kustomizationfile_test.go @@ -8,6 +8,8 @@ import ( "strings" "testing" + "github.com/google/go-cmp/cmp" + "sigs.k8s.io/kustomize/api/filesys" "sigs.k8s.io/kustomize/api/konfig" "sigs.k8s.io/kustomize/api/types" @@ -158,8 +160,8 @@ patchesStrategicMerge: } bytes, _ := fSys.ReadFile(mf.path) - if !reflect.DeepEqual(kustomizationContentWithComments, bytes) { - t.Fatal("written kustomization with comments is not the same as original one") + if diff := cmp.Diff(kustomizationContentWithComments, bytes); diff != "" { + t.Errorf("Mismatch (-expected, +actual):\n%s", diff) } } @@ -252,10 +254,8 @@ generatorOptions: } bytes, _ := fSys.ReadFile(mf.path) - if string(expected) != string(bytes) { - t.Fatalf( - "expected =\n%s\n\nactual =\n%s\n", - string(expected), string(bytes)) + if diff := cmp.Diff(expected, bytes); diff != "" { + t.Errorf("Mismatch (-expected, +actual):\n%s", diff) } } @@ -290,10 +290,8 @@ kind: Kustomization } bytes, _ := fSys.ReadFile(mf.path) - if string(expected) != string(bytes) { - t.Fatalf( - "expected =\n%s\n\nactual =\n%s\n", - string(expected), string(bytes)) + if diff := cmp.Diff(expected, bytes); diff != "" { + t.Errorf("Mismatch (-expected, +actual):\n%s", diff) } } @@ -335,10 +333,105 @@ kind: Kustomization } bytes, _ := fSys.ReadFile(mf.path) - if string(expected) != string(bytes) { - t.Fatalf( - "expected =\n%s\n\nactual =\n%s\n", - string(expected), string(bytes)) + if diff := cmp.Diff(expected, bytes); diff != "" { + t.Errorf("Mismatch (-expected, +actual):\n%s", diff) + } +} + +func TestRenameAndKeepOutdatedPatchesField(t *testing.T) { + kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +patchesJson6902: +- path: patch1.yaml + target: + kind: Deployment +patches: +- path: patch2.yaml + target: + kind: Deployment +- path: patch3.yaml + target: + kind: Service +`) + + expected := []byte(` +patches: +- path: patch2.yaml + target: + kind: Deployment +- path: patch3.yaml + target: + kind: Service +- path: patch1.yaml + target: + kind: Deployment +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +`) + fSys := filesys.MakeFsInMemory() + testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) + 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 diff := cmp.Diff(expected, bytes); diff != "" { + t.Errorf("Mismatch (-expected, +actual):\n%s", diff) + } +} + +func TestFixOutdatedPatchesFieldTitle(t *testing.T) { + kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +patchesJson6902: +- path: patch1.yaml + target: + kind: Service +- path: patch2.yaml + target: + group: apps + kind: Deployment + version: v1 +`) + + expected := []byte(` +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +patches: +- path: patch1.yaml + target: + kind: Service +- path: patch2.yaml + target: + group: apps + kind: Deployment + version: v1 +`) + fSys := filesys.MakeFsInMemory() + testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) + 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 diff := cmp.Diff(expected, bytes); diff != "" { + t.Errorf("Mismatch (-expected, +actual):\n%s", diff) } }