-
Notifications
You must be signed in to change notification settings - Fork 39
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ac90ec8
fix: delay removal of orphanPVC to avoid race condition
gurjotkaur20 ba70b59
fix: updated log messages
gurjotkaur20 7212552
fix: deletionTimeStamp label name changed and refactoring deleteOrpha…
gurjotkaur20 6d715fd
fix: removed else
gurjotkaur20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ import ( | |
"fmt" | ||
"regexp" | ||
"sort" | ||
"strconv" | ||
"time" | ||
|
||
autoscalev2 "k8s.io/api/autoscaling/v2" | ||
networkingv1 "k8s.io/api/networking/v1" | ||
|
@@ -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 | ||
|
||
if _, ok := pvc.GetLabels()["toBeDeleted"]; ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and the below should be consts |
||
deletionTS := pvc.GetLabels()["deletionTimestamp"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
} | ||
} | ||
} | ||
|
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
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.
This became way too big, we should refactor this, separating it into functions