Skip to content

Commit ce1548e

Browse files
committed
Account for podman versions older than 4.3.0
The addition of --userns keep-id:uid=...,gid=... is supported only on podman versions >= 4.3.0 [1] If we have an older version, let's just keep the same logic as before. [1] https://github.com/containers/podman/blob/main/troubleshooting.md#39-podman-run-fails-with-error-unrecognized-namespace-mode-keep-iduid1000gid1000-passed
1 parent 4b9c310 commit ce1548e

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

scripts/pattern-util.sh

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#!/bin/bash
2+
23
function is_available {
34
command -v $1 >/dev/null 2>&1 || { echo >&2 "$1 is required but it's not installed. Aborting."; exit 1; }
45
}
56

7+
function version {
8+
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
9+
}
10+
611
if [ -z "$PATTERN_UTILITY_CONTAINER" ]; then
712
PATTERN_UTILITY_CONTAINER="quay.io/hybridcloudpatterns/utility-container"
813
fi
@@ -11,15 +16,30 @@ readonly commands=(podman)
1116
for cmd in ${commands[@]}; do is_available "$cmd"; done
1217

1318
UNSUPPORTED_PODMAN_VERSIONS="1.6 1.5"
19+
PODMAN_VERSION_STR=$(podman --version)
1420
for i in ${UNSUPPORTED_PODMAN_VERSIONS}; do
1521
# We add a space
16-
if podman --version | grep -q -E "\b${i}"; then
17-
echo "Unsupported podman version. We recommend >= 4.2.0"
22+
if echo "${PODMAN_VERSION_STR}" | grep -q -E "\b${i}"; then
23+
echo "Unsupported podman version. We recommend > 4.3.0"
1824
podman --version
1925
exit 1
2026
fi
2127
done
2228

29+
# podman --version outputs:
30+
# podman version 4.8.2
31+
PODMAN_VERSION=$(echo "${PODMAN_VERSION_STR}" | awk '{ print $NF }')
32+
33+
# podman < 4.3.0 do not support keep-id:uid=...
34+
if [ $(version "${PODMAN_VERSION}") -lt $(version "4.3.0") ]; then
35+
PODMAN_ARGS="-v ${HOME}:/root"
36+
else
37+
# We do not rely on bash's $UID and $GID because on MacOSX $GID is not set
38+
MYUID=$(id -u)
39+
MYGID=$(id -g)
40+
PODMAN_ARGS="--user ${MYUID}:${MYGID} --userns keep-id:uid=${MYUID},gid=${MYGID}"
41+
fi
42+
2343
if [ -n "$KUBECONFIG" ]; then
2444
if [[ ! "${KUBECONFIG}" =~ ^$HOME* ]]; then
2545
echo "${KUBECONFIG} is pointing outside of the HOME folder, this will make it unavailable from the container."
@@ -31,17 +51,13 @@ fi
3151
# $HOME is mounted as itself for any files that are referenced with absolute paths
3252
# $HOME is mounted to /root because the UID in the container is 0 and that's where SSH looks for credentials
3353

34-
# We do not rely on bash's $UID and $GID because on MacOSX $GID is not set
35-
MYUID=$(id -u)
36-
MYGID=$(id -g)
3754
podman run -it --rm --pull=newer \
3855
--security-opt label=disable \
39-
--user "${MYUID}:${MYGID}" \
40-
--userns "keep-id:uid=${MYUID},gid=${MYGID}" \
4156
-e EXTRA_HELM_OPTS \
4257
-e KUBECONFIG \
4358
-v "${HOME}":"${HOME}" \
4459
-v "${HOME}":/pattern-home \
60+
${PODMAN_ARGS} \
4561
-w "$(pwd)" \
4662
"$PATTERN_UTILITY_CONTAINER" \
4763
$@

0 commit comments

Comments
 (0)