Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use client.Client instead of K8sClient in finalizer. #1183

Merged
merged 2 commits into from
Dec 22, 2024
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
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@
}

if err = (&redis.Reconciler{
Client: mgr.GetClient(),
K8sClient: k8sclient,
Dk8sClient: dk8sClient,
Client: mgr.GetClient(),
K8sClient: k8sclient,

Check warning on line 123 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L122-L123

Added lines #L122 - L123 were not covered by tests
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Redis")
os.Exit(1)
Expand Down
6 changes: 2 additions & 4 deletions pkg/controllers/redis/redis_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -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()
Expand Down
9 changes: 2 additions & 7 deletions pkg/controllers/redis/redis_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())

Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/rediscluster/rediscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
62 changes: 43 additions & 19 deletions pkg/k8sutils/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"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"
Expand All @@ -22,16 +22,16 @@
)

// 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 {

Check warning on line 25 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L25

Added line #L25 was not covered by tests
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 {

Check warning on line 29 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L29

Added line #L29 was not covered by tests
return err
}
}
controllerutil.RemoveFinalizer(cr, RedisFinalizer)
if err := ctrlclient.Update(context.TODO(), cr); err != nil {
if err := ctrlclient.Update(ctx, cr); err != nil {

Check warning on line 34 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L34

Added line #L34 was not covered by tests
log.FromContext(ctx).Error(err, "Could not remove finalizer", "finalizer", RedisFinalizer)
return err
}
Expand All @@ -41,16 +41,16 @@
}

// 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 {

Check warning on line 44 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L44

Added line #L44 was not covered by tests
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 {

Check warning on line 48 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L48

Added line #L48 was not covered by tests
return err
}
}
controllerutil.RemoveFinalizer(cr, RedisClusterFinalizer)
if err := ctrlclient.Update(context.TODO(), cr); err != nil {
if err := ctrlclient.Update(ctx, cr); err != nil {

Check warning on line 53 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L53

Added line #L53 was not covered by tests
log.FromContext(ctx).Error(err, "Could not remove finalizer "+RedisClusterFinalizer)
return err
}
Expand All @@ -60,16 +60,16 @@
}

// 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 {

Check warning on line 63 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L63

Added line #L63 was not covered by tests
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 {

Check warning on line 67 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L67

Added line #L67 was not covered by tests
return err
}
}
controllerutil.RemoveFinalizer(cr, RedisReplicationFinalizer)
if err := ctrlclient.Update(context.TODO(), cr); err != nil {
if err := ctrlclient.Update(ctx, cr); err != nil {

Check warning on line 72 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L72

Added line #L72 was not covered by tests
log.FromContext(ctx).Error(err, "Could not remove finalizer "+RedisReplicationFinalizer)
return err
}
Expand All @@ -83,7 +83,7 @@
if cr.GetDeletionTimestamp() != nil {
if controllerutil.ContainsFinalizer(cr, RedisSentinelFinalizer) {
controllerutil.RemoveFinalizer(cr, RedisSentinelFinalizer)
if err := ctrlclient.Update(context.TODO(), cr); err != nil {
if err := ctrlclient.Update(ctx, cr); err != nil {

Check warning on line 86 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L86

Added line #L86 was not covered by tests
log.FromContext(ctx).Error(err, "Could not remove finalizer "+RedisSentinelFinalizer)
return err
}
Expand All @@ -96,16 +96,22 @@
func AddFinalizer(ctx context.Context, cr client.Object, finalizer string, cl client.Client) error {
if !controllerutil.ContainsFinalizer(cr, finalizer) {
controllerutil.AddFinalizer(cr, finalizer)
return cl.Update(context.TODO(), cr)
return cl.Update(ctx, cr)

Check warning on line 99 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L99

Added line #L99 was not covered by tests
}
return nil
}

// 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 {

Check warning on line 105 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L105

Added line #L105 was not covered by tests
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)

Check warning on line 114 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L108-L114

Added lines #L108 - L114 were not covered by tests
if err != nil && !errors.IsNotFound(err) {
log.FromContext(ctx).Error(err, "Could not delete Persistent Volume Claim", "PVCName", PVCName)
return err
Expand All @@ -114,12 +120,18 @@
}

// 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 {

Check warning on line 123 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L123

Added line #L123 was not covered by tests
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)

Check warning on line 134 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L128-L134

Added lines #L128 - L134 were not covered by tests
if err != nil && !errors.IsNotFound(err) {
log.FromContext(ctx).Error(err, "Could not delete Persistent Volume Claim "+PVCName)
return err
Expand All @@ -128,7 +140,13 @@
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)

Check warning on line 149 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L143-L149

Added lines #L143 - L149 were not covered by tests
if err != nil && !errors.IsNotFound(err) {
log.FromContext(ctx).Error(err, "Could not delete Persistent Volume Claim "+PVCName)
return err
Expand All @@ -140,11 +158,17 @@
}

// 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 {

Check warning on line 161 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L161

Added line #L161 was not covered by tests
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)

Check warning on line 171 in pkg/k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

pkg/k8sutils/finalizer.go#L165-L171

Added lines #L165 - L171 were not covered by tests
if err != nil && !errors.IsNotFound(err) {
log.FromContext(ctx).Error(err, "Could not delete Persistent Volume Claim "+PVCName)
return err
Expand Down
Loading
Loading