Skip to content

Conversation

Copy link

Copilot AI commented Jan 13, 2026

Adds soft node affinity constraints to complement the existing hard requiredDuringSchedulingIgnoredDuringExecution support. This enables prioritizing spot/preemptible nodes while falling back to regular nodes when unavailable.

Changes

Core implementation:

  • Extended NamespaceConfig with PreferredNodeSelectorTerms field (uses preferredNodeSelectorTerms in config for consistency with nodeSelectorTerms)
  • Added patch building logic (buildPreferredAffinityPath, buildPreferredAffinityPatch, buildPreferredAffinityInitPatch)
  • Updated buildPatch() to handle preferred affinity alongside required affinity
  • Modified config validation to accept at least one of: nodeSelectorTerms, preferredNodeSelectorTerms, or tolerations

Documentation:

  • Updated README with preferred affinity semantics (weights 1-100, soft scheduling)
  • Added example ConfigMap entries for preferred-only and combined required+preferred scenarios

Naming convention:

  • Field named preferredNodeSelectorTerms for consistency with existing nodeSelectorTerms field, maintaining backwards compatibility

Example Usage

Prefer spot nodes but allow fallback to any node:

testing-ns-preferred: |
  preferredNodeSelectorTerms:
    - weight: 100
      preference:
        matchExpressions:
        - key: spot
          operator: In
          values:
          - "true"

Require dedicated nodes, prefer spot subset:

testing-ns-combined: |
  nodeSelectorTerms:
    - matchExpressions:
      - key: dedicated
        operator: In
        values:
        - "true"
  preferredNodeSelectorTerms:
    - weight: 100
      preference:
        matchExpressions:
        - key: spot
          operator: In
          values:
          - "true"

The implementation follows the same JSON patch approach as existing required affinity support, ensuring consistency and compatibility.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add support for preferredDuringSchedulingIgnoredDuringExecution</issue_title>
<issue_description>Hey, putting in a request for this to be added as it would be nice to have. The use case I am looking at is specifically around spot node pools in Azure/AKS. I would like to be able to prioritize using the spot node pools, but fall back on regular/dedicated nodes if the spot node pools get evicted.</issue_description>

<agent_instructions>Add support for configuring preferredDuringSchedulingIgnoredDuringExecution alongside with the currently available requiredDuringSchedulingIgnoredDuringExecution. Documentation is available here: https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@idgenchev Hi @tadeha, didn't have much time to support the project recently but I'm planning to add this feature next month and the new release will come immediately after.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 13, 2026 17:22
Co-authored-by: idgenchev <1568180+idgenchev@users.noreply.github.com>
Co-authored-by: idgenchev <1568180+idgenchev@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for preferredDuringSchedulingIgnoredDuringExecution Add support for preferredDuringSchedulingIgnoredDuringExecution Jan 13, 2026
Copilot AI requested a review from idgenchev January 13, 2026 17:27
README.md Outdated
It is a replacement for the [PodNodeSelector](https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#podnodeselector) admission controller and it is useful when using a managed k8s control plane such as [GKE](https://cloud.google.com/kubernetes-engine) or [EKS](https://aws.amazon.com/eks) where you do not have the ability to enable additional admission controller plugins and the [PodNodeSelector](https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#podnodeselector) might not be available. The only admission controller plugin required to run the namespace-node-affinity mutating webhook is the `MutatingAdmissionWebhook` which is already enabled on most managed Kubernetes services such as [EKS](https://docs.aws.amazon.com/eks/latest/userguide/platform-versions.html).

It might still be useful on [AKS](https://azure.microsoft.com/en-gb/services/kubernetes-service/) where the [PodNodeSelector](https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#podnodeselector) admission controller is [readily available](https://docs.microsoft.com/en-us/azure/aks/faq#what-kubernetes-admission-controllers-does-aks-support-can-admission-controllers-be-added-or-removed) as using `namespace-node-affinity` allows a litte bit more flexibility than the node selector by allowing you to set node affinity (only `requiredDuringSchedulingIgnoredDuringExecution` is supported for now) for all pods in the namespace.
It might still be useful on [AKS](https://azure.microsoft.com/en-gb/services/kubernetes-service/) where the [PodNodeSelector](https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#podnodeselector) admission controller is [readily available](https://docs.microsoft.com/en-us/azure/aks/faq#what-kubernetes-admission-controllers-does-aks-support-can-admission-controllers-be-added-or-removed) as using `namespace-node-affinity` allows a litte bit more flexibility than the node selector by allowing you to set node affinity (both `requiredDuringSchedulingIgnoredDuringExecution` and `preferredDuringSchedulingIgnoredDuringExecution` are supported) for all pods in the namespace.
Copy link
Owner

Choose a reason for hiding this comment

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

No need to specify that requiredDuringSchedulingIgnoredDuringExecution and preferredDuringSchedulingIgnoredDuringExecution are supported anymore as these are the only two options.

Copy link
Author

Choose a reason for hiding this comment

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

Updated to remove the specific mention of the two types in commit 40b3f35.

operator: "Exists"
effect: "NoSchedule"
testing-ns-preferred: |
preferredDuringSchedulingIgnoredDuringExecution:
Copy link
Owner

Choose a reason for hiding this comment

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

Naming here becomes inconsistent and confusing. Something like preferredNodeSelectorTerms will work better as we already have nodeSelectorTerms and I'd like to keep that as is for backwards compatibility.

Copy link
Author

Choose a reason for hiding this comment

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

Renamed from preferredDuringSchedulingIgnoredDuringExecution to preferredNodeSelectorTerms in commit 40b3f35 for naming consistency.

Comment on lines 39 to 40
AddPreferredDuringScheduling = "/spec/affinity/nodeAffinity/preferredDuringSchedulingIgnoredDuringExecution"
AddToPreferredDuringScheduling = "/spec/affinity/nodeAffinity/preferredDuringSchedulingIgnoredDuringExecution/-"
Copy link
Owner

Choose a reason for hiding this comment

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

Same here, with the naming. Maybe something like AddPreferredNodeSelectorTerms will work better here.

Copy link
Author

Choose a reason for hiding this comment

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

Renamed constants to AddPreferredNodeSelectorTerms and AddToPreferredNodeSelectorTerms in commit 40b3f35.

Tolerations []corev1.Toleration `json:"tolerations"`
ExcludedLabels map[string]string `json:"excludedLabels"`
NodeSelectorTerms []corev1.NodeSelectorTerm `json:"nodeSelectorTerms"`
PreferredDuringSchedulingIgnoredDuringExecution []corev1.PreferredSchedulingTerm `json:"preferredDuringSchedulingIgnoredDuringExecution"`
Copy link
Owner

Choose a reason for hiding this comment

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

Here as well - PreferredNodeSelectorTerms will work better if the NodeSelectorTerms filed is kept as is for backwards compatibility.

Copy link
Author

Choose a reason for hiding this comment

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

Updated struct field to PreferredNodeSelectorTerms in commit 40b3f35 to maintain consistency with NodeSelectorTerms.

Co-authored-by: idgenchev <1568180+idgenchev@users.noreply.github.com>
@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 91.93548% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.65%. Comparing base (cfcfe8e) to head (40b3f35).

Files with missing lines Patch % Lines
injector/injector.go 91.93% 4 Missing and 1 partial ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #73      +/-   ##
==========================================
+ Coverage   83.27%   84.65%   +1.37%     
==========================================
  Files           3        3              
  Lines         305      365      +60     
==========================================
+ Hits          254      309      +55     
- Misses         48       52       +4     
- Partials        3        4       +1     
Flag Coverage Δ
unittests 84.65% <91.93%> (+1.37%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@idgenchev idgenchev marked this pull request as ready for review January 15, 2026 14:30
Copilot AI requested a review from idgenchev January 15, 2026 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for preferredDuringSchedulingIgnoredDuringExecution

3 participants