-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Use remote NodeRef in Machine and MachineSet controllers #1052
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import ( | |
"k8s.io/klog" | ||
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1" | ||
controllerError "sigs.k8s.io/cluster-api/pkg/controller/error" | ||
"sigs.k8s.io/cluster-api/pkg/controller/remote" | ||
"sigs.k8s.io/cluster-api/pkg/util" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
|
@@ -181,8 +182,8 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul | |
|
||
if m.Status.NodeRef != nil { | ||
klog.Infof("Deleting node %q for machine %q", m.Status.NodeRef.Name, m.Name) | ||
if err := r.deleteNode(ctx, m.Status.NodeRef.Name); err != nil { | ||
klog.Errorf("Error deleting node %q for machine %q", name, err) | ||
if err := r.deleteNode(ctx, cluster, m.Status.NodeRef.Name); err != nil && !apierrors.IsNotFound(err) { | ||
klog.Errorf("Error deleting node %q for machine %q: %v", m.Status.NodeRef.Name, name, err) | ||
return reconcile.Result{}, err | ||
} | ||
} | ||
|
@@ -253,6 +254,9 @@ func (r *ReconcileMachine) getCluster(ctx context.Context, machine *clusterv1.Ma | |
return cluster, nil | ||
} | ||
|
||
// isDeletedAllowed returns false if the Machine we're trying to delete is the | ||
// Machine hosting this controller. This method is meant to be functional | ||
// only when the controllers are running in the workload cluster. | ||
func (r *ReconcileMachine) isDeleteAllowed(machine *clusterv1.Machine) bool { | ||
if r.nodeName == "" || machine.Status.NodeRef == nil { | ||
return true | ||
|
@@ -274,15 +278,30 @@ func (r *ReconcileMachine) isDeleteAllowed(machine *clusterv1.Machine) bool { | |
return node.UID != machine.Status.NodeRef.UID | ||
} | ||
|
||
func (r *ReconcileMachine) deleteNode(ctx context.Context, name string) error { | ||
var node corev1.Node | ||
if err := r.Client.Get(ctx, client.ObjectKey{Name: name}, &node); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
klog.V(2).Infof("Node %q not found", name) | ||
return nil | ||
func (r *ReconcileMachine) deleteNode(ctx context.Context, cluster *clusterv1.Cluster, name string) error { | ||
if cluster == nil { | ||
// Try to retrieve the Node from the local cluster, if no Cluster reference is found. | ||
var node corev1.Node | ||
if err := r.Client.Get(ctx, client.ObjectKey{Name: name}, &node); err != nil { | ||
return err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like it could be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed this part, IsNotFound is checked from the caller |
||
} | ||
klog.Errorf("Failed to get node %q: %v", name, err) | ||
return err | ||
return r.Client.Delete(ctx, &node) | ||
} | ||
|
||
// Otherwise, proceed to get the remote cluster client and get the Node. | ||
remoteClient, err := remote.NewClusterClient(r.Client, cluster) | ||
if err != nil { | ||
klog.Errorf("Error creating a remote client for cluster %q while deleting Machine %q, won't retry: %v", | ||
cluster.Name, name, err) | ||
return nil | ||
} | ||
return r.Client.Delete(ctx, &node) | ||
|
||
corev1Remote, err := remoteClient.CoreV1() | ||
if err != nil { | ||
klog.Errorf("Error creating a remote client for cluster %q while deleting Machine %q, won't retry: %v", | ||
cluster.Name, name, err) | ||
return nil | ||
} | ||
|
||
return corev1Remote.Nodes().Delete(name, &metav1.DeleteOptions{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has potential to loop forever if the node doesn't exist already. This should also error check that if the object can't be found then it's a success, not a retryable error. Unless There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The caller does the check at the moment, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep! thanks |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah this was a little bit confusing to read through. could consider a comment here because a nil cluster clearly means something special is happening, but I don't know why a nil cluster implies the management cluster is the workload cluster
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some comments, in case Cluster is specified, we look for the KubeConfig secret