From 21ee7f71257471e7be3aafc5d1642a4546dae598 Mon Sep 17 00:00:00 2001 From: yugo kobayashi Date: Mon, 25 Jul 2022 20:20:26 +0000 Subject: [PATCH] Emit a warning when deprecated fields are used --- api/internal/target/kusttarget.go | 10 ++++++ api/types/kustomization.go | 37 +++++++++++++++++-- api/types/kustomization_test.go | 60 +++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 3 deletions(-) diff --git a/api/internal/target/kusttarget.go b/api/internal/target/kusttarget.go index 4bbf7e98a..daa1960f2 100644 --- a/api/internal/target/kusttarget.go +++ b/api/internal/target/kusttarget.go @@ -6,6 +6,7 @@ package target import ( "encoding/json" "fmt" + "os" "strings" "github.com/pkg/errors" @@ -69,6 +70,15 @@ func (kt *KustTarget) Load() error { if err != nil { return err } + + // show warning message when using deprecated fields. + warningMessages := k.CheckDeprecatedFields() + if warningMessages != nil { + for _, msg := range *warningMessages { + fmt.Fprintf(os.Stderr, "%v\n", msg) + } + } + k.FixKustomizationPostUnmarshalling() errs := k.EnforceFields() if len(errs) > 0 { diff --git a/api/types/kustomization.go b/api/types/kustomization.go index c194dcc3c..5bb29087f 100644 --- a/api/types/kustomization.go +++ b/api/types/kustomization.go @@ -59,12 +59,14 @@ type Kustomization struct { // CommonAnnotations to add to all objects. CommonAnnotations map[string]string `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"` + // Deprecated: Use the Patches field instead, which provides a superset of the functionality of PatchesStrategicMerge. // PatchesStrategicMerge specifies the relative path to a file // containing a strategic merge patch. Format documented at // https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md // URLs and globs are not supported. PatchesStrategicMerge []PatchStrategicMerge `json:"patchesStrategicMerge,omitempty" yaml:"patchesStrategicMerge,omitempty"` + // Deprecated: Use the Patches field instead, which provides a superset of the functionality of JSONPatches. // JSONPatches is a list of JSONPatch for applying JSON patch. // Format documented at https://tools.ietf.org/html/rfc6902 // and http://jsonpatch.com @@ -88,6 +90,7 @@ type Kustomization struct { // specification. This can also be done with a patch. Replicas []Replica `json:"replicas,omitempty" yaml:"replicas,omitempty"` + // Deprecated: Vars will be removed in future release. Migrate to Replacements instead. // Vars allow things modified by kustomize to be injected into a // kubernetes object specification. A var is a name (e.g. FOO) associated // with a field in a specific resource instance. The field must @@ -116,9 +119,7 @@ type Kustomization struct { // CRDs themselves are not modified. Crds []string `json:"crds,omitempty" yaml:"crds,omitempty"` - // Deprecated. - // Anything that would have been specified here should - // be specified in the Resources field instead. + // Deprecated: Anything that would have been specified here should be specified in the Resources field instead. Bases []string `json:"bases,omitempty" yaml:"bases,omitempty"` // @@ -172,6 +173,33 @@ type Kustomization struct { BuildMetadata []string `json:"buildMetadata,omitempty" yaml:"buildMetadata,omitempty"` } +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 + 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 +) + +// CheckDeprecatedFields check deprecated field is used or not. +func (k *Kustomization) CheckDeprecatedFields() *[]string { + var warningMessages []string + if k.Bases != nil { + warningMessages = append(warningMessages, deprecatedBaseWarningMessage) + } + if k.PatchesJson6902 != nil { + warningMessages = append(warningMessages, deprecatedPatchesJson6902Message) + } + if k.PatchesStrategicMerge != nil { + warningMessages = append(warningMessages, deprecatedPatchesStrategicMergeMessage) + } + if k.Vars != nil { + warningMessages = append(warningMessages, deprecatedVarsMessage) + } + return &warningMessages +} + // FixKustomizationPostUnmarshalling fixes things // like empty fields that should not be empty, or // moving content of deprecated fields to newer @@ -187,8 +215,11 @@ func (k *Kustomization) FixKustomizationPostUnmarshalling() { k.APIVersion = KustomizationVersion } } + + // 'bases' field was deprecated in favor of the 'resources' field. k.Resources = append(k.Resources, k.Bases...) k.Bases = nil + for i, g := range k.ConfigMapGenerator { if g.EnvSource != "" { k.ConfigMapGenerator[i].EnvSources = diff --git a/api/types/kustomization_test.go b/api/types/kustomization_test.go index c7600eeed..e60e89bb6 100644 --- a/api/types/kustomization_test.go +++ b/api/types/kustomization_test.go @@ -16,6 +16,66 @@ func fixKustomizationPostUnmarshallingCheck(k, e *Kustomization) bool { k.Bases == nil } +func TestKustomization_CheckDeprecatedFields(t *testing.T) { + tests := []struct { + name string + k Kustomization + want *[]string + }{ + { + name: "using_bases", + k: Kustomization{ + Bases: []string{"base"}, + }, + want: &[]string{deprecatedBaseWarningMessage}, + }, + { + name: "usingPatchesJson6902", + k: Kustomization{ + PatchesJson6902: []Patch{}, + }, + want: &[]string{deprecatedPatchesJson6902Message}, + }, + { + name: "usingPatchesStrategicMerge", + k: Kustomization{ + PatchesStrategicMerge: []PatchStrategicMerge{}, + }, + want: &[]string{deprecatedPatchesStrategicMergeMessage}, + }, + { + name: "usingVar", + k: Kustomization{ + Vars: []Var{}, + }, + want: &[]string{deprecatedVarsMessage}, + }, + { + name: "usingAll", + k: Kustomization{ + Bases: []string{"base"}, + PatchesJson6902: []Patch{}, + PatchesStrategicMerge: []PatchStrategicMerge{}, + Vars: []Var{}, + }, + want: &[]string{ + deprecatedBaseWarningMessage, + deprecatedPatchesJson6902Message, + deprecatedPatchesStrategicMergeMessage, + deprecatedVarsMessage, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + k := tt.k + if got := k.CheckDeprecatedFields(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Kustomization.CheckDeprecatedFields() = %v, want %v", got, tt.want) + } + }) + } +} + func TestFixKustomizationPostUnmarshalling(t *testing.T) { var k Kustomization k.Bases = append(k.Bases, "foo")