Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit a warning on build when deprecated fields are used #4723

Conversation

koba1t
Copy link
Member

@koba1t koba1t commented Jul 25, 2022

fix #4706

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 25, 2022
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jul 25, 2022
@koba1t
Copy link
Member Author

koba1t commented Jul 25, 2022

/assign @natasha41575

Copy link
Contributor

@natasha41575 natasha41575 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good! A few small comments. Thanks for picking this up.

k Kustomization
want *[]string
}{
// TODO: Add test cases.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this TODO still needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few tests are already added, and It isn't needed.
I'll delete this comment.

const (
deprecatedBaseWarningMessage = "# Warning: bases was deprecated, Please use resources or try `kustomize edit fix`."
deprecatedPatchesJson6902Message = "# Warning: patchesJson6902 is deprecated, Please use patches or try `kustomize edit fix`."
deprecatedPatchesStrategicMergeMessage = "# Warning: bases is patchesStrategicMerge, Please use patches or try `kustomize edit fix`."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like patchesStrategicMerge isn't handled by kustomize edit fix at the moment, so I think we should either wait for that change to go in first, or we need to change this error message to not mention kustomize edit fix yet.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this message.

So, I add edit fix function with #4733
If you have time, Could you watch this?

const (
deprecatedBaseWarningMessage = "# Warning: bases was deprecated, Please use resources or try `kustomize edit fix`."
deprecatedPatchesJson6902Message = "# Warning: patchesJson6902 is deprecated, Please use patches or try `kustomize edit fix`."
deprecatedPatchesStrategicMergeMessage = "# Warning: bases is patchesStrategicMerge, Please use patches or try `kustomize edit fix`."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
deprecatedPatchesStrategicMergeMessage = "# Warning: bases is patchesStrategicMerge, Please use patches or try `kustomize edit fix`."
deprecatedPatchesStrategicMergeMessage = "# Warning: patchesStrategicMerge is deprecated, Please use patches or try `kustomize edit fix`."

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I fix.

@@ -172,6 +172,31 @@ type Kustomization struct {
BuildMetadata []string `json:"buildMetadata,omitempty" yaml:"buildMetadata,omitempty"`
}

const (
deprecatedBaseWarningMessage = "# Warning: bases was deprecated, Please use resources or try `kustomize edit fix`."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: quote the field names (here, and in the other messages as well)

# Warning: bases was deprecated, please use 'resources' or try `kustomize edit fix`.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it.

@k8s-ci-robot
Copy link
Contributor

@koba1t: This PR has multiple commits, and the default merge method is: merge.
You can request commits to be squashed using the label: tide/merge-method-squash

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@netlify
Copy link

netlify bot commented Jul 29, 2022

Deploy Preview for kubernetes-sigs-kustomize canceled.

Name Link
🔨 Latest commit 438758b
🔍 Latest deploy log https://app.netlify.com/sites/kubernetes-sigs-kustomize/deploys/62e3d749a7928e00089e5dbb

@koba1t
Copy link
Member Author

koba1t commented Jul 29, 2022

hi @natasha41575
I think I completed fixing these.
Could you recheck it?

deprecatedBaseWarningMessage = "# Warning: bases was deprecated, Please use 'resources' or try `kustomize edit fix`."
deprecatedPatchesJson6902Message = "# Warning: patchesJson6902 is deprecated, Please use 'patches' or try `kustomize edit fix`."
deprecatedPatchesStrategicMergeMessage = "# Warning: patchesStrategicMerge is deprecated, Please use 'patches'."
deprecatedVarsMessage = "# Warning: vars is deprecated, Please use 'replacements'."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KnVerey Would you like to take a look a these warning messages before we merge?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Please change the commas after "deprecated" to periods.
  • Please quote the names of the old fields, to match the names of the new.
  • Since the point of kustomize edit fix is to do these updates for you, it isn't really an alternative to using the new fields. How about Warning: 'X' is deprecated. Please use 'Y' instead. Run 'kustomize edit fix' to update your Kustomization automatically..
  • For replacements, we can add something like "(EXPERIMENTAL)" to the edit fix part.
  • If this merges before add edit fix for patchesStrategicMerge to patches #4733, that PR will need to update the message.

Copy link
Member Author

@koba1t koba1t Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @KnVerey
I think I completed to fix from your comment.

If this merges before #4733, that PR will need to update the message.

I think it is better to merge first to #4733 if I can.
I added a warning message to patchesStrategicMerge in this PR.

@natasha41575
Would you review #4733 before merging this PR?

@koba1t koba1t force-pushed the emit_a_warning_when_deprecated_fields_are_used branch 3 times, most recently from e12bf75 to 90231f4 Compare August 2, 2022 15:41
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 2, 2022
api/types/kustomization.go Outdated Show resolved Hide resolved
deprecatedBaseWarningMessage = "# Warning: bases was deprecated, Please use 'resources' or try `kustomize edit fix`."
deprecatedPatchesJson6902Message = "# Warning: patchesJson6902 is deprecated, Please use 'patches' or try `kustomize edit fix`."
deprecatedPatchesStrategicMergeMessage = "# Warning: patchesStrategicMerge is deprecated, Please use 'patches'."
deprecatedVarsMessage = "# Warning: vars is deprecated, Please use 'replacements'."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Please change the commas after "deprecated" to periods.
  • Please quote the names of the old fields, to match the names of the new.
  • Since the point of kustomize edit fix is to do these updates for you, it isn't really an alternative to using the new fields. How about Warning: 'X' is deprecated. Please use 'Y' instead. Run 'kustomize edit fix' to update your Kustomization automatically..
  • For replacements, we can add something like "(EXPERIMENTAL)" to the edit fix part.
  • If this merges before add edit fix for patchesStrategicMerge to patches #4733, that PR will need to update the message.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Aug 11, 2022
@koba1t koba1t force-pushed the emit_a_warning_when_deprecated_fields_are_used branch from 6b200a3 to 16a10df Compare August 11, 2022 18:30
api/types/kustomization.go Outdated Show resolved Hide resolved
const (
deprecatedWarningToRunEditFix = "# Warning: Run 'kustomize edit fix' to update your Kustomization automatically."
deprecatedWarningToRunEditFixExperimential = "# Warning: [EXPERIMENTAL]Run 'kustomize edit fix' to update your Kustomization automatically."
deprecatedBaseWarningMessage = "# Warning: 'bases' is deprecated. Please use 'resources' instead." + "\n" + deprecatedWarningToRunEditFix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is a single problem, I would prefer to have it printed on one line, without the second "warning" text

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I restore it to one-line warning messages.

api/types/kustomization.go Outdated Show resolved Hide resolved
@@ -69,6 +70,15 @@ func (kt *KustTarget) Load() error {
if err != nil {
return err
}

// show warning message when using deprecated fields.
warningMessages := k.CheckDeprecatedFields()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this is going to work, given that FixKustomizationPreUnmarshalling above mutates the file to accommodate some of the deprecated fields?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like FixKustomizationPreUnmarshalling is fixing for raw []byte data before unmarshaling to yaml.

// 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))
}
doLegacy, err := useLegacyPatch(data)
if err != nil {
return nil, err
}
if doLegacy {
pattern := regexp.MustCompile("patches:")
data = pattern.ReplaceAll(data, []byte("patchesStrategicMerge:"))
}
return data, nil
}

I think this function doesn't affect some of the deprecated fields in Kustomization struct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, this changes imageTags to images, which is a deprecation I wasn't even aware of. Also, it makes it seem like the field called patchesStrategicMerge was originally called patches, and we later re-introduced patches with a different meaning. 🤦

Ideally, I think we should warn about imageTags too, so we can get rid of FixKustomizationPreUnmarshalling along with all the deprecated fields.

Also, I wonder if folks using the super old version of patches would get a weird warning about patchesStrategicMerge (which they aren't using) because of this method. Can we check this please?

Copy link
Member Author

@koba1t koba1t Aug 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, I think we should warn about imageTags too, so we can get rid of FixKustomizationPreUnmarshalling along with all the deprecated fields.

I think it is hard to delete the FixKustomizationPreUnmarshalling function while we support the super old version of patches.
Because super old patches (It is called patchesStrategicMerge today) have a different struct to today's patches field.
I think we will get an error when unmarshaling []byte to Kustomization struct if we delete the pre unmarshalling process.

PatchesStrategicMerge []PatchStrategicMerge `json:"patchesStrategicMerge,omitempty" yaml:"patchesStrategicMerge,omitempty"`
// JSONPatches is a list of JSONPatch for applying JSON patch.
// Format documented at https://tools.ietf.org/html/rfc6902
// and http://jsonpatch.com
PatchesJson6902 []Patch `json:"patchesJson6902,omitempty" yaml:"patchesJson6902,omitempty"`
// Patches is a list of patches, where each one can be either a
// Strategic Merge Patch or a JSON patch.
// Each patch can be applied to multiple target objects.
Patches []Patch `json:"patches,omitempty" yaml:"patches,omitempty"`

type PatchStrategicMerge string

// Patch represent either a Strategic Merge Patch or a JSON patch
// and its targets.
// The content of the patch can either be from a file
// or from an inline string.
type Patch struct {
// Path is a relative file path to the patch file.
Path string `json:"path,omitempty" yaml:"path,omitempty"`
// Patch is the content of a patch.
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
// Target points to the resources that the patch is applied to
Target *Selector `json:"target,omitempty" yaml:"target,omitempty"`
// Options is a list of options for the patch
Options map[string]bool `json:"options,omitempty" yaml:"options,omitempty"`
}

Copy link
Member Author

@koba1t koba1t Aug 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I wonder if folks using the super old version of patches would get a weird warning about patchesStrategicMerge (which they aren't using) because of this method. Can we check this please?

That is right. If we use very old style patches, the warning message of patchesStrategicMerge appear...

api/types/kustomization.go Outdated Show resolved Hide resolved
api/types/kustomization.go Outdated Show resolved Hide resolved
api/types/kustomization.go Outdated Show resolved Hide resolved
@@ -116,7 +119,7 @@ type Kustomization struct {
// CRDs themselves are not modified.
Crds []string `json:"crds,omitempty" yaml:"crds,omitempty"`

// Deprecated.
// Deprecated: Replace with the Resources.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding a line, add "Deprecated:" to the existing message below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KnVerey
Copy link
Contributor

KnVerey commented Aug 12, 2022

I think it is better to merge first to #4733 if I can.
I added a warning message to patchesStrategicMerge in this PR.

I agree with you. So we don't forget that that order is required:

/hold for #4733 to merge first

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 12, 2022
@koba1t
Copy link
Member Author

koba1t commented Aug 12, 2022

Hi @KnVerey
I think I completed to fix from your comment.
If you have time, could you please recheck again?

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 26, 2022
@koba1t koba1t force-pushed the emit_a_warning_when_deprecated_fields_are_used branch from 4c7bfde to 21ee7f7 Compare September 1, 2022 23:06
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 1, 2022
@koba1t
Copy link
Member Author

koba1t commented Nov 7, 2022

/remove-hold

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 7, 2022
@natasha41575
Copy link
Contributor

Nice!

$ kustomize build > out
# Warning: 'bases' is deprecated. Please use 'resources' instead. Run 'kustomize edit fix' to update your Kustomization automatically.
# Warning: 'patchesStrategicMerge' is deprecated. Please use 'patches' instead. Run 'kustomize edit fix' to update your Kustomization automatically.

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: koba1t, natasha41575

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 16, 2022
@natasha41575
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 16, 2022
@k8s-ci-robot k8s-ci-robot merged commit b20e611 into kubernetes-sigs:master Nov 16, 2022
@koba1t koba1t deleted the emit_a_warning_when_deprecated_fields_are_used branch November 22, 2022 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Emit a deprecation warning when deprecated fields are used
4 participants