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

Fixed sort-by not sorting Resources as expected #100435

Merged
merged 1 commit into from May 26, 2021

Conversation

lauchokyip
Copy link
Member

@lauchokyip lauchokyip commented Mar 21, 2021

What type of PR is this?

/kind bug

What this PR does / why we need it:

When using sort-by to sort Resources, output is sorted unexpectedly

Which issue(s) this PR fixes:

Fixes kubernetes/kubectl#1019

Special notes for your reviewer:

Does this PR introduce a user-facing change?

Support correct sorting for cpu, memory, storage, ephemeral-storage, hugepages, and attachable-volumes

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Mar 21, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @lauchokyip. 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.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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.

@k8s-ci-robot k8s-ci-robot added needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Mar 21, 2021
@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Mar 21, 2021
@lauchokyip
Copy link
Member Author

/cc @eddiezane

@rikatz
Copy link
Contributor

rikatz commented Mar 21, 2021

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 21, 2021
Copy link
Contributor

@rikatz rikatz left a comment

Choose a reason for hiding this comment

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

@lauchokyip thanks for your PR.

I've left some comments, PTAL :)

Comment on lines 753 to 771

// IsQuantity returns true if the reflect.Value is type Quantity
func IsQuantity(obj reflect.Value) bool {
objInterface := obj.Interface()
switch objInterface.(type) {
case apiresource.Quantity:
return true
default:
return false
}
}

// QuantityToString returns the string value of Quantity.
// IsQuantity needs to be called in advance to check if it's Quantity
func QuantityToString(obj reflect.Value) string {
objInterface := obj.Interface()
quantity := objInterface.(apiresource.Quantity)
return quantity.String()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm thinking if those two functions couldn't be simplified into one, as:

func GetQuantity(obj reflect.Value) reflect.Value {
	switch objInterface := obj.Interface(); objInterface.(type) {
	case apiresource.Quantity:
		objStr := objInterface.(apiresource.Quantity)
		return reflect.ValueOf(objStr.String())
	default:
		return obj
	}
}

And then, when it's called, just to a later iField = cmdutil.GetQuantity(iField) :)

@@ -95,6 +97,8 @@ func (s *SortingPrinter) sortObj(obj runtime.Object) error {
return meta.SetList(obj, objs)
}

// SortObjects sorts the runtime.Obejct based on filedInput and returns RuntimeSort that implements
Copy link
Contributor

Choose a reason for hiding this comment

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

typo here - Obejct/Object

@lauchokyip
Copy link
Member Author

Thank You @rikatz, your feedbacks are very helpful!

@lauchokyip
Copy link
Member Author

/retest

@rikatz
Copy link
Contributor

rikatz commented Apr 5, 2021

/lgtm

Thank you!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 5, 2021
@eddiezane
Copy link
Member

/hold cancel

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. release-note-none Denotes a PR that doesn't merit a release note. labels May 19, 2021
@eddiezane
Copy link
Member

Great job on this one! I think we all learned a ton 😅 .

/approve

For lgtm
/cc @KnVerey @rikatz

@k8s-ci-robot k8s-ci-robot requested a review from rikatz May 19, 2021 20:00
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: eddiezane, lauchokyip, rikatz

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 May 19, 2021
@pacoxu
Copy link
Member

pacoxu commented May 25, 2021

Will this support sort of ephemeral-storage?

@lauchokyip
Copy link
Member Author

lauchokyip commented May 25, 2021

Hi @pacoxu I believe it will because essentially the field pods.spec.containers.resources.limits.ephemeral-storage is the same as pods.spec.containers.resources.limits.cpu / pods.spec.containers.resources.limits.memory, and they are all Resources type

@eddiezane @KnVerey Do you think I should add more test cases for all of them

@pacoxu
Copy link
Member

pacoxu commented May 25, 2021

This change LGTM.
I think you can fix others(storage and ephemeral-storage) in a new PR(if needed).

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 25, 2021
@@ -95,6 +96,8 @@ func (s *SortingPrinter) sortObj(obj runtime.Object) error {
return meta.SetList(obj, objs)
}

// SortObjects sorts the runtime.Object based on filedInput and returns RuntimeSort that implements
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
// SortObjects sorts the runtime.Object based on filedInput and returns RuntimeSort that implements
// SortObjects sorts the runtime.Object based on fieldInput and returns RuntimeSort that implements

// check if it's a Quantity
itypeQuantity, err := resource.ParseQuantity(itype)
if err != nil {
return sortorder.NaturalLess(itype, jtype), nil
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 ignoring the error parsing jtypeQuanity below, I think we should return this default behavior if either one cannot be parsed to a quantity.

q, _ := resource.ParseQuantity(str)
return q
}
func createPodSpecResource(memReq, memLimit, cpuReq, cpuLimit string) corev1.PodSpec {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's pass t *testing.T as the first argument and use it to make the test fail if the setup is invalid. In other words, if the helper is given an invalid quantity, the test should fail, not ignore the parsing error and potentially not test what it is expected to. You can call t.Helper() to make it a proper test helper with better line information on failures.

Copy link
Member Author

Choose a reason for hiding this comment

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

Make sense!

@KnVerey
Copy link
Contributor

KnVerey commented May 25, 2021

Do you think I should add more test cases for all of them

No, I don't think it's necessary to add coverage for additional container.resources keys. I agree with you that the implementation is not sensitive to the quantity being at one key in resources vs another, and you already have coverage for two different ones.

@lauchokyip
Copy link
Member Author

@KnVerey , fixed, PTAL 👍🏼

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 25, 2021
Copy link
Contributor

@KnVerey KnVerey left a comment

Choose a reason for hiding this comment

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

Just a couple more little test quibbles, but after that I think this looks ready to go!

@@ -47,6 +48,61 @@ func encodeOrDie(obj runtime.Object) []byte {
}
return data
}
func createResource(str string) (resource.Quantity, error) {
q, err := resource.ParseQuantity(str)
Copy link
Contributor

Choose a reason for hiding this comment

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

This could just return resource.ParseQuantity(str)... so let's get rid of this function and inline the resource.ParseQuantity(str) calls.


return podSpec
}
func createUnstructuredPodResource(t *testing.T, memReq, memLimit, cpuReq, cpuLimit string) unstructured.Unstructured {
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason not to add t.Helper() here 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.

I just thought because that function doesnn't have t.Errorf() so it might not be needed 😕 will add that in

Copy link
Contributor

Choose a reason for hiding this comment

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

What t.Helper() does is let Go know that this is a test helper, so that it can skip it when printing line and file information, which makes error reporting easier to understand when a test fails. A pretty minor thing, but since the Testing instance already has to be passed in, might as well get this benefit. 🙂

@lauchokyip
Copy link
Member Author

lauchokyip commented May 26, 2021

@KnVerey fixed! ready for final look 😃

@KnVerey
Copy link
Contributor

KnVerey commented May 26, 2021

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 26, 2021
@k8s-ci-robot k8s-ci-robot merged commit 02d0878 into kubernetes:master May 26, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.22 milestone May 26, 2021
@lauchokyip lauchokyip deleted the fixSort branch June 2, 2021 18:17
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/kubectl area/test 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. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cli Categorizes an issue or PR as relevant to SIG CLI. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sort-by not sorting as expected
6 participants