-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Pvc auto deletion logic #1514
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
Open
Alexj9837
wants to merge
13
commits into
main
Choose a base branch
from
pvc-auto-deletion-logic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f452f57
chore: making new branch (#1478)
Alexj9837 db3dc7a
reduced the history of successful jobs, updated deletion script
Alexj9837 12991da
Update helm/blueapi/templates/cronjob.yaml
Alexj9837 767b7a9
Update comment formatting in values.yaml
Alexj9837 329d1b8
Update podSecurityContext in values.yaml
Alexj9837 d48694f
Update values.yaml for pod security context and comments
Alexj9837 77b81a4
changes made to feedback given
Alexj9837 94e4e27
added docs for protected and timestamp
Alexj9837 7699ffe
Merge branch 'main' into pvc-auto-deletion-logic
Alexj9837 6a1bc44
Remove unnecessary newline in configmap.yaml
Alexj9837 5f79ad6
revert configmap changes
Alexj9837 54e6d99
adding wait to the deletion logic
Alexj9837 b06cd29
Modify podSecurityContext with fsGroup comment
Alexj9837 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,27 @@ | ||
| #!/bin/bash | ||
| set -eou pipefail | ||
| # Get all PVCs by running pods | ||
| ALL_PVCS=$(kubectl get pvc -n $RELEASE_NAMESPACE -o=jsonpath='{.items[*].metadata.name}' | tr ' ' '\n' | sort -u) | ||
| BLUEAPI_PVCS=$( echo $ALL_PVCS | tr ' ' '\n' | grep "^$RELEASE_FULLNAME-scratch-" || true) | ||
| NOW=$(date +%s) | ||
| #loop through all pvcs. | ||
| for pvc in $BLUEAPI_PVCS; do | ||
| #check if pvc has last-used annotation | ||
| #get last used annotation | ||
| LAST_USED=$(kubectl get pvc "$pvc" -n $RELEASE_NAMESPACE -o=jsonpath='{.metadata.annotations.last-used}') | ||
| #checking if its not null | ||
| if [ -n "$LAST_USED" ]; then | ||
| #check if last_used is older than 3 months | ||
| if [ $(($NOW - LAST_USED)) -gt 7884000 ]; then | ||
| #checking if the pvc is protected, if it is protected skip deletion | ||
| if [ "$(kubectl get pvc "$pvc" -n $RELEASE_NAMESPACE -o=jsonpath='{.metadata.annotations.protected}')" = "true" ]; then | ||
| echo " PVC $pvc is protected, skipping deletion" | ||
| continue | ||
| fi | ||
| #PVC has not been used for more than three months, delete it | ||
| kubectl delete pvc "$pvc" -n $RELEASE_NAMESPACE --wait=true | ||
| fi | ||
| else | ||
| echo " $pvc has no last-used annotation" | ||
| fi | ||
| done |
This file contains hidden or 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,11 @@ | ||
| #!/bin/bash | ||
| set -eou pipefail | ||
| # Get all PVCs currently mounted by running pods | ||
| MOUNTED_PVCS=$(kubectl get pods -n $RELEASE_NAMESPACE \ | ||
| -o=jsonpath='{.items[*].spec.volumes[*].persistentVolumeClaim.claimName}' | tr ' ' '\n' | sort -u) | ||
| BLUEAPI_PVCS=$( echo $MOUNTED_PVCS | tr ' ' '\n' | grep "^$RELEASE_FULLNAME-scratch-"|| true) | ||
| #loop through all the pvcs annotating ones thare are mounted | ||
| NOW=$(date +%s) | ||
| for pvc in $BLUEAPI_PVCS; do | ||
| kubectl annotate --overwrite pvc "$pvc" -n $RELEASE_NAMESPACE last-used="$NOW" | ||
| done |
This file contains hidden or 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,22 @@ | ||
| {{- if .Values.timeStampCron.enabled }} | ||
| apiVersion: v1 | ||
| kind: ConfigMap | ||
| metadata: | ||
| name : {{include "blueapi.fullname" . }}-pvc-stamper-script | ||
| data: | ||
| {{- $files := .Files }} | ||
| time-stamper.sh: |- | ||
| {{ $files.Get "files/scripts/time-stamper.sh" | indent 4 }} | ||
| --- | ||
| {{- end }} | ||
|
|
||
| {{- if .Values.pvcAutoDeletion.enabled }} | ||
| apiVersion: v1 | ||
| kind: ConfigMap | ||
| metadata: | ||
| name : {{include "blueapi.fullname" . }}-pvc-auto-deletion-script | ||
| data: | ||
| {{- $files := .Files }} | ||
| pvc-deletion.sh: |- | ||
| {{ $files.Get "files/scripts/pvc-deletion.sh" | indent 4 }} | ||
| {{- end }} |
This file contains hidden or 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,173 @@ | ||
| {{- if .Values.timeStampCron.enabled }} | ||
| apiVersion: v1 | ||
| kind: ServiceAccount | ||
| metadata: | ||
| name: {{ include "blueapi.fullname" . }}-last-used-stamper | ||
| namespace: {{ .Release.Namespace }} | ||
| automountServiceAccountToken: true | ||
| --- | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: Role | ||
| metadata: | ||
| name: {{ include "blueapi.fullname" . }}-last-used-stamper | ||
| namespace: {{ .Release.Namespace }} | ||
| rules: | ||
| - apiGroups: [""] | ||
| resources: ["pods", "persistentvolumeclaims"] | ||
| verbs: ["get", "list", "patch"] | ||
| --- | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: RoleBinding | ||
| metadata: | ||
| name: {{ include "blueapi.fullname" . }}-last-used-stamper | ||
| namespace: {{ .Release.Namespace }} | ||
| subjects: | ||
| - kind: ServiceAccount | ||
| name: {{ include "blueapi.fullname" . }}-last-used-stamper | ||
| namespace: {{ .Release.Namespace }} | ||
| roleRef: | ||
| kind: Role | ||
| name: {{ include "blueapi.fullname" . }}-last-used-stamper | ||
| apiGroup: rbac.authorization.k8s.io | ||
| --- | ||
| apiVersion: batch/v1 | ||
| kind: CronJob | ||
| metadata: | ||
| name: {{ include "blueapi.fullname" . }}-last-used-stamper | ||
| namespace: {{ .Release.Namespace }} | ||
| spec: | ||
| concurrencyPolicy: Forbid | ||
| successfulJobsHistoryLimit: 1 | ||
| failedJobsHistoryLimit: 1 | ||
| schedule: "@daily" | ||
|
|
||
| jobTemplate: | ||
| spec: | ||
| # amount of attempts of labeling a pvc | ||
| backoffLimit: 3 | ||
| # job stops after 180 seconds | ||
| activeDeadlineSeconds: 180 | ||
| template: | ||
| spec: | ||
| serviceAccountName: {{ include "blueapi.fullname" . }}-last-used-stamper | ||
| {{- with .Values.tolerations }} | ||
| tolerations: | ||
| {{- toYaml . | nindent 12 }} | ||
| {{- end }} | ||
| {{- with .Values.nodeSelector }} | ||
| nodeSelector: | ||
| {{- toYaml . | nindent 12 }} | ||
| {{- end }} | ||
| {{- with .Values.affinity }} | ||
| affinity: | ||
| {{- toYaml . | nindent 12 }} | ||
| {{- end }} | ||
| volumes: | ||
| - name: {{include "blueapi.fullname" . }}-pvc-stamper-script | ||
| configMap: | ||
| name: {{include "blueapi.fullname" . }}-pvc-stamper-script | ||
| defaultMode: 0555 | ||
| containers: | ||
| - name: last-used-stamper | ||
| env: | ||
| - name: RELEASE_NAME | ||
| value: {{ .Release.Name }} | ||
| - name: RELEASE_NAMESPACE | ||
| value: {{ .Release.Namespace }} | ||
| - name: RELEASE_FULLNAME | ||
| value: {{include "blueapi.fullname" . }} | ||
| volumeMounts: | ||
| - name: {{include "blueapi.fullname" . }}-pvc-stamper-script | ||
| mountPath: /scripts | ||
| image: rancher/kubectl@sha256:05d2b313e2f397e0ade252136aed47abd72d56ead11d1b027ac70f66362c8495 # v1.36.0 | ||
| imagePullPolicy: IfNotPresent | ||
| command: ["/scripts/time-stamper.sh"] | ||
| restartPolicy: OnFailure | ||
| {{- end }} | ||
| {{- if .Values.pvcAutoDeletion.enabled }} | ||
| --- | ||
| apiVersion: v1 | ||
| kind: ServiceAccount | ||
| metadata: | ||
| name: {{ include "blueapi.fullname" . }}-pvc-auto-deletion | ||
| namespace: {{ .Release.Namespace }} | ||
| automountServiceAccountToken: true | ||
| --- | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: Role | ||
| metadata: | ||
| name: {{ include "blueapi.fullname" . }}-pvc-auto-deletion | ||
| namespace: {{ .Release.Namespace }} | ||
| rules: | ||
| - apiGroups: [""] | ||
| resources: ["persistentvolumeclaims"] | ||
| verbs: ["get", "list", "patch","delete"] | ||
| --- | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: RoleBinding | ||
| metadata: | ||
| name: {{ include "blueapi.fullname" . }}-pvc-auto-deletion | ||
| namespace: {{ .Release.Namespace }} | ||
| subjects: | ||
| - kind: ServiceAccount | ||
| name: {{ include "blueapi.fullname" . }}-pvc-auto-deletion | ||
| namespace: {{ .Release.Namespace }} | ||
| roleRef: | ||
| kind: Role | ||
| name: {{ include "blueapi.fullname" . }}-pvc-auto-deletion | ||
| apiGroup: rbac.authorization.k8s.io | ||
| --- | ||
| apiVersion: batch/v1 | ||
| kind: CronJob | ||
| metadata: | ||
| name: {{ include "blueapi.fullname" . }}-pvc-auto-deletion | ||
| namespace: {{ .Release.Namespace }} | ||
| spec: | ||
| concurrencyPolicy: Forbid | ||
| successfulJobsHistoryLimit: 1 | ||
| failedJobsHistoryLimit: 1 | ||
| schedule: "@weekly" | ||
|
|
||
| jobTemplate: | ||
| spec: | ||
| # amount of attempts for pvc deletion | ||
| backoffLimit: 3 | ||
| # job stops after 300 seconds | ||
| activeDeadlineSeconds: 300 | ||
| template: | ||
| spec: | ||
| serviceAccountName: {{ include "blueapi.fullname" . }}-pvc-auto-deletion | ||
| {{- with .Values.tolerations }} | ||
| tolerations: | ||
| {{- toYaml . | nindent 12 }} | ||
| {{- end }} | ||
| {{- with .Values.nodeSelector }} | ||
| nodeSelector: | ||
| {{- toYaml . | nindent 12 }} | ||
| {{- end }} | ||
| {{- with .Values.affinity }} | ||
| affinity: | ||
| {{- toYaml . | nindent 12 }} | ||
| {{- end }} | ||
| volumes: | ||
| - name: {{include "blueapi.fullname" . }}-pvc-auto-deletion-script | ||
| configMap: | ||
| name: {{include "blueapi.fullname" . }}-pvc-auto-deletion-script | ||
| defaultMode: 0555 | ||
| containers: | ||
| - name: pvc-auto-deletion | ||
| env: | ||
| - name: RELEASE_NAME | ||
| value: {{ .Release.Name }} | ||
| - name: RELEASE_NAMESPACE | ||
| value: {{ .Release.Namespace }} | ||
| - name: RELEASE_FULLNAME | ||
| value: {{include "blueapi.fullname" . }} | ||
| volumeMounts: | ||
| - name: {{include "blueapi.fullname" . }}-pvc-auto-deletion-script | ||
| mountPath: /scripts | ||
| image: rancher/kubectl@sha256:05d2b313e2f397e0ade252136aed47abd72d56ead11d1b027ac70f66362c8495 # v1.36.0 | ||
| imagePullPolicy: IfNotPresent | ||
| command: ["/scripts/pvc-deletion.sh"] | ||
| restartPolicy: OnFailure | ||
| {{- end }} | ||
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.