Skip to content

Commit ae3e35e

Browse files
authored
Add exe compression upx (#545)
* remove comments to reduce diff * add upx compression in the build * use fastest compession * pin xz version * pin curl version too * use repo upx version * change size requirements
1 parent ce55006 commit ae3e35e

File tree

5 files changed

+32
-29
lines changed

5 files changed

+32
-29
lines changed

.gitlab/scripts/check_layer_size.sh

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,35 @@
99

1010
set -e
1111

12+
validate_size() {
13+
local max_size=$1
14+
local file_size=$2
15+
if [ "$file_size" -gt "$max_size" ]; then
16+
echo "Size $file_size exceeded limit $max_size kb"
17+
exit 1
18+
fi
19+
}
20+
1221
if [ -z "$LAYER_FILE" ]; then
1322
echo "[ERROR]: LAYER_FILE not specified"
1423
exit 1
1524
fi
1625

17-
MAX_LAYER_COMPRESSED_SIZE_KB=$(expr 20 \* 1024) # 20 MB, amd64 is 19, while arm64 is 18
18-
MAX_LAYER_UNCOMPRESSED_SIZE_KB=$(expr 53 \* 1024) # 53 MB, amd is 53, while arm64 is 47
19-
20-
LAYERS_DIR=".layers"
26+
MAX_LAYER_COMPRESSED_SIZE_KB=$(( 23 * 1024)) # 23 MB, amd64 is 22, while arm64 is 20
27+
MAX_LAYER_UNCOMPRESSED_SIZE_KB=$(( 30 * 1024)) # 53 MB -> 30MB with UPX
2128

22-
FILE=$LAYERS_DIR/$LAYER_FILE
23-
FILE_SIZE=$(stat --printf="%s" $FILE)
29+
FILE=".layers"/$LAYER_FILE
30+
FILE_SIZE=$(stat --printf="%s" "$FILE")
2431
FILE_SIZE_KB="$(( ${FILE_SIZE%% *} / 1024))"
25-
echo "Layer file ${FILE} has zipped size ${FILE_SIZE_KB} kb"
26-
if [ "$FILE_SIZE_KB" -gt "$MAX_LAYER_COMPRESSED_SIZE_KB" ]; then
27-
echo "Zipped size exceeded limit $MAX_LAYER_COMPRESSED_SIZE_KB kb"
28-
exit 1
29-
fi
32+
3033
mkdir tmp
31-
unzip -q $FILE -d tmp
34+
unzip -q "$FILE" -d tmp
3235
UNZIPPED_FILE_SIZE=$(du -shb tmp/ | cut -f1)
3336
UNZIPPED_FILE_SIZE_KB="$(( ${UNZIPPED_FILE_SIZE%% *} / 1024))"
3437
rm -rf tmp
38+
39+
echo "Layer file ${FILE} has zipped size ${FILE_SIZE_KB} kb"
3540
echo "Layer file ${FILE} has unzipped size ${UNZIPPED_FILE_SIZE_KB} kb"
36-
if [ "$UNZIPPED_FILE_SIZE_KB" -gt "$MAX_LAYER_UNCOMPRESSED_SIZE_KB" ]; then
37-
echo "Unzipped size exceeded limit $MAX_LAYER_UNCOMPRESSED_SIZE_KB kb"
38-
exit 1
39-
fi
41+
42+
validate_size "$MAX_LAYER_COMPRESSED_SIZE_KB" $FILE_SIZE_KB
43+
validate_size "$MAX_LAYER_UNCOMPRESSED_SIZE_KB" $UNZIPPED_FILE_SIZE_KB

scripts/Dockerfile.alpine.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ WORKDIR /extensions
5555
COPY --from=builder /tmp/dd/datadog-agent/"${CMD_PATH}"/datadog-agent /extensions/datadog-agent
5656
RUN strip /extensions/datadog-agent
5757

58-
5958
COPY ./scripts/$DATADOG_WRAPPER /$DATADOG_WRAPPER
6059
RUN chmod +x /$DATADOG_WRAPPER
6160
RUN zip -r datadog_extension.zip /extensions /$DATADOG_WRAPPER

scripts/Dockerfile.bottlecap.alpine.build

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ ARG DATADOG_WRAPPER=datadog_wrapper
3232
ARG GO_AGENT_PATH
3333

3434
RUN apt-get update
35-
RUN apt-get install -y zip binutils
35+
RUN apt-get install -y zip binutils upx
3636

3737
COPY .layers/$GO_AGENT_PATH/extensions/datadog-agent /datadog-agent-go
38-
RUN strip /datadog-agent-go # just in case
38+
RUN strip /datadog-agent-go
39+
RUN upx -1 /datadog-agent-go
40+
3941

4042
RUN mkdir /extensions
4143
WORKDIR /extensions

scripts/Dockerfile.bottlecap.build

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ RUN yum install -y curl gcc gcc-c++ make unzip
77
# Install Protocol Buffers compiler by hand, since AL2 does not have a recent enough version.
88
COPY ./scripts/install-protoc.sh /
99
RUN chmod +x /install-protoc.sh && /install-protoc.sh
10-
11-
# Install Rust Toolchain
1210
RUN curl https://sh.rustup.rs -sSf | \
1311
sh -s -- --profile minimal --default-toolchain stable-$PLATFORM-unknown-linux-gnu -y
1412
ENV PATH=/root/.cargo/bin:$PATH
1513
RUN rustup component add rust-src --toolchain stable-$PLATFORM-unknown-linux-gnu
16-
17-
# Build Bottlecap
1814
RUN mkdir -p /tmp/dd
1915
COPY ./bottlecap/src /tmp/dd/bottlecap/src
2016
COPY ./bottlecap/Cargo.toml /tmp/dd/bottlecap/Cargo.toml
@@ -24,16 +20,16 @@ WORKDIR /tmp/dd/bottlecap
2420
RUN --mount=type=cache,target=/usr/local/cargo/registry cargo +stable build --release --target $PLATFORM-unknown-linux-gnu
2521
RUN cp /tmp/dd/bottlecap/target/$PLATFORM-unknown-linux-gnu/release/bottlecap /tmp/dd/bottlecap/bottlecap
2622

27-
# Zip Extension
2823
FROM ubuntu:22.04 as compresser
2924
ARG DATADOG_WRAPPER=datadog_wrapper
3025
ARG GO_AGENT_PATH
3126

3227
RUN apt-get update
33-
RUN apt-get install -y zip binutils
34-
28+
RUN apt-get install -y zip binutils upx
3529
COPY .layers/$GO_AGENT_PATH/extensions/datadog-agent /datadog-agent-go
36-
RUN strip /datadog-agent-go # just in case
30+
RUN strip /datadog-agent-go
31+
RUN upx -1 /datadog-agent-go
32+
3733

3834
RUN mkdir /extensions
3935
WORKDIR /extensions

scripts/Dockerfile.bottlecap.dev

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ WORKDIR /tmp/dd/bottlecap
1919
RUN --mount=type=cache,target=/usr/local/cargo/registry cargo +stable build --release --target $PLATFORM-unknown-linux-gnu
2020
RUN cp /tmp/dd/bottlecap/target/$PLATFORM-unknown-linux-gnu/release/bottlecap /tmp/dd/bottlecap/bottlecap
2121

22-
# zip the extension
2322
FROM ubuntu:22.04 as compresser
2423
ARG DATADOG_WRAPPER=datadog_wrapper
2524

2625
RUN apt-get update
27-
RUN apt-get install -y zip binutils
26+
RUN apt-get install -y zip binutils upx
2827
COPY --from=public.ecr.aws/datadog/lambda-extension:64 /opt/datadog-agent-go /datadog-agent-go
28+
RUN du -h /datadog-agent-go
29+
RUN upx -1 /datadog-agent-go
30+
RUN du -h /datadog-agent-go
2931
RUN mkdir /extensions
3032
WORKDIR /extensions
3133

0 commit comments

Comments
 (0)