Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions internal/controller/clientprofile_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
Expand Down Expand Up @@ -188,13 +189,24 @@ func (r *ClientProfileReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

func (r *ClientProfileReconcile) reconcile() error {
// Load the ClientProfile
if err := r.Get(r.ctx, client.ObjectKeyFromObject(&r.clientProfile), &r.clientProfile); err != nil {
if k8serrors.IsNotFound(err) {
r.log.Info("ClientProfile not found, ignoring")
return nil
}
r.log.Error(err, "Failed loading ClientProfile")
return err
}

if err := r.loadAndValidate(); err != nil {
return err
}

reconcileErr := r.reconcilePhases()

statusErr := r.Status().Update(r.ctx, &r.clientProfile)
// Removing the cleanup finalizer lets the ClientProfile be deleted, so there may be no status to update
statusErr := client.IgnoreNotFound(r.Status().Update(r.ctx, &r.clientProfile))
if statusErr != nil {
r.log.Error(statusErr, "Failed to update ClientProfile status.")
}
Expand Down Expand Up @@ -252,11 +264,6 @@ func (r *ClientProfileReconcile) reconcilePhases() error {
}

func (r *ClientProfileReconcile) loadAndValidate() error {
// Load the ClientProfile
if err := r.Get(r.ctx, client.ObjectKeyFromObject(&r.clientProfile), &r.clientProfile); err != nil {
r.log.Error(err, "Failed loading ClientProfile")
return err
}
r.cleanUp = r.clientProfile.DeletionTimestamp != nil

// Validate a pointer to a ceph cluster resource
Expand Down
43 changes: 43 additions & 0 deletions internal/controller/clientprofile_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -202,6 +203,48 @@ var _ = Describe("ClientProfile Controller with Fake Client", func() {
})
})

Context("When the ClientProfile does not exist", func() {
It("should reconcile successfully", func() {
_, err := reconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: "missing-profile",
Namespace: defaultNamespace,
},
})
Expect(err).NotTo(HaveOccurred())
})
})

Context("When ClientProfile is being deleted without referencing ClientProfileReplication", func() {
It("should remove the finalizer and reconcile successfully", func() {
deletingProfile := &csiv1.ClientProfile{
ObjectMeta: metav1.ObjectMeta{
Name: "deleting-profile",
Namespace: defaultNamespace,
Finalizers: []string{cleanupFinalizer},
},
Spec: csiv1.ClientProfileSpec{
CephConnectionRef: corev1.LocalObjectReference{
Name: testCephConnection.Name,
},
},
}
Expect(fakeClient.Create(ctx, deletingProfile)).To(Succeed())
Expect(fakeClient.Delete(ctx, deletingProfile)).To(Succeed())

_, err := reconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: deletingProfile.Name,
Namespace: deletingProfile.Namespace,
},
})
Expect(err).NotTo(HaveOccurred())

err = fakeClient.Get(ctx, client.ObjectKeyFromObject(deletingProfile), &csiv1.ClientProfile{})
Expect(errors.IsNotFound(err)).To(BeTrue())
})
})

Context("When ClientProfile is being deleted with referencing ClientProfileReplication", func() {
It("should fail to delete", func() {
// Create objects with ClientProfile marked for deletion
Expand Down
Loading