Skip to content

Commit 35b4a22

Browse files
committed
Push failed test k8s logs to S3 bucket
1 parent 415a478 commit 35b4a22

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

Jenkinsfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,21 @@ void pushLogFile(String FILE_NAME) {
103103
}
104104
}
105105

106+
void pushK8SLogs(String TEST_NAME) {
107+
def LOG_FILE_PATH="e2e-tests/logs/"
108+
def FILE_NAMES="logs_${TEST_NAME}_*"
109+
echo "Push k8s logs to S3!"
110+
111+
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'AMI/OVF', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
112+
sh """
113+
S3_PATH=s3://percona-jenkins-artifactory/\$JOB_NAME/\$(git rev-parse --short HEAD)/logs/
114+
aws s3 ls \$S3_PATH || :
115+
aws s3 rm \$S3_PATH --recursive --exclude "*" --include "${FILE_NAMES}" || :
116+
aws s3 cp --quiet ${LOG_FILE_PATH} \$S3_PATH --recursive --exclude "*" --include "$FILE_NAMES" || :
117+
"""
118+
}
119+
}
120+
106121
void popArtifactFile(String FILE_NAME) {
107122
echo "Try to get $FILE_NAME file from S3!"
108123

@@ -218,6 +233,7 @@ void runTest(Integer TEST_ID) {
218233
return true
219234
}
220235
catch (exc) {
236+
pushK8SLogs("$testName")
221237
if (retryCount >= 1 || currentBuild.nextBuild != null) {
222238
currentBuild.result = 'FAILURE'
223239
return true

e2e-tests/functions

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ wait_pod() {
149149
echo -n .
150150
let retry+=1
151151
if [ $retry -ge 360 ]; then
152+
collect_k8s_logs
152153
kubectl_bin describe pod/$pod
153154
kubectl_bin logs $pod
154155
kubectl_bin logs ${OPERATOR_NS:+-n $OPERATOR_NS} $(get_operator_pod) \
@@ -157,6 +158,7 @@ wait_pod() {
157158
| grep -v 'Getting tasks for pod' \
158159
| grep -v 'Getting pods from source' \
159160
| tail -100
161+
160162
echo max retry count $retry reached. something went wrong with operator or kubernetes cluster
161163
exit 1
162164
fi
@@ -176,12 +178,14 @@ wait_cron() {
176178
echo -n .
177179
let retry+=1
178180
if [ $retry -ge 360 ]; then
181+
collect_k8s_logs
179182
kubectl_bin logs ${OPERATOR_NS:+-n $OPERATOR_NS} $(get_operator_pod) \
180183
| grep -v 'level=info' \
181184
| grep -v 'level=debug' \
182185
| grep -v 'Getting tasks for pod' \
183186
| grep -v 'Getting pods from source' \
184187
| tail -100
188+
185189
echo max retry count $retry reached. something went wrong with operator or kubernetes cluster
186190
exit 1
187191
fi
@@ -200,8 +204,10 @@ wait_backup_agent() {
200204
echo -n .
201205
let retry+=1
202206
if [ $retry -ge 360 ]; then
207+
collect_k8s_logs
203208
kubectl_bin logs $agent_pod -c backup-agent \
204209
| tail -100
210+
205211
echo max retry count $retry reached. something went wrong with operator or kubernetes cluster
206212
exit 1
207213
fi
@@ -223,12 +229,14 @@ wait_backup() {
223229
let retry+=1
224230
current_status=$(kubectl_bin get psmdb-backup $backup_name -o jsonpath='{.status.state}')
225231
if [[ $retry -ge 360 || ${current_status} == 'error' ]]; then
232+
collect_k8s_logs
226233
kubectl_bin logs ${OPERATOR_NS:+-n $OPERATOR_NS} $(get_operator_pod) \
227234
| grep -v 'level=info' \
228235
| grep -v 'level=debug' \
229236
| grep -v 'Getting tasks for pod' \
230237
| grep -v 'Getting pods from source' \
231238
| tail -100
239+
232240
echo "Backup object psmdb-backup/${backup_name} is in ${current_state} state."
233241
echo something went wrong with operator or kubernetes cluster
234242
exit 1
@@ -282,12 +290,14 @@ wait_deployment() {
282290
echo -n .
283291
let retry+=1
284292
if [ $retry -ge 360 ]; then
293+
collect_k8s_logs
285294
kubectl_bin logs ${OPERATOR_NS:+-n $OPERATOR_NS} $(get_operator_pod) \
286295
| grep -v 'level=info' \
287296
| grep -v 'level=debug' \
288297
| grep -v 'Getting tasks for pod' \
289298
| grep -v 'Getting pods from source' \
290299
| tail -100
300+
291301
echo max retry count $retry reached. something went wrong with operator or kubernetes cluster
292302
exit 1
293303
fi
@@ -328,6 +338,7 @@ wait_restore() {
328338
let retry+=1
329339
current_state=$(kubectl_bin get psmdb-restore restore-$backup_name -o jsonpath='{.status.state}')
330340
if [[ $retry -ge $wait_time || ${current_state} == 'error' ]]; then
341+
collect_k8s_logs
331342
kubectl_bin logs ${OPERATOR_NS:+-n $OPERATOR_NS} $(get_operator_pod) \
332343
| grep -v 'level=info' \
333344
| grep -v 'level=debug' \
@@ -528,6 +539,7 @@ retry() {
528539

529540
until "$@"; do
530541
if [[ $n -ge $max ]]; then
542+
collect_k8s_logs
531543
echo "The command '$@' has failed after $n attempts."
532544
exit 1
533545
fi
@@ -567,6 +579,7 @@ wait_for_running() {
567579
timeout=$((timeout + 1))
568580
echo -n '.'
569581
if [[ ${timeout} -gt 1500 ]]; then
582+
collect_k8s_logs
570583
echo
571584
echo "Waiting timeout has been reached. Exiting..."
572585
exit 1
@@ -589,12 +602,14 @@ wait_for_delete() {
589602
echo -n .
590603
let retry+=1
591604
if [ $retry -ge $wait_time ]; then
605+
collect_k8s_logs
592606
kubectl_bin logs ${OPERATOR_NS:+-n $OPERATOR_NS} $(get_operator_pod) \
593607
| grep -v 'level=info' \
594608
| grep -v 'level=debug' \
595609
| grep -v 'Getting tasks for pod' \
596610
| grep -v 'Getting pods from source' \
597611
| tail -100
612+
598613
echo max retry count $retry reached. something went wrong with operator or kubernetes cluster
599614
exit 1
600615
fi
@@ -610,6 +625,8 @@ compare_generation() {
610625

611626
current_generation="$(kubectl_bin get ${resource_type} "${resource_name}" -o jsonpath='{.metadata.generation}')"
612627
if [[ ${generation} != "${current_generation}" ]]; then
628+
collect_k8s_logs
629+
613630
echo "Generation for ${resource_type}/${resource_name} is: ${current_generation}, but should be: ${generation}"
614631
exit 1
615632
fi
@@ -962,6 +979,7 @@ get_service_endpoint() {
962979
return
963980
fi
964981

982+
collect_k8s_logs
965983
exit 1
966984
}
967985

@@ -1137,6 +1155,7 @@ wait_cluster_consistency() {
11371155
until [[ "$(kubectl_bin get psmdb "${cluster_name}" -o jsonpath='{.status.state}')" == "ready" ]]; do
11381156
let retry+=1
11391157
if [ $retry -ge $wait_time ]; then
1158+
collect_k8s_logs
11401159
echo max retry count $retry reached. something went wrong with operator or kubernetes cluster
11411160
exit 1
11421161
fi
@@ -1163,6 +1182,7 @@ check_backup_deletion() {
11631182
retry=0
11641183
until [[ $(curl -sw '%{http_code}' -o /dev/null $path) -eq 403 ]] || [[ $(curl -sw '%{http_code}' -o /dev/null $path) -eq 404 ]]; do
11651184
if [ $retry -ge 10 ]; then
1185+
collect_k8s_logs
11661186
echo max retry count $retry reached. something went wrong with operator or kubernetes cluster
11671187
echo "Backup was not removed from bucket -- $storage_name"
11681188
exit 1
@@ -1224,6 +1244,7 @@ function get_mongod_ver_from_image() {
12241244
version_info=$(run_simple_cli_inside_image ${image} 'mongod --version' | $sed -r 's/^.*db version v(([0-9]+\.){2}[0-9]+-[0-9]+).*$/\1/g')
12251245

12261246
if [[ ! ${version_info} =~ ^([0-9]+\.){2}[0-9]+-[0-9]+$ ]]; then
1247+
collect_k8s_logs
12271248
printf "No mongod version obtained from %s. Exiting" ${image}
12281249
exit 1
12291250
fi
@@ -1236,6 +1257,7 @@ function get_pbm_version() {
12361257
local version_info=$(run_simple_cli_inside_image ${image} 'pbm-agent version' | $sed -r 's/^Version:\ (([0-9]+\.){2}[0-9]+)\ .*/\1/g')
12371258

12381259
if [[ ! ${version_info} =~ ^([0-9]+\.){2}[0-9]+$ ]]; then
1260+
collect_k8s_logs
12391261
printf "No pbm version obtained from %s. Exiting" ${image}
12401262
exit 1
12411263
fi
@@ -1276,6 +1298,31 @@ function generate_vs_json() {
12761298
echo ${version_service_source} | jq '.' >${target_path}
12771299
}
12781300

1301+
collect_k8s_logs() {
1302+
if [[ ${ENABLE_LOGGING} == "true" ]]; then
1303+
rm -f ${logs_dir}/logs_${test_name}_* || :
1304+
1305+
local check_namespaces="$namespace${OPERATOR_NS:+ $OPERATOR_NS}"
1306+
1307+
for ns in $check_namespaces; do
1308+
local pods=$(kubectl_bin get pods -o name | awk -F "/" '{print $2}')
1309+
for p in $pods; do
1310+
local containers=$(kubectl_bin -n "$ns" get pod $p -o jsonpath='{.spec.containers[*].name}')
1311+
for c in $containers; do
1312+
kubectl_bin -n "$ns" logs $p -c $c >${logs_dir}/logs_${test_name}_$p_$c.txt
1313+
echo logs saved in: ${logs_dir}/logs_${test_name}_$p_$c.txt
1314+
done
1315+
done
1316+
done
1317+
for object in "psmdb psmdb-backup psmdb-restore pods deployments services events sts"; do
1318+
echo "##### START: $object #####" >>${logs_dir}/logs_${test_name}_simple.txt
1319+
kubectl_bin get $object --all-namespaces >>${logs_dir}/logs_${test_name}_simple.txt
1320+
echo "##### END: $object ####\n" >>${logs_dir}/logs_${test_name}_simple.txt
1321+
kubectl_bin get $object --all-namespaces >${logs_dir}/logs_${test_name}_$object.yaml
1322+
done
1323+
fi
1324+
}
1325+
12791326
check_passwords_leak() {
12801327
local secrets
12811328
local passwords

0 commit comments

Comments
 (0)