-
Notifications
You must be signed in to change notification settings - Fork 176
K8SPSMDB-1518: allow specifying logrotate configuration #2151
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
Changes from all commits
e29e544
cfd50e0
8f2e658
135050b
2e8b590
8f54b3f
5e69d7c
da7af91
31d2284
7c32278
ee251c6
ea21154
979fa25
a5ad117
6b1ab34
49dd962
f11ccc3
39a6382
e38f489
7c0b50a
55667d0
e0b3f58
37812f2
a906613
afa8920
99b7367
35a08ff
7eab046
c499c0f
b618502
8743537
778d792
d055c34
cb75d75
e87af6e
08df282
87c835c
3073a01
580f1c9
c6e3a52
950ecfd
692f432
c78dbf0
70255f4
f269653
6775fcd
dfe4966
e985718
f9b073b
b93307a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,21 +1,100 @@ | ||||||
| #!/bin/sh | ||||||
| #!/bin/bash | ||||||
| set -e | ||||||
| set -o xtrace | ||||||
|
|
||||||
| export PATH="$PATH":/opt/fluent-bit/bin | ||||||
| export PATH="$PATH:/opt/fluent-bit/bin" | ||||||
|
|
||||||
| if [ "$1" = 'logrotate' ]; then | ||||||
| LOGROTATE_SCHEDULE="${LOGROTATE_SCHEDULE:-0 0 * * *}" | ||||||
|
|
||||||
| is_logrotate_config_invalid() { | ||||||
| local config_file="$1" | ||||||
| if [ -z "$config_file" ] || [ ! -f "$config_file" ]; then | ||||||
| return 1 | ||||||
| fi | ||||||
| # Specifying -d runs in debug mode, so even in case of errors, it will exit with 0. | ||||||
| # We need to check the output for "error" but skip those lines that are related to the missing logrotate.status file. | ||||||
| # Filter out logrotate.status lines first, then check for remaining errors | ||||||
| ( | ||||||
| set +e | ||||||
| logrotate -d "$config_file" 2>&1 | grep -v "logrotate.status" | grep -qi "error" | ||||||
mayankshah1607 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| ) | ||||||
| return $? | ||||||
| } | ||||||
mayankshah1607 marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+8
to
+21
|
||||||
|
|
||||||
| run_logrotate() { | ||||||
| local logrotate_status_file="/data/db/logs/logrotate.status" | ||||||
| local logrotate_conf_file="/opt/percona/logcollector/logrotate/logrotate.conf" | ||||||
| local logrotate_additional_conf_files=() | ||||||
| local conf_d_dir="/opt/percona/logcollector/logrotate/conf.d" | ||||||
|
|
||||||
| # Check if mongodb.conf exists and validate it | ||||||
| if [ -f "$conf_d_dir/mongodb.conf" ]; then | ||||||
| logrotate_conf_file="$conf_d_dir/mongodb.conf" | ||||||
| if is_logrotate_config_invalid "$logrotate_conf_file"; then | ||||||
| echo "ERROR: Logrotate configuration is invalid, fallback to default configuration" | ||||||
| logrotate_conf_file="/opt/percona/logcollector/logrotate/logrotate.conf" | ||||||
| fi | ||||||
| fi | ||||||
|
|
||||||
| # Process all .conf files in conf.d directory (excluding mongodb.conf which is already handled) | ||||||
| if [ -d "$conf_d_dir" ]; then | ||||||
| for conf_file in "$conf_d_dir"/*.conf; do | ||||||
| # Check if glob matched any files (if no .conf files exist, the glob returns itself) | ||||||
| [ -f "$conf_file" ] || continue | ||||||
| # Skip mongodb.conf as it's already processed above | ||||||
| [ "$(basename "$conf_file")" = "mongodb.conf" ] && continue | ||||||
| if is_logrotate_config_invalid "$conf_file"; then | ||||||
| echo "ERROR: Logrotate configuration file $conf_file is invalid, it will be ignored" | ||||||
| else | ||||||
| logrotate_additional_conf_files+=("$conf_file") | ||||||
| fi | ||||||
| done | ||||||
| fi | ||||||
| # Ensure logrotate can run with current UID | ||||||
| if [[ $EUID != 1001 ]]; then | ||||||
| # logrotate requires UID in /etc/passwd | ||||||
| sed -e "s^x:1001:^x:$EUID:^" /etc/passwd >/tmp/passwd | ||||||
mayankshah1607 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| cat /tmp/passwd >/etc/passwd | ||||||
| rm -rf /tmp/passwd | ||||||
mayankshah1607 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| fi | ||||||
| exec go-cron "0 0 * * *" sh -c "logrotate -s /data/db/logs/logrotate.status /opt/percona/logcollector/logrotate/logrotate.conf;" | ||||||
| else | ||||||
| if [ "$1" = 'fluent-bit' ]; then | ||||||
| fluentbit_opt+='-c /opt/percona/logcollector/fluentbit/fluentbit.conf' | ||||||
| fi | ||||||
|
|
||||||
| exec "$@" $fluentbit_opt | ||||||
| fi | ||||||
| local logrotate_cmd="logrotate -s \"$logrotate_status_file\" \"$logrotate_conf_file\"" | ||||||
| for additional_conf in "${logrotate_additional_conf_files[@]}"; do | ||||||
| logrotate_cmd="$logrotate_cmd \"$additional_conf\"" | ||||||
| done | ||||||
|
|
||||||
| set -o xtrace | ||||||
| exec go-cron "$LOGROTATE_SCHEDULE" sh -c "$logrotate_cmd" | ||||||
mayankshah1607 marked this conversation as resolved.
Show resolved
Hide resolved
mayankshah1607 marked this conversation as resolved.
Show resolved
Hide resolved
mayankshah1607 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } | ||||||
|
|
||||||
| run_fluentbit() { | ||||||
| local fluentbit_opt=(-c /opt/percona/logcollector/fluentbit/fluentbit.conf) | ||||||
| mkdir -p /tmp/fluentbit/custom | ||||||
| set +e | ||||||
| local fluentbit_conf_dir="/opt/percona/logcollector/fluentbit/custom" | ||||||
| for conf_file in $fluentbit_conf_dir/*.conf; do | ||||||
|
Check notice on line 74 in build/logcollector/entrypoint.sh
|
||||||
mayankshah1607 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| for conf_file in $fluentbit_conf_dir/*.conf; do | |
| for conf_file in "$fluentbit_conf_dir"/*.conf; do |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,2 +1,2 @@ | ||||||||||||||||||||||||||
| @INCLUDE fluentbit_*.conf | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| @INCLUDE fluentbit_*.conf | |
| @INCLUDE fluentbit_*.conf | |
| # Custom configuration includes | |
| # NOTE: | |
| # - `/tmp/fluentbit/custom/*.conf` is populated by entrypoint.sh, which validates | |
| # configs from `/opt/percona/logcollector/fluentbit/custom` before copying them. | |
| # - `custom/*.conf` is kept for backward compatibility with older setups that | |
| # mounted configs directly into the image path. | |
| # Prefer mounting into `/opt/percona/logcollector/fluentbit/custom` so that | |
| # validation and copying to `/tmp/fluentbit/custom/` work as intended. | |
| @INCLUDE custom/*.conf |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -1579,6 +1579,23 @@ type LogCollectorSpec struct { | |||||||
| ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"` | ||||||||
| Env []corev1.EnvVar `json:"env,omitempty"` | ||||||||
| EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"` | ||||||||
| LogRotate *LogRotateSpec `json:"logrotate,omitempty"` | ||||||||
| } | ||||||||
|
|
||||||||
mayankshah1607 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| // LogRotateSpec defines the configuration for the logrotate container. | ||||||||
| type LogRotateSpec struct { | ||||||||
| // Configuration allows overriding the default logrotate configuration. | ||||||||
| Configuration string `json:"configuration,omitempty"` | ||||||||
| // ExtraConfig allows specifying logrotate configuration file in addition to the main configuration file. | ||||||||
| // This should be a reference to a ConfigMap or a Secret in the same namespace. | ||||||||
| // Key must contain the .conf extension to be processed correctly. | ||||||||
| // | ||||||||
| // NOTE: mongodb.conf is reserved for the default configuration specified by .configuration field. | ||||||||
| ExtraConfig corev1.LocalObjectReference `json:"extraConfig,omitempty"` | ||||||||
| // Schedule allows specifying the schedule for logrotate. | ||||||||
| // This should be a valid cron expression. | ||||||||
| //+kubebuilder:default:="0 0 * * *" | ||||||||
mayankshah1607 marked this conversation as resolved.
Show resolved
Hide resolved
mayankshah1607 marked this conversation as resolved.
Show resolved
Hide resolved
mayankshah1607 marked this conversation as resolved.
Show resolved
Hide resolved
mayankshah1607 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| //+kubebuilder:default:="0 0 * * *" | |
| //+kubebuilder:default:="0 0 * * *" | |
| //+kubebuilder:validation:Pattern=`^(\S+\s+){4}\S+$` |
mayankshah1607 marked this conversation as resolved.
Show resolved
Hide resolved
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.