Skip to content

Commit

Permalink
Merge pull request #346 from iamabhishek-dubey/finalizer-fix
Browse files Browse the repository at this point in the history
[Add]Added code for statefulset finalizer
  • Loading branch information
sandy724 authored Oct 7, 2022
2 parents 415af77 + 59bcf0b commit ae19667
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions k8sutils/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func HandleRedisFinalizer(cr *redisv1beta1.Redis, cl client.Client) error {
if err := finalizeRedisPVC(cr); err != nil {
return err
}
if err := finalizeRedisStatefulSet(cr); err != nil {
return err
}
controllerutil.RemoveFinalizer(cr, RedisFinalizer)
if err := cl.Update(context.TODO(), cr); err != nil {
logger.Error(err, "Could not remove finalizer "+RedisFinalizer)
Expand All @@ -55,6 +58,9 @@ func HandleRedisClusterFinalizer(cr *redisv1beta1.RedisCluster, cl client.Client
if err := finalizeRedisClusterPVC(cr); err != nil {
return err
}
if err := finalizeRedisClusterStatefulSets(cr); err != nil {
return err
}
controllerutil.RemoveFinalizer(cr, RedisClusterFinalizer)
if err := cl.Update(context.TODO(), cr); err != nil {
logger.Error(err, "Could not remove finalizer "+RedisClusterFinalizer)
Expand Down Expand Up @@ -138,3 +144,27 @@ func finalizeRedisClusterPVC(cr *redisv1beta1.RedisCluster) error {
}
return nil
}

// finalizeRedisStatefulSet delete statefulset for Redis
func finalizeRedisStatefulSet(cr *redisv1beta1.Redis) error {
logger := finalizerLogger(cr.Namespace, RedisFinalizer)
err := generateK8sClient().AppsV1().StatefulSets(cr.Namespace).Delete(context.TODO(), cr.Name, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
logger.Error(err, "Could not delete StatefulSets "+cr.Name)
return err
}
return nil
}

// finalizeRedisClusterStatefulSets delete statefulset for Redis Cluster
func finalizeRedisClusterStatefulSets(cr *redisv1beta1.RedisCluster) error {
logger := finalizerLogger(cr.Namespace, RedisClusterFinalizer)
for _, sts := range []string{cr.Name + "-leader", cr.Name + "-follower"} {
err := generateK8sClient().AppsV1().StatefulSets(cr.Namespace).Delete(context.TODO(), sts, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
logger.Error(err, "Could not delete statefulset "+sts)
return err
}
}
return nil
}

0 comments on commit ae19667

Please sign in to comment.