Skip to content

Commit 2e2db05

Browse files
committed
Add verify script to detect dead code
1 parent f7fc231 commit 2e2db05

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

hack/verify-vpa-flags.sh renamed to hack/verify-vpa.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ set -o nounset
1919
set -o pipefail
2020

2121
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
22-
VPA_FLAGS_SCRIPT="${KUBE_ROOT}/vertical-pod-autoscaler/hack/verify-vpa-flags.sh"
22+
VPA_VERIFY_SCRIPTS="${KUBE_ROOT}/vertical-pod-autoscaler/hack/verify-*.sh"
2323

24-
echo "Running VPA flags verification..."
25-
"$VPA_FLAGS_SCRIPT"
24+
for script in $VPA_VERIFY_SCRIPTS; do
25+
echo "Running VPA verification script ${script}..."
26+
"${script}"
27+
done

vertical-pod-autoscaler/hack/update-codegen.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ set -o pipefail
2020

2121
GO_CMD=${1:-go}
2222
CURRENT_DIR=$(dirname "${BASH_SOURCE[0]}")
23-
REPO_ROOT="$(git rev-parse --show-toplevel)"
24-
CODEGEN_PKG=$($GO_CMD list -m -mod=readonly -f "{{.Dir}}" k8s.io/code-generator)
2523
cd "${CURRENT_DIR}/.."
24+
go mod download
25+
CODEGEN_PKG=$($GO_CMD list -m -mod=readonly -f "{{.Dir}}" k8s.io/code-generator)
26+
27+
REPO_ROOT="$(git rev-parse --show-toplevel)"
2628

2729
# shellcheck source=/dev/null
2830
source "${CODEGEN_PKG}/kube_codegen.sh"
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2025 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+
# This script verifies if the golang linker is eliminating dead code in
18+
# various components we care about, such as kube-apiserver, kubelet and others
19+
# Usage: `hack/verify-deadcode-elimination.sh`.
20+
21+
set -o errexit
22+
set -o nounset
23+
set -o pipefail
24+
25+
SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
26+
cd "${SCRIPT_ROOT}"
27+
COMPONENTS=("admission-controller" "recommender" "updater")
28+
FAILED=false
29+
30+
# Install whydeadcode
31+
go install github.com/aarzilli/whydeadcode@latest
32+
33+
# Prefer full path for running zeitgeist
34+
WHYDEADCODE_BIN="$(which whydeadcode)"
35+
36+
for binary in "${COMPONENTS[@]}"; do
37+
echo "Processing ${binary} ..."
38+
pushd "pkg/${binary}"
39+
output=$(GOLDFLAGS=-dumpdep go build -ldflags=-dumpdep -o "${binary}" 2>&1 | grep "\->" | ${WHYDEADCODE_BIN} 2>&1)
40+
if [[ -n "$output" ]]; then
41+
echo "golang linker is not eliminating dead code in ${binary}, please check the trace output below:"
42+
echo "(NOTE: that there may be false positives, but the first trace should be a real issue)"
43+
echo "$output"
44+
FAILED=true
45+
FAILED_BINARIES+=("${binary}")
46+
fi
47+
48+
# Find the binary and print its size
49+
echo "Finding ${binary} binary and checking its size:"
50+
ls -altrh "${binary}"
51+
echo ""
52+
popd
53+
done
54+
55+
if [[ "$FAILED" == "true" ]]; then
56+
echo "Dead code elimination check failed for the following binaries:"
57+
for failed_binary in "${FAILED_BINARIES[@]}"; do
58+
echo " - ${failed_binary}"
59+
done
60+
exit 1
61+
fi

0 commit comments

Comments
 (0)