Merge pull request #4723 from koba1t/emit_a_warning_when_deprecated_fields_are_used

Emit a warning on build when deprecated fields are used
This commit is contained in:
Kubernetes Prow Robot
2022-11-16 08:32:50 -08:00
committed by GitHub
3 changed files with 104 additions and 3 deletions

View File

@@ -60,12 +60,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
@@ -89,6 +91,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
@@ -117,9 +120,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"`
//
@@ -173,6 +174,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
@@ -188,8 +216,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 =

View File

@@ -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")