Skip to content

Commit

Permalink
Execute cache cleanup as a post-upgrade (#311)
Browse files Browse the repository at this point in the history
* Execute cache cleanup as a post-upgrade

* Fix description for job clearing cache
  • Loading branch information
VyacheslavSemin authored May 20, 2024
1 parent ce279c5 commit d6fc8cf
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 29 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,22 @@ The `helm delete` command removes all the Kubernetes components associated with
| `install.existingConfigmap.tblCreate.name` | The name of the existing ConfigMap that contains the sql file for craeting tables from the database | `init-db-scripts` |
| `install.existingConfigmap.tblCreate.keyName` | The name of the sql file containing instructions for creating tables from the database. Must be the same as the `key` name in `install.existingConfigmap.tblCreate.name` | `createdb.sql` |
| `install.existingConfigmap.initdb` | The name of the existing ConfigMap that contains the initdb script. If set, the two previous parameters are ignored. Must contain a key `initdb.sh` | `""` |
| `clearCache.job.enabled` | Enable the execution of job Clear Cache after upgrading ONLYOFFICE Docs. Job by Clear Cache has a `post-upgrade` hook executes after any resources have been upgraded in Kubernetes. He clears the Cache directory | `true` |
| `clearCache.job.annotations` | Defines annotations that will be additionally added to Clear Cache Job. If set to, it takes priority over the `commonAnnotations` | `{}` |
| `clearCache.job.podAnnotations` | Map of annotations to add to the Clear Cache Pod | `{}` |
| `clearCache.job.customPodAntiAffinity` | Prohibiting the scheduling of Clear Cache Job Pod relative to other Pods containing the specified labels on the same node | `{}` |
| `clearCache.job.podAffinity` | Defines [Pod affinity](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#inter-pod-affinity-and-anti-affinity) rules for Clear Cache Job Pod scheduling by nodes relative to other Pods | `{}` |
| `clearCache.job.nodeAffinity` | Defines [Node affinity](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity) rules for Clear Cache Job Pod scheduling by nodes | `{}` |
| `clearCache.job.nodeSelector` | Node labels for Clear Cache Job Pod assignment. If set to, it takes priority over the `nodeSelector` | `{}` |
| `clearCache.job.tolerations` | Tolerations for Clear Cache Job Pod assignment. If set to, it takes priority over the `tolerations` | `[]` |
| `clearCache.job.image.repository` | Job by Clear Cache ONLYOFFICE Docs image repository | `onlyoffice/docs-utils` |
| `clearCache.job.image.tag` | Job by Clear Cache ONLYOFFICE Docs image tag | `8.0.1-1` |
| `clearCache.job.image.pullPolicy` | Job by Clear Cache ONLYOFFICE Docs image pull policy | `IfNotPresent` |
| `clearCache.job.containerSecurityContext.enabled` | Enable security context for the Clear Cache container | `false` |
| `clearCache.job.resources.requests` | The requested resources for the job Clear Cache container | `{}` |
| `clearCache.job.resources.limits` | The resources limits for the job Clear Cache container | `{}` |
| `clearCache.existingConfigmap.name` | The name of the existing ConfigMap that contains the clears the Cache directory custom script. If set, the default configmap will not be created | `""` |
| `clearCache.existingConfigmap.keyName` | The name of the script containing instructions for clears the Cache directory. Must be the same as the `key` name in `clearCache.existingConfigmap.name` if a custom script is used | `clearCache.sh` |
| `grafanaDashboard.job.annotations` | Defines annotations that will be additionally added to Grafana Dashboard Job. If set to, it takes priority over the `commonAnnotations` | `{}` |
| `grafanaDashboard.job.podAnnotations` | Map of annotations to add to the Grafana Dashboard Pod | `{}` |
| `grafanaDashboard.job.customPodAntiAffinity` | Prohibiting the scheduling of Grafana Dashboard Job Pod relative to other Pods containing the specified labels on the same node | `{}` |
Expand Down
20 changes: 20 additions & 0 deletions templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,26 @@ Return the configmap name of creating tables for install ds
{{- end }}
{{- end -}}

{{/*
Get the configmap name containing the ds clearCache script
*/}}
{{- define "ds.clearCache.configMapName" -}}
{{- if .Values.clearCache.existingConfigmap.name -}}
{{- printf "%s" (tpl .Values.clearCache.existingConfigmap.name $) -}}
{{- else }}
{{- printf "clear-cache" -}}
{{- end -}}
{{- end -}}

{{/*
Return true if a configmap object containing the ds clearCache script should be created
*/}}
{{- define "ds.clearCache.createConfigMap" -}}
{{- if empty .Values.clearCache.existingConfigmap.name }}
{{- true -}}
{{- end -}}
{{- end -}}

{{/*
Get the ds labels
*/}}
Expand Down
41 changes: 41 additions & 0 deletions templates/configmaps/clear-cache.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{{- if and (eq (include "ds.clearCache.createConfigMap" .) "true") .Values.clearCache.job.enabled (not .Values.persistence.storageS3) }}
apiVersion: v1
kind: ConfigMap
metadata:
name: clear-cache
namespace: {{ include "ds.namespace" . | quote }}
{{- if .Values.commonLabels }}
labels:
{{- include "ds.labels.commonLabels" . | trim | nindent 4 }}
{{- end }}
annotations:
"helm.sh/hook": post-upgrade
"helm.sh/hook-weight": "1"
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
{{- if .Values.commonAnnotations }}
{{- include "ds.annotations.commonAnnotations" ( dict "keyName" .Values.commonAnnotations "context" $ ) | nindent 4 }}
{{- end }}
data:
clearCache.sh: |-
#!/bin/bash
FILES_DIR=/var/lib/{{ .Values.product.name }}/documentserver/App_Data/cache/files/
ls $FILES_DIR > /dev/null
if [ $? -ne 0 ]; then
echo -e "\e[0;31m Error accessing the $FILES_DIR directory \e[0m"
exit 1
fi
for ENTRY in `ls $FILES_DIR`; do
case $ENTRY in
errors)
;;
forgotten)
;;
{{ .Values.docservice.image.tag }})
;;
*)
rm -rfv $FILES_DIR$ENTRY
;;
esac
done
echo -e "\e[0;32m Clearing the cache folder is finished \e[0m"
{{- end }}
1 change: 1 addition & 0 deletions templates/configmaps/documentserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ data:
{{- if .Values.requestFilteringAgent.denyIPAddressList }}
DENY_IP_ADDRESS_LIST: {{ toJson .Values.requestFilteringAgent.denyIPAddressList | squote }}
{{- end }}
STORAGE_SUBDIRECTORY_NAME: {{ .Values.docservice.image.tag }}
19 changes: 0 additions & 19 deletions templates/configmaps/pre-upgrade.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,6 @@ data:
echo -e "\e[0;31m DB is not available \e[0m"
exit 1
fi
{{- if not .Values.persistence.storageS3 }}
FILES_DIR=/var/lib/{{ .Values.product.name }}/documentserver/App_Data/cache/files/
ls $FILES_DIR > /dev/null
if [ $? -ne 0 ]; then
echo -e "\e[0;31m Error accessing the $FILES_DIR directory \e[0m"
exit 1
fi
for ENTRY in `ls $FILES_DIR`; do
case $ENTRY in
errors)
;;
forgotten)
;;
*)
rm -rfv $FILES_DIR$ENTRY
;;
esac
done
{{- end }}
if [[ "$PRIVATE_CLUSTER" != "true" ]]; then
wget -O /scripts/{{ .Values.upgrade.existingConfigmap.tblRemove.keyName }} \
{{- if eq .Values.connections.dbType "postgres" }}
Expand Down
91 changes: 91 additions & 0 deletions templates/jobs/clear-cache.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{{- if and .Values.clearCache.job.enabled (not .Values.persistence.storageS3) }}
apiVersion: batch/v1
kind: Job
metadata:
name: clear-cache
namespace: {{ include "ds.namespace" . | quote }}
labels:
app.kubernetes.io/component: clear-cache
{{- if .Values.commonLabels }}
{{- include "ds.labels.commonLabels" . | trim | nindent 4 }}
{{- end }}
annotations:
"helm.sh/hook": post-upgrade
"helm.sh/hook-weight": "2"
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
{{- if or .Values.commonAnnotations .Values.clearCache.job.annotations }}
{{- $dictValue := default .Values.commonAnnotations .Values.clearCache.job.annotations }}
{{- include "ds.annotations.commonAnnotations" ( dict "keyName" $dictValue "context" $ ) | nindent 4 }}
{{- end }}
spec:
backoffLimit: 2
template:
metadata:
labels:
app.kubernetes.io/component: clear-cache
{{- if .Values.commonLabels }}
{{- include "ds.labels.commonLabels" . | trim | nindent 8 }}
{{- end }}
{{- if .Values.clearCache.job.podAnnotations }}
annotations: {{- include "ds.annotations.commonAnnotations" ( dict "keyName" .Values.clearCache.job.podAnnotations "context" $ ) | nindent 8 }}
{{- end }}
spec:
serviceAccountName: {{ include "ds.serviceAccountName" . }}
{{- if .Values.podSecurityContext.enabled }}
securityContext: {{- toYaml .Values.podSecurityContext.jobs | nindent 8 }}
{{- end }}
{{- if or .Values.clearCache.job.customPodAntiAffinity .Values.clearCache.job.podAffinity .Values.clearCache.job.nodeAffinity }}
affinity:
{{- with .Values.clearCache.job.customPodAntiAffinity }}
podAntiAffinity:
{{- toYaml . | nindent 10 }}
{{- end }}
{{- with .Values.clearCache.job.podAffinity }}
podAffinity:
{{- toYaml . | nindent 10 }}
{{- end }}
{{- with .Values.clearCache.job.nodeAffinity }}
nodeAffinity:
{{- toYaml . | nindent 10 }}
{{- end }}
{{- end }}
{{- if or .Values.nodeSelector .Values.clearCache.job.nodeSelector }}
nodeSelector: {{ toYaml (default .Values.nodeSelector .Values.clearCache.job.nodeSelector) | nindent 8 }}
{{- end }}
{{- if or .Values.tolerations .Values.clearCache.job.tolerations }}
tolerations: {{ toYaml (default .Values.tolerations .Values.clearCache.job.tolerations) | nindent 8 }}
{{- end }}
{{- if .Values.imagePullSecrets }}
imagePullSecrets:
- name: {{ .Values.imagePullSecrets }}
{{- end }}
volumes:
- name: ds-files
persistentVolumeClaim:
claimName: {{ template "ds.pvc.name" . }}
- name: clear-cache-scripts
configMap:
name: {{ template "ds.clearCache.configMapName" . }}
defaultMode: 0755
containers:
- name: clear-cache
image: {{ .Values.clearCache.job.image.repository }}:{{ .Values.clearCache.job.image.tag }}
imagePullPolicy: {{ .Values.clearCache.job.image.pullPolicy }}
{{- if .Values.clearCache.job.containerSecurityContext.enabled }}
securityContext: {{- omit .Values.clearCache.job.containerSecurityContext "enabled" | toYaml | nindent 12 }}
{{- end }}
resources: {{- toYaml .Values.clearCache.job.resources | nindent 12 }}
volumeMounts:
- name: ds-files
mountPath: /var/lib/{{ .Values.product.name }}/documentserver/App_Data/cache/files
- name: clear-cache-scripts
mountPath: /scripts/{{ .Values.clearCache.existingConfigmap.keyName }}
subPath: {{ .Values.clearCache.existingConfigmap.keyName }}
command: ["/bin/sh", "-c"]
{{- if .Values.webProxy.enabled }}
args: ["http_proxy={{ .Values.webProxy.http }} https_proxy={{ .Values.webProxy.https }} no_proxy={{ .Values.webProxy.noProxy }} /scripts/{{ .Values.clearCache.existingConfigmap.keyName }}"]
{{- else }}
args: ["/scripts/{{ .Values.clearCache.existingConfigmap.keyName }}"]
{{- end }}
restartPolicy: Never
{{- end }}
9 changes: 0 additions & 9 deletions templates/jobs/pre-upgrade.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ spec:
- name: {{ .Values.imagePullSecrets }}
{{- end }}
volumes:
{{- if not .Values.persistence.storageS3 }}
- name: ds-files
persistentVolumeClaim:
claimName: {{ template "ds.pvc.name" . }}
{{- end }}
- name: stop-scripts
configMap:
name: {{ template "ds.upgrade.configMapName" . }}
Expand Down Expand Up @@ -94,10 +89,6 @@ spec:
- name: PRIVATE_CLUSTER
value: {{ .Values.privateCluster | quote }}
volumeMounts:
{{- if not .Values.persistence.storageS3 }}
- name: ds-files
mountPath: /var/lib/{{ .Values.product.name }}/documentserver/App_Data/cache/files
{{- end }}
- name: stop-scripts
mountPath: /scripts/stop.sh
subPath: stop.sh
Expand Down
Loading

0 comments on commit d6fc8cf

Please sign in to comment.