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

parse pod's node affinity once in preFilter #99213

Merged
merged 1 commit into from Feb 25, 2021

Conversation

AliceZhang2016
Copy link
Contributor

@AliceZhang2016 AliceZhang2016 commented Feb 18, 2021

What type of PR is this?

/kind feature

What this PR does / why we need it:

scheduler: parse Pod's Node affinity once in PreFilter phase

Which issue(s) this PR fixes:

Fixes #96164

Special notes for your reviewer:

Does this PR introduce a user-facing change?

The NodeAffinity plugin implements the PreFilter extension, offering enhanced performance for Filter.

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


@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 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. labels Feb 18, 2021
@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


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 cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. do-not-merge/needs-kind Indicates a PR lacks a `kind/foo` label and requires one. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Feb 18, 2021
@k8s-ci-robot
Copy link
Contributor

Welcome @AliceZhang2016!

It looks like this is your first PR to kubernetes/kubernetes 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kubernetes has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Feb 18, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @AliceZhang2016. 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 sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. sig/testing Categorizes an issue or PR as relevant to SIG Testing. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Feb 18, 2021
}

// Clone implements the mandatory Clone interface. We don't really copy the data since
// there is not need for that
Copy link
Member

Choose a reason for hiding this comment

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

We don't deep copy the data since values never get overridden

return false
}
return true
}

