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

Useful helper functions for Unstructured #51940

Merged
merged 2 commits into from Nov 2, 2017

Conversation

ash2k
Copy link
Member

@ash2k ash2k commented Sep 5, 2017

Which issue this PR fixes:
Fixes #40790

Release note:

NONE

/kind feature
/sig api-machinery
/area client-libraries
/assign @sttts @liggitt

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. area/client-libraries labels Sep 5, 2017
@k8s-ci-robot
Copy link
Contributor

Hi @ash2k. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

I understand the commands that are listed here.

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. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Sep 5, 2017
@k8s-github-robot k8s-github-robot added the release-note-none Denotes a PR that doesn't merit a release note. label Sep 5, 2017
@sttts
Copy link
Contributor

sttts commented Sep 5, 2017

/ok-to-test

@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Sep 5, 2017
nested := getNestedField(obj, fields...)
// NestedInt64Pointer returns the *int64 value of a nested field.
// Returns nil if value is not found or is not an int64/*int64.
func NestedInt64Pointer(obj map[string]interface{}, fields ...string) *int64 {
Copy link
Contributor

Choose a reason for hiding this comment

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

why a pointer and not the value?

Copy link
Contributor

Choose a reason for hiding this comment

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

The other funcs return values or clones, never the original object or a reference to it.

// NestedString returns the string value of a nested field.
// Returns empty string if value is not found or is not a string.
func NestedString(obj map[string]interface{}, fields ...string) string {
if str, ok := NestedField(obj, fields...).(string); ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer to see errors in the result if the type is wrong.

Same for the other type-casts below.

// NestedStringSlice returns the []string value of a nested field.
// Returns nil if value is not found or is not a []interface{}.
func NestedStringSlice(obj map[string]interface{}, fields ...string) []string {
if m, ok := NestedField(obj, fields...).([]interface{}); ok {
strSlice := make([]string, 0, len(m))
for _, v := range m {
if str, ok := v.(string); ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer to return an error if the cast fails.

// NestedStringMap returns the map[string]string value of a nested field.
// Returns nil if value is not found or is not a map[string]interface{}.
func NestedStringMap(obj map[string]interface{}, fields ...string) map[string]string {
if m, ok := NestedField(obj, fields...).(map[string]interface{}); ok {
strMap := make(map[string]string, len(m))
for k, v := range m {
if str, ok := v.(string); ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

same here, I prefer an error in this case

m = m[field].(map[string]interface{})
for _, field := range fields[:len(fields)-1] {
if _, ok := m[field].(map[string]interface{}); !ok {
m[field] = make(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.

this is unexpected that everything of wrong type is overridden.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also here we need errors.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, I agree with you. This is how it is implemented right now, I just made it public. I'm happy to change it.
Do you want all of these functions to return an error? Maybe bool for getters?

Copy link
Contributor

Choose a reason for hiding this comment

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

bool is also fine, maybe even better.

@lavalamp
Copy link
Member

lavalamp commented Sep 7, 2017

/assign @krousey

@mbohlool mbohlool assigned sttts and liggitt and unassigned krousey Sep 7, 2017
@ash2k ash2k changed the title Useful helper functions for Unstructured [WIP] Useful helper functions for Unstructured Sep 18, 2017
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 18, 2017
@k8s-ci-robot k8s-ci-robot removed the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Sep 25, 2017
@ash2k
Copy link
Member Author

ash2k commented Oct 31, 2017

@sttts Would be great to get this merged into 1.9, PTAL.

return
}
ts, _ := timestamp.MarshalQueryParameter()
u.setNestedField(ts, "metadata", "deletionTimestamp")
}

func (u *Unstructured) GetDeletionGracePeriodSeconds() *int64 {
return getNestedInt64Pointer(u.Object, "metadata", "deletionGracePeriodSeconds")
val, ok := NestedInt64(u.Object, "metadata", "deletionGracePeriodSeconds")
Copy link
Contributor

Choose a reason for hiding this comment

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

related #54803

@sttts
Copy link
Contributor

sttts commented Nov 1, 2017

Can you squash commit 2, 3 and 5?

func setNestedField(obj map[string]interface{}, value interface{}, fields ...string) {
// SetNestedField sets the value of a nested field.
// Returns false if value cannot be set because one of the nesting levels is not a map[string]interface{}.
func SetNestedField(obj map[string]interface{}, value interface{}, fields ...string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wished we had a debugging/strict-test mode where value is type checked. We use(d) to write arbitrary values into objects (#54803) and the compiler cannot help us with type checks.

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 can send a follow up PR with a environment variable flag a-la

DefaultConverter = NewConverter(parseBool(os.Getenv("KUBE_PATCH_CONVERSION_DETECTOR")))
and
mutationDetectionEnabled, _ = strconv.ParseBool(os.Getenv("KUBE_CACHE_MUTATION_DETECTOR"))

Copy link
Contributor

Choose a reason for hiding this comment

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

Would be great to run unit and maybe even integration tests with this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Follow up in #55297

delete(m, fields[len(fields)-1])
}

func getNestedString(obj map[string]interface{}, fields ...string) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: nestedStringOrDefault with default string arg?

Copy link
Member Author

Choose a reason for hiding this comment

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

getNestedString() is used in lots of places and default is an empty string everywhere. That is why I kept it as it is. Also I didn't want to touch the name because it would've increased the PR size even further. I can send a follow-up PR with rename if needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense.

func setNestedSlice(obj map[string]interface{}, value []string, fields ...string) {
// SetNestedStringSlice sets the string slice value of a nested field.
// Returns false if value cannot be set because one of the nesting levels is not a map[string]interface{}.
func SetNestedStringSlice(obj map[string]interface{}, value []string, fields ...string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

For the setters the bool return value feels unnatural.

Copy link
Member

Choose a reason for hiding this comment

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

Does an explicit error here look better? Something like that in #55168 (comment)?

@sttts
Copy link
Contributor

sttts commented Nov 1, 2017

Having triaged issues in the old code recently, this PR is big step forward in cleaning up this area. Left a few comments, but I think we are not far from getting this in.

@liggitt @wojtek-t are you fine with the benchmarks? Any further concerns?

@ash2k
Copy link
Member Author

ash2k commented Nov 1, 2017

Rebased+squashed. I left first commit unchanged - it is just moving code around, no changes. Second one with all the changes.

@sttts
Copy link
Contributor

sttts commented Nov 1, 2017

Waiting for @liggitt @wojtek-t's ok performance-wise. Will do a final review pass then.

@ash2k
Copy link
Member Author

ash2k commented Nov 1, 2017

/retest

@deads2k
Copy link
Contributor

deads2k commented Nov 1, 2017

@liggitt @wojtek-t are you fine with the benchmarks? Any further concerns?

.08% change? I don't think they can really be against it.

@sttts
Copy link
Contributor

sttts commented Nov 1, 2017

@deads2k feel free to go through the code. More eyes help in this highly untyped and fragile area.

}

gvk := obj.GetObjectKind().GroupVersionKind()
if len(gvk.Kind) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Straight move? I'd expect unstructured objects to always have a gvk.Version too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I haven't touched this code. See 7aeaedd#diff-31522f2d01b79a28cf7d5adaa03d5b26L685

@@ -478,31 +272,47 @@ func (u *Unstructured) GetDeletionTimestamp() *metav1.Time {

func (u *Unstructured) SetDeletionTimestamp(timestamp *metav1.Time) {
if timestamp == nil {
u.setNestedField(nil, "metadata", "deletionTimestamp")
RemoveNestedField(u.Object, "metadata", "deletionTimestamp")
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd expect a similar remove on SetCreationTimestamp

Copy link
Contributor

Choose a reason for hiding this comment

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

Creation timestamp is not optional, at least the argument of the Go func is not. So at worst, we miss a feature here on the Go level.

@deads2k
Copy link
Contributor

deads2k commented Nov 1, 2017

@deads2k feel free to go through the code. More eyes help in this highly untyped and fragile area.

It lgtm, but this area of the code is full of weird gotchas. @sttts if you like it in the morning, I think it's fine.

/approve

@k8s-github-robot k8s-github-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 1, 2017
@sttts
Copy link
Contributor

sttts commented Nov 2, 2017

/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 2, 2017
@k8s-github-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ash2k, deads2k, sttts

Associated issue: 40790

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

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@k8s-github-robot
Copy link

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

@k8s-github-robot k8s-github-robot merged commit c3f3137 into kubernetes:master Nov 2, 2017
@ash2k ash2k deleted the unstructured-helpers branch November 2, 2017 09:56
@smarterclayton
Copy link
Contributor

These shouldn't be in this package - I'm moving them into runtime, because meta/v1/unstructured is only for "typed" unstructured (i.e., anything that obeys our metadata rules), not generic to unstructured.

@ash2k
Copy link
Member Author

ash2k commented Nov 20, 2017

That makes sense. Thanks for an update.

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/client-libraries cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. 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/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose/add useful methods on unstructured.Unstructured