Skip to content

Conversation

@jayesh-srivastava
Copy link
Member

What this PR does / why we need it:
When a K8s upgrade is performed on a Managed cluster, new nodes will come up with new UIDs. However, the MachinePool controller has an early return condition that only validates the count of NodeRefs but doesn't check if the UIDs are still valid. This leads to MachinePools retaining stale NodeRef UIDs after upgrades, causing UID mismatches that persist until manual intervention.
This PR adds UID validation logic before the early return condition.

  • A new name-to-node map with the name nodeNameMap is created.
  • We iterate over the mp.Status.NodeRefs and using the above map, get each each Node.
  • If the Node doesn't exists or the fetched Node's UID is not matching to the UID in NodeRef, we break and continue with further reconciliation. This will set the correct nodeRef in the Machine Pool.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #12388

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-area PR is missing an area label labels Jun 24, 2025
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. area/machine Issues or PRs related to machine lifecycle management labels Jun 24, 2025
@k8s-ci-robot
Copy link
Contributor

@jayesh-srivastava: The label(s) area/pool cannot be applied, because the repository doesn't have them.

In response to this:

/area machine pool

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-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/needs-area PR is missing an area label label Jun 24, 2025
@jayesh-srivastava
Copy link
Member Author

/area machinepool

@k8s-ci-robot k8s-ci-robot added the area/machinepool Issues or PRs related to machinepools label Jun 24, 2025
Copy link
Contributor

@mboersma mboersma left a comment

Choose a reason for hiding this comment

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

It seems reasonable to add this additional verification in general, but I wonder if this problem has been seen in providers other than CAPZ.

// Validate that the UIDs in NodeRefs are still valid
if s.nodeRefMap != nil {
// Create a name-to-node mapping for efficient lookup
nodeNameMap := make(map[string]*corev1.Node)
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
nodeNameMap := make(map[string]*corev1.Node)
nodeNameMap := make(map[string]*corev1.Node, len(s.nodeRefMap))

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 have addressed this suggestion.

@sbueringer
Copy link
Member

It seems reasonable to add this additional verification in general, but I wonder if this problem has been seen in providers other than CAPZ.

cc @richardcase @justinsb

@MadJlzz
Copy link

MadJlzz commented Jul 2, 2025

Is it possible that because of such mismatch, after a scale with kubectl downgrading the number of replicas from 3 to 2 instances - capi decides to drain and delete ALL of the nodes of the machine pool? This happened to me this morning. I am using the following versions

> kg bootstrapproviders,controlplaneproviders,coreproviders,infrastructureproviders -A
NAMESPACE                  NAME                                                  INSTALLEDVERSION   READY
kubeadm-bootstrap-system   bootstrapprovider.operator.cluster.x-k8s.io/kubeadm   v1.9.5             True

NAMESPACE                      NAME                                                     INSTALLEDVERSION   READY
kubeadm-control-plane-system   controlplaneprovider.operator.cluster.x-k8s.io/kubeadm   v1.9.5             True

NAMESPACE     NAME                                                 INSTALLEDVERSION   READY
capi-system   coreprovider.operator.cluster.x-k8s.io/cluster-api   v1.9.5             True

NAMESPACE     NAME                                                     INSTALLEDVERSION   READY
capz-system   infrastructureprovider.operator.cluster.x-k8s.io/azure   v1.17.4            True

@AndiDog
Copy link
Contributor

AndiDog commented Jul 15, 2025

It seems reasonable to add this additional verification in general, but I wonder if this problem has been seen in providers other than CAPZ.

cc @richardcase @justinsb

I haven't seen this with CAPA on recently upgraded EC2-only (non-EKS) clusters. But worker nodes typically roll on Kubernetes upgrade due to the changed machine image, and I guess then this wouldn't be an issue since it's not expected that any old nodes remain. I wouldn't know why existing Node objects would be recreated with CAPZ, setting a new UID? Any source/issue for that?

Copy link
Member

@richardcase richardcase left a comment

Choose a reason for hiding this comment

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

Looks good to me @jayesh-srivastava . Just the one comment/suggestion

foundNode, exists := nodeNameMap[nodeRef.Name]

// If node not found or UID doesn't match, mark as invalid
if !exists || foundNode.UID != nodeRef.UID {
Copy link
Member

Choose a reason for hiding this comment

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

It might be good to have a higher verbosity log entry if the node names are the same but the UIDs are different.

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 22, 2025
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Sep 21, 2025
Copy link
Contributor

@AndiDog AndiDog left a comment

Choose a reason for hiding this comment

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

LGTM

@sbueringer
Copy link
Member

/assign @richardcase @mboersma

lgtm from your side?

@richardcase
Copy link
Member

From my side:

/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 12, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: c7fa774464cd9a1993182fdfbafcc15966e27bec

@sbueringer sbueringer added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Nov 12, 2025
@sbueringer
Copy link
Member

/test pull-cluster-api-e2e-main-gke

@sbueringer
Copy link
Member

Thx!

/lgtm
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sbueringer

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 Nov 12, 2025
@k8s-ci-robot k8s-ci-robot merged commit 84e2bb6 into kubernetes-sigs:main Nov 12, 2025
21 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.12 milestone Nov 12, 2025
alicefr pushed a commit to alicefr/cluster-api that referenced this pull request Nov 14, 2025
…sigs#12392)

* Fix MachinePool nodeRef UID mismatch after K8s upgrade

* Add len when creating nodeNameMap

* Add logs
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/machine Issues or PRs related to machine lifecycle management area/machinepool Issues or PRs related to machinepools cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Node UID in MachinePool nodeRef mismatch post Kubernetes upgrade

7 participants