Skip to content

Commit

Permalink
remove FixKustomizationPreUnmarshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
koba1t committed Dec 13, 2022
1 parent 4cccb83 commit 52faaf8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 55 deletions.
14 changes: 5 additions & 9 deletions api/internal/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,21 @@ func (kt *KustTarget) Load() error {
if err != nil {
return err
}
content, err = types.FixKustomizationPreUnmarshalling(content)
if err != nil {
return err
}

var k types.Kustomization
err = k.Unmarshal(content)
if err != nil {
if err := k.Unmarshal(content); err != nil {
return err
}

// show warning message when using deprecated fields.
warningMessages := k.CheckDeprecatedFields()
if warningMessages != nil {
if warningMessages := k.CheckDeprecatedFields(); warningMessages != nil {
for _, msg := range *warningMessages {
fmt.Fprintf(os.Stderr, "%v\n", msg)
}
}

k.FixKustomizationPostUnmarshalling()
k.FixKustomization()

errs := k.EnforceFields()
if len(errs) > 0 {
return fmt.Errorf(
Expand Down
22 changes: 0 additions & 22 deletions api/types/fix.go

This file was deleted.

15 changes: 13 additions & 2 deletions api/types/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ type Kustomization struct {
// patch, but this operator is simpler to specify.
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
// specified source to N specified targets.
Replacements []ReplacementField `json:"replacements,omitempty" yaml:"replacements,omitempty"`
Expand Down Expand Up @@ -181,6 +184,7 @@ const (
deprecatedWarningToRunEditFix = "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
deprecatedImageTagsWarningMessage = "# Warning: 'imageTags' is deprecated. Please use 'images' instead." + " " + deprecatedWarningToRunEditFix
deprecatedPatchesJson6902Message = "# Warning: 'patchesJson6902' 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
Expand All @@ -192,6 +196,9 @@ func (k *Kustomization) CheckDeprecatedFields() *[]string {
if k.Bases != nil {
warningMessages = append(warningMessages, deprecatedBaseWarningMessage)
}
if k.ImageTags != nil {
warningMessages = append(warningMessages, deprecatedImageTagsWarningMessage)
}
if k.PatchesJson6902 != nil {
warningMessages = append(warningMessages, deprecatedPatchesJson6902Message)
}
Expand All @@ -204,11 +211,11 @@ func (k *Kustomization) CheckDeprecatedFields() *[]string {
return &warningMessages
}

// FixKustomizationPostUnmarshalling fixes things
// FixKustomization fixes things
// like empty fields that should not be empty, or
// moving content of deprecated fields to newer
// fields.
func (k *Kustomization) FixKustomizationPostUnmarshalling() {
func (k *Kustomization) FixKustomization() {
if k.Kind == "" {
k.Kind = KustomizationKind
}
Expand All @@ -224,6 +231,10 @@ func (k *Kustomization) FixKustomizationPostUnmarshalling() {
k.Resources = append(k.Resources, k.Bases...)
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 {
if g.EnvSource != "" {
k.ConfigMapGenerator[i].EnvSources =
Expand Down
13 changes: 11 additions & 2 deletions api/types/kustomization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ func TestKustomization_CheckDeprecatedFields(t *testing.T) {
},
want: &[]string{deprecatedBaseWarningMessage},
},
{
name: "using_ImageTags",
k: Kustomization{
ImageTags: []Image{},
},
want: &[]string{deprecatedImageTagsWarningMessage},
},
{
name: "usingPatchesJson6902",
k: Kustomization{
Expand All @@ -54,12 +61,14 @@ func TestKustomization_CheckDeprecatedFields(t *testing.T) {
name: "usingAll",
k: Kustomization{
Bases: []string{"base"},
ImageTags: []Image{},
PatchesJson6902: []Patch{},
PatchesStrategicMerge: []PatchStrategicMerge{},
Vars: []Var{},
},
want: &[]string{
deprecatedBaseWarningMessage,
deprecatedImageTagsWarningMessage,
deprecatedPatchesJson6902Message,
deprecatedPatchesStrategicMergeMessage,
deprecatedVarsMessage,
Expand Down Expand Up @@ -88,7 +97,7 @@ func TestFixKustomizationPostUnmarshalling(t *testing.T) {
k.CommonLabels = map[string]string{
"foo": "bar",
}
k.FixKustomizationPostUnmarshalling()
k.FixKustomization()

expected := Kustomization{
TypeMeta: TypeMeta{
Expand Down Expand Up @@ -120,7 +129,7 @@ func TestFixKustomizationPostUnmarshalling_2(t *testing.T) {
},
}
k.Bases = append(k.Bases, "foo")
k.FixKustomizationPostUnmarshalling()
k.FixKustomization()

expected := Kustomization{
TypeMeta: TypeMeta{
Expand Down
28 changes: 13 additions & 15 deletions kustomize/commands/internal/kustfile/kustomizationfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,18 @@ func (mf *kustomizationFile) Read() (*types.Kustomization, error) {
if err != nil {
return nil, err
}
data, err = types.FixKustomizationPreUnmarshalling(data)
if err != nil {
return nil, err
}

var k types.Kustomization
err = k.Unmarshal(data)
if err != nil {
if err := k.Unmarshal(data); err != nil {
return nil, err
}
k.FixKustomizationPostUnmarshalling()
err = mf.parseCommentedFields(data)
if err != nil {

k.FixKustomization()

if err := mf.parseCommentedFields(data); err != nil {
return nil, err
}
return &k, err
return &k, nil
}

func (mf *kustomizationFile) Write(kustomization *types.Kustomization) error {
Expand Down Expand Up @@ -264,12 +261,13 @@ func (mf *kustomizationFile) hasField(name string) bool {
}

/*
isCommentOrBlankLine determines if a line is a comment or blank line
Return true for following lines
# This line is a comment
# This line is also a comment with several leading white spaces
isCommentOrBlankLine determines if a line is a comment or blank line
Return true for following lines
# This line is a comment
# This line is also a comment with several leading white spaces
(The line above is a blank line)
(The line above is a blank line)
*/
func isCommentOrBlankLine(line []byte) bool {
s := bytes.TrimRight(bytes.TrimLeft(line, " "), "\n")
Expand Down
10 changes: 5 additions & 5 deletions kustomize/commands/internal/kustfile/kustomizationfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestWriteAndRead(t *testing.T) {
if err != nil {
t.Fatalf("Couldn't read kustomization file: %v\n", err)
}
kustomization.FixKustomizationPostUnmarshalling()
kustomization.FixKustomization()
if !reflect.DeepEqual(kustomization, content) {
t.Fatal("Read kustomization is different from written kustomization")
}
Expand Down Expand Up @@ -189,7 +189,7 @@ patchesStrategicMerge:
func TestPreserveCommentsWithAdjust(t *testing.T) {
kustomizationContentWithComments := []byte(`
# Some comments
# This is some comment we should preserve
Expand Down Expand Up @@ -225,7 +225,7 @@ generatorOptions:

expected := []byte(`
# Some comments
# This is some comment we should preserve
Expand Down Expand Up @@ -326,7 +326,7 @@ kind: Kustomization
func TestCommentsWithDocumentSeperatorAtBeginning(t *testing.T) {
kustomizationContentWithComments := []byte(`
# Some comments
# This is some comment we should preserve
# don't delete it
Expand All @@ -339,7 +339,7 @@ namespace: mynamespace

expected := []byte(`
# Some comments
# This is some comment we should preserve
# don't delete it
Expand Down

0 comments on commit 52faaf8

Please sign in to comment.