Skip to content

Commit

Permalink
fix: remove owner references on internal nodes
Browse files Browse the repository at this point in the history
This patch removes the owner reference of the physical node from the
internal node, as this caused some issues with the tear down with
ArgoCD. As the physical node does not belong to the application, the
InternalNode (owned by it) and all the resources owned by it were not
removed. The garbage collection of the InternalNode resources is
performed by a controller instead.
  • Loading branch information
claudiolor committed Dec 9, 2024
1 parent 12b0c2c commit 21a7518
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/predicate"

networkingv1beta1 "github.com/liqotech/liqo/apis/networking/v1beta1"
Expand Down Expand Up @@ -103,7 +102,7 @@ func (r *NodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res c
}
internalNode.Spec.Interface.Node.IP = networkingv1beta1.IP(ip.String())

return controllerutil.SetControllerReference(node, internalNode, r.Scheme)
return nil
}); err != nil {
klog.Errorf("Unable to create or update InternalNode %q: %s", internalNode.Name, err)
return ctrl.Result{}, err
Expand All @@ -123,6 +122,7 @@ func (r *NodeReconciler) SetupWithManager(mgr ctrl.Manager) error {
}
return ctrl.NewControllerManagedBy(mgr).Named(consts.CtrlNode).
Owns(&networkingv1beta1.InternalNode{}).
// We need to reconcile only physical Nodes as we need to apply the networking rules for each of them.
For(&corev1.Node{}, builder.WithPredicates(predicate.Not(filterByLabelsPredicate))).
Complete(r)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -106,6 +108,21 @@ func (r *InternalNodeReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrl.Result{}, nil
}

// Check whether there is the corresponding Node for the given InternalNode
var ownerNode corev1.Node
err = r.Client.Get(ctx, types.NamespacedName{Name: req.Name}, &ownerNode)
switch {
case apierrors.IsNotFound(err):
// Delete the internal node as there is no corresponding node
klog.Infof("Deleting InternalNode %v as there is no corresponding Node resource", req.NamespacedName)
if err := r.Client.Delete(ctx, internalnode); err != nil {
return ctrl.Result{}, fmt.Errorf("unable to delete InternalNode %v: %w", req.NamespacedName, err)
}
return ctrl.Result{}, nil
case err != nil:
return ctrl.Result{}, fmt.Errorf("unable to get corresponding Node for InternalNode %v: %w", req.NamespacedName, err)
}

if !containsFinalizer {
if err = r.enforceInternalNodeFinalizerPresence(ctx, internalnode); err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -155,20 +172,29 @@ func (r *InternalNodeReconciler) SetupWithManager(mgr ctrl.Manager) error {
For(&networkingv1beta1.InternalNode{}).
Watches(&networkingv1beta1.Configuration{}, handler.EnqueueRequestsFromMapFunc(r.genericEnqueuerfunc)).
Watches(&ipamv1alpha1.IP{}, handler.EnqueueRequestsFromMapFunc(r.genericEnqueuerfunc)).
Watches(&corev1.Node{}, handler.EnqueueRequestsFromMapFunc(r.genericEnqueuerfunc)).
Complete(r)
}

func (r *InternalNodeReconciler) genericEnqueuerfunc(ctx context.Context, _ client.Object) []reconcile.Request {
func (r *InternalNodeReconciler) genericEnqueuerfunc(ctx context.Context, obj client.Object) []reconcile.Request {
internalNodes, err := getters.ListInternalNodesByLabels(ctx, r.Client, labels.Everything())
if err != nil {
klog.Error(err)
return nil
}

_, isNode := obj.(*corev1.Node)

var requests []reconcile.Request
for i := range internalNodes.Items {
requests = append(requests, reconcile.Request{
NamespacedName: client.ObjectKeyFromObject(&internalNodes.Items[i]),
})
iNode := &internalNodes.Items[i]

// When the object that triggered the watch is a Node, enqueue the event only for the InternalNode related to that Node.
if isNode && iNode.Name == obj.GetName() {
requests = append(requests, reconcile.Request{
NamespacedName: client.ObjectKeyFromObject(iNode),
})
}
}
return requests
}

0 comments on commit 21a7518

Please sign in to comment.