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

Fix unstructured metadata accessors to respect omitempty semantics #67635

Merged

Conversation

nikhita
Copy link
Member

@nikhita nikhita commented Aug 21, 2018

Fixes #67541
Fixes #48211
Fixes #49075
Follow up of #67562

ObjectMeta has fields with omitempty json tags. This means that when the fields have zero values, they should not be persisted in the object.

Before this PR, some of the metadata accessors for unstructured objects did not respect these semantics i.e they would persist a field even if it had a zero value.

This PR updates the accessors so that the field is removed from the unstructured object map if it contains a zero value.

/sig api-machinery
/kind bug
/area custom-resources
/cc sttts liggitt yue9944882 roycaihw
/assign sttts liggitt

Release note:

NONE

@k8s-ci-robot k8s-ci-robot added the release-note Denotes a PR that will be considered when it comes time to generate release notes. label Aug 21, 2018
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. kind/bug Categorizes issue or PR as related to a bug. labels Aug 21, 2018
@k8s-ci-robot k8s-ci-robot added area/custom-resources cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. area/apiserver labels Aug 21, 2018
@nikhita
Copy link
Member Author

nikhita commented Aug 21, 2018

Does this need a "action required" release note?

@nikhita
Copy link
Member Author

nikhita commented Aug 21, 2018

Some packages in hack/.golint_failures are passing golint. Please remove them.

  staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured

How is this even passing golint? There are so many linting errors in there. 🤷‍♀️

/lint

Copy link
Contributor

@k8s-ci-robot k8s-ci-robot left a comment

Choose a reason for hiding this comment

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

@nikhita: 2 warnings.

In response to this:

Some packages in hack/.golint_failures are passing golint. Please remove them.

 staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured

How is this even passing golint? There are so many linting errors in there. 🤷‍♀️

/lint

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.

u.setNestedField(string(uid), "metadata", "uid")
}

func (u *Unstructured) GetResourceVersion() string {
return getNestedString(u.Object, "metadata", "resourceVersion")
}

func (u *Unstructured) SetResourceVersion(version string) {
u.setNestedField(version, "metadata", "resourceVersion")
func (u *Unstructured) SetResourceVersion(resourceVersion string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Golint naming: receiver name u should be consistent with previous receiver name obj for Unstructured. More info.

u.setNestedField(name, "metadata", "name")
}

func (u *Unstructured) GetGenerateName() string {
return getNestedString(u.Object, "metadata", "generateName")
}

func (u *Unstructured) SetGenerateName(name string) {
u.setNestedField(name, "metadata", "generateName")
func (u *Unstructured) SetGenerateName(generateName string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Golint naming: receiver name u should be consistent with previous receiver name obj for Unstructured. More info.

Copy link
Member

Choose a reason for hiding this comment

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

well that's annoying

@nikhita
Copy link
Member Author

nikhita commented Aug 21, 2018

@cblecker @fejta re: golint and /lint above ^^

ObjectMeta has fields with omitempty json tags. This means that when
the fields have zero values, they should not be persisted in the object.

Before this commit, some of the metadata accessors for unstructured
objects did not respect these semantics i.e they would persist a field
even if it had a zero value.

This commit updates the accessors so that the field is removed from the
unstructured object map if it contains a zero value.
@sttts
Copy link
Contributor

sttts commented Aug 21, 2018

Looks good to me. @liggitt any further comments?

#67637 will add tests for the empty patch behaviour.

@nikhita
Copy link
Member Author

nikhita commented Aug 21, 2018

/lint

Copy link
Contributor

@k8s-ci-robot k8s-ci-robot left a comment

Choose a reason for hiding this comment

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

@nikhita: 2 unresolved warnings and 6 new warnings.

In response to this:

/lint

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.

@@ -79,8 +79,8 @@ func (obj *Unstructured) ToList() (*UnstructuredList, error) {
return ret, nil
}

func (obj *Unstructured) EachListItem(fn func(runtime.Object) error) error {
field, ok := obj.Object["items"]
func (u *Unstructured) EachListItem(fn func(runtime.Object) error) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Golint comments: exported method Unstructured.EachListItem should have comment or be unexported. More info.

Copy link
Member Author

Choose a reason for hiding this comment

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

None of the methods in that file have comments and it doesn't really make sense to add to each one either. :/

@@ -100,15 +100,15 @@ func (obj *Unstructured) EachListItem(fn func(runtime.Object) error) error {
return nil
}

func (obj *Unstructured) UnstructuredContent() map[string]interface{} {
if obj.Object == nil {
func (u *Unstructured) UnstructuredContent() map[string]interface{} {
Copy link
Contributor

Choose a reason for hiding this comment

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

Golint comments: exported method Unstructured.UnstructuredContent should have comment or be unexported. More info.

}

func (obj *Unstructured) SetUnstructuredContent(content map[string]interface{}) {
obj.Object = content
func (u *Unstructured) SetUnstructuredContent(content map[string]interface{}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Golint comments: exported method Unstructured.SetUnstructuredContent should have comment or be unexported. More info.

@@ -48,26 +48,26 @@ type Unstructured struct {
var _ metav1.Object = &Unstructured{}
var _ runtime.Unstructured = &Unstructured{}

func (obj *Unstructured) GetObjectKind() schema.ObjectKind { return obj }
func (u *Unstructured) GetObjectKind() schema.ObjectKind { return u }
Copy link
Contributor

Choose a reason for hiding this comment

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

Golint comments: exported method Unstructured.GetObjectKind should have comment or be unexported. More info.


func (obj *Unstructured) IsList() bool {
field, ok := obj.Object["items"]
func (u *Unstructured) IsList() bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Golint comments: exported method Unstructured.IsList should have comment or be unexported. More info.

if !ok {
return false
}
_, ok = field.([]interface{})
return ok
}
func (obj *Unstructured) ToList() (*UnstructuredList, error) {
if !obj.IsList() {
func (u *Unstructured) ToList() (*UnstructuredList, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Golint comments: exported method Unstructured.ToList should have comment or be unexported. More info.

@liggitt
Copy link
Member

liggitt commented Aug 21, 2018

don't feed the linter... it'll just want another cookie :)

@nikhita nikhita force-pushed the customresource-subresource-patch-04 branch from 0eb1f27 to dabd56f Compare August 21, 2018 13:27
@nikhita
Copy link
Member Author

nikhita commented Aug 21, 2018

don't feed the linter... it'll just want another cookie :)

:)

I removed the last commit which tried to fix the golint errors. It's back to how it was before in the previous PR.

@nikhita
Copy link
Member Author

nikhita commented Aug 21, 2018

Regarding the linting:

# Packages with a corresponding foo_test package will make golint fail
# with a useless error. Just ignore that, see golang/lint#68.

Looks like this completely ignores all errors if it sees a foo_test package.
Related issue in golint: golang/lint#68

@nikhita
Copy link
Member Author

nikhita commented Aug 21, 2018

If golint sees a foo_test package in foo, it gives an error like:
staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructured_test.go is in package unstructured_test, not unstructured and does not do any linting. We swallow this error and skip the linting.

failedLint=$(golint "$p"/*.go 2>/dev/null)

@liggitt
Copy link
Member

liggitt commented Aug 21, 2018

this LGTM. @sttts, was there a way we were collecting dev-related release notes for client-go/apimachinery usage? I'm not sure this change makes sense as a user-facing release note, but seems worthwhile to capture for users of the go libraries

@liggitt
Copy link
Member

liggitt commented Aug 21, 2018

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 21, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: liggitt, nikhita

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 approved Indicates a PR has been approved by an approver from all required OWNERS files. release-note-none Denotes a PR that doesn't merit a release note. and removed release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Aug 21, 2018
@nikhita
Copy link
Member Author

nikhita commented Aug 21, 2018

was there a way we were collecting dev-related release notes for client-go/apimachinery usage?

manually. :/
ref:

I was doing the manual release note process work for client-go anyway, so I'll take care of the release note for this one.

@nikhita
Copy link
Member Author

nikhita commented Aug 21, 2018

Quick update here: there is a golint fix now - #67675 🎉

@roycaihw
Copy link
Member

This LGTM. One question: do we need to check zero values for TypeMeta (SetKind and SetAPIVersion) as well?

@liggitt
Copy link
Member

liggitt commented Aug 21, 2018

One question: do we need to check zero values for TypeMeta (SetKind and SetAPIVersion) as well?

No valid serialized object has those set to empty values, so I don't think so

@k8s-github-robot
Copy link

Automatic merge from submit-queue (batch tested with PRs 67298, 67518, 67635, 67673). If you want to cherry-pick this change to another branch, please follow the instructions here.

@k8s-github-robot k8s-github-robot merged commit ce8a628 into kubernetes:master Aug 22, 2018
@sttts
Copy link
Contributor

sttts commented Aug 22, 2018

was there a way we were collecting dev-related release notes for client-go/apimachinery usage

@liggitt we don't have prow support. But we usually use dev-release-notes

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. area/apiserver area/custom-resources cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. 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.

None yet

6 participants