-
Notifications
You must be signed in to change notification settings - Fork 43
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
(finalizers): refactor #124
Merged
AdheipSingh
merged 3 commits into
datainfrahq:master
from
itamar-marom:refactor/finalizers
Jan 14, 2024
+341
−86
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package druid | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/datainfrahq/druid-operator/apis/druid/v1alpha1" | ||
appsv1 "k8s.io/api/apps/v1" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const ( | ||
deletePVCFinalizerName = "deletepvc.finalizers.druid.apache.org" | ||
) | ||
|
||
var ( | ||
defaultFinalizers []string | ||
) | ||
|
||
func updateFinalizers(ctx context.Context, sdk client.Client, m *v1alpha1.Druid, emitEvents EventEmitter) error { | ||
desiredFinalizers := m.GetFinalizers() | ||
additionFinalizers := defaultFinalizers | ||
|
||
desiredFinalizers = RemoveString(desiredFinalizers, deletePVCFinalizerName) | ||
if !m.Spec.DisablePVCDeletionFinalizer { | ||
additionFinalizers = append(additionFinalizers, deletePVCFinalizerName) | ||
} | ||
|
||
for _, finalizer := range additionFinalizers { | ||
if !ContainsString(desiredFinalizers, finalizer) { | ||
desiredFinalizers = append(desiredFinalizers, finalizer) | ||
} | ||
} | ||
|
||
m.SetFinalizers(desiredFinalizers) | ||
_, err := writers.Update(ctx, sdk, m, m, emitEvents) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func executeFinalizers(ctx context.Context, sdk client.Client, m *v1alpha1.Druid, emitEvents EventEmitter) error { | ||
if m.Spec.DisablePVCDeletionFinalizer == false { | ||
if err := executePVCFinalizer(ctx, sdk, m, emitEvents); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
/* | ||
executePVCFinalizer will execute a PVC deletion of all Druid's PVCs. | ||
Flow: | ||
1. Get sts List and PVC List | ||
2. Range and Delete sts first and then delete pvc. PVC must be deleted after sts termination has been executed | ||
else pvc finalizer shall block deletion since a pod/sts is referencing it. | ||
3. Once delete is executed we block program and return. | ||
*/ | ||
func executePVCFinalizer(ctx context.Context, sdk client.Client, m *v1alpha1.Druid, emitEvents EventEmitter) error { | ||
if ContainsString(m.ObjectMeta.Finalizers, deletePVCFinalizerName) { | ||
pvcLabels := map[string]string{ | ||
"druid_cr": m.Name, | ||
} | ||
|
||
pvcList, err := readers.List(ctx, sdk, m, pvcLabels, emitEvents, func() objectList { return &v1.PersistentVolumeClaimList{} }, func(listObj runtime.Object) []object { | ||
items := listObj.(*v1.PersistentVolumeClaimList).Items | ||
result := make([]object, len(items)) | ||
for i := 0; i < len(items); i++ { | ||
result[i] = &items[i] | ||
} | ||
return result | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
stsList, err := readers.List(ctx, sdk, m, makeLabelsForDruid(m.Name), emitEvents, func() objectList { return &appsv1.StatefulSetList{} }, func(listObj runtime.Object) []object { | ||
items := listObj.(*appsv1.StatefulSetList).Items | ||
result := make([]object, len(items)) | ||
for i := 0; i < len(items); i++ { | ||
result[i] = &items[i] | ||
} | ||
return result | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msg := fmt.Sprintf("Trigerring finalizer for CR [%s] in namespace [%s]", m.Name, m.Namespace) | ||
// sendEvent(sdk, m, v1.EventTypeNormal, DruidFinalizer, msg) | ||
logger.Info(msg) | ||
if err := deleteSTSAndPVC(ctx, sdk, m, stsList, pvcList, emitEvents); err != nil { | ||
return err | ||
} else { | ||
msg := fmt.Sprintf("Finalizer success for CR [%s] in namespace [%s]", m.Name, m.Namespace) | ||
// sendEvent(sdk, m, v1.EventTypeNormal, DruidFinalizerSuccess, msg) | ||
logger.Info(msg) | ||
AdheipSingh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// remove our finalizer from the list and update it. | ||
m.ObjectMeta.Finalizers = RemoveString(m.ObjectMeta.Finalizers, deletePVCFinalizerName) | ||
|
||
_, err = writers.Update(ctx, sdk, m, m, emitEvents) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package druid | ||
|
||
import ( | ||
"time" | ||
|
||
druidv1alpha1 "github.com/datainfrahq/druid-operator/apis/druid/v1alpha1" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
// +kubebuilder:docs-gen:collapse=Imports | ||
|
||
/* | ||
finalizers_test | ||
*/ | ||
var _ = Describe("Test finalizers logic", func() { | ||
const ( | ||
filePath = "testdata/finalizers.yaml" | ||
timeout = time.Second * 45 | ||
interval = time.Millisecond * 250 | ||
) | ||
|
||
var ( | ||
druid = &druidv1alpha1.Druid{} | ||
) | ||
|
||
Context("When creating a druid cluster", func() { | ||
It("Should create the druid object", func() { | ||
By("Creating a new druid") | ||
druidCR, err := readDruidClusterSpecFromFile(filePath) | ||
Expect(err).Should(BeNil()) | ||
Expect(k8sClient.Create(ctx, druidCR)).To(Succeed()) | ||
|
||
By("Getting a newly created druid") | ||
Eventually(func() bool { | ||
err := k8sClient.Get(ctx, types.NamespacedName{Name: druidCR.Name, Namespace: druidCR.Namespace}, druid) | ||
return err == nil | ||
}, timeout, interval).Should(BeTrue()) | ||
}) | ||
It("Should add the delete PVC finalizer", func() { | ||
By("Waiting for the finalizer to be created") | ||
Eventually(func() bool { | ||
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, druid) | ||
if err == nil && ContainsString(druid.GetFinalizers(), deletePVCFinalizerName) { | ||
return true | ||
} | ||
return false | ||
}, timeout, interval).Should(BeTrue()) | ||
}) | ||
It("Should delete druid successfully", func() { | ||
By("Waiting for the druid cluster to be deleted") | ||
Expect(k8sClient.Delete(ctx, druid)).To(Succeed()) | ||
Eventually(func() bool { | ||
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, druid) | ||
return err != nil | ||
}, timeout, interval).Should(BeTrue()) | ||
}) | ||
}) | ||
|
||
Context("When creating a druid cluster with disablePVCDeletion", func() { | ||
It("Should create the druid object", func() { | ||
By("Creating a new druid") | ||
druidCR, err := readDruidClusterSpecFromFile(filePath) | ||
druidCR.Spec.DisablePVCDeletionFinalizer = true | ||
Expect(err).Should(BeNil()) | ||
Expect(k8sClient.Create(ctx, druidCR)).To(Succeed()) | ||
|
||
By("Getting a newly created druid") | ||
Eventually(func() bool { | ||
err := k8sClient.Get(ctx, types.NamespacedName{Name: druidCR.Name, Namespace: druidCR.Namespace}, druid) | ||
return err == nil | ||
}, timeout, interval).Should(BeTrue()) | ||
}) | ||
It("Should not add the delete PVC finalizer", func() { | ||
By("Call for the update finalizer function") | ||
Expect(updateFinalizers(ctx, k8sClient, druid, emitEvent)).Should(BeNil()) | ||
|
||
By("Getting a updated druid") | ||
Eventually(func() bool { | ||
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, druid) | ||
return err == nil | ||
}, timeout, interval).Should(BeTrue()) | ||
|
||
By("Checking the absence of the finalizer") | ||
Expect(ContainsString(druid.GetFinalizers(), deletePVCFinalizerName)).Should(BeFalse()) | ||
}) | ||
It("Should delete druid successfully", func() { | ||
Expect(k8sClient.Delete(ctx, druid)).To(Succeed()) | ||
Eventually(func() bool { | ||
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, druid) | ||
return err != nil | ||
}, timeout, interval).Should(BeTrue()) | ||
}) | ||
}) | ||
|
||
Context("When creating a druid cluster", func() { | ||
It("Should create the druid object", func() { | ||
By("Creating a new druid") | ||
druidCR, err := readDruidClusterSpecFromFile(filePath) | ||
Expect(err).Should(BeNil()) | ||
Expect(k8sClient.Create(ctx, druidCR)).To(Succeed()) | ||
|
||
By("Getting the CR") | ||
Eventually(func() bool { | ||
err := k8sClient.Get(ctx, types.NamespacedName{Name: druidCR.Name, Namespace: druidCR.Namespace}, druid) | ||
return err == nil | ||
}, timeout, interval).Should(BeTrue()) | ||
}) | ||
It("Should add the delete PVC finalizer", func() { | ||
By("Waiting for the finalizer to be created") | ||
Eventually(func() bool { | ||
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, druid) | ||
if err == nil && ContainsString(druid.GetFinalizers(), deletePVCFinalizerName) { | ||
return true | ||
} | ||
return false | ||
}, timeout, interval).Should(BeTrue()) | ||
}) | ||
It("Should remove the delete PVC finalizer", func() { | ||
By("Disabling the deletePVC finalizer") | ||
druid.Spec.DisablePVCDeletionFinalizer = true | ||
Expect(k8sClient.Update(ctx, druid)).To(BeNil()) | ||
By("Waiting for the finalizer to be deleted") | ||
Eventually(func() bool { | ||
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, druid) | ||
if err == nil && !ContainsString(druid.GetFinalizers(), deletePVCFinalizerName) { | ||
return true | ||
} | ||
return false | ||
}, timeout, interval).Should(BeTrue()) | ||
}) | ||
It("Should delete druid successfully", func() { | ||
Expect(k8sClient.Delete(ctx, druid)).To(Succeed()) | ||
Eventually(func() bool { | ||
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, druid) | ||
return err != nil | ||
}, timeout, interval).Should(BeTrue()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as we our refactoring, we can convert this to an event.