-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #372 from jakobmoellerdev/OCPBUGS-13558
OCPBUGS-13558: fix: ensure LVMVolumeGroupNodeStatus is removed by dedicated cleanup controller in case of multi-node
- Loading branch information
Showing
10 changed files
with
201 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,6 +146,8 @@ spec: | |
verbs: | ||
- get | ||
- list | ||
- patch | ||
- update | ||
- watch | ||
- apiGroups: | ||
- "" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,8 @@ rules: | |
verbs: | ||
- get | ||
- list | ||
- patch | ||
- update | ||
- watch | ||
- apiGroups: | ||
- "" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package node | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
const cleanupFinalizer = "lvm.topolvm.io/node-removal-hook" | ||
const fieldOwner = "lvms" | ||
|
||
type RemovalController struct { | ||
client.Client | ||
} | ||
|
||
//+kubebuilder:rbac:groups=core,resources=nodes,verbs=get;list;patch;update;watch | ||
//+kubebuilder:rbac:groups=lvm.topolvm.io,resources=lvmvolumegroupnodestatuses,verbs=get;list;watch;create;update;patch;delete | ||
//+kubebuilder:rbac:groups=lvm.topolvm.io,resources=lvmvolumegroupnodestatuses/status,verbs=get;update;patch | ||
//+kubebuilder:rbac:groups=lvm.topolvm.io,resources=lvmvolumegroupnodestatuses/finalizers,verbs=update | ||
|
||
// Reconcile takes care of watching a node, adding a finalizer, and reacting to a removal request by deleting | ||
// the unwanted LVMVolumeGroupNodeStatus that was associated with the node, before removing the finalizer. | ||
// It does nothing on active Nodes. If it can be assumed that there will always be only one node (SNO), | ||
// this controller should not be started. | ||
func (r *RemovalController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
logger := log.FromContext(ctx) | ||
|
||
node := &v1.Node{} | ||
if err := r.Get(ctx, req.NamespacedName, node); err != nil { | ||
// we'll ignore not-found errors, since they can't be fixed by an immediate | ||
// requeue (we'll need to wait for a new notification), and we can get them | ||
// on deleted requests. | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
||
if node.DeletionTimestamp.IsZero() { | ||
// Add a finalizer in case the node is fresh or the controller newly deployed | ||
if needsUpdate := controllerutil.AddFinalizer(node, cleanupFinalizer); needsUpdate { | ||
if err := r.Update(ctx, node, client.FieldOwner(fieldOwner)); err != nil { | ||
return ctrl.Result{}, fmt.Errorf("node finalizer could not be updated: %w", err) | ||
} | ||
} | ||
// nothing to do here, the node exists and is happy, | ||
// maybe there is a NodeVolumeGroupStatus but we don't care | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
logger.Info("node getting deleted, removing leftover LVMVolumeGroupNodeStatus") | ||
|
||
vgNodeStatusList := &lvmv1alpha1.LVMVolumeGroupNodeStatusList{} | ||
if err := r.Client.List(ctx, vgNodeStatusList, client.MatchingFields{"metadata.name": node.GetName()}); err != nil { | ||
return ctrl.Result{}, fmt.Errorf("error retrieving fitting LVMVolumeGroupNodeStatus for Node %s: %w", node.GetName(), err) | ||
} | ||
|
||
if len(vgNodeStatusList.Items) == 0 { | ||
logger.Info("LVMVolumeGroupNodeStatus already deleted") | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
for i := range vgNodeStatusList.Items { | ||
if err := r.Client.Delete(ctx, &vgNodeStatusList.Items[i]); err != nil { | ||
return ctrl.Result{}, fmt.Errorf("could not cleanup LVMVolumeGroupNodeStatus for Node %s: %w", node.GetName(), err) | ||
} | ||
} | ||
|
||
logger.Info("every LVMVolumeGroupNodeStatus for node was removed, removing finalizer to allow node removal") | ||
if needsUpdate := controllerutil.RemoveFinalizer(node, cleanupFinalizer); needsUpdate { | ||
if err := r.Update(ctx, node, client.FieldOwner(fieldOwner)); err != nil { | ||
return ctrl.Result{}, fmt.Errorf("node finalizer could not be removed: %w", err) | ||
} | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *RemovalController) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr).For(&v1.Node{}).Watches(&lvmv1alpha1.LVMVolumeGroupNodeStatus{}, | ||
handler.EnqueueRequestsFromMapFunc(r.getNodeForLVMVolumeGroupNodeStatus)).Complete(r) | ||
} | ||
|
||
func (r *RemovalController) getNodeForLVMVolumeGroupNodeStatus(ctx context.Context, object client.Object) []reconcile.Request { | ||
node := &v1.Node{} | ||
node.SetName(object.GetName()) | ||
|
||
err := r.Client.Get(ctx, client.ObjectKeyFromObject(node), node) | ||
if errors.IsNotFound(err) { | ||
return []reconcile.Request{} | ||
} | ||
|
||
if err != nil { | ||
log.FromContext(ctx).Error(err, "could not get Node for LVMVolumeGroupNodeStatus", "LVMVolumeGroupNodeStatus", object.GetName()) | ||
return []reconcile.Request{} | ||
} | ||
|
||
return []reconcile.Request{{NamespacedName: types.NamespacedName{Name: node.GetName()}}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cluster | ||
|
||
import ( | ||
"context" | ||
corev1 "k8s.io/api/core/v1" | ||
"os" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
type SNOCheck interface { | ||
IsSNO(ctx context.Context) bool | ||
} | ||
|
||
func NewMasterSNOCheck(clnt client.Client) SNOCheck { | ||
return &masterSNOCheck{clnt: clnt} | ||
} | ||
|
||
type masterSNOCheck struct { | ||
clnt client.Client | ||
} | ||
|
||
func (chk *masterSNOCheck) IsSNO(ctx context.Context) bool { | ||
logger := log.FromContext(ctx) | ||
nodes := &corev1.NodeList{} | ||
if err := chk.clnt.List(context.Background(), nodes, client.MatchingLabels{ | ||
ControlPlaneIDLabel: "", | ||
}); err != nil { | ||
logger.Error(err, "unable to retrieve nodes for SNO check with lease configuration") | ||
os.Exit(1) | ||
} | ||
return nodes.Items != nil && len(nodes.Items) == 1 | ||
} |