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

Delay removal of orphanPVC to avoid the removal of PVC in use #67

Merged
merged 4 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
65 changes: 60 additions & 5 deletions controllers/druid/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"fmt"
"regexp"
"sort"
"strconv"
"time"

autoscalev2 "k8s.io/api/autoscaling/v2"
networkingv1 "k8s.io/api/networking/v1"
Expand Down Expand Up @@ -490,12 +492,65 @@ func deleteOrphanPVC(ctx context.Context, sdk client.Client, drd *v1alpha1.Druid
for i, pvc := range pvcList {

if !ContainsString(mountedPVC, pvc.GetName()) {
err := writers.Delete(ctx, sdk, drd, pvcList[i], emitEvents, &client.DeleteAllOfOptions{})
if err != nil {
return err

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This became way too big, we should refactor this, separating it into functions

if _, ok := pvc.GetLabels()["toBeDeleted"]; ok {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the below should be consts

deletionTS := pvc.GetLabels()["deletionTimestamp"]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can change the label name as the same label is used by the finalizer.


deletionTimestamp, err := strconv.ParseInt(deletionTS, 10, 64)

if err != nil {
msg := fmt.Sprintf("Unable to parse label deletionTimestamp [%s:%s]", deletionTS, pvcList[i].GetName())
logger.Info(msg, "name", drd.Name, "namespace", drd.Namespace)
return err
}

timeNow := time.Now().Unix()
timeDiff := timeDifference(deletionTimestamp, timeNow)

if timeDiff >= int64(time.Second/time.Second)*60 {
// delete pvc
err = writers.Delete(ctx, sdk, drd, pvcList[i], emitEvents, &client.DeleteAllOfOptions{})
if err != nil {
return err
} else {
msg := fmt.Sprintf("Deleted orphaned pvc [%s:%s] successfully", pvcList[i].GetName(), drd.Namespace)
logger.Info(msg, "name", drd.Name, "namespace", drd.Namespace)
}
} else {
// wait for 60s
msg := fmt.Sprintf("pvc [%s:%s] marked to be deleted after %ds", pvcList[i].GetName(), drd.Namespace, timeDiff)
logger.Info(msg, "name", drd.Name, "namespace", drd.Namespace)
}
} else {
msg := fmt.Sprintf("Deleted orphaned pvc [%s:%s] successfully", pvcList[i].GetName(), drd.Namespace)
logger.Info(msg, "name", drd.Name, "namespace", drd.Namespace)
// set labels when pvc comes for deletion for the first time
getPvcLabels := pvc.GetLabels()
getPvcLabels["toBeDeleted"] = "yes"
getPvcLabels["deletionTimestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
pvc.SetLabels(getPvcLabels)

_, err = writers.Update(ctx, sdk, drd, pvc, emitEvents)
if err != nil {
return err
} else {
msg := fmt.Sprintf("marked pvc for deletion , added labels %s and %s successfully [%s]", "toBeDeleted", "deletionTimestamp", pvc.GetName())
logger.Info(msg, "name", drd.Name, "namespace", drd.Namespace)
}
}
} else {
// do not delete pvc
if _, ok := pvc.GetLabels()["toBeDeleted"]; ok {
getPvcLabels := pvc.GetLabels()
delete(getPvcLabels, "toBeDeleted")
delete(getPvcLabels, "deletionTimestamp")
pvc.SetLabels(getPvcLabels)

_, err = writers.Update(ctx, sdk, drd, pvc, emitEvents)
if err != nil {
return err
} else {
msg := fmt.Sprintf("unmarked pvc for deletion, removed labels %s and %s successfully in pvc [%s]", "toBeDeleted", "deletionTimestamp", pvc.GetName())
logger.Info(msg, "name", drd.Name, "namespace", drd.Namespace)
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions controllers/druid/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"strconv"
"strings"
"time"
)

func firstNonEmptyStr(s1 string, s2 string) string {
Expand Down Expand Up @@ -77,3 +78,12 @@ func Str2Int(s string) int {
}
return i
}

// to find the time difference between two epoch times
func timeDifference(epochTime1, epochTime2 int64) int64 {
t1 := time.Unix(epochTime1, 0)
t2 := time.Unix(epochTime2, 0)

diff := time.Duration(t2.Sub(t1))
return int64(diff.Seconds())
}