Skip to content

Commit

Permalink
Merge pull request #4723 from koba1t/emit_a_warning_when_deprecated_f…
Browse files Browse the repository at this point in the history
…ields_are_used

Emit a warning on build when deprecated fields are used
  • Loading branch information
k8s-ci-robot committed Nov 16, 2022
2 parents e638e40 + 21ee7f7 commit b20e611
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
10 changes: 10 additions & 0 deletions api/internal/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package target
import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -67,6 +68,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 {
Expand Down
37 changes: 34 additions & 3 deletions api/types/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"`

//
Expand Down Expand Up @@ -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
Expand All @@ -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 =
Expand Down
60 changes: 60 additions & 0 deletions api/types/kustomization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit b20e611

Please sign in to comment.