Skip to content

Commit

Permalink
fix: allow correct indexing for status objects and run in integration…
Browse files Browse the repository at this point in the history
… tests

Signed-off-by: jakobmoellerdev <[email protected]>
  • Loading branch information
jakobmoellerdev committed Aug 1, 2023
1 parent ceef64c commit 0a735aa
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
11 changes: 10 additions & 1 deletion controllers/lvmcluster_controller_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("LVMCluster controller", func() {
Expand Down Expand Up @@ -189,7 +190,15 @@ var _ = Describe("LVMCluster controller", func() {

// delete lvmVolumeGroupNodeStatus as it should be deleted by vgmanager
// and if it is present lvmcluster reconciler takes it as vg is present on node
Expect(k8sClient.Delete(ctx, lvmVolumeGroupNodeStatusIn)).Should(Succeed())

// we will now remove the node which will cause the LVM cluster status to also lose that vg
Expect(k8sClient.Delete(ctx, nodeIn)).Should(Succeed())
// deletion of LVMCluster CR
Eventually(func() bool {
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(lvmVolumeGroupNodeStatusIn),
&lvmv1alpha1.LVMVolumeGroupNodeStatus{})
return errors.IsNotFound(err)
}, timeout, interval).Should(BeTrue())

// deletion of LVMCluster CR
Eventually(func() bool {
Expand Down
10 changes: 5 additions & 5 deletions controllers/node_removal_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ func (r *NodeRemovalController) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

logger.Info(fmt.Sprintf("node %s getting deleted, removing leftover NodeVolumeGroupStatus", req.NamespacedName))
logger.Info("node getting deleted, removing leftover LVMVolumeGroupNodeStatus")

vgNodeStatusList := &lvmv1alpha1.LVMVolumeGroupNodeStatusList{}
err := r.Client.List(ctx, vgNodeStatusList, client.MatchingFields{"metadata.name": node.GetName()})
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
}
if err != nil {
return ctrl.Result{}, fmt.Errorf("error retrieving fitting LVMVolumeGroupNodeStatus for Node %s: %w", node.GetName(), err)
}

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 updated: %w", err)
Expand Down
10 changes: 10 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ var _ = BeforeSuite(func() {
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

err = (&NodeRemovalController{
Client: k8sManager.GetClient(),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

err = k8sManager.GetFieldIndexer().IndexField(context.Background(), &lvmv1alpha1.LVMVolumeGroupNodeStatus{}, "metadata.name", func(object client.Object) []string {
return []string{object.GetName()}
})
Expect(err).ToNot(HaveOccurred(), "unable to create name index on LVMVolumeGroupNodeStatus")

go func() {
defer GinkgoRecover()
err = k8sManager.Start(ctx)
Expand Down
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
package main

import (
"context"
"flag"
"fmt"
"os"
"sigs.k8s.io/controller-runtime/pkg/client"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
Expand Down Expand Up @@ -113,6 +115,13 @@ func main() {
os.Exit(1)
}

if err = mgr.GetFieldIndexer().IndexField(context.Background(), &lvmv1alpha1.LVMVolumeGroupNodeStatus{}, "metadata.name", func(object client.Object) []string {
return []string{object.GetName()}
}); err != nil {
setupLog.Error(err, "unable to create name index on LVMVolumeGroupNodeStatus")
os.Exit(1)
}

if err = (&lvmv1alpha1.LVMCluster{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "LVMCluster")
os.Exit(1)
Expand Down

0 comments on commit 0a735aa

Please sign in to comment.