// nodeMatchesNodeSelector checks if a node's labels satisfy a list of node selector terms,
// terms are ORed, and an empty list of terms will match nothing.
func nodeMatchesNodeSelector(node *v1.Node, nodeSelector *v1.NodeSelector) bool {
func NodeMatchesNodeSelector(node *v1.Node, nodeSelector *v1.NodeSelector) bool {
Copy link
Member

Choose a reason for hiding this comment

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

rather, we should be able to get rid of this function now. And perhaps the entire file.

}
var affinity *v1.NodeAffinity
if pod.Spec.Affinity != nil {
affinity = pod.Spec.Affinity.NodeAffinity
Copy link
Member

Choose a reason for hiding this comment

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

use nodeaffinity.NewLazyErrorNodeSelector to store the parsed affinity.

and perhaps return a preScoreState object directly

@alculquicondor
Copy link
Member

/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 Feb 18, 2021
return s
}

// PreFilter builds and writes cycle state used by Filter
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
// PreFilter builds and writes cycle state used by Filter
// PreFilter builds and writes cycle state used by Filter.

Make comment as a sentence. Referring to Comment sentences

if s.requiredNodeSelector != nil {
if !s.requiredNodeSelector.Matches(labels.Set(node.Labels)) {
flagFilter = false
}
Copy link
Contributor

Choose a reason for hiding this comment

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

use ./hack/update-gofmt.sh to format the files.

}
}
if s.requiredNodeAffinity != nil {
if s.requiredNodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution != nil && !pluginhelper.NodeMatchesNodeSelector(node, s.requiredNodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: split the line.

if err != nil {
// fallback to calculate requiredNodeSelector and requiredNodeAffinity
// here when PreFilter is disabled
requiredNodeSelector, requiredNodeAffinity, err := getPodRequiredNodeSelectorAndAffinity(pod)
Copy link
Contributor

Choose a reason for hiding this comment

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

	requiredNodeSelector, requiredNodeAffinity, err := getPodRequiredNodeSelectorAndAffinity(pod)
	if err != nil {
		return framework.AsStatus(err)
	}
	state := &preFilterState{
		requiredNodeSelector: requiredNodeSelector,
		requiredNodeAffinity: requiredNodeAffinity,
	}

abstract a func to reduce duplicated code?

@@ -68,9 +97,39 @@ func (pl *NodeAffinity) Filter(ctx context.Context, state *framework.CycleState,
if pl.addedNodeSelector != nil && !pl.addedNodeSelector.Match(node) {
return framework.NewStatus(framework.UnschedulableAndUnresolvable, errReasonEnforced)
}
/*
if !pluginhelper.PodMatchesNodeSelectorAndAffinityTerms(pod, node) {
Copy link
Contributor

Choose a reason for hiding this comment

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

There are several other components depending on pluginhelper.PodMatchesNodeSelectorAndAffinityTerms. This change duplicates similar logic with the function, is it possible that we refactor helper functions to still share codes with other components?

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. do-not-merge/contains-merge-commits Indicates a PR which contains merge commits. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Feb 23, 2021
}
if flagFilter {
if s.requiredNodeAffinity != nil {
flagFilter, _ = s.requiredNodeAffinity.Match(node)
Copy link
Member

Choose a reason for hiding this comment

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

Add a comment for the ignored error:

// Ignoring parsing errors for backwards compatibility.

}

var flagFilter bool = true
if s != nil {
Copy link
Member

Choose a reason for hiding this comment

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

this check is unnecessary.

pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution != nil {
affinity = nodeaffinity.NewLazyErrorNodeSelector(pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution)
}
return &preFilterState{requiredNodeSelector: selector, requiredNodeAffinity: affinity}, nil
Copy link
Member

Choose a reason for hiding this comment

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

remove the error returned, as it is always nil

if pod.Spec.Affinity != nil &&
pod.Spec.Affinity.NodeAffinity != nil &&
pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution != nil {
affinity = nodeaffinity.NewLazyErrorNodeSelector(pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution)
Copy link
Member

Choose a reason for hiding this comment

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

add a comment:

Using LazyErrorNodeSelector for backwards compatibility of parsing errors

if s != nil {
if s.requiredNodeSelector != nil {
if !s.requiredNodeSelector.Matches(labels.Set(node.Labels)) {
flagFilter = false
Copy link
Member

Choose a reason for hiding this comment

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

it's simpler if you just return UnschedulableAndUnresolvable here.

labels: map[string]string{
"GPU": "NVIDIA-GRID-K1",
},
name: "Matches node affinity correctly even if PreFilter is not called",
Copy link
Member

Choose a reason for hiding this comment

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

Let's move the name of the test to the beginning of the struct. Please fix the existing tests too.

"baz": "blah",
},
name: "Matches node selector correctly even if PreFilter is not called",
disablePreFilter: true,
Copy link
Member

Choose a reason for hiding this comment

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

it looks like this variable is not being used.

cycleState.Write(preFilterStateKey, state)
return nil
}

// PreFilterExtensions does not exist for this plugin.
Copy link
Member

Choose a reason for hiding this comment

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

// PreFilterExtensions not necessary for this plugin as state doesn't depend on nodes

name: "no selector",
pod: &v1.Pod{},

Copy link
Member

Choose a reason for hiding this comment

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

remove empty line

state := framework.NewCycleState()
var gotStatus *framework.Status
if !test.disablePreFilter {
_ = p.(framework.PreFilterPlugin).PreFilter(context.Background(), state, test.pod)
Copy link
Member

Choose a reason for hiding this comment

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

don't discard the error. Call t.Errorf

@AliceZhang2016
Copy link
Contributor Author

I signed it

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. release-note-none Denotes a PR that doesn't merit a release note. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Feb 24, 2021
@k8s-ci-robot k8s-ci-robot removed the sig/testing Categorizes an issue or PR as relevant to SIG Testing. label Feb 24, 2021
@k8s-ci-robot k8s-ci-robot added area/test sig/testing Categorizes an issue or PR as relevant to SIG Testing. labels Feb 25, 2021
@BenTheElder
Copy link
Member

/retest
[pull-kubernetes-unit: new job, not yet blocking, fixed the config, nothing to see here!]

@AliceZhang2016
Copy link
Contributor Author

/test pull-kubernetes-e2e-kind

@AliceZhang2016
Copy link
Contributor Author

/test pull-kubernetes-e2e-gce-ubuntu-containerd

@AliceZhang2016
Copy link
Contributor Author

/kind feature

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. and removed do-not-merge/needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Feb 25, 2021
@BenTheElder
Copy link
Member

/retest

@alculquicondor
Copy link
Member

/remove-area test
/remove-sig testing

@k8s-ci-robot k8s-ci-robot removed area/test sig/testing Categorizes an issue or PR as relevant to SIG Testing. labels Feb 25, 2021
Copy link
Member

@alculquicondor alculquicondor left a comment

Choose a reason for hiding this comment

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

/lgtm
Good job

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

k8s-ci-robot commented Feb 25, 2021

@AliceZhang2016: The following tests failed, say /retest to rerun all failed tests:

Test name Commit Details Rerun command
pull-kubernetes-e2e-azure-disk-vmss 6c4d21ea75149bacf049244de56d50db7de9d11f link /test pull-kubernetes-e2e-azure-disk-vmss
pull-kubernetes-e2e-azure-file-windows 6c4d21ea75149bacf049244de56d50db7de9d11f link /test pull-kubernetes-e2e-azure-file-windows
pull-kubernetes-e2e-gce-storage-slow 6c4d21ea75149bacf049244de56d50db7de9d11f link /test pull-kubernetes-e2e-gce-storage-slow
pull-kubernetes-e2e-azure-disk-windows 6c4d21ea75149bacf049244de56d50db7de9d11f link /test pull-kubernetes-e2e-azure-disk-windows
pull-kubernetes-e2e-gce-csi-serial 6c4d21ea75149bacf049244de56d50db7de9d11f link /test pull-kubernetes-e2e-gce-csi-serial
pull-kubernetes-e2e-gci-gce-ipvs 6c4d21ea75149bacf049244de56d50db7de9d11f link /test pull-kubernetes-e2e-gci-gce-ipvs
pull-kubernetes-e2e-azure-file 6c4d21ea75149bacf049244de56d50db7de9d11f link /test pull-kubernetes-e2e-azure-file
pull-kubernetes-e2e-aks-engine-azure 6c4d21ea75149bacf049244de56d50db7de9d11f link /test pull-kubernetes-e2e-aks-engine-azure
pull-kubernetes-e2e-gce-storage-snapshot 6c4d21ea75149bacf049244de56d50db7de9d11f link /test pull-kubernetes-e2e-gce-storage-snapshot
pull-kubernetes-e2e-azure-disk 6c4d21ea75149bacf049244de56d50db7de9d11f link /test pull-kubernetes-e2e-azure-disk
pull-kubernetes-conformance-image-test 6c4d21ea75149bacf049244de56d50db7de9d11f link /test pull-kubernetes-conformance-image-test
pull-kubernetes-e2e-ubuntu-gce-network-policies 6c4d21ea75149bacf049244de56d50db7de9d11f link /test pull-kubernetes-e2e-ubuntu-gce-network-policies
pull-kubernetes-e2e-azure-file-windows-containerd 6c4d21ea75149bacf049244de56d50db7de9d11f link /test pull-kubernetes-e2e-azure-file-windows-containerd
pull-kubernetes-e2e-gce-alpha-features 6c4d21ea75149bacf049244de56d50db7de9d11f link /test pull-kubernetes-e2e-gce-alpha-features

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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.

@alculquicondor
Copy link
Member

/hold
this requires a release note. Please update:

The NodeAffinity plugin implements the PreFilter extension, offering enhanced performance for Filter.

@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 Feb 25, 2021
@alculquicondor
Copy link
Member

/retest

@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 release-note-none Denotes a PR that doesn't merit a release note. labels Feb 25, 2021
@alculquicondor
Copy link
Member

/hold cancel

@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 Feb 25, 2021
@k8s-ci-robot k8s-ci-robot merged commit 4fefc85 into kubernetes:master Feb 25, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.21 milestone Feb 25, 2021
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/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. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/backlog Higher priority than priority/awaiting-more-evidence. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. 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.

Parse Pod's Node affinity once in PreFilter and PreScore
5 participants