2023-05-16 08:04:06 +00:00
|
|
|
apiVersion: batch/v1
|
|
|
|
kind: CronJob
|
|
|
|
metadata:
|
|
|
|
name: snapshot-creator
|
|
|
|
spec:
|
|
|
|
schedule: "@daily"
|
|
|
|
jobTemplate:
|
|
|
|
spec:
|
|
|
|
template:
|
|
|
|
spec:
|
2023-05-17 00:46:41 +00:00
|
|
|
securityContext:
|
|
|
|
runAsNonRoot: true
|
|
|
|
seccompProfile:
|
|
|
|
type: RuntimeDefault
|
2023-05-16 08:04:06 +00:00
|
|
|
restartPolicy: OnFailure
|
|
|
|
serviceAccountName: forgejo-snapshot
|
|
|
|
initContainers:
|
|
|
|
- name: template-snapshot-name
|
|
|
|
image: bitnami/kubectl:1.27.1
|
2023-05-17 00:46:41 +00:00
|
|
|
securityContext:
|
|
|
|
allowPrivilegeEscalation: false
|
|
|
|
capabilities:
|
|
|
|
drop:
|
|
|
|
- ALL
|
2023-05-16 08:04:06 +00:00
|
|
|
command: ["/bin/sh"]
|
|
|
|
args:
|
|
|
|
- -c
|
|
|
|
- |-
|
|
|
|
sed \
|
|
|
|
-e "s/TEMPLATE_NAME/forgejo-snapshot-$(date -u --rfc-3339=date)/" \
|
|
|
|
-e "s/TEMPLATE_PVC_NAME/forgejo-data-forgejo-0/" \
|
|
|
|
< /in/forgejo-volume-snapshot-template.yaml \
|
|
|
|
> /out/forgejo-volume-snapshot.yaml
|
|
|
|
volumeMounts:
|
|
|
|
- name: snapshot-template
|
|
|
|
mountPath: /in
|
|
|
|
- name: snapshot-yaml
|
|
|
|
mountPath: /out
|
|
|
|
containers:
|
|
|
|
- name: create-volume-snapshot
|
|
|
|
image: bitnami/kubectl:1.27.1
|
2023-05-17 00:46:41 +00:00
|
|
|
securityContext:
|
|
|
|
allowPrivilegeEscalation: false
|
|
|
|
capabilities:
|
|
|
|
drop:
|
|
|
|
- ALL
|
2023-05-16 08:04:06 +00:00
|
|
|
args:
|
|
|
|
- -n
|
|
|
|
- $(POD_NAMESPACE)
|
|
|
|
- apply
|
|
|
|
- -f
|
|
|
|
- /in/forgejo-volume-snapshot.yaml
|
|
|
|
env:
|
|
|
|
- name: POD_NAMESPACE
|
|
|
|
valueFrom:
|
|
|
|
fieldRef:
|
|
|
|
fieldPath: metadata.namespace
|
|
|
|
volumeMounts:
|
|
|
|
- name: snapshot-yaml
|
|
|
|
mountPath: /in
|
|
|
|
- name: cleanup-volume-snapshot
|
|
|
|
image: bitnami/kubectl:1.27.1
|
2023-05-17 00:46:41 +00:00
|
|
|
securityContext:
|
|
|
|
allowPrivilegeEscalation: false
|
|
|
|
capabilities:
|
|
|
|
drop:
|
|
|
|
- ALL
|
2023-05-16 08:04:06 +00:00
|
|
|
command: ["sh"]
|
|
|
|
args:
|
|
|
|
- -c
|
|
|
|
- |-
|
|
|
|
datestr="$(date -d '2 weeks ago' -Ins --utc | sed 's/+0000/Z/')"
|
|
|
|
kubectl -n forgejo get volumesnapshots \
|
|
|
|
--template '{{range .items}}{{.metadata.name}} {{.metadata.creationTimestamp}}{{"\n"}}{{end}}' \
|
|
|
|
| while read snapshot_name snapshot_date; do
|
|
|
|
echo "$snapshot_name" "$snapshot_date" "$datestr" | awk '$2 <= $3 { print $1 }'
|
|
|
|
done \
|
2024-01-06 05:13:05 +00:00
|
|
|
| xargs --no-run-if-empty -n 1 echo kubectl -n $(POD_NAMESPACE) delete volumesnapshot "$snapshot_name"
|
2023-05-16 08:04:06 +00:00
|
|
|
env:
|
|
|
|
- name: POD_NAMESPACE
|
|
|
|
valueFrom:
|
|
|
|
fieldRef:
|
|
|
|
fieldPath: metadata.namespace
|
|
|
|
volumes:
|
|
|
|
- name: snapshot-template
|
|
|
|
configMap:
|
|
|
|
name: forgejo-volume-snapshot-template
|
|
|
|
- name: snapshot-yaml
|
|
|
|
emptyDir: {}
|