Sync from SUSE:SLFO:Main obs-service-docker_label_helper revision 15706eadc3ca99b631ff5a48b4c92313

This commit is contained in:
Adrian Schröter 2024-05-03 17:13:38 +02:00
commit 5fa0eac737
7 changed files with 284 additions and 0 deletions

23
.gitattributes vendored Normal file
View File

@ -0,0 +1,23 @@
## Default LFS
*.7z filter=lfs diff=lfs merge=lfs -text
*.bsp filter=lfs diff=lfs merge=lfs -text
*.bz2 filter=lfs diff=lfs merge=lfs -text
*.gem filter=lfs diff=lfs merge=lfs -text
*.gz filter=lfs diff=lfs merge=lfs -text
*.jar filter=lfs diff=lfs merge=lfs -text
*.lz filter=lfs diff=lfs merge=lfs -text
*.lzma filter=lfs diff=lfs merge=lfs -text
*.obscpio filter=lfs diff=lfs merge=lfs -text
*.oxt filter=lfs diff=lfs merge=lfs -text
*.pdf filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.rpm filter=lfs diff=lfs merge=lfs -text
*.tbz filter=lfs diff=lfs merge=lfs -text
*.tbz2 filter=lfs diff=lfs merge=lfs -text
*.tgz filter=lfs diff=lfs merge=lfs -text
*.ttf filter=lfs diff=lfs merge=lfs -text
*.txz filter=lfs diff=lfs merge=lfs -text
*.whl filter=lfs diff=lfs merge=lfs -text
*.xz filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text
*.zst filter=lfs diff=lfs merge=lfs -text

61
README Normal file
View File

@ -0,0 +1,61 @@
obs-service-docker_label_helper
===============================
This service can be enabled to run during buildtime, when it will edit the
Dockerfile to expand the LABEL instruction.
A new syntax using entry/exit flags has been introduced to provide
compatibility with stock docker build command while allowing to add prefixed
labels based on existing LABEL instructions.
Both new and old syntax are supported, but you should not mix both in the
same Dockerfile.
1 - New syntax using entry/exit flags
Only LABEL between # labelprefix and # endlabelprefix will be considered
Example:
```
FROM opensuse/tumbleweed
# labelprefix=org.opensuse.nano
LABEL org.opencontainers.image.title="Example container"
LABEL org.opencontainers.image.description="This contains nano"
# endlabelprefix
LABEL not.expanded.label="example"
```
expands to
```
FROM opensuse/tumbleweed
LABEL org.opensuse.nano.title="Example container"
LABEL org.opencontainers.image.title="Example container"
LABEL org.opensuse.nano.description="This contains nano"
LABEL org.opencontainers.image.description="This contains nano"
LABEL not.expanded.label="example"
```
2 - Old syntax
Expand the PREFIXEDLABEL instruction.
Example:
```
FROM opensuse/tumbleweed
# labelprefix=org.opensuse.nano
PREFIXEDLABEL org.opencontainers.image.title="Example container"
PREFIXEDLABEL org.opencontainers.image.description="This contains nano"
```
expands to
```
FROM opensuse/tumbleweed
LABEL org.opensuse.nano.title="Example container"
LABEL org.opencontainers.image.title="Example container"
LABEL org.opensuse.nano.description="This contains nano"
LABEL org.opencontainers.image.description="This contains nano"
```

27
docker_label_helper Normal file
View File

