Skip to content

Commit 98ba5d7

Browse files
[CINDER-CSI] shrink image, remove unnecessary utils (#2233) (#2238)
1 parent 8872f00 commit 98ba5d7

File tree

3 files changed

+151
-3
lines changed

3 files changed

+151
-3
lines changed

Dockerfile

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,33 @@ CMD ["sh", "-c", "/bin/barbican-kms-plugin --socketpath ${socketpath} --cloud-co
106106
##
107107
## cinder-csi-plugin
108108
##
109-
FROM --platform=${TARGETPLATFORM} ${DEBIAN_IMAGE} as cinder-csi-plugin
110109

111-
# Install e4fsprogs for format
112-
RUN clean-install btrfs-progs e2fsprogs mount udev xfsprogs
110+
# step 1: copy all necessary files from Debian distro to /dest folder
111+
# all magic heppens in tools/csi-deps.sh
112+
FROM --platform=${TARGETPLATFORM} ${DEBIAN_IMAGE} as cinder-csi-plugin-utils
113113

114+
RUN clean-install bash rsync mount udev btrfs-progs e2fsprogs xfsprogs
115+
COPY tools/csi-deps.sh /tools/csi-deps.sh
116+
RUN /tools/csi-deps.sh
117+
118+
# step 2: check if all necessary files are copied and work properly
119+
# the build have to finish without errors, but the result image will not be used
120+
FROM --platform=${TARGETPLATFORM} ${DISTROLESS_IMAGE} as cinder-csi-plugin-utils-check
121+
122+
COPY --from=cinder-csi-plugin-utils /dest /
123+
COPY --from=cinder-csi-plugin-utils /bin/sh /bin/sh
124+
COPY tools/csi-deps-check.sh /tools/csi-deps-check.sh
125+
126+
SHELL ["/bin/sh"]
127+
RUN /tools/csi-deps-check.sh
128+
129+
# step 3: build tiny cinder-csi-plugin image with only necessary files
130+
FROM --platform=${TARGETPLATFORM} ${DISTROLESS_IMAGE} as cinder-csi-plugin
131+
132+
# Copying csi-deps-check.sh simply ensures that the resulting image has a dependency
133+
# on cinder-csi-plugin-utils-check and therefore that the check has passed
134+
COPY --from=cinder-csi-plugin-utils-check /tools/csi-deps-check.sh /bin/csi-deps-check.sh
135+
COPY --from=cinder-csi-plugin-utils /dest /
114136
COPY --from=builder /build/cinder-csi-plugin /bin/cinder-csi-plugin
115137
COPY --from=certs /etc/ssl/certs /etc/ssl/certs
116138

tools/csi-deps-check.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/sh
2+
3+
# Copyright 2022 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
19+
# We will check all necessary utils in the image.
20+
# They all have to launch without errors.
21+
22+
# This utils are using by
23+
# go mod k8s.io/mount-utils
24+
/bin/mount -V
25+
/bin/umount -V
26+
/sbin/blkid -V
27+
/sbin/blockdev -V
28+
/sbin/dumpe2fs -V
29+
/sbin/fsck --version
30+
/sbin/mke2fs -V
31+
/sbin/mkfs.ext4 -V
32+
/sbin/mkfs.xfs -V
33+
/usr/sbin/xfs_io -V
34+
/sbin/xfs_repair -V
35+
/usr/sbin/xfs_growfs -V
36+
/bin/btrfs --version
37+
38+
# This utils are using by
39+
# go mod k8s.io/cloud-provider-openstack/pkg/util/mount
40+
/bin/udevadm --version
41+
/bin/findmnt -V

tools/csi-deps.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/bin/sh
2+
3+
# Copyright 2022 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
20+
#
21+
# We will copy all dependencies for CSI Node driver to /dest directory
22+
# all utils are using by cinder-csi-plugin
23+
# to format/mount/unmount/resize the volumes.
24+
#
25+
# It is very important to have slim image,
26+
# because it runs as root (privileged mode) on the nodes
27+
#
28+
29+
DEST=/dest
30+
31+
copy_deps() {
32+
PROG="$1"
33+
34+
mkdir -p "${DEST}$(dirname $PROG)"
35+
36+
if [ -d "${PROG}" ]; then
37+
rsync -av "${PROG}/" "${DEST}${PROG}/"
38+
else
39+
cp -Lv "$PROG" "${DEST}${PROG}"
40+
fi
41+
42+
if [ -x ${PROG} -o $(/usr/bin/ldd "$PROG" >/dev/null) ]; then
43+
DEPS="$(/usr/bin/ldd "$PROG" | /bin/grep '=>' | /usr/bin/awk '{ print $3 }')"
44+
45+
for d in $DEPS; do
46+
mkdir -p "${DEST}$(dirname $d)"
47+
cp -Lv "$d" "${DEST}${d}"
48+
done
49+
fi
50+
}
51+
52+
# Commmon lib /lib64/ld-linux-*.so.2
53+
# needs for all utils
54+
mkdir -p ${DEST}/lib64 && cp -Lv /lib64/ld-linux-*.so.2 ${DEST}/lib64/
55+
56+
# This utils are using by
57+
# go mod k8s.io/mount-utils
58+
copy_deps /etc/mke2fs.conf
59+
copy_deps /bin/mount
60+
copy_deps /bin/umount
61+
copy_deps /sbin/blkid
62+
copy_deps /sbin/blockdev
63+
copy_deps /sbin/dumpe2fs
64+
copy_deps /sbin/fsck
65+
copy_deps /sbin/fsck.xfs
66+
cp /sbin/fsck* ${DEST}/sbin/
67+
copy_deps /sbin/e2fsck
68+
# from pkg e2fsprogs - e2image, e2label, e2scrub and etc.
69+
cp /sbin/e2* ${DEST}/sbin/
70+
copy_deps /sbin/mke2fs
71+
copy_deps /sbin/resize2fs
72+
cp /sbin/mkfs* ${DEST}/sbin/
73+
copy_deps /sbin/mkfs.xfs
74+
copy_deps /sbin/xfs_repair
75+
copy_deps /usr/sbin/xfs_growfs
76+
copy_deps /usr/sbin/xfs_io
77+
cp /usr/sbin/xfs* ${DEST}/usr/sbin/
78+
copy_deps /bin/btrfs
79+
cp /bin/btrfs* ${DEST}/bin/
80+
81+
# This utils are using by
82+
# go mod k8s.io/cloud-provider-openstack/pkg/util/mount
83+
copy_deps /bin/udevadm
84+
copy_deps /lib/udev/rules.d
85+
copy_deps /bin/findmnt

0 commit comments

Comments
 (0)