@@ -24,7 +24,6 @@ import (
2424
2525 "github.com/pkg/errors"
2626 corev1 "k8s.io/api/core/v1"
27- apierrors "k8s.io/apimachinery/pkg/api/errors"
2827 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2928 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3029 "k8s.io/apimachinery/pkg/runtime/schema"
@@ -143,7 +142,7 @@ func (r *Reconciler) reconcileNode(ctx context.Context, s *scope) (ctrl.Result,
143142 _ , nodeHadInterruptibleLabel := s .node .Labels [clusterv1 .InterruptibleLabel ]
144143
145144 // Reconcile node taints
146- if err := r .patchNode (ctx , remoteClient , s .node , nodeLabels , nodeAnnotations , machine ); err != nil {
145+ if err := r .patchNode (ctx , remoteClient , s .node , nodeLabels , nodeAnnotations , s . owningMachineSet , s . owningMachineDeployment ); err != nil {
147146 return ctrl.Result {}, errors .Wrapf (err , "failed to reconcile Node %s" , klog .KObj (s .node ))
148147 }
149148 if ! nodeHadInterruptibleLabel && interruptible {
@@ -246,7 +245,7 @@ func (r *Reconciler) getNode(ctx context.Context, c client.Reader, providerID st
246245
247246// PatchNode is required to workaround an issue on Node.Status.Address which is incorrectly annotated as patchStrategy=merge
248247// and this causes SSA patch to fail in case there are two addresses with the same key https://github.com/kubernetes-sigs/cluster-api/issues/8417
249- func (r * Reconciler ) patchNode (ctx context.Context , remoteClient client.Client , node * corev1.Node , newLabels , newAnnotations map [string ]string , m * clusterv1.Machine ) error {
248+ func (r * Reconciler ) patchNode (ctx context.Context , remoteClient client.Client , node * corev1.Node , newLabels , newAnnotations map [string ]string , ms * clusterv1.MachineSet , md * clusterv1. MachineDeployment ) error {
250249 newNode := node .DeepCopy ()
251250
252251 // Adds the annotations from the Machine.
@@ -336,20 +335,14 @@ func (r *Reconciler) patchNode(ctx context.Context, remoteClient client.Client,
336335 hasTaintChanges := taints .RemoveNodeTaint (newNode , clusterv1 .NodeUninitializedTaint )
337336
338337 // Set Taint to a node in an old MachineSet and unset Taint from a node in a new MachineSet
339- isOutdated , notFound , err := shouldNodeHaveOutdatedTaint (ctx , r . Client , m )
338+ isOutdated , err := shouldNodeHaveOutdatedTaint (ms , md )
340339 if err != nil {
341340 return errors .Wrapf (err , "failed to check if Node %s is outdated" , klog .KRef ("" , node .Name ))
342341 }
343-
344- // It is only possible to identify if we have to set or remove the NodeOutdatedRevisionTaint if shouldNodeHaveOutdatedTaint
345- // found all relevant objects.
346- // Example: when the MachineDeployment or Machineset can't be found due to a background deletion of objects.
347- if ! notFound {
348- if isOutdated {
349- hasTaintChanges = taints .EnsureNodeTaint (newNode , clusterv1 .NodeOutdatedRevisionTaint ) || hasTaintChanges
350- } else {
351- hasTaintChanges = taints .RemoveNodeTaint (newNode , clusterv1 .NodeOutdatedRevisionTaint ) || hasTaintChanges
352- }
342+ if isOutdated {
343+ hasTaintChanges = taints .EnsureNodeTaint (newNode , clusterv1 .NodeOutdatedRevisionTaint ) || hasTaintChanges
344+ } else {
345+ hasTaintChanges = taints .RemoveNodeTaint (newNode , clusterv1 .NodeOutdatedRevisionTaint ) || hasTaintChanges
353346 }
354347
355348 if ! hasAnnotationChanges && ! hasLabelChanges && ! hasTaintChanges {
@@ -362,47 +355,23 @@ func (r *Reconciler) patchNode(ctx context.Context, remoteClient client.Client,
362355// shouldNodeHaveOutdatedTaint tries to compare the revision of the owning MachineSet to the MachineDeployment.
363356// It returns notFound = true if the OwnerReference is not set or the APIServer returns NotFound for the MachineSet or MachineDeployment.
364357// Note: This three cases could happen during background deletion of objects.
365- func shouldNodeHaveOutdatedTaint (ctx context. Context , c client. Client , m * clusterv1.Machine ) (outdated bool , notFound bool , err error ) {
366- if _ , hasLabel := m . Labels [ clusterv1 . MachineDeploymentNameLabel ]; ! hasLabel {
367- return false , false , nil
358+ func shouldNodeHaveOutdatedTaint (ms * clusterv1. MachineSet , md * clusterv1.MachineDeployment ) (outdated bool , err error ) {
359+ if ms == nil || md == nil {
360+ return false , nil
368361 }
369362
370- // Resolve the MachineSet name via owner references because the label value
371- // could also be a hash.
372- objKey , notFound , err := getOwnerMachineSetObjectKey (m .ObjectMeta )
373- if err != nil || notFound {
374- return false , notFound , err
375- }
376- ms := & clusterv1.MachineSet {}
377- if err := c .Get (ctx , * objKey , ms ); err != nil {
378- if apierrors .IsNotFound (err ) {
379- return false , true , nil
380- }
381- return false , false , err
382- }
383- md := & clusterv1.MachineDeployment {}
384- objKey = & client.ObjectKey {
385- Namespace : m .Namespace ,
386- Name : m .Labels [clusterv1 .MachineDeploymentNameLabel ],
387- }
388- if err := c .Get (ctx , * objKey , md ); err != nil {
389- if apierrors .IsNotFound (err ) {
390- return false , true , nil
391- }
392- return false , false , err
393- }
394363 msRev , err := mdutil .Revision (ms )
395364 if err != nil {
396- return false , false , err
365+ return false , err
397366 }
398367 mdRev , err := mdutil .Revision (md )
399368 if err != nil {
400- return false , false , err
369+ return false , err
401370 }
402371 if msRev < mdRev {
403- return true , false , nil
372+ return true , nil
404373 }
405- return false , false , nil
374+ return false , nil
406375}
407376
408377func getOwnerMachineSetObjectKey (obj metav1.ObjectMeta ) (* client.ObjectKey , bool , error ) {
0 commit comments