Skip to content

Commit 296624c

Browse files
ppxlcesmarvin
authored andcommitted
Merge branch 'release/v12.15-1'
2 parents d2d8e6a + 97763fb commit 296624c

File tree

7 files changed

+164
-65
lines changed

7 files changed

+164
-65
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [v12.15-1] - 2023-06-13
11+
### Fixed
12+
- [#22] Allow connections from all nodes of a cluster (cidr /16) in kubernetes environments.
13+
1014
## [v12.14-2] - 2023-04-21
1115
### Changed
1216
- [#20] Upgrade Base Image to 3.17.3-2

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ RUN set -x -o errexit \
1818
FROM registry.cloudogu.com/official/base:3.17.3-2
1919

2020
LABEL NAME="official/postgresql" \
21-
VERSION="12.14-2" \
21+
VERSION="12.15-1" \
2222
maintainer="[email protected]"
2323

2424
ENV LANG=en_US.utf8 \
2525
PGDATA=/var/lib/postgresql \
26-
POSTGRESQL_VERSION=12.14-r0
26+
POSTGRESQL_VERSION=12.15-r0
2727

2828
# install postgresql and gosu
2929
# Note: the current postgresql version from alpine is installed

Jenkinsfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!groovy
2-
@Library(['github.com/cloudogu/ces-build-lib@1.64.1', 'github.com/cloudogu/dogu-build-lib@v2.0.0'])
2+
@Library(['github.com/cloudogu/ces-build-lib@1.65.0', 'github.com/cloudogu/dogu-build-lib@v2.1.0'])
33
import com.cloudogu.ces.cesbuildlib.*
44
import com.cloudogu.ces.dogubuildlib.*
55

@@ -17,6 +17,11 @@ node('docker') {
1717
Markdown markdown = new Markdown(this, "3.11.0")
1818
markdown.check()
1919
}
20+
21+
stage('Bats Tests') {
22+
Bats bats = new Bats(this, docker)
23+
bats.checkAndExecuteTests()
24+
}
2025
}
2126

2227
node('vagrant') {

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ MAKEFILES_VERSION=7.5.0
55
include build/make/variables.mk
66
include build/make/self-update.mk
77
include build/make/release.mk
8+
include build/make/bats.mk

batsTests/startup.bats

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#! /bin/bash
2+
# Bind an unbound BATS variables that fail all tests when combined with 'set -o nounset'
3+
export BATS_TEST_START_TIME="0"
4+
export BATSLIB_FILE_PATH_REM=""
5+
export BATSLIB_FILE_PATH_ADD=""
6+
7+
load '/workspace/target/bats_libs/bats-support/load.bash'
8+
load '/workspace/target/bats_libs/bats-assert/load.bash'
9+
load '/workspace/target/bats_libs/bats-mock/load.bash'
10+
load '/workspace/target/bats_libs/bats-file/load.bash'
11+
12+
setup() {
13+
export STARTUP_DIR=/workspace/resources
14+
export WORKDIR=/workspace
15+
netstat="$(mock_create)"
16+
export netstat
17+
export PATH="${BATS_TMPDIR}:${PATH}"
18+
ln -s "${netstat}" "${BATS_TMPDIR}/netstat"
19+
}
20+
21+
teardown() {
22+
unset STARTUP_DIR
23+
unset WORKDIR
24+
rm "${BATS_TMPDIR}/netstat"
25+
}
26+
27+
@test "create_hba() should use cidr 16 if the dogu is running in a k8s cluster" {
28+
mock_set_output "${netstat}" "Kernel-IP-Routentabelle
29+
Ziel Router Genmask Flags MSS Fenster irtt Iface
30+
192.168.179.0 0.0.0.0 255.255.255.0 U 0 0 0 wlp0s20f3"
31+
32+
source /workspace/resources/startup.sh
33+
local POD_NAMESPACE
34+
export POD_NAMESPACE="ecosystem"
35+
36+
run create_hba
37+
38+
assert_success
39+
assert_equal "$(mock_get_call_num "${netstat}")" "1"
40+
assert_line '# generated, do not override'
41+
assert_line '# "local" is for Unix domain socket connections only'
42+
assert_line 'local all all trust'
43+
assert_line '# IPv4 local connections:'
44+
assert_line 'host all all 127.0.0.1/32 trust'
45+
assert_line '# IPv6 local connections:'
46+
assert_line 'host all all ::1/128 trust'
47+
assert_line '# container networks'
48+
assert_line "host all all 192.168.179.0/16 password"
49+
}
50+
51+
@test "create_hba() should use regular cidr if the dogu is not running in a k8s cluster" {
52+
mock_set_output "${netstat}" "Kernel-IP-Routentabelle
53+
Ziel Router Genmask Flags MSS Fenster irtt Iface
54+
192.168.179.0 0.0.0.0 255.255.255.0 U 0 0 0 wlp0s20f3"
55+
56+
source /workspace/resources/startup.sh
57+
58+
run create_hba
59+
60+
assert_success
61+
assert_equal "$(mock_get_call_num "${netstat}")" "1"
62+
assert_line '# generated, do not override'
63+
assert_line '# "local" is for Unix domain socket connections only'
64+
assert_line 'local all all trust'
65+
assert_line '# IPv4 local connections:'
66+
assert_line 'host all all 127.0.0.1/32 trust'
67+
assert_line '# IPv6 local connections:'
68+
assert_line 'host all all ::1/128 trust'
69+
assert_line '# container networks'
70+
assert_line "host all all 192.168.179.0/24 password"
71+
}

dogu.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"Name": "official/postgresql",
3-
"Version": "12.14-2",
3+
"Version": "12.15-1",
44
"DisplayName": "PostgreSQL",
55
"Description": "PostgreSQL Database.",
66
"Url": "https://www.postgresql.org/",

resources/startup.sh

Lines changed: 79 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,33 @@ function mask2cidr() {
99
IFS=.
1010
for DEC in $1; do
1111
case $DEC in
12-
255) (( NBITS+=8 ));;
12+
255) ((NBITS += 8)) ;;
1313
254)
14-
(( NBITS+=7 ))
14+
((NBITS += 7))
1515
break
1616
;;
1717
252)
18-
(( NBITS+=6 ))
18+
((NBITS += 6))
1919
break
2020
;;
2121
248)
22-
(( NBITS+=5 ))
22+
((NBITS += 5))
2323
break
2424
;;
2525
240)
26-
(( NBITS+=4 ))
26+
((NBITS += 4))
2727
break
2828
;;
2929
224)
30-
(( NBITS+=3 ))
30+
((NBITS += 3))
3131
break
3232
;;
3333
192)
34-
(( NBITS+=2 ))
34+
((NBITS += 2))
3535
break
3636
;;
3737
128)
38-
(( NBITS+=1 ))
38+
((NBITS += 1))
3939
break
4040
;;
4141
0) ;;
@@ -59,10 +59,23 @@ function create_hba() {
5959
echo 'host all all ::1/128 trust'
6060
echo '# container networks'
6161
for NETWITHMASK in $(netstat -nr | tail -n +3 | grep -v '^0' | awk '{print $1"/"$3}'); do
62+
local NET
6263
NET=$(echo "${NETWITHMASK}" | awk -F'/' '{print $1}')
64+
local MASK
6365
MASK=$(echo "${NETWITHMASK}" | awk -F'/' '{print $2}')
66+
local CIDR
6467
CIDR=$(mask2cidr "$MASK")
65-
echo "host all all ${NET}/${CIDR} password"
68+
local isNotRunningUnderK8s="${POD_NAMESPACE:-"not running k8s"}"
69+
local netmaskCidrValue
70+
if [ "${isNotRunningUnderK8s}" == "not running k8s" ]; then
71+
netmaskCidrValue="${NET}/${CIDR}"
72+
else
73+
# Hyper-scalers default to a CIDR of /32 which blocks any network traffic from others pods esp. from other nodes.
74+
# /16 allows traffic from a sufficiently large network range from the kubernetes cluster, independently how the
75+
# cluster is configured.
76+
netmaskCidrValue="${NET}/16"
77+
fi
78+
echo "host all all ${netmaskCidrValue} password"
6679
done
6780
}
6881

@@ -116,69 +129,74 @@ function setDoguLogLevel() {
116129
currentLogLevel=$(doguctl config --default "WARN" "logging/root")
117130

118131
case "${currentLogLevel}" in
119-
"ERROR")
120-
export POSTGRESQL_LOGLEVEL="ERROR"
132+
"ERROR")
133+
export POSTGRESQL_LOGLEVEL="ERROR"
121134
;;
122-
"INFO")
123-
export POSTGRESQL_LOGLEVEL="INFO"
135+
"INFO")
136+
export POSTGRESQL_LOGLEVEL="INFO"
124137
;;
125-
"DEBUG")
126-
export POSTGRESQL_LOGLEVEL="DEBUG5"
138+
"DEBUG")
139+
export POSTGRESQL_LOGLEVEL="DEBUG5"
127140
;;
128-
*)
129-
export POSTGRESQL_LOGLEVEL="WARNING"
141+
*)
142+
export POSTGRESQL_LOGLEVEL="WARNING"
130143
;;
131144
esac
132145
# Remove old log level setting, if existent
133146
sed -i '/^log_min_messages/d' /var/lib/postgresql/postgresql.conf
134147
# Append new log level setting
135-
echo "log_min_messages = ${POSTGRESQL_LOGLEVEL}" >> /var/lib/postgresql/postgresql.conf
148+
echo "log_min_messages = ${POSTGRESQL_LOGLEVEL}" >>/var/lib/postgresql/postgresql.conf
136149
}
137150

151+
function runMain() {
152+
chown -R postgres "$PGDATA"
153+
154+
# create /run/postgresql, if not existent
155+
mkdir -p /run/postgresql
156+
chown postgres:postgres /run/postgresql
157+
158+
if [ -z "$(ls -A "$PGDATA")" ]; then
159+
initializePostgreSQL
160+
write_pg_hba_conf
161+
elif [ -e "${PGDATA}"/postgresqlFullBackup.dump ]; then
162+
# Moving backup and emptying PGDATA directory
163+
mv "${PGDATA}"/postgresqlFullBackup.dump /tmp/postgresqlFullBackup.dump
164+
# New PostgreSQL version requires completely empty folder
165+
166+
rm -rf "${PGDATA:?}"/.??*
167+
rm -rf "${PGDATA:?}"/*
168+
169+
initializePostgreSQL
170+
171+
echo "Restoring database dump..."
172+
# Start postgres to restore backup
173+
gosu postgres postgres &
174+
PID=$!
175+
waitForPostgreSQLStartup
176+
# Restore backup
177+
psql -U postgres -f /tmp/postgresqlFullBackup.dump postgres
178+
rm /tmp/postgresqlFullBackup.dump
179+
# Kill postgres
180+
pkill -P ${PID}
181+
kill ${PID}
182+
waitForPostgreSQLShutdown
183+
echo "Database dump successfully restored"
184+
185+
write_pg_hba_conf
186+
else
187+
write_pg_hba_conf
188+
fi
189+
190+
setDoguLogLevel
138191

192+
# set stage for health check
193+
doguctl state ready
139194

140-
chown -R postgres "$PGDATA"
141-
142-
# create /run/postgresql, if not existent
143-
mkdir -p /run/postgresql
144-
chown postgres:postgres /run/postgresql
145-
146-
if [ -z "$(ls -A "$PGDATA")" ]; then
147-
initializePostgreSQL
148-
write_pg_hba_conf
149-
elif [ -e "${PGDATA}"/postgresqlFullBackup.dump ]; then
150-
# Moving backup and emptying PGDATA directory
151-
mv "${PGDATA}"/postgresqlFullBackup.dump /tmp/postgresqlFullBackup.dump
152-
# New PostgreSQL version requires completely empty folder
153-
154-
rm -rf "${PGDATA:?}"/.??*
155-
rm -rf "${PGDATA:?}"/*
156-
157-
initializePostgreSQL
158-
159-
echo "Restoring database dump..."
160-
# Start postgres to restore backup
161-
gosu postgres postgres &
162-
PID=$!
163-
waitForPostgreSQLStartup
164-
# Restore backup
165-
psql -U postgres -f /tmp/postgresqlFullBackup.dump postgres
166-
rm /tmp/postgresqlFullBackup.dump
167-
# Kill postgres
168-
pkill -P ${PID}
169-
kill ${PID}
170-
waitForPostgreSQLShutdown
171-
echo "Database dump successfully restored"
195+
# start database
196+
exec gosu postgres postgres
197+
}
172198

173-
write_pg_hba_conf
174-
else
175-
write_pg_hba_conf
199+
# make the script only run when executed, not when sourced from bats tests
200+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
201+
runMain
176202
fi
177-
178-
setDoguLogLevel
179-
180-
# set stage for health check
181-
doguctl state ready
182-
183-
# start database
184-
exec gosu postgres postgres

0 commit comments

Comments
 (0)