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

Move sortPodsBasedOnPriority to pod util #322

Merged
merged 3 commits into from
Jun 22, 2020

Conversation

lixiang233
Copy link
Contributor

As #294 mentioned, we may need to sort pods in other strategies, so I moved this func to pod util.

@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 Jun 10, 2020
@k8s-ci-robot
Copy link
Contributor

Hi @lixiang233. Thanks for your PR.

I'm waiting for a kubernetes-sigs 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 cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 10, 2020
Comment on lines 33 to 57
var (
lowPriority = int32(0)
highPriority = int32(10000)
)

func setHighPriority(pod *v1.Pod) { pod.Spec.Priority = &highPriority }
func setLowPriority(pod *v1.Pod) { pod.Spec.Priority = &lowPriority }

func makeBestEffortPod(pod *v1.Pod) {
pod.Spec.Containers[0].Resources.Requests = nil
pod.Spec.Containers[0].Resources.Requests = nil
pod.Spec.Containers[0].Resources.Limits = nil
pod.Spec.Containers[0].Resources.Limits = nil
}

func makeBurstablePod(pod *v1.Pod) {
pod.Spec.Containers[0].Resources.Limits = nil
pod.Spec.Containers[0].Resources.Limits = nil
}

func makeGuaranteedPod(pod *v1.Pod) {
pod.Spec.Containers[0].Resources.Limits[v1.ResourceCPU] = pod.Spec.Containers[0].Resources.Requests[v1.ResourceCPU]
pod.Spec.Containers[0].Resources.Limits[v1.ResourceMemory] = pod.Spec.Containers[0].Resources.Requests[v1.ResourceMemory]
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I move these to test util? @ingvagabund @seanmalloy

Copy link
Member

Choose a reason for hiding this comment

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

@lixiang233 yes, in my opinion I think it would be good to have makeGuaranteedPod, makeBurstablePod and makeBestEffortPod in test/test_utils.go

@seanmalloy
Copy link
Member

/ok-to-test
/kind cleanup

@lixiang233 I think the code change look pretty good. I just have two questions.

Are you planning to update the RemovePodsViolatingInterPodAntiAffinity strategy to sort pods by priority in a follow up pull request?

You mentioned in #294 that the documentation in the Pod Evictions section of the README is not quite accurate because only the LowNodeUtilization strategy sorts pods. Can you update the README in this PR too?

@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. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jun 11, 2020
@lixiang233
Copy link
Contributor Author

Changed test util and README.

I'm willing to work on sorting pods in RemovePodsViolatingInterPodAntiAffinity strategy. I'll probably update it and create a PR in the next week. @seanmalloy

// SortPodsBasedOnPriorityLowToHigh sorts pods based on their priorities from low to high.
// If pods have same priorities, they will be sorted by QoS in the following order:
// BestEffort, Burstable, Guaranteed
func SortPodsBasedOnPriorityLowToHigh(pods []*v1.Pod) {
Copy link
Contributor

@ingvagabund ingvagabund Jun 12, 2020

Choose a reason for hiding this comment

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

What about to have:

func PodComparePriorityLowToHigh(i, j int) bool {
	if pods[i].Spec.Priority == nil && pods[j].Spec.Priority != nil {
		return true
	}
	if pods[j].Spec.Priority == nil && pods[i].Spec.Priority != nil {
		return false
	}
	if (pods[j].Spec.Priority == nil && pods[i].Spec.Priority == nil) || (*pods[i].Spec.Priority == *pods[j].Spec.Priority) {
		if IsBestEffortPod(pods[i]) {
			return true
		}
		if IsBurstablePod(pods[i]) && IsGuaranteedPod(pods[j]) {
			return true
		}
		return false
	}
	return *pods[i].Spec.Priority < *pods[j].Spec.Priority
}

instead and replace all invocations of SortPodsBasedOnPriorityLowToHigh with sort.Slice(pods, PodComparePriorityLowToHigh? So the implementation is minimal and the function can be used by other sort.Slice* functions. E.g. SliceIsSorted or SliceStable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This implementation looks like less func of QueueSortPlugin, as func in Slice(slice interface{}, less func(i, j int) only receives two int params, we may implement and use it like:

func PodComparePriorityLowToHigh(i, j pod) bool {...}

sort.Slice(pods, func(i, j int) bool {
        return PodComparePriorityLowToHigh(pod[i], pod[j])
}

That looks a little complex, besides I think pod util should provide a sort func and not only a less func, maybe we can provide both and refactor sort func like:

func PodComparePriorityLowToHigh(i, j pod) bool {...}

func SortPodsBasedOnPriorityLowToHigh(pods []*v1.Pod) {
        sort.Slice(pods, func(i, j int) bool {
                return PodComparePriorityLowToHigh(pod[i], pod[j])
        }
}

What do you think @ingvagabund ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry about that. Yeah, you are right. Too much lambda calculus in my head :) We can elaborate on that later once we have more callers of the same "less" implementation for sorting.

@@ -52,23 +50,6 @@ func setHighPriority(pod *v1.Pod) { pod.Spec.Priority = &highPriority }
func setLowPriority(pod *v1.Pod) { pod.Spec.Priority = &lowPriority }
func setNodeUnschedulable(node *v1.Node) { node.Spec.Unschedulable = true }
Copy link
Contributor

Choose a reason for hiding this comment

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

There's nothing specific to LowNodeUtilization strategy about these helper functions. You can move all of them under test/test_utils.go as well.

@lixiang233
Copy link
Contributor Author

Updated, I think low/high priority should be defined by user, so only provide SetPodPriority in testutil, I'll squash these 2 commits later if it's ok. @ingvagabund

@ingvagabund
Copy link
Contributor

@lixiang233 can you put all the helpers function moved to the same commit? Two commits are more preferable here: 1 for moving the helper functions, 1 for moving the sort function.

@lixiang233
Copy link
Contributor Author

Completed. @ingvagabund

@seanmalloy
Copy link
Member

/lgtm
/assign @ingvagabund @damemi

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 16, 2020
@ingvagabund
Copy link
Contributor

@lixiang233 just a nit. Can you switch order of the commits so the helpers are not copied into pkg/descheduler/pod/pods_test.go and then removed in the next? Othwerwise, lgtm,

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 16, 2020
@lixiang233
Copy link
Contributor Author

switched. @ingvagabund

@ingvagabund
Copy link
Contributor

@lixiang233 perfect, thanks!!!
/lgtm

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

@damemi damemi left a comment

Choose a reason for hiding this comment

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

There's nothing wrong with this change itself, but without applying the change to other strategies as mentioned in #294 this PR doesn't really do much.

I think moving sortPodsBasedOnPriority is just a part of a whole PR which would use that function in other strategies. So I would like to see the rest of #294 implemented here (for at least a couple strategies), so that we can see the whole outcome of this change (and what it is intended to provide). Moving the function alone doesn't necessitate its own PR

@lixiang233
Copy link
Contributor Author

I'm planning to work on RemovePodsViolatingInterPodAntiAffinity strategy and will commit recently. I think we can divide the whole process into two parts, one is for multi-pods-related-strategies the other is for single-pod-related-strategies because these two types of strategies use different sorting functions. So I prefer to complete the work of multi-pods-related-strategies in this PR and open another for future work of single-pod-related-strategies.

What do you think @damemi ?

@damemi
Copy link
Contributor

damemi commented Jun 16, 2020

@lixiang233 that sounds good to me, feel free to add the changes for multi-pods in a new commit. Thanks!

I definitely agree that we don't need to make too many changes at once, I just felt like we could have a little more here to show the ultimate goal.

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 19, 2020
@lixiang233
Copy link
Contributor Author

Changed RemovePodsViolatingInterPodAntiAffinity strategy and added a simple testcase for it. Please take a look @seanmalloy @ingvagabund @damemi

Copy link
Contributor

@damemi damemi left a comment

Choose a reason for hiding this comment

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

This looks good to me, I think the separate commits make sense here but if anyone prefers to squash I wouldn't mind that either.
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: damemi, lixiang233

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 Jun 22, 2020
@seanmalloy
Copy link
Member

This looks good to me. Travis CI still seems to be flaky, so the e2e tests did not run. Someone should run the e2e tests on their laptop before this is merged.

@seanmalloy
Copy link
Member

/lgtm

I was able to run the e2e tests on my laptop using k8s 1.18.2 ...

make test-e2e
ls: cannot access '_output/bin/golangci-lint': No such file or directory
./test/run-e2e-tests.sh
=== RUN   TestE2E
--- PASS: TestE2E (31.29s)
    e2e_test.go:261: Eviction of pods starting
    e2e_test.go:261: Eviction of pods starting
=== RUN   TestDeschedulingInterval
--- PASS: TestDeschedulingInterval (0.02s)
PASS
ok  	sigs.k8s.io/descheduler/test/e2e	31.509s

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 22, 2020
@k8s-ci-robot k8s-ci-robot merged commit c713537 into kubernetes-sigs:master Jun 22, 2020
@lixiang233 lixiang233 deleted the move_pod_sort branch June 23, 2020 01:00
briend pushed a commit to briend/descheduler that referenced this pull request Feb 11, 2022
Move sortPodsBasedOnPriority to pod util
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. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. 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. 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.

5 participants