Skip to content

Commit 9ede04c

Browse files
author
OpenShift Bot
authored
Merge pull request #14116 from stevekuznetsov/skuznets/cleanup_all
Merged by openshift-bot
2 parents a2e505b + 8c7f1a7 commit 9ede04c

19 files changed

+259
-456
lines changed

hack/common.sh

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,6 @@ os::build::internal::build_binaries() {
210210
fi
211211
done
212212

213-
# Temporarily enable swap for the duration of the build until we move
214-
# to Go 1.7
215-
os::build::enable_swap
216-
trap "os::build::disable_swap" EXIT
217-
218213
local host_platform=$(os::build::host_platform)
219214
local platform
220215
for platform in "${platforms[@]+"${platforms[@]}"}"; do
@@ -785,87 +780,6 @@ function os::build::image() {
785780
}
786781
readonly -f os::build::image
787782

788-
# os::build::enable_swap attempts to enable swap for the system if a) this is Linux and b)
789-
# the amount of physical memory is less than 10GB. This is a stopgap until we have
790-
# better control over memory use in Go 1.7+.
791-
function os::build::enable_swap() {
792-
# if we aren't on linux or have more than 9GB of memory
793-
if [[ -n "${OS_BUILD_SWAP_DISABLE-}" || "$(go env GOHOSTOS)" != "linux" || "$( os::build::physmem_gb )" -gt 9 ]]; then
794-
return
795-
fi
796-
# if we don't have the swapon command available
797-
if ! swapon &>/dev/null; then
798-
return
799-
fi
800-
# if swap is already on
801-
if [[ "$( swapon --show --noheadings | wc -l )" -ne 0 ]]; then
802-
return
803-
fi
804-
echo "++ temporarily enabling swap space to assist in building in limited memory - use OS_BUILD_SWAP_DISABLE=1 to bypass"
805-
806-
(
807-
set -e
808-
sudo cp /etc/fstab /origin-backupfstab
809-
sudo dd if=/dev/zero of=/origin-swapfile bs=1M count=${OS_BUILD_SWAP_SIZE:-2048}
810-
sudo chmod 600 /origin-swapfile
811-
sudo mkswap /origin-swapfile
812-
sudo swapon /origin-swapfile
813-
sudo /bin/sh -c 'echo "/origin-swapfile none swap defaults 0 0" >> /etc/fstab'
814-
) &>/dev/null || true
815-
}
816-
readonly -f os::build::enable_swap
817-
818-
# os::build::disable_swap undoes the effects of os::build::enable_swap
819-
function os::build::disable_swap() {
820-
# if we aren't on linux or have more than 9GB of memory
821-
if [[ -n "${OS_BUILD_SWAP_DISABLE-}" || "$(go env GOHOSTOS)" != "linux" ]]; then
822-
return
823-
fi
824-
# if we previously set up a swapfile
825-
if [[ ! -f /origin-swapfile ]]; then
826-
return
827-
fi
828-
(
829-
set +e
830-
sudo swapoff /origin-swapfile
831-
sudo cp /origin-backupfstab /etc/fstab
832-
sudo rm -f /origin-backupfstab /origin-swapfile
833-
) || true
834-
}
835-
readonly -f os::build::disable_swap
836-
837-
# os::build::physmem_gb returns the approximate gigabytes of memory on the system
838-
# This is copied verbatim from kube for the purposes of detecting how much available
839-
# memory we have for building.
840-
function os::build::physmem_gb() {
841-
local mem
842-
843-
# Linux kernel version >=3.14, in kb
844-
if mem=$(grep MemAvailable /proc/meminfo | awk '{ print $2 }'); then
845-
echo $(( ${mem} / 1048576 ))
846-
return
847-
fi
848-
849-
# Linux, in kb
850-
if mem=$(grep MemTotal /proc/meminfo | awk '{ print $2 }'); then
851-
echo $(( ${mem} / 1048576 ))
852-
return
853-
fi
854-
855-
# OS X, in bytes. Note that get_physmem, as used, should only ever
856-
# run in a Linux container (because it's only used in the multiple
857-
# platform case, which is a Dockerized build), but this is provided
858-
# for completeness.
859-
if mem=$(sysctl -n hw.memsize 2>/dev/null); then
860-
echo $(( ${mem} / 1073741824 ))
861-
return
862-
fi
863-
864-
# If we can't infer it, just give up and assume a low memory system
865-
echo 1
866-
}
867-
readonly -f os::build::physmem_gb
868-
869783
# os::build::require_clean_tree exits if the current Git tree is not clean.
870784
function os::build::require_clean_tree() {
871785
if ! git diff-index --quiet HEAD -- || test $(git ls-files --exclude-standard --others | wc -l) != 0; then

hack/lib/cleanup.sh

Lines changed: 149 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,79 @@
33
# This library holds functions that are used to clean up local
44
# system state after other scripts have run.
55

6+
# os::cleanup::all will clean up all of the processes and data that
7+
# a script leaves around after running. All of the sub-tasks called
8+
# from this function should gracefully handle when they do not need
9+
# to do anything.
10+
#
11+
# Globals:
12+
# - ARTIFACT_DIR
13+
# - SKIP_TEARDOWN
14+
# - SKIP_IMAGE_CLEANUP
15+
# Arguments:
16+
# 1 - return code of the script
17+
# Returns:
18+
# None
19+
function os::cleanup::all() {
20+
local return_code="$1"
21+
22+
# All of our cleanup is best-effort, so we do not care
23+
# if any specific step fails.
24+
set +o errexit
25+
26+
os::test::junit::generate_report
27+
28+
os::log::info "[CLEANUP] Beginning cleanup routines..."
29+
os::cleanup::dump_events
30+
os::cleanup::dump_etcd
31+
os::cleanup::dump_container_logs
32+
os::cleanup::dump_pprof_output
33+
os::cleanup::find_cache_alterations
34+
os::cleanup::truncate_large_logs
35+
36+
if [[ -z "${SKIP_TEARDOWN:-}" ]]; then
37+
os::cleanup::containers
38+
os::cleanup::processes
39+
os::cleanup::prune_etcd
40+
fi
41+
os::util::describe_return_code "${return_code}"
42+
}
43+
readonly -f os::cleanup::all
44+
645
# os::cleanup::dump_etcd dumps the full contents of etcd to a file.
746
#
847
# Globals:
9-
# ARTIFACT_DIR
48+
# - ARTIFACT_DIR
49+
# - API_SCHEME
50+
# - API_HOST
51+
# - ETCD_PORT
1052
# Arguments:
1153
# None
1254
# Returns:
1355
# None
1456
function os::cleanup::dump_etcd() {
15-
os::log::info "Dumping etcd contents to ${ARTIFACT_DIR}/etcd_dump.json"
16-
os::util::curl_etcd "/v2/keys/?recursive=true" > "${ARTIFACT_DIR}/etcd_dump.json"
57+
if [[ -n "${API_SCHEME:-}" && -n "${API_HOST:-}" && -n "${ETCD_PORT:-}" ]]; then
58+
os::log::info "[CLEANUP] Dumping etcd contents to $( os::util::repository_relative_path "${ARTIFACT_DIR}/etcd_dump.json" )"
59+
os::util::curl_etcd "/v2/keys/?recursive=true" > "${ARTIFACT_DIR}/etcd_dump.json"
60+
fi
1761
}
62+
readonly -f os::cleanup::dump_etcd
63+
64+
# os::cleanup::prune_etcd removes the etcd data store from disk.
65+
#
66+
# Globals:
67+
# ARTIFACT_DIR
68+
# Arguments:
69+
# None
70+
# Returns:
71+
# None
72+
function os::cleanup::prune_etcd() {
73+
if [[ -n "${ETCD_DATA_DIR:-}" ]]; then
74+
os::log::info "[CLEANUP] Pruning etcd data directory"
75+
${USE_SUDO:+sudo} rm -rf "${ETCD_DATA_DIR}"
76+
fi
77+
}
78+
readonly -f os::cleanup::prune_etcd
1879

1980
# os::cleanup::containers operates on our containers to stop the containers
2081
# and optionally remove the containers and any volumes they had attached.
@@ -27,11 +88,11 @@ function os::cleanup::dump_etcd() {
2788
# None
2889
function os::cleanup::containers() {
2990
if ! os::util::find::system_binary docker >/dev/null 2>&1; then
30-
os::log::warning "No \`docker\` binary found, skipping container cleanup."
91+
os::log::warning "[CLEANUP] No \`docker\` binary found, skipping container cleanup."
3192
return
3293
fi
3394

34-
os::log::info "Stopping docker containers"
95+
os::log::info "[CLEANUP] Stopping docker containers"
3596
for id in $( os::cleanup::internal::list_our_containers ); do
3697
os::log::debug "Stopping ${id}"
3798
docker stop "${id}" >/dev/null
@@ -41,7 +102,7 @@ function os::cleanup::containers() {
41102
return
42103
fi
43104

44-
os::log::info "Removing docker containers"
105+
os::log::info "[CLEANUP] Removing docker containers"
45106
for id in $( os::cleanup::internal::list_our_containers ); do
46107
os::log::debug "Removing ${id}"
47108
docker stop "${id}" >/dev/null
@@ -60,14 +121,14 @@ readonly -f os::cleanup::containers
60121
# None
61122
function os::cleanup::dump_container_logs() {
62123
if ! os::util::find::system_binary docker >/dev/null 2>&1; then
63-
os::log::warning "No \`docker\` binary found, skipping container cleanup."
124+
os::log::warning "[CLEANUP] No \`docker\` binary found, skipping container log harvest."
64125
return
65126
fi
66127

67128
local container_log_dir="${LOG_DIR}/containers"
68129
mkdir -p "${container_log_dir}"
69130

70-
os::log::info "Dumping container logs to ${container_log_dir}"
131+
os::log::info "[CLEANUP] Dumping container logs to $( os::util::repository_relative_path "${container_log_dir}" )"
71132
for id in $( os::cleanup::internal::list_our_containers ); do
72133
local name; name="$( docker inspect --format '{{ .Name }}' "${id}" )"
73134
os::log::debug "Dumping logs for ${id} to ${name}.log"
@@ -138,6 +199,7 @@ readonly -f os::cleanup::internal::list_containers
138199
# Returns:
139200
# None
140201
function os::cleanup::tmpdir() {
202+
os::log::info "[CLEANUP] Cleaning up temporary directories"
141203
# ensure that the directories are clean
142204
if os::util::find::system_binary "findmnt" &>/dev/null; then
143205
for target in $( ${USE_SUDO:+sudo} findmnt --output TARGET --list ); do
@@ -163,11 +225,87 @@ readonly -f os::cleanup::tmpdir
163225
# Returns:
164226
# None
165227
function os::cleanup::dump_events() {
166-
os::log::info "Dumping cluster events to ${ARTIFACT_DIR}/events.txt"
228+
os::log::info "[CLEANUP] Dumping cluster events to $( os::util::repository_relative_path "${ARTIFACT_DIR}/events.txt" )"
167229
local kubeconfig
168-
if [[ -n "${ADMIN_KUBECONFIG}" ]]; then
230+
if [[ -n "${ADMIN_KUBECONFIG:-}" ]]; then
169231
kubeconfig="--config=${ADMIN_KUBECONFIG}"
170232
fi
171-
oc get events --all-namespaces ${kubeconfig} > "${ARTIFACT_DIR}/events.txt"
233+
oc get events --all-namespaces ${kubeconfig:-} > "${ARTIFACT_DIR}/events.txt" 2>&1
172234
}
173235
readonly -f os::cleanup::dump_events
236+
237+
# os::cleanup::find_cache_alterations ulls information out of the server
238+
# log so that we can get failure management in jenkins to highlight it
239+
# and really have it smack people in their logs. This is a severe
240+
# correctness problem.
241+
#
242+
# Globals:
243+
# - LOG_DIR
244+
# Arguments:
245+
# None
246+
# Returns:
247+
# None
248+
function os::cleanup::find_cache_alterations() {
249+
grep -ra5 "CACHE.*ALTERED" "${LOG_DIR}" || true
250+
}
251+
readonly -f os::cleanup::find_cache_alterations
252+
253+
# os::cleanup::dump_pprof_output dumps profiling output for the
254+
# `openshift` binary
255+
#
256+
# Globals:
257+
# - LOG_DIR
258+
# Arguments:
259+
# None
260+
# Returns:
261+
# None
262+
function os::cleanup::dump_pprof_output() {
263+
if go tool -n pprof >/dev/null 2>&1 && [[ -s cpu.pprof ]]; then
264+
os::log::info "[CLEANUP] \`pprof\` output logged to $( os::util::repository_relative_path "${LOG_DIR}/pprof.out" )"
265+
go tool pprof -text "./_output/local/bin/$(os::util::host_platform)/openshift" cpu.pprof >"${LOG_DIR}/pprof.out" 2>&1
266+
fi
267+
}
268+
readonly -f os::cleanup::dump_pprof_output
269+
270+
# os::cleanup::truncate_large_logs truncates very large files under
271+
# $LOG_DIR and $ARTIFACT_DIR so we do not upload them to cloud storage
272+
# after CI runs.
273+
#
274+
# Globals:
275+
# - LOG_DIR
276+
# - ARTIFACT_DIR
277+
# Arguments:
278+
# None
279+
# Returns:
280+
# None
281+
function os::cleanup::truncate_large_logs() {
282+
local max_file_size="100M"
283+
os::log::info "[CLEANUP] Truncating log files over ${max_file_size}"
284+
for file in $(find "${ARTIFACT_DIR}" "${LOG_DIR}" -type f -name '*.log' \( -size +${max_file_size} \)); do
285+
mv "${file}" "${file}.tmp"
286+
echo "LOGFILE TOO LONG ($(du -h "${file}.tmp")), PREVIOUS BYTES TRUNCATED. LAST ${max_file_size} OF LOGFILE:" > "${file}"
287+
tail -c ${max_file_size} "${file}.tmp" >> "${file}"
288+
rm "${file}.tmp"
289+
done
290+
}
291+
readonly -f os::cleanup::truncate_large_logs
292+
293+
# os::cleanup::processes kills all processes created by the test
294+
# script.
295+
#
296+
# Globals:
297+
# None
298+
# Arguments:
299+
# None
300+
# Returns:
301+
# None
302+
function os::cleanup::processes() {
303+
os::log::info "[CLEANUP] Killing child processes"
304+
for job in $( jobs -pr ); do
305+
for child in $( pgrep -P "${job}" ); do
306+
${USE_SUDO:+sudo} kill "${child}" &> /dev/null
307+
done
308+
${USE_SUDO:+sudo} kill "${job}" &> /dev/null
309+
done
310+
}
311+
readonly -f os::cleanup::processes

hack/lib/init.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ set -o errexit
99
set -o nounset
1010
set -o pipefail
1111

12+
OS_SCRIPT_START_TIME="$( date +%s )"; export OS_SCRIPT_START_TIME
13+
1214
# os::util::absolute_path returns the absolute path to the directory provided
1315
function os::util::absolute_path() {
1416
local relative_path="$1"

0 commit comments

Comments
 (0)