Merge pull request #4923 from koba1t/chore/remove_FixKustomizationPreUnmarshalling

Remove fix kustomization step before Unmarshalling the yaml structure
This commit is contained in:
Kubernetes Prow Robot
2022-12-13 12:11:35 -08:00
committed by GitHub
8 changed files with 49 additions and 60 deletions

View File

@@ -101,10 +101,7 @@ func (lc *localizer) load() (*types.Kustomization, string, error) {
if err != nil { if err != nil {
return nil, "", errors.Wrap(err) return nil, "", errors.Wrap(err)
} }
content, err = types.FixKustomizationPreUnmarshalling(content)
if err != nil {
return nil, "", errors.WrapPrefixf(err, "invalid kustomization")
}
var kust types.Kustomization var kust types.Kustomization
err = (&kust).Unmarshal(content) err = (&kust).Unmarshal(content)
if err != nil { if err != nil {

View File

@@ -210,7 +210,7 @@ bases:
- beta - beta
configMapGenerator: configMapGenerator:
- env: env.properties - env: env.properties
images: imageTags:
- name: postgres - name: postgres
newName: my-registry/my-postgres newName: my-registry/my-postgres
newTag: v1 newTag: v1

View File

@@ -58,25 +58,21 @@ func (kt *KustTarget) Load() error {
if err != nil { if err != nil {
return err return err
} }
content, err = types.FixKustomizationPreUnmarshalling(content)
if err != nil {
return err
}
var k types.Kustomization var k types.Kustomization
err = k.Unmarshal(content) if err := k.Unmarshal(content); err != nil {
if err != nil {
return err return err
} }
// show warning message when using deprecated fields. // show warning message when using deprecated fields.
warningMessages := k.CheckDeprecatedFields() if warningMessages := k.CheckDeprecatedFields(); warningMessages != nil {
if warningMessages != nil {
for _, msg := range *warningMessages { for _, msg := range *warningMessages {
fmt.Fprintf(os.Stderr, "%v\n", msg) fmt.Fprintf(os.Stderr, "%v\n", msg)
} }
} }
k.FixKustomizationPostUnmarshalling() k.FixKustomization()
errs := k.EnforceFields() errs := k.EnforceFields()
if len(errs) > 0 { if len(errs) > 0 {
return fmt.Errorf( return fmt.Errorf(

View File

@@ -1,22 +0,0 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package types
import (
"regexp"
)
// FixKustomizationPreUnmarshalling modifies the raw data
// before marshalling - e.g. changes old field names to
// new field names.
func FixKustomizationPreUnmarshalling(data []byte) ([]byte, error) {
deprecatedFieldsMap := map[string]string{
"imageTags:": "images:",
}
for oldname, newname := range deprecatedFieldsMap {
pattern := regexp.MustCompile(oldname)
data = pattern.ReplaceAll(data, []byte(newname))
}
return data, nil
}

View File

@@ -83,6 +83,9 @@ type Kustomization struct {
// patch, but this operator is simpler to specify. // patch, but this operator is simpler to specify.
Images []Image `json:"images,omitempty" yaml:"images,omitempty"` Images []Image `json:"images,omitempty" yaml:"images,omitempty"`
// Deprecated: Use the Images field instead.
ImageTags []Image `json:"imageTags,omitempty" yaml:"imageTags,omitempty"`
// Replacements is a list of replacements, which will copy nodes from a // Replacements is a list of replacements, which will copy nodes from a
// specified source to N specified targets. // specified source to N specified targets.
Replacements []ReplacementField `json:"replacements,omitempty" yaml:"replacements,omitempty"` Replacements []ReplacementField `json:"replacements,omitempty" yaml:"replacements,omitempty"`
@@ -181,6 +184,7 @@ const (
deprecatedWarningToRunEditFix = "Run 'kustomize edit fix' to update your Kustomization automatically." deprecatedWarningToRunEditFix = "Run 'kustomize edit fix' to update your Kustomization automatically."
deprecatedWarningToRunEditFixExperimential = "[EXPERIMENTAL] Run 'kustomize edit fix' to update your Kustomization automatically." deprecatedWarningToRunEditFixExperimential = "[EXPERIMENTAL] Run 'kustomize edit fix' to update your Kustomization automatically."
deprecatedBaseWarningMessage = "# Warning: 'bases' is deprecated. Please use 'resources' instead." + " " + deprecatedWarningToRunEditFix deprecatedBaseWarningMessage = "# Warning: 'bases' is deprecated. Please use 'resources' instead." + " " + deprecatedWarningToRunEditFix
deprecatedImageTagsWarningMessage = "# Warning: 'imageTags' is deprecated. Please use 'images' instead." + " " + deprecatedWarningToRunEditFix
deprecatedPatchesJson6902Message = "# Warning: 'patchesJson6902' is deprecated. Please use 'patches' instead." + " " + deprecatedWarningToRunEditFix deprecatedPatchesJson6902Message = "# Warning: 'patchesJson6902' is deprecated. Please use 'patches' instead." + " " + deprecatedWarningToRunEditFix
deprecatedPatchesStrategicMergeMessage = "# Warning: 'patchesStrategicMerge' is deprecated. Please use 'patches' instead." + " " + deprecatedWarningToRunEditFix deprecatedPatchesStrategicMergeMessage = "# Warning: 'patchesStrategicMerge' is deprecated. Please use 'patches' instead." + " " + deprecatedWarningToRunEditFix
deprecatedVarsMessage = "# Warning: 'vars' is deprecated. Please use 'replacements' instead." + " " + deprecatedWarningToRunEditFixExperimential deprecatedVarsMessage = "# Warning: 'vars' is deprecated. Please use 'replacements' instead." + " " + deprecatedWarningToRunEditFixExperimential
@@ -192,6 +196,9 @@ func (k *Kustomization) CheckDeprecatedFields() *[]string {
if k.Bases != nil { if k.Bases != nil {
warningMessages = append(warningMessages, deprecatedBaseWarningMessage) warningMessages = append(warningMessages, deprecatedBaseWarningMessage)
} }
if k.ImageTags != nil {
warningMessages = append(warningMessages, deprecatedImageTagsWarningMessage)
}
if k.PatchesJson6902 != nil { if k.PatchesJson6902 != nil {
warningMessages = append(warningMessages, deprecatedPatchesJson6902Message) warningMessages = append(warningMessages, deprecatedPatchesJson6902Message)
} }
@@ -204,11 +211,11 @@ func (k *Kustomization) CheckDeprecatedFields() *[]string {
return &warningMessages return &warningMessages
} }
// FixKustomizationPostUnmarshalling fixes things // FixKustomization fixes things
// like empty fields that should not be empty, or // like empty fields that should not be empty, or
// moving content of deprecated fields to newer // moving content of deprecated fields to newer
// fields. // fields.
func (k *Kustomization) FixKustomizationPostUnmarshalling() { func (k *Kustomization) FixKustomization() {
if k.Kind == "" { if k.Kind == "" {
k.Kind = KustomizationKind k.Kind = KustomizationKind
} }
@@ -224,6 +231,10 @@ func (k *Kustomization) FixKustomizationPostUnmarshalling() {
k.Resources = append(k.Resources, k.Bases...) k.Resources = append(k.Resources, k.Bases...)
k.Bases = nil k.Bases = nil
// 'imageTags' field was deprecated in favor of the 'images' field.
k.Images = append(k.Images, k.ImageTags...)
k.ImageTags = nil
for i, g := range k.ConfigMapGenerator { for i, g := range k.ConfigMapGenerator {
if g.EnvSource != "" { if g.EnvSource != "" {
k.ConfigMapGenerator[i].EnvSources = k.ConfigMapGenerator[i].EnvSources =

View File

@@ -29,6 +29,13 @@ func TestKustomization_CheckDeprecatedFields(t *testing.T) {
}, },
want: &[]string{deprecatedBaseWarningMessage}, want: &[]string{deprecatedBaseWarningMessage},
}, },
{
name: "using_ImageTags",
k: Kustomization{
ImageTags: []Image{},
},
want: &[]string{deprecatedImageTagsWarningMessage},
},
{ {
name: "usingPatchesJson6902", name: "usingPatchesJson6902",
k: Kustomization{ k: Kustomization{
@@ -54,12 +61,14 @@ func TestKustomization_CheckDeprecatedFields(t *testing.T) {
name: "usingAll", name: "usingAll",
k: Kustomization{ k: Kustomization{
Bases: []string{"base"}, Bases: []string{"base"},
ImageTags: []Image{},
PatchesJson6902: []Patch{}, PatchesJson6902: []Patch{},
PatchesStrategicMerge: []PatchStrategicMerge{}, PatchesStrategicMerge: []PatchStrategicMerge{},
Vars: []Var{}, Vars: []Var{},
}, },
want: &[]string{ want: &[]string{
deprecatedBaseWarningMessage, deprecatedBaseWarningMessage,
deprecatedImageTagsWarningMessage,
deprecatedPatchesJson6902Message, deprecatedPatchesJson6902Message,
deprecatedPatchesStrategicMergeMessage, deprecatedPatchesStrategicMergeMessage,
deprecatedVarsMessage, deprecatedVarsMessage,
@@ -88,7 +97,7 @@ func TestFixKustomizationPostUnmarshalling(t *testing.T) {
k.CommonLabels = map[string]string{ k.CommonLabels = map[string]string{
"foo": "bar", "foo": "bar",
} }
k.FixKustomizationPostUnmarshalling() k.FixKustomization()
expected := Kustomization{ expected := Kustomization{
TypeMeta: TypeMeta{ TypeMeta: TypeMeta{
@@ -120,7 +129,7 @@ func TestFixKustomizationPostUnmarshalling_2(t *testing.T) {
}, },
} }
k.Bases = append(k.Bases, "foo") k.Bases = append(k.Bases, "foo")
k.FixKustomizationPostUnmarshalling() k.FixKustomization()
expected := Kustomization{ expected := Kustomization{
TypeMeta: TypeMeta{ TypeMeta: TypeMeta{

View File

@@ -165,21 +165,18 @@ func (mf *kustomizationFile) Read() (*types.Kustomization, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
data, err = types.FixKustomizationPreUnmarshalling(data)
if err != nil {
return nil, err
}
var k types.Kustomization var k types.Kustomization
err = k.Unmarshal(data) if err := k.Unmarshal(data); err != nil {
if err != nil {
return nil, err return nil, err
} }
k.FixKustomizationPostUnmarshalling()
err = mf.parseCommentedFields(data) k.FixKustomization()
if err != nil {
if err := mf.parseCommentedFields(data); err != nil {
return nil, err return nil, err
} }
return &k, err return &k, nil
} }
func (mf *kustomizationFile) Write(kustomization *types.Kustomization) error { func (mf *kustomizationFile) Write(kustomization *types.Kustomization) error {
@@ -264,12 +261,13 @@ func (mf *kustomizationFile) hasField(name string) bool {
} }
/* /*
isCommentOrBlankLine determines if a line is a comment or blank line isCommentOrBlankLine determines if a line is a comment or blank line
Return true for following lines Return true for following lines
# This line is a comment # This line is a comment
# This line is also a comment with several leading white spaces
(The line above is a blank line) # This line is also a comment with several leading white spaces
(The line above is a blank line)
*/ */
func isCommentOrBlankLine(line []byte) bool { func isCommentOrBlankLine(line []byte) bool {
s := bytes.TrimRight(bytes.TrimLeft(line, " "), "\n") s := bytes.TrimRight(bytes.TrimLeft(line, " "), "\n")

View File

@@ -82,7 +82,7 @@ func TestWriteAndRead(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Couldn't read kustomization file: %v\n", err) t.Fatalf("Couldn't read kustomization file: %v\n", err)
} }
kustomization.FixKustomizationPostUnmarshalling() kustomization.FixKustomization()
if !reflect.DeepEqual(kustomization, content) { if !reflect.DeepEqual(kustomization, content) {
t.Fatal("Read kustomization is different from written kustomization") t.Fatal("Read kustomization is different from written kustomization")
} }
@@ -189,7 +189,7 @@ patchesStrategicMerge:
func TestPreserveCommentsWithAdjust(t *testing.T) { func TestPreserveCommentsWithAdjust(t *testing.T) {
kustomizationContentWithComments := []byte(` kustomizationContentWithComments := []byte(`
# Some comments # Some comments
# This is some comment we should preserve # This is some comment we should preserve
@@ -225,7 +225,7 @@ generatorOptions:
expected := []byte(` expected := []byte(`
# Some comments # Some comments
# This is some comment we should preserve # This is some comment we should preserve
@@ -326,7 +326,7 @@ kind: Kustomization
func TestCommentsWithDocumentSeperatorAtBeginning(t *testing.T) { func TestCommentsWithDocumentSeperatorAtBeginning(t *testing.T) {
kustomizationContentWithComments := []byte(` kustomizationContentWithComments := []byte(`
# Some comments # Some comments
# This is some comment we should preserve # This is some comment we should preserve
# don't delete it # don't delete it
@@ -339,7 +339,7 @@ namespace: mynamespace
expected := []byte(` expected := []byte(`
# Some comments # Some comments
# This is some comment we should preserve # This is some comment we should preserve
# don't delete it # don't delete it