Skip to content

Commit

Permalink
Add finalizers
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Gupta <[email protected]>
  • Loading branch information
shubham-cmyk committed Nov 10, 2023
1 parent ae512de commit 7f9ae5d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 15 deletions.
21 changes: 6 additions & 15 deletions k8sutils/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strconv"

redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2"
mockLog "github.com/OT-CONTAINER-KIT/redis-operator/mocks/log"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -64,15 +63,14 @@ func HandleRedisClusterFinalizer(cr *redisv1beta2.RedisCluster, cl client.Client
}

// Handle RedisReplicationFinalizer finalize resource if instance is marked to be deleted
func HandleRedisReplicationFinalizer(cr *redisv1beta2.RedisReplication, cl client.Client) error {
logger := finalizerLogger(cr.Namespace, RedisReplicationFinalizer)
func HandleRedisReplicationFinalizer(ctrlclient client.Client, k8sClient kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.RedisReplication) error {
if cr.GetDeletionTimestamp() != nil {
if controllerutil.ContainsFinalizer(cr, RedisReplicationFinalizer) {
if err := finalizeRedisReplicationPVC(cr); err != nil {
if err := finalizeRedisReplicationPVC(k8sClient, logger, cr); err != nil {
return err
}
controllerutil.RemoveFinalizer(cr, RedisReplicationFinalizer)
if err := cl.Update(context.TODO(), cr); err != nil {
if err := ctrlclient.Update(context.TODO(), cr); err != nil {
logger.Error(err, "Could not remove finalizer "+RedisReplicationFinalizer)
return err
}
Expand Down Expand Up @@ -136,7 +134,7 @@ func AddRedisSentinelFinalizer(cr *redisv1beta2.RedisSentinel, cl client.Client)
}

// finalizeRedisPVC delete PVC
func finalizeRedisPVC(client kubernetes.Interface, logger mockLog.LoggerInterface, cr *redisv1beta2.Redis) error {
func finalizeRedisPVC(client kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.Redis) error {
PVCName := fmt.Sprintf("%s-%s-0", cr.Name, cr.Name)
err := client.CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
Expand Down Expand Up @@ -179,22 +177,15 @@ func finalizeRedisClusterPVC(cr *redisv1beta2.RedisCluster) error {
}

// finalizeRedisReplicationPVC delete PVCs
func finalizeRedisReplicationPVC(cr *redisv1beta2.RedisReplication) error {
logger := finalizerLogger(cr.Namespace, RedisReplicationFinalizer)
client, err := GenerateK8sClient(GenerateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return err
}
func finalizeRedisReplicationPVC(client kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.RedisReplication) error {
for i := 0; i < int(cr.Spec.GetReplicationCounts("replication")); i++ {
PVCName := cr.Name + "-" + cr.Name + "-" + strconv.Itoa(i)
PVCName := fmt.Sprintf("%s-%s-%d", cr.Name, cr.Name, i)
err := client.CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
logger.Error(err, "Could not delete Persistent Volume Claim "+PVCName)
return err
}
}

return nil
}

Expand Down
85 changes: 85 additions & 0 deletions k8sutils/finalizers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sClientFake "k8s.io/client-go/kubernetes/fake"
"k8s.io/utils/pointer"
)

// func TestHandleRedisFinalizer(t *testing.T) {
Expand Down Expand Up @@ -120,3 +121,87 @@ func TestFinalizeRedisPVC(t *testing.T) {
})
}
}

func TestFinalizeRedisReplicationPVC(t *testing.T) {
tests := []struct {
name string
existingPVCs []*corev1.PersistentVolumeClaim
redisReplication *v1beta2.RedisReplication
expectError bool
}{
{
name: "Successful deletion of Redis Replication PVCs",
redisReplication: &v1beta2.RedisReplication{
ObjectMeta: metav1.ObjectMeta{
Name: "redis-replication",
Namespace: "redis",
},
Spec: v1beta2.RedisReplicationSpec{
Size: pointer.Int32(3),
},
},
existingPVCs: []*corev1.PersistentVolumeClaim{
{
ObjectMeta: metav1.ObjectMeta{
Name: "redis-replication-redis-replication-0",
Namespace: "redis",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "redis-replication-redis-replication-1",
Namespace: "redis",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "redis-replication-redis-replication-2",
Namespace: "redis",
},
},
},
expectError: false,
},
{
name: "PVC does not exist and no error should be returned",
existingPVCs: nil,
redisReplication: &v1beta2.RedisReplication{
ObjectMeta: metav1.ObjectMeta{
Name: "redis-replication",
Namespace: "redis",
},
Spec: v1beta2.RedisReplicationSpec{
Size: pointer.Int32(3),
},
},
expectError: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
logger := testr.New(t)
var k8sClient *k8sClientFake.Clientset
if tc.existingPVCs != nil {
k8sClient = k8sClientFake.NewSimpleClientset(tc.existingPVCs[0].DeepCopyObject(), tc.existingPVCs[1].DeepCopyObject(), tc.existingPVCs[2].DeepCopyObject())
} else {
k8sClient = k8sClientFake.NewSimpleClientset()
}

err := finalizeRedisReplicationPVC(k8sClient, logger, tc.redisReplication)
if tc.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}

// Verify PVCs are deleted
if !tc.expectError {
for _, pvc := range tc.existingPVCs {
_, err := k8sClient.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(context.TODO(), pvc.Name, metav1.GetOptions{})
assert.True(t, k8serrors.IsNotFound(err))
}
}
})
}
}

0 comments on commit 7f9ae5d

Please sign in to comment.