|
| 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