14
14
function os::cleanup::dump_etcd() {
15
15
os::log::info " Dumping etcd contents to ${ARTIFACT_DIR} /etcd_dump.json"
16
16
os::util::curl_etcd " /v2/keys/?recursive=true" > " ${ARTIFACT_DIR} /etcd_dump.json"
17
- }
17
+ }
18
+
19
+ # os::cleanup::containers operates on k8s containers to stop the containers
20
+ # and optionally remove the containers and any volumes they had attached.
21
+ #
22
+ # Globals:
23
+ # - SKIP_IMAGE_CLEANUP
24
+ # Arguments:
25
+ # None
26
+ # Returns:
27
+ # None
28
+ function os::cleanup::containers() {
29
+ if ! os::util::find::system_binary docker > /dev/null 2>&1 ; then
30
+ os::log::warning " No \` docker\` binary found, skipping container cleanup."
31
+ return
32
+ fi
33
+
34
+ os::log::info " Stopping k8s docker containers"
35
+ for id in $( os::cleanup::internal::list_k8s_containers ) ; do
36
+ os::log::debug " Stopping ${id} "
37
+ docker stop " ${id} " > /dev/null
38
+ done
39
+
40
+ if [[ -n " ${SKIP_IMAGE_CLEANUP:- } " ]]; then
41
+ return
42
+ fi
43
+
44
+ os::log::info " Removing k8s docker containers"
45
+ for id in $( os::cleanup::internal::list_k8s_containers ) ; do
46
+ os::log::debug " Removing ${id} "
47
+ docker stop " ${id} " > /dev/null
48
+ done
49
+ }
50
+ readonly -f os::cleanup::containers
51
+
52
+ # os::cleanup::dump_container_logs operates on k8s containers to dump any logs
53
+ # from the containers.
54
+ #
55
+ # Globals:
56
+ # None
57
+ # Arguments:
58
+ # None
59
+ # Returns:
60
+ # None
61
+ function os::cleanup::dump_container_logs() {
62
+ if ! os::util::find::system_binary docker > /dev/null 2>&1 ; then
63
+ os::log::warning " No \` docker\` binary found, skipping container cleanup."
64
+ return
65
+ fi
66
+
67
+ local container_log_dir=" ${LOG_DIR} /containers"
68
+ mkdir -p " ${container_log_dir} "
69
+
70
+ os::log::info " Dumping container logs to ${container_log_dir} "
71
+ for id in $( os::cleanup::internal::list_k8s_containers ) ; do
72
+ local name; name=" $( docker inspect --format ' {{ .Name }}' " ${id} " ) "
73
+ os::log::debug " Dumping logs for ${id} to ${name} .log"
74
+ docker logs " ${id} " > " ${container_log_dir} /${name} .log" 2>&1
75
+ done
76
+ }
77
+ readonly -f os::cleanup::dump_container_logs
78
+
79
+
80
+
81
+ # os::cleanup::internal::list_k8s_containers returns a space-delimited list of
82
+ # docker containers that belonged to k8s.
83
+ #
84
+ # Globals:
85
+ # None
86
+ # Arguments:
87
+ # None
88
+ # Returns:
89
+ # None
90
+ function os::cleanup::internal::list_k8s_containers() {
91
+ local ids;
92
+ for short_id in $( docker ps -aq ) ; do
93
+ local id; id=" $( docker inspect --format ' {{ .Id }}' " ${short_id} " ) "
94
+ local name; name=" $( docker inspect --format ' {{ .Name }}' " ${id} " ) "
95
+ if [[ " ${name} " =~ ^/k8s_.* ]]; then
96
+ ids+=( " ${id} " )
97
+ fi
98
+ done
99
+
100
+ echo " ${ids[*]} "
101
+ }
102
+ readonly -f os::cleanup::internal::list_k8s_containers
0 commit comments