@ -0,0 +1,27 @@
#!/bin/bash
set -eu
if [ "${BUILD_DIST+x}" != "x" ]; then
echo "Not running in an OBS build container"
exit 1
fi
BUILD_DATA="${BUILD_DIST/.dist/.data}"
if [ -e "${BUILD_DATA}" ]; then
. "${BUILD_DATA}"
# The build script renames the recipe (to strip _service:foo:), but doesn't update .data
RECIPEFILE="${RECIPEFILE##*:}"
if [ "${RECIPEFILE}" != "Dockerfile" ]; then
echo "Recipe is not a Dockerfile - exiting"
exit 0
fi
fi
# Note: Avoid assigning to $1 etc. as that converts whitespace (field separators) to single spaces.
gawk -i inplace '
match($0, /^# labelprefix=(.*)$/, m) { labelprefix=m[1]; next }
labelprefix != "" && match($0, /^(PREFIXED)?LABEL[[:space:]]+[^=]*\.([^.=]*)=(.*)$/, m) { printf "LABEL %s.%s=%s\n", labelprefix, m[2], m[3]; gsub(/^PREFIXEDLABEL/, "LABEL") }
match($0, /^# endlabelprefix/) { labelprefix=""; next }
1' Dockerfile

View File

@ -0,0 +1,4 @@
<service name="docker_label_helper">
<summary>Add prefixed labels in Dockerfile builds</summary>
<description>An OBS service which expands LABEL instructions in Dockerfile with prefixes. Useful for implementing https://en.opensuse.org/Building_derived_containers#Labels.</description>
</service>

View File

@ -0,0 +1,52 @@
-------------------------------------------------------------------
Wed Mar 1 07:54:19 UTC 2023 - Fabian Vogt <fvogt@suse.com>
- Handle LABEL statements with any whitespace
- Handle LABEL values containing "=" properly
-------------------------------------------------------------------
Wed Dec 7 09:48:52 UTC 2022 - Dominique Leuenberger <dimstar@opensuse.org>
- Do not ever use "%setup -n .": rpm 4.18 tries to be cleaner and
remove stuff it extraced, which would lead to 'rm -rf .', which
rm does not like. Use "%setup -c" instead, which creates the
appropriate %{name}-%{version} directory expected.
-------------------------------------------------------------------
Thu Jul 21 12:50:28 UTC 2022 - Fabian Vogt <fvogt@suse.com>
- Avoid mangling whitespace by using gsub instead of assigning to
fields
-------------------------------------------------------------------
Wed Dec 15 14:52:19 UTC 2021 - Fabian Vogt <fvogt@suse.com>
- Improve summary, description and README.md a bit
- Mention bsc#1193429 in this changelog
-------------------------------------------------------------------
Fri Jul 23 08:50:08 UTC 2021 - Bruno Leon <bruno.leon@suse.com>
- Improve regexp instead of having two as proposed by Fabian Vogt
-------------------------------------------------------------------
Wed Jul 21 10:38:53 UTC 2021 - Bruno Leon <bruno.leon@suse.com>
- Retain compatibility with old syntax. Update documentation.
-------------------------------------------------------------------
Tue Jul 20 14:28:28 UTC 2021 - Bruno Leon <bruno.leon@suse.com>
- Use entry/exit point to determine whether labels should be expanded.
Doing so makes the Dockerfile compatible with standard
docker build command.
-------------------------------------------------------------------
Fri Sep 11 10:07:44 UTC 2020 - Fabian Vogt <fvogt@suse.com>
- Add documentation and a test
-------------------------------------------------------------------
Tue May 5 09:03:39 UTC 2020 - Fabian Vogt <fvogt@suse.com>
- Initial commit

View File

@ -0,0 +1,58 @@
#
# spec file for package obs-service-docker_label_helper
#
# Copyright (c) 2023 SUSE LLC
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.
# Please submit bugfixes or comments via https://bugs.opensuse.org/
#
Name: obs-service-docker_label_helper
Version: 0.0
Release: 0
Summary: OBS Service to add prefixed labels in Dockerfile builds
License: GPL-2.0-or-later
Group: Development/Tools/Building
URL: https://build.opensuse.org
Source0: docker_label_helper.service
Source1: docker_label_helper
Source2: README
Source3: test.sh
Requires: gawk
BuildArch: noarch
# For %check
BuildRequires: diffutils
%description
An OBS service which expands LABEL instructions in Dockerfile with prefixes.
Useful for implementing https://en.opensuse.org/Building_derived_containers#Labels.
%prep
%setup -q -D -T -c
cp %{SOURCE2} .
%build
%install
mkdir -p %{buildroot}%{_prefix}/lib/obs/service
install -m 0644 %{SOURCE0} %{buildroot}%{_prefix}/lib/obs/service
install -m 0755 %{SOURCE1} %{buildroot}%{_prefix}/lib/obs/service
%check
sh %{SOURCE3}
%files
%doc README
%dir %{_prefix}/lib/obs
%{_prefix}/lib/obs/service
%changelog

59
test.sh Normal file
View File

@ -0,0 +1,59 @@
#!/bin/sh
set -eu
tmpdir=$(mktemp -d)
trap 'rm -rf ${tmpdir}' EXIT
script="$(realpath "$(dirname $0)")/docker_label_helper"
cd $tmpdir
# Test old syntax
cat >Dockerfile <<EOF
# labelprefix=org.opensuse.nano
PREFIXEDLABEL org.opencontainers.image.title="Example container."
PREFIXEDLABEL org.opencontainers.image.description="This contains nano"
PREFIXEDLABEL org.opencontainers.image.source="https://build.opensuse.org/package/show/openSUSE:Factory/opensuse-tumbleweed-image?rev=1afbea7e9b8ecf976071564312c2db66"
PREFIXEDLABEL test.whitespace="Two spaces. One tab."
EOF
export BUILD_DIST=
sh "${script}"
diff -u Dockerfile - <<EOF
LABEL org.opensuse.nano.title="Example container."
LABEL org.opencontainers.image.title="Example container."
LABEL org.opensuse.nano.description="This contains nano"
LABEL org.opencontainers.image.description="This contains nano"
LABEL org.opensuse.nano.source="https://build.opensuse.org/package/show/openSUSE:Factory/opensuse-tumbleweed-image?rev=1afbea7e9b8ecf976071564312c2db66"
LABEL org.opencontainers.image.source="https://build.opensuse.org/package/show/openSUSE:Factory/opensuse-tumbleweed-image?rev=1afbea7e9b8ecf976071564312c2db66"
LABEL org.opensuse.nano.whitespace="Two spaces. One tab."
LABEL test.whitespace="Two spaces. One tab."
EOF
rm -f Dockerfile
# Test new syntax
cat >Dockerfile <<EOF
# labelprefix=org.opensuse.nano
LABEL org.opencontainers.image.title="Example container."
LABEL org.opencontainers.image.description="This contains nano"
LABEL org.opencontainers.image.source="https://build.opensuse.org/package/show/openSUSE:Factory/opensuse-tumbleweed-image?rev=1afbea7e9b8ecf976071564312c2db66"
LABEL test.whitespace="Two spaces. One tab."
# endlabelprefix
LABEL not.expanded.label="example"
EOF
export BUILD_DIST=
sh "${script}"
diff -u Dockerfile - <<EOF
LABEL org.opensuse.nano.title="Example container."
LABEL org.opencontainers.image.title="Example container."
LABEL org.opensuse.nano.description="This contains nano"
LABEL org.opencontainers.image.description="This contains nano"
LABEL org.opensuse.nano.source="https://build.opensuse.org/package/show/openSUSE:Factory/opensuse-tumbleweed-image?rev=1afbea7e9b8ecf976071564312c2db66"
LABEL org.opencontainers.image.source="https://build.opensuse.org/package/show/openSUSE:Factory/opensuse-tumbleweed-image?rev=1afbea7e9b8ecf976071564312c2db66"
LABEL org.opensuse.nano.whitespace="Two spaces. One tab."
LABEL test.whitespace="Two spaces. One tab."
LABEL not.expanded.label="example"
EOF