30 Commits

Author SHA1 Message Date
Chris Evich
f44e9891d1 Merge branch 'fix_idmaps' into 'main'
Fix newuidmap...`: newuidmap: open of uid_map failed: Permission denied

See merge request qontainers/pipglr!15
2022-12-08 12:05:44 +00:00
Chris Evich
bf242e8a52 Fix newuidmap...`: newuidmap: open of uid_map failed: Permission denied
Within a container, caps need to be set on /usr/bin/new{uid,gid}map
which reflect the (inner) podman user's root namespaced ID of 10000.

Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-12-08 06:59:02 -05:00
Chris Evich
db6f2bceb9 Attempt fix v2.0.0 newuidmap permission denied
Recently, I believe an update or packaging problem has been causing
podman to throw errors like:

```
level=error msg="running `/usr/bin/newuidmap ...`: newuidmap: open of
uid_map failed: Permission denied\n"
```

This seems to have something to do with the shadow-utils package, which
owns this binary.  I've examined the file attribuites and permissions
along with /etc/sub{uid,gid} contents.  The only thing that seems to
resolve the issue is reinstalling shadow-utils.  Attempt that fix here
and hope it clears up the problem (present in v2.0.0)

Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-12-07 06:14:45 -05:00
Chris Evich
f1423ae0b2 Merge branch 'fix_volumes' into 'main'
Fix volume mounts

See merge request qontainers/pipglr!14
2022-12-06 08:15:33 +00:00
Chris Evich
f88c1a43bd Fix volume mounts
* Add note about volume-mounts being cumulative with base-image
* Fix register & run labels to use (correct) base image's
  `/home/podman/.local/share/containers/` instead of defining
  a new (wrong/useless) `storage` volume.
* Fix register & run labels to mask over `/var/lib/containers`
  with a read-only tmpfs to block any nested rootful use of
  podman as a security precaution.

Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-12-06 03:07:40 -05:00
Chris Evich
5b28cca4e1 Merge branch 'docs_and_cache' into 'main'
Docs: Update volume creation section

See merge request qontainers/pipglr!13
2022-12-06 07:31:15 +00:00
Chris Evich
4fbe33bdbd Revert "Cache prior build layers for ~5 days"
This reverts commit 4c1af406f7.  For
whatever reason, trying to build this using the community runners always
results in an out-of-diskspace error.

Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-12-06 02:20:14 -05:00
Chris Evich
4c1af406f7 Cache prior build layers for ~5 days
This vastly improves build performance at the cost of some extra
registry server storage (which there is plenty of).   Note: The cache
TTL needs to be coordinated with the registry server's tag-cleanup
policy.

Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-12-06 02:18:55 -05:00
Chris Evich
51ce5e4e0a Use single uid/gid map
The podman base-image is intended to support running nested-podman both
root and rootless.  Since pipglr only ever runs rootless, eliminate the
nested usernamespace mapping needed to support nested-root usage.

Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-12-06 02:18:55 -05:00
Chris Evich
75bdc3196f Docs: Update volume creation section
Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-12-06 02:18:27 -05:00
Chris Evich
d36e05d452 Merge branch 'add_readme' into 'main'
Include docs inside image

See merge request qontainers/pipglr!12
2022-12-01 19:23:04 +00:00
Chris Evich
29a806305e Include docs inside image
Signed-off-by: Chris Evich <cevich@redhat.com>
2022-11-30 09:47:18 -05:00
Chris Evich
19a7e501be Docs update
Signed-off-by: Chris Evich <cevich@redhat.com>
2022-11-30 09:44:34 -05:00
Chris Evich
b591a3d7c5 Remove broken workflow rules
They were preventing builds on main and on tags.  Rather than try and
debug them, just kill them as they're mostly unnecessary for this repo.

Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-11-25 23:28:47 -05:00
Chris Evich
b21181a446 Fix build job not running on main or tag push
Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-11-25 23:26:35 -05:00
Chris Evich
c80dfffa14 Merge branch 'switch_to_buildah' into 'main'
Switch to using buildah

See merge request qontainers/pipglr!11
2022-11-26 04:20:51 +00:00
Chris Evich
cbb3eb0e22 Switch to using buildah
Observations show the runtime is about the same (since performance is mostly
storage-io bound.  However, buildah images are more compact and there
are simpler possibilities for build-caching available for future use.

Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-11-25 23:15:26 -05:00
Chris Evich
98e2bee2a5 Merge branch 'kill_vars' into 'main'
Remove unnecessary variables

See merge request qontainers/pipglr!10
2022-11-24 16:51:33 +00:00
Chris Evich
4b25e62b52 Remove unnecessary variables
Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-11-24 11:48:23 -05:00
Chris Evich
1c08727c5d Merge branch 'simplify_metadata' into 'main'
Simplify image metadata

See merge request qontainers/pipglr!9
2022-11-24 02:41:37 +00:00
Chris Evich
af84720d71 Simplify image metadata
Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-11-23 21:38:06 -05:00
Chris Evich
c748c4f928 Merge branch 'image_metadata' into 'main'
Add metadata to image

See merge request qontainers/pipglr!8
2022-11-24 01:47:48 +00:00
Chris Evich
5164551f98 Add metadata to image
Also, pin base image to a specific version instead of `latest`.

Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-11-23 18:54:24 -05:00
Chris Evich
63b00ad2e7 Merge branch 'fix_maintenance' into 'main'
Fix maintenance task

See merge request qontainers/pipglr!7
2022-11-23 17:12:59 +00:00
Chris Evich
2bda4c3be6 Fix maintenance task
The function was defined but never called, resulting in immediate exit
of the maintenance script.  Fix this, also add a configuration build-arg and
ENV to control the cleaning interval.

Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-11-23 12:10:07 -05:00
Chris Evich
3b72178739 Merge branch 'cachevol' into 'main'
Add missing cache volume

See merge request qontainers/pipglr!6
2022-11-22 22:31:28 +00:00
Chris Evich
df8f46eb2d Update docs and Containerfile to match
Fully tested README.md instructions end-to-end on F36.

Signed-off-by: Chris Evich <cevich@redhat.com>
2022-11-22 14:53:41 -05:00
Chris Evich
a1931efcc1 Add missing cache volume
Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-11-22 11:35:36 -05:00
Chris Evich
3d6603945d Bugfix
Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-11-19 07:42:23 -05:00
Chris Evich
ae40196724 Pre-create podman storage root
Suspected cause of ownership problems when mounting a volume at this
location.

Signed-off-by: Chris Evich <chris_gitlab@icuc.me>
2022-11-19 07:33:57 -05:00
4 changed files with 142 additions and 61 deletions

View File

@@ -1,25 +1,39 @@
---
stages:
- build
build:
stage: build
default:
image: quay.io/buildah/stable:v1.28.0
tags:
- docker
- linux
image:
name: gcr.io/kaniko-project/executor:v1.6.0-debug
entrypoint: ["/busybox/sh", "-c"]
envars:
stage: test
script: |
echo "Select CI env. vars.:";
printenv | egrep '^CI_' | sort
commit_check:
stage: test
variables:
BASE_TAG: latest
FLAVOR: stable
BADRX: '^(squash!)|(fixup!)'
script: |
dnf install -y git
shortlogtmp=$(mktemp -p '' commit_check_tmp_XXXX)
git log --oneline --no-show-signature "${CI_MERGE_REQUEST_DIFF_BASE_SHA}..HEAD" > "$shortlogtmp"
if egrep -q "$BADRX" "$shortlogtmp"; then
egrep "$BADRX" "$shortlogtmp"
die "Found the above commits matching '$BADRX'"
fi
build:
stage: deploy
variables:
BUILDAH_FORMAT: docker
BUILDAH_ISOLATION: chroot
STORAGE_DRIVER: vfs
before_script:
- echo "$CI_REGISTRY_PASSWORD" | buildah login -u "$CI_REGISTRY_USER" --password-stdin $CI_REGISTRY
script:
- 'mkdir -p /kaniko/.docker'
- 'echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json'
- |
echo "Select CI env. vars.:";
printenv | egrep '^CI_' | sort
# N/B: There could be more than one merge-request open with this branch's HEAD
- |
IMAGE_TAG="${CI_COMMIT_BRANCH}";
@@ -31,10 +45,12 @@ build:
IMAGE_TAG="latest";
fi
echo "Building/Pushing to: ${CI_REGISTRY_IMAGE}:${IMAGE_TAG}";
- |
/kaniko/executor \
--context $CI_PROJECT_DIR \
--dockerfile $CI_PROJECT_DIR/Containerfile \
--destination "${CI_REGISTRY_IMAGE}:${IMAGE_TAG}" \
--build-arg "BASE_TAG=$BASE_TAG" \
--build-arg "FLAVOR=$FLAVOR"
- >-
buildah build \
--label "org.opencontainers.image.source=${CI_PROJECT_URL}" \
--label "org.opencontainers.image.revision=$CI_COMMIT_SHA" \
--label "org.opencontainers.image.created=$CI_JOB_STARTED_AT" \
--label "org.opencontainers.image.version=${IMAGE_TAG}" \
-t "${CI_REGISTRY_IMAGE}:${IMAGE_TAG}" "$CI_PROJECT_DIR"
- buildah images
- buildah push "${CI_REGISTRY_IMAGE}:${IMAGE_TAG}"

View File

@@ -6,9 +6,7 @@
# https://docs.gitlab.com/runner/executors/docker.html#use-podman-to-run-docker-commands
#
ARG FLAVOR="stable"
ARG BASE_TAG="latest"
FROM quay.io/podman/$FLAVOR:$BASE_TAG
FROM quay.io/podman/stable:v4.3.1
# This is a list of packages to remove and/or exclude from the image.
# Primarily this is done for security reasons, should a runner process
@@ -59,6 +57,7 @@ RUN for rpm in ${EXCLUDE_PACKAGES}; do x+="--exclude=$rpm "; done && \
$DNFCMD update && \
$DNFCMD install $x $RUNNER_RPM_URL && \
$DNFCMD upgrade && \
$DNFCMD reinstall shadow-utils && \
if [[ "${DNFCMD}" == "${_DNFCMD}" ]]; then \
dnf clean all && \
rm -rf /var/cache/dnf; \
@@ -96,27 +95,38 @@ RUN sed -i -r \
/etc/containers/containers.conf && \
chmod +x /usr/local/bin/gitlab-runner-wrapper && \
chmod +x /usr/local/bin/podman-in-podman-maintenance && \
chown -R podman.podman /home/podman && \
chown -R podman:podman /home/podman && \
chmod u+s /usr/bin/new{uid,gid}map && \
rm -f /home/podman/.bash* && \
echo DOCKER_HOST="unix:///tmp/podman-run-1000/podman/podman.sock" > /etc/profile.d/podman.sh
echo DOCKER_HOST="unix:///tmp/podman-run-1000/podman/podman.sock" > /etc/profile.d/podman.sh && \
echo "podman:10000:10000" | tee /etc/subuid > /etc/subgid && \
setcap -n 10000 cap_setuid+ep /usr/bin/newuidmap && \
setcap -n 10000 cap_setuid+ep /usr/bin/newgidmap
# Runtime rootless-mode configuration
USER podman
VOLUME ["/home/podman/.local/share/containers/storage/",\
"/home/podman/.gitlab-runner/"]
# N/B: Volumes are cumulative with the base image
VOLUME ["/home/podman/.gitlab-runner/", "/cache"]
WORKDIR /home/podman
ENTRYPOINT ["/usr/local/bin/gitlab-runner-wrapper"]
# Ensure root storage directory exists with correct permissions
RUN mkdir -p .local/share/containers/storage
# Gitlab-runner configuration options. Default to unprivileged (nested)
# runner. Privileged is required to permit nested container image building.
ARG RUNNER_NAME="qontainers-pipglr"
ARG PRIVILEGED_RUNNER="false"
# Running inner-podman privileged is necessary at the time of this commit.
ARG PRIVILEGED_RUNNER="true"
# Tags allow pinning jobs to specific runners, comma-separated list of
# tags to add to runner (no spaces!)
ARG RUNNER_TAGS="podman-in-podman"
# Permit running jobs without any tag at all
ARG RUNNER_UNTAGGED="true"
ENV REGISTER_NON_INTERACTIVE="true" \
# Adjust based on usage and storage size to prevent ENOSPACE problems
ARG CLEAN_INTERVAL="24h"
ENV CLEAN_INTERVAL="$CLEAN_INTERVAL" \
REGISTER_NON_INTERACTIVE="true" \
RUNNER_TAG_LIST="$RUNNER_TAGS" \
REGISTER_RUN_UNTAGGED="$RUNNER_UNTAGGED" \
REGISTER_ACCESS_LEVEL="ref_protected" \
@@ -129,15 +139,19 @@ ENV REGISTER_NON_INTERACTIVE="true" \
DOCKER_HOST="unix:///tmp/podman-run-1000/podman/podman.sock" \
DOCKER_DEVICES="/dev/fuse" \
DOCKER_IMAGE="registry.fedoraproject.org/fedora-minimal:latest" \
DOCKER_CACHE_DIR="/home/podman/.cache/gitlab-runner" \
DOCKER_CACHE_DIR="/cache" \
DOCKER_VOLUMES="/cache" \
DOCKER_NETWORK_MODE="host" \
DOCKER_PRIVILEGED="$PRIVILEGED_RUNNER"
# Not a real build-arg. Simply here to save lots of typing.
ARG _pm="--systemd=true --device=/dev/fuse --security-opt label=disable --user podman --volume pipglr-podman-root:/home/podman/.local/share/containers/storage:Z --volume pipglr-runner-config:/home/podman/.gitlab-runner:Z -e PODMAN_RUNNER_DEBUG -e LOG_LEVEL"
ARG _pm="--systemd=true --device=/dev/fuse --security-opt label=disable --user podman --volume pipglr-podman-root:/home/podman/.local/share/containers --volume pipglr-config:/home/podman/.gitlab-runner -v pipglr-podman-cache:/cache --tmpfs /var/lib/containers,ro,size=1k -e PODMAN_RUNNER_DEBUG -e LOG_LEVEL"
# These labels simply make it easier to register and execute the runner.
# Define them last so they are absent should a image-build failure occur.
LABEL register="podman run -it --rm $_pm --secret REGISTRATION_TOKEN,type=env \$IMAGE register"
# Note: Privileged mode is required to permit building container images with inner-podman
LABEL run="podman run -d --rm --privileged --name gitlab-runner $_pm \$IMAGE run"
LABEL run="podman run -d --privileged --name pipglr $_pm \$IMAGE run"
# In case it's helpful, include the documentation
ADD /README.md /home/podman/

View File

@@ -27,21 +27,29 @@ lacks this feature, Several labels are set on the image to support
easy registration and execution of a runner container using a special
bash command. See the examples below for more information.
#### Volume Ownership Bug
#### [Volume setup]
Some versions of podman contain a bug where named volumes aren't owned
by the namespaced user within a rootless container (i.e. in conjunction
with the --user option). Since the `podman` user/group inside the `pipglr`
container is known, it's possible to manually set/reset ownership:
Since podman inside the container runs as user `podman`, the volumes
used by it need to be pre-created with ownership information. While,
we're at it, might as well add the performance-improving `noatime`,
option as well.
```bash
VOLUME=pipglr-podman-root
podman volume create $VOLUME
cd $(podman unshare podman volume mount $VOLUME)
podman unshare chown 1000:1000
podman volume unmount $VOLUME
$ VOLOPTS="o=uid=1000,gid=1000,noatime"; \
for VOLUME in pipglr-podman-root pipglr-config pipglr-podman-cache; do \
podman volume create --opt $VOLOPTS $VOLUME || true ; \
VOLPTH=$(podman unshare podman volume mount $VOLUME)
podman unshare chown -c -R 1000:1000 $VOLPTH && \
podman unshare chmod -c 02770 $VOLPTH && \
podman unshare podman volume unmount $VOLUME ; \
done
```
If you get `podman system service` startup permission-denied errors, or
errors from gitlab-runner, unable to connect to the podman socket, this is
likely the cause. You can fix it after-the-fact using the same commands
above.
#### Runner registration
Each time the registration command is run, a new runner is added into
@@ -52,20 +60,27 @@ For modern versions of podman, registration can be performed with the
following commands:
```bash
IMAGE="=registry.gitlab.com/qontainers/pipglr:latest"
echo '<actual registration token>' | podman secret create REGISTRATION_TOKEN -
podman container runlabel $IMAGE register --secret REGISTRATION_TOKEN,type=env
$ IMAGE="registry.gitlab.com/qontainers/pipglr:latest"
$ echo '<actual registration token>' | podman secret create REGISTRATION_TOKEN -
$ podman container runlabel register $IMAGE
```
Where `<actual registration token>` is the value obtained from the "runners"
settings page of a gitlab group or project.
settings page of a gitlab group or project. When you're finished registering
as many runners as you want, the secret is no-longer needed and may be removed:
Note: Some versions of podman don't support the `container runlabel` sub-command.
```bash
$ podman secret rm REGISTRATION_TOKEN
```
##### Note
Some versions of podman don't support the `container runlabel` sub-command.
If this is the case, you may simulate it with the following command (in addition
to the other example commands above):
```bash
eval $(podman inspect --format=json $IMAGE | jq -r .[].Labels.register)
$ eval $(podman inspect --format=json $IMAGE | jq -r .[].Labels.register)
```
#### Runner Startup
@@ -74,9 +89,11 @@ With one or more runners successfully registered and configured, the GitLab
runner container may be launched with the following commands:
```bash
podman container runlabel $IMAGE run
$ podman container runlabel run $IMAGE
```
##### Note
As above, if you're missing the `container runlabel` sub-command, the following
may be used instead (assuming `$IMAGE` remains set):
@@ -84,17 +101,41 @@ may be used instead (assuming `$IMAGE` remains set):
$ eval $(podman inspect --format=json $IMAGE | jq -r .[].Labels.run)
```
#### Runner configuration
You may inspect/modify the gitlab-runner configuration as you see fit, just be
sure to use the `podman unshare` command-wrapper to enter the usernamespace.
For example, to display the config:
```bash
$ podman unshare cat $(podman unshare podman volume mount pipglr-config)/config.toml
```
Edit the config with your favorite `$EDITOR`:
```bash
$ podman unshare $EDITOR $(podman unshare podman volume mount pipglr-config)/config.toml
```
#### Debugging
Before starting the runner, you may `export PODMAN_RUNNER_DEBUG=debug` to enable
debugging on the inner-podman. Whereas `export LOG_LEVEL=debug` can be used to
debug the gitlab-runner itself.
The first thing to check is the container output:
```bash
$ podman logs --since 0 pipglr
```
Next, try running pipglr after an `export PODMAN_RUNNER_DEBUG=debug` to enable
debugging on the inner-podman. If more runner detail is needed, you can instead/additionally
set `export LOG_LEVEL=debug` to debug the gitlab-runner itself.
## Building
This image may be built simply with:
`podman build -t registry.gitlab.com/qontainers/pipglr:latest .`
```bash
$ podman build -t registry.gitlab.com/qontainers/pipglr:latest .
```
This will utilize the latest stable version of podman and the latest
stable version of the gitlab runner.
@@ -120,6 +161,10 @@ Several build arguments are available to control the output image:
exact podman version. Possible values include, `latest`, `vX`, `vX.Y`,
and `vX.Y.Z` (where, `X`, `Y`, and `Z` represent the podman semantic
version numbers). It's also possible to specify an image SHA.
* `CLEAN_INTERVAL` - A `sleep` (command) compatible time-argument that
determines how often to clean out podman storage of disused containers and
images. Defaults to 24-hours, but should be adjusted based on desired caching-effect
versus available storage space and rate of job execution.
* `EXCLUDE_PACKAGES` - A space-separated list of RPM packages to prevent
their existence in the final image. This is intended as a security measure
to limit the attack-surface should a gitlab-runner process escape it's
@@ -140,10 +185,9 @@ Several build arguments are available to control the output image:
and port supports various observability and debugging features of the
gitlab runner. For more information see the [gitlab runner advanced
configuration documentation](https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-global-section).
* `PRIVILEGED_RUNNER` - Defaults to 'false', may be set 'true'. When
`true`, this causes inner-containers to be created with the `--privileged`
flag. This is a potential security weakness, but is necessary for
(among other things) allowing nested container image builds.
* `PRIVILEGED_RUNNER` - Defaults to 'true', may be set 'true' if you're brave.
However this may result in the gitlab-runner failing to launch inner-containers.
Setting it false will also prevent building container images using the runner.
* `RUNNER_TAGS` - Defaults to `podman_in_podman`, may be set to any comma-separated
list (with no spaces!) of tags. These show up in GitLab (not the runner
configuration), and determines where jobs are run.

View File

@@ -4,16 +4,23 @@
# a podman-in-podman gitlab runner container. Any usage
# outside that context is not supported and may cause harm.
set -e
set -eo pipefail
maintain_podman() {
# Two days seems to be a good happy-medium beween filling up
# about 40gig of storage space from moderate CI activity,
# and maintaining a useful level of caching.
while sleep 2d; do
while sleep "$CLEAN_INTERVAL"; do
if [[ -n "$PODMAN_RUNNER_DEBUG" ]]; then
echo "$(date --iso-8601=second) ${BASH_SOURCE[0] performing podman maintenance}"
fi
podman system prune --all --force
done
}
if [[ -z "$CLEAN_INTERVAL" ]]; then
echo "ERROR: Empty/unset \$CLEAN_INTERVAL"
exit 1
fi
maintain_podman