diff --git a/main.go b/main.go index 354ba1ccb..900b121c7 100644 --- a/main.go +++ b/main.go @@ -119,9 +119,8 @@ func main() { } if err = (&redis.Reconciler{ - Client: mgr.GetClient(), - K8sClient: k8sclient, - Dk8sClient: dk8sClient, + Client: mgr.GetClient(), + K8sClient: k8sclient, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Redis") os.Exit(1) diff --git a/pkg/controllers/redis/redis_controller.go b/pkg/controllers/redis/redis_controller.go index 9fb9ae5d5..199483527 100644 --- a/pkg/controllers/redis/redis_controller.go +++ b/pkg/controllers/redis/redis_controller.go @@ -23,7 +23,6 @@ import ( redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" intctrlutil "github.com/OT-CONTAINER-KIT/redis-operator/pkg/controllerutil" "github.com/OT-CONTAINER-KIT/redis-operator/pkg/k8sutils" - "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -32,8 +31,7 @@ import ( // Reconciler reconciles a Redis object type Reconciler struct { client.Client - K8sClient kubernetes.Interface - Dk8sClient dynamic.Interface + K8sClient kubernetes.Interface } func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { @@ -44,7 +42,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu return intctrlutil.RequeueWithErrorChecking(ctx, err, "failed to get redis instance") } if instance.ObjectMeta.GetDeletionTimestamp() != nil { - if err = k8sutils.HandleRedisFinalizer(ctx, r.Client, r.K8sClient, instance); err != nil { + if err = k8sutils.HandleRedisFinalizer(ctx, r.Client, instance); err != nil { return intctrlutil.RequeueWithError(ctx, err, "failed to handle redis finalizer") } return intctrlutil.Reconciled() diff --git a/pkg/controllers/redis/redis_controller_suite_test.go b/pkg/controllers/redis/redis_controller_suite_test.go index 68a75c2a8..86cd0ad59 100644 --- a/pkg/controllers/redis/redis_controller_suite_test.go +++ b/pkg/controllers/redis/redis_controller_suite_test.go @@ -26,7 +26,6 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "github.com/onsi/gomega/gexec" - "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/scheme" ctrl "sigs.k8s.io/controller-runtime" @@ -96,13 +95,9 @@ var _ = BeforeSuite(func() { k8sClient, err := kubernetes.NewForConfig(cfg) Expect(err).ToNot(HaveOccurred()) - dk8sClient, err := dynamic.NewForConfig(cfg) - Expect(err).ToNot(HaveOccurred()) - err = (&Reconciler{ - Client: k8sManager.GetClient(), - K8sClient: k8sClient, - Dk8sClient: dk8sClient, + Client: k8sManager.GetClient(), + K8sClient: k8sClient, }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred()) diff --git a/pkg/controllers/rediscluster/rediscluster_controller.go b/pkg/controllers/rediscluster/rediscluster_controller.go index e06ccd48d..cba1f6291 100644 --- a/pkg/controllers/rediscluster/rediscluster_controller.go +++ b/pkg/controllers/rediscluster/rediscluster_controller.go @@ -55,7 +55,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu return intctrlutil.RequeueWithErrorChecking(ctx, err, "failed to get redis cluster instance") } if instance.ObjectMeta.GetDeletionTimestamp() != nil { - if err = k8sutils.HandleRedisClusterFinalizer(ctx, r.Client, r.K8sClient, instance); err != nil { + if err = k8sutils.HandleRedisClusterFinalizer(ctx, r.Client, instance); err != nil { return intctrlutil.RequeueWithError(ctx, err, "failed to handle redis cluster finalizer") } return intctrlutil.Reconciled() diff --git a/pkg/controllers/redisreplication/redisreplication_controller.go b/pkg/controllers/redisreplication/redisreplication_controller.go index b205468b3..bc53305f8 100644 --- a/pkg/controllers/redisreplication/redisreplication_controller.go +++ b/pkg/controllers/redisreplication/redisreplication_controller.go @@ -112,7 +112,7 @@ type reconciler struct { func (r *Reconciler) reconcileFinalizer(ctx context.Context, instance *redisv1beta2.RedisReplication) (ctrl.Result, error) { if k8sutils.IsDeleted(instance) { - if err := k8sutils.HandleRedisReplicationFinalizer(ctx, r.Client, r.K8sClient, instance); err != nil { + if err := k8sutils.HandleRedisReplicationFinalizer(ctx, r.Client, instance); err != nil { return intctrlutil.RequeueWithError(ctx, err, "") } return intctrlutil.Reconciled() diff --git a/pkg/k8sutils/finalizer.go b/pkg/k8sutils/finalizer.go index 6331fd319..e884af0ee 100644 --- a/pkg/k8sutils/finalizer.go +++ b/pkg/k8sutils/finalizer.go @@ -5,9 +5,9 @@ import ( "fmt" redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/kubernetes" "k8s.io/utils/env" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -22,11 +22,11 @@ const ( ) // HandleRedisFinalizer finalize resource if instance is marked to be deleted -func HandleRedisFinalizer(ctx context.Context, ctrlclient client.Client, k8sClient kubernetes.Interface, cr *redisv1beta2.Redis) error { +func HandleRedisFinalizer(ctx context.Context, ctrlclient client.Client, cr *redisv1beta2.Redis) error { if cr.GetDeletionTimestamp() != nil { if controllerutil.ContainsFinalizer(cr, RedisFinalizer) { if cr.Spec.Storage != nil && !cr.Spec.Storage.KeepAfterDelete { - if err := finalizeRedisPVC(ctx, k8sClient, cr); err != nil { + if err := finalizeRedisPVC(ctx, ctrlclient, cr); err != nil { return err } } @@ -41,11 +41,11 @@ func HandleRedisFinalizer(ctx context.Context, ctrlclient client.Client, k8sClie } // HandleRedisClusterFinalizer finalize resource if instance is marked to be deleted -func HandleRedisClusterFinalizer(ctx context.Context, ctrlclient client.Client, k8sClient kubernetes.Interface, cr *redisv1beta2.RedisCluster) error { +func HandleRedisClusterFinalizer(ctx context.Context, ctrlclient client.Client, cr *redisv1beta2.RedisCluster) error { if cr.GetDeletionTimestamp() != nil { if controllerutil.ContainsFinalizer(cr, RedisClusterFinalizer) { if cr.Spec.Storage != nil && !cr.Spec.Storage.KeepAfterDelete { - if err := finalizeRedisClusterPVC(ctx, k8sClient, cr); err != nil { + if err := finalizeRedisClusterPVC(ctx, ctrlclient, cr); err != nil { return err } } @@ -60,11 +60,11 @@ func HandleRedisClusterFinalizer(ctx context.Context, ctrlclient client.Client, } // Handle RedisReplicationFinalizer finalize resource if instance is marked to be deleted -func HandleRedisReplicationFinalizer(ctx context.Context, ctrlclient client.Client, k8sClient kubernetes.Interface, cr *redisv1beta2.RedisReplication) error { +func HandleRedisReplicationFinalizer(ctx context.Context, ctrlclient client.Client, cr *redisv1beta2.RedisReplication) error { if cr.GetDeletionTimestamp() != nil { if controllerutil.ContainsFinalizer(cr, RedisReplicationFinalizer) { if cr.Spec.Storage != nil && !cr.Spec.Storage.KeepAfterDelete { - if err := finalizeRedisReplicationPVC(ctx, k8sClient, cr); err != nil { + if err := finalizeRedisReplicationPVC(ctx, ctrlclient, cr); err != nil { return err } } @@ -102,10 +102,16 @@ func AddFinalizer(ctx context.Context, cr client.Object, finalizer string, cl cl } // finalizeRedisPVC delete PVC -func finalizeRedisPVC(ctx context.Context, client kubernetes.Interface, cr *redisv1beta2.Redis) error { +func finalizeRedisPVC(ctx context.Context, client client.Client, cr *redisv1beta2.Redis) error { pvcTemplateName := env.GetString(EnvOperatorSTSPVCTemplateName, cr.Name) PVCName := fmt.Sprintf("%s-%s-0", pvcTemplateName, cr.Name) - err := client.CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{}) + pvc := &corev1.PersistentVolumeClaim{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: cr.Namespace, + Name: PVCName, + }, + } + err := client.Delete(ctx, pvc) if err != nil && !errors.IsNotFound(err) { log.FromContext(ctx).Error(err, "Could not delete Persistent Volume Claim", "PVCName", PVCName) return err @@ -114,12 +120,18 @@ func finalizeRedisPVC(ctx context.Context, client kubernetes.Interface, cr *redi } // finalizeRedisClusterPVC delete PVCs -func finalizeRedisClusterPVC(ctx context.Context, client kubernetes.Interface, cr *redisv1beta2.RedisCluster) error { +func finalizeRedisClusterPVC(ctx context.Context, client client.Client, cr *redisv1beta2.RedisCluster) error { for _, role := range []string{"leader", "follower"} { for i := 0; i < int(cr.Spec.GetReplicaCounts(role)); i++ { pvcTemplateName := env.GetString(EnvOperatorSTSPVCTemplateName, cr.Name+"-"+role) PVCName := fmt.Sprintf("%s-%s-%s-%d", pvcTemplateName, cr.Name, role, i) - err := client.CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{}) + pvc := &corev1.PersistentVolumeClaim{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: cr.Namespace, + Name: PVCName, + }, + } + err := client.Delete(ctx, pvc) if err != nil && !errors.IsNotFound(err) { log.FromContext(ctx).Error(err, "Could not delete Persistent Volume Claim "+PVCName) return err @@ -128,7 +140,13 @@ func finalizeRedisClusterPVC(ctx context.Context, client kubernetes.Interface, c if cr.Spec.Storage.NodeConfVolume { for i := 0; i < int(cr.Spec.GetReplicaCounts(role)); i++ { PVCName := fmt.Sprintf("%s-%s-%s-%d", "node-conf", cr.Name, role, i) - err := client.CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{}) + pvc := &corev1.PersistentVolumeClaim{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: cr.Namespace, + Name: PVCName, + }, + } + err := client.Delete(ctx, pvc) if err != nil && !errors.IsNotFound(err) { log.FromContext(ctx).Error(err, "Could not delete Persistent Volume Claim "+PVCName) return err @@ -140,11 +158,17 @@ func finalizeRedisClusterPVC(ctx context.Context, client kubernetes.Interface, c } // finalizeRedisReplicationPVC delete PVCs -func finalizeRedisReplicationPVC(ctx context.Context, client kubernetes.Interface, cr *redisv1beta2.RedisReplication) error { +func finalizeRedisReplicationPVC(ctx context.Context, client client.Client, cr *redisv1beta2.RedisReplication) error { for i := 0; i < int(cr.Spec.GetReplicationCounts("replication")); i++ { pvcTemplateName := env.GetString(EnvOperatorSTSPVCTemplateName, cr.Name) PVCName := fmt.Sprintf("%s-%s-%d", pvcTemplateName, cr.Name, i) - err := client.CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{}) + pvc := &corev1.PersistentVolumeClaim{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: cr.Namespace, + Name: PVCName, + }, + } + err := client.Delete(ctx, pvc) if err != nil && !errors.IsNotFound(err) { log.FromContext(ctx).Error(err, "Could not delete Persistent Volume Claim "+PVCName) return err diff --git a/pkg/k8sutils/finalizer_test.go b/pkg/k8sutils/finalizer_test.go index 4f46d8f7f..9439982d9 100644 --- a/pkg/k8sutils/finalizer_test.go +++ b/pkg/k8sutils/finalizer_test.go @@ -145,7 +145,7 @@ func TestHandleRedisFinalizer(t *testing.T) { assert.NoError(t, err) } - err := HandleRedisFinalizer(context.TODO(), tc.mockClient, k8sClient, tc.cr) + err := HandleRedisFinalizer(context.TODO(), tc.mockClient, tc.cr) if tc.expectError { assert.Error(t, err) } else { @@ -276,7 +276,7 @@ func TestHandleRedisClusterFinalizer(t *testing.T) { } } - err := HandleRedisClusterFinalizer(context.TODO(), tc.mockClient, k8sClient, tc.cr) + err := HandleRedisClusterFinalizer(context.TODO(), tc.mockClient, tc.cr) if tc.expectError { assert.Error(t, err) } else { @@ -474,7 +474,7 @@ func TestHandleRedisReplicationFinalizer(t *testing.T) { } } - err := HandleRedisReplicationFinalizer(context.TODO(), tc.mockClient, k8sClient, tc.cr) + err := HandleRedisReplicationFinalizer(context.TODO(), tc.mockClient, tc.cr) if tc.expectError { assert.Error(t, err) } else { @@ -612,7 +612,13 @@ func TestFinalizeRedisPVC(t *testing.T) { assert.NoError(t, err) } - err := finalizeRedisPVC(context.TODO(), k8sClient, cr) + ctrlclient := &mockClient.MockClient{ + DeleteFn: func(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error { + return nil + }, + } + + err := finalizeRedisPVC(context.TODO(), ctrlclient, cr) if tc.expectError { assert.Error(t, err) assert.Equal(t, tc.errorExpected, err) @@ -695,7 +701,13 @@ func TestFinalizeRedisReplicationPVC(t *testing.T) { k8sClient = k8sClientFake.NewSimpleClientset() } - err := finalizeRedisReplicationPVC(context.TODO(), k8sClient, tc.redisReplication) + ctrlclient := &mockClient.MockClient{ + DeleteFn: func(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error { + return nil + }, + } + + err := finalizeRedisReplicationPVC(context.TODO(), ctrlclient, tc.redisReplication) if tc.expectError { assert.Error(t, err) } else { @@ -765,7 +777,13 @@ func TestFinalizeRedisClusterPVC(t *testing.T) { k8sClient = k8sClientFake.NewSimpleClientset() } - err := finalizeRedisClusterPVC(context.TODO(), k8sClient, tc.redisCluster) + ctrlclient := &mockClient.MockClient{ + DeleteFn: func(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error { + return nil + }, + } + + err := finalizeRedisClusterPVC(context.TODO(), ctrlclient, tc.redisCluster) if tc.expectError { assert.Error(t, err) } else {