Skip to content

Commit 681ea18

Browse files
committed
see if we can do it without root
1 parent 0632cb5 commit 681ea18

File tree

2 files changed

+13
-27
lines changed

2 files changed

+13
-27
lines changed

Dockerfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
FROM percona/percona-postgresql-operator:2.6.0-ppg16.8-postgres
22

3-
# Switch to root user temporarily to gain necessary privileges for setup
4-
USER 0
5-
63
COPY postgres-oom-adjuster.sh /usr/local/bin/
74
COPY entrypoint-wrapper.sh /usr/local/bin/
85

entrypoint-wrapper.sh

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,23 @@
22
set -e
33

44
# For Percona PostgreSQL Operator, we need to focus on protecting the postmaster process
5-
# Original user in the Percona PostgreSQL Operator image
6-
ORIGINAL_USER=26
75
ORIGINAL_ENTRYPOINT="/opt/crunchy/bin/postgres-ha/bootstrap-postgres-ha.sh"
86

9-
# Set OOM score adjustment for our own process (will be inherited)
10-
if [ -f "/proc/self/oom_score_adj" ]; then
11-
echo -900 > /proc/self/oom_score_adj
12-
echo "Set OOM score adjustment to -900 for pid 1"
13-
else
14-
echo "WARNING: Cannot set OOM score adjustment (file not found)"
7+
# Try to set OOM score adjustment if possible, but don't fail if we can't
8+
if command -v sudo >/dev/null 2>&1; then
9+
# If sudo is available, use it to set OOM score
10+
sudo -n sh -c 'echo -900 > /proc/self/oom_score_adj' >/dev/null 2>&1 || true
11+
echo "Attempted to set OOM score adjustment using sudo"
12+
elif command -v setcap >/dev/null 2>&1; then
13+
# Try to give the oom-adjuster script the capability to adjust OOM scores
14+
setcap 'cap_sys_resource=+ep' /usr/local/bin/postgres-oom-adjuster.sh >/dev/null 2>&1 || true
15+
echo "Attempted to grant capabilities to OOM adjuster"
1516
fi
1617

17-
# Start the OOM adjuster in the background
18+
# Start a background process to monitor and adjust PostgreSQL if possible
1819
nohup /usr/local/bin/postgres-oom-adjuster.sh >> /tmp/postgres-oom-adjuster.log 2>&1 &
1920
echo "Started postmaster OOM adjuster in background"
2021

21-
# Set environment variables for PostgreSQL child processes
22-
export PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
23-
export PG_OOM_ADJUST_VALUE=0
24-
25-
# Switching to the original user and executing original entrypoint
26-
echo "Switching to user $ORIGINAL_USER and executing original entrypoint: $ORIGINAL_ENTRYPOINT $@"
27-
28-
# Check which user-switching command is available
29-
if command -v runuser >/dev/null 2>&1; then
30-
# Use runuser (available on RHEL/CentOS/Fedora)
31-
exec runuser -u "#$ORIGINAL_USER" -- "$ORIGINAL_ENTRYPOINT" "$@"
32-
else
33-
# Fall back to su
34-
exec su -s /bin/bash $ORIGINAL_USER -c "$ORIGINAL_ENTRYPOINT $*"
35-
fi
22+
# Execute the original entrypoint
23+
echo "Executing original entrypoint: $ORIGINAL_ENTRYPOINT $@"
24+
exec "$ORIGINAL_ENTRYPOINT" "$@"

0 commit comments

Comments
 (0)