From 3e0f5ea327c9d6d5268d37f231cfd3b355984d99 Mon Sep 17 00:00:00 2001 From: Jeffrey Regan Date: Thu, 18 Oct 2018 13:09:25 -0700 Subject: [PATCH] Delete penultimate use of Patches field. --- pkg/commands/edit/add/addpatch.go | 2 +- pkg/commands/kustfile/kustomizationfile.go | 7 ++-- .../kustfile/kustomizationfile_test.go | 42 +++++++++++++++++++ pkg/types/kustomization.go | 17 ++++---- 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/pkg/commands/edit/add/addpatch.go b/pkg/commands/edit/add/addpatch.go index bb3d5e19e..01af65499 100644 --- a/pkg/commands/edit/add/addpatch.go +++ b/pkg/commands/edit/add/addpatch.go @@ -89,7 +89,7 @@ func (o *addPatchOptions) RunAddPatch(fSys fs.FileSystem) error { } for _, p := range patches { - if patch.Exist(m.PatchesStrategicMerge, p) || kustfile.StringInSlice(p, m.Patches) { + if patch.Exist(m.PatchesStrategicMerge, p) { log.Printf("patch %s already in kustomization file", p) continue } diff --git a/pkg/commands/kustfile/kustomizationfile.go b/pkg/commands/kustfile/kustomizationfile.go index 4edf743fb..f352c09a6 100644 --- a/pkg/commands/kustfile/kustomizationfile.go +++ b/pkg/commands/kustfile/kustomizationfile.go @@ -110,16 +110,17 @@ func (mf *kustomizationFile) Read() (*types.Kustomization, error) { if err != nil { return nil, err } - var kustomization types.Kustomization - err = yaml.Unmarshal(data, &kustomization) + var k types.Kustomization + err = yaml.Unmarshal(data, &k) if err != nil { return nil, err } + k.DealWithDeprecatedFields() err = mf.parseCommentedFields(data) if err != nil { return nil, err } - return &kustomization, err + return &k, err } func (mf *kustomizationFile) Write(kustomization *types.Kustomization) error { diff --git a/pkg/commands/kustfile/kustomizationfile_test.go b/pkg/commands/kustfile/kustomizationfile_test.go index c192cc4f2..f58dff928 100644 --- a/pkg/commands/kustfile/kustomizationfile_test.go +++ b/pkg/commands/kustfile/kustomizationfile_test.go @@ -50,6 +50,48 @@ func TestWriteAndRead(t *testing.T) { } } +// Deprecated fields should not survive being read. +func TestDeprecationOfPatches(t *testing.T) { + hasDeprecatedFields := []byte(` +namePrefix: acme +patches: +- alice +patchesStrategicMerge: +- bob +`) + fSys := fs.MakeFakeFS() + fSys.WriteTestKustomizationWith(hasDeprecatedFields) + mf, err := NewKustomizationFile(fSys) + if err != nil { + t.Fatalf("Unexpected Error: %v", err) + } + k, err := mf.Read() + if err != nil { + t.Fatalf("Couldn't read kustomization file: %v\n", err) + } + if k.NamePrefix != "acme" { + t.Fatalf("Unexpected name prefix") + } + if len(k.Patches) > 0 { + t.Fatalf("Expected nothing in Patches.") + } + if len(k.PatchesStrategicMerge) != 2 { + t.Fatalf( + "Expected len(k.PatchesStrategicMerge) == 2, got %d", + len(k.PatchesStrategicMerge)) + } + m := make(map[string]bool) + for _, v := range k.PatchesStrategicMerge { + m[string(v)] = true + } + if _, f := m["alice"]; !f { + t.Fatalf("Expected alice in PatchesStrategicMerge") + } + if _, f := m["bob"]; !f { + t.Fatalf("Expected bob in PatchesStrategicMerge") + } +} + func TestNewNotExist(t *testing.T) { fakeFS := fs.MakeFakeFS() _, err := NewKustomizationFile(fakeFS) diff --git a/pkg/types/kustomization.go b/pkg/types/kustomization.go index 3e64ee4e1..e5dc648c5 100644 --- a/pkg/types/kustomization.go +++ b/pkg/types/kustomization.go @@ -124,13 +124,16 @@ type Kustomization struct { // DealWithDeprecatedFields should be called immediately after // loading from storage. func (k *Kustomization) DealWithDeprecatedFields() { - // The Patches field, meant to hold StrategicMerge patches, - // is deprecated. Append anything found there to the - // PatchesStrategicMerge field. - // This happened when the PatchesJson6902 field was introduced. - k.PatchesStrategicMerge = patch.Append( - k.PatchesStrategicMerge, k.Patches...) - k.Patches = []string{} + if len(k.Patches) > 0 { + // The Patches field, meant to hold strategic merge + // patches, is deprecated. Append anything found + // there to the PatchesStrategicMerge field. + // This happened when the PatchesJson6902 field + // was introduced. + k.PatchesStrategicMerge = patch.Append( + k.PatchesStrategicMerge, k.Patches...) + k.Patches = []string{} + } } // ConfigMapArgs contains the metadata of how to generate a configmap.