Merge "Add cronjob to regularly delete logs to gerrit-replica chart"
diff --git a/helm-charts/gerrit-replica/README.md b/helm-charts/gerrit-replica/README.md
index 9693bd7..843dfac 100644
--- a/helm-charts/gerrit-replica/README.md
+++ b/helm-charts/gerrit-replica/README.md
@@ -216,12 +216,19 @@
 can also be stored in a persistent volume. This volume has to be a read-write-many
 volume to be able to be used by multiple pods.
 
-| Parameter                     | Description                                     | Default           |
-|-------------------------------|-------------------------------------------------|-------------------|
-| `logStorage.enabled`          | Whether to enable persistence of logs           | `false`           |
-| `logStorage.externalPVC.use`  | Whether to use a PVC deployed outside the chart | `false`           |
-| `logStorage.externalPVC.name` | Name of the external PVC                        | `gerrit-logs-pvc` |
-| `logStorage.size`             | Size of the volume                              | `5Gi`             |
+| Parameter                          | Description                                        | Default                  |
+|------------------------------------|----------------------------------------------------|--------------------------|
+| `logStorage.enabled`               | Whether to enable persistence of logs              | `false`                  |
+| `logStorage.externalPVC.use`       | Whether to use a PVC deployed outside the chart    | `false`                  |
+| `logStorage.externalPVC.name`      | Name of the external PVC                           | `gerrit-logs-pvc`        |
+| `logStorage.size`                  | Size of the volume                                 | `5Gi`                    |
+| `logStorage.cleanup.enabled`       | Whether to regularly delete old logs               | `false`                  |
+| `logStorage.cleanup.schedule`      | Cron schedule defining when to run the cleanup job | `0 0 * * *`              |
+| `logStorage.cleanup.retentionDays` | Number of days to retain the logs                  | `14`                     |
+| `logStorage.cleanup.resources`     | Resources the container is allowed to use          | `requests.cpu: 100m`     |
+|                                    |                                                    | `requests.memory: 256Mi` |
+|                                    |                                                    | `limits.cpu: 100m`       |
+|                                    |                                                    | `limits.memory: 256Mi`   |
 
 Each pod will create a separate folder for its logs, allowing to trace logs to
 the respective pods.
diff --git a/helm-charts/gerrit-replica/templates/log-cleaner.cronjob.yaml b/helm-charts/gerrit-replica/templates/log-cleaner.cronjob.yaml
new file mode 100644
index 0000000..bf2239b
--- /dev/null
+++ b/helm-charts/gerrit-replica/templates/log-cleaner.cronjob.yaml
@@ -0,0 +1,53 @@
+{{- if and .Values.logStorage.enabled .Values.logStorage.cleanup.enabled }}
+apiVersion: batch/v1beta1
+kind: CronJob
+metadata:
+  name: {{ .Release.Name }}-log-cleaner
+  labels:
+    app: log-cleaner
+    chart: {{ template "gerrit-replica.chart" . }}
+    heritage: {{ .Release.Service }}
+    release: {{ .Release.Name }}
+spec:
+  schedule: {{ .Values.logStorage.cleanup.schedule | quote }}
+  concurrencyPolicy: "Forbid"
+  jobTemplate:
+    spec:
+      template:
+        {{ if .Values.istio.enabled -}}
+        metadata:
+          annotations:
+            sidecar.istio.io/inject: "false"
+        {{- end }}
+        spec:
+          restartPolicy: OnFailure
+          containers:
+          - name: log-cleaner
+            imagePullPolicy: {{ .Values.images.imagePullPolicy }}
+            image: busybox
+            command:
+            - sh
+            - -c
+            args:
+            - |
+              find /var/logs/ \
+                -mindepth 1 \
+                -type f \
+                -mtime +{{ .Values.logStorage.cleanup.retentionDays }} \
+                -print \
+                -delete
+              find /var/logs/ -type d -empty -delete
+            resources:
+{{ toYaml .Values.logStorage.cleanup.resources | indent 14 }}
+            volumeMounts:
+            - name: logs
+              mountPath: "/var/logs"
+          volumes:
+          - name: logs
+            persistentVolumeClaim:
+              {{- if .Values.logStorage.externalPVC.use }}
+              claimName: {{ .Values.logStorage.externalPVC.name }}
+              {{- else }}
+              claimName: {{ .Release.Name }}-log-pvc
+              {{- end }}
+{{- end }}
diff --git a/helm-charts/gerrit-replica/values.yaml b/helm-charts/gerrit-replica/values.yaml
index 40dbfd7..58fafd0 100644
--- a/helm-charts/gerrit-replica/values.yaml
+++ b/helm-charts/gerrit-replica/values.yaml
@@ -66,6 +66,17 @@
     use: false
     name: gerrit-logs-pvc
   size: 5Gi
+  cleanup:
+    enabled: false
+    schedule: "0 0 * * *"
+    retentionDays: 14
+    resources:
+      requests:
+        cpu: 100m
+        memory: 256Mi
+      limits:
+        cpu: 100m
+        memory: 256Mi
 
 
 istio: