Skip to content

Commit 87ce2f8

Browse files
committed
Push failed test k8s logs to S3 bucket
1 parent 4b49361 commit 87ce2f8

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-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://percona-jenkins-artifactory/\$JOB_NAME/\$(git rev-parse --short HEAD)/logs --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

@@ -220,6 +235,7 @@ void runTest(Integer TEST_ID) {
220235
catch (exc) {
221236
if (retryCount >= 1 || currentBuild.nextBuild != null) {
222237
currentBuild.result = 'FAILURE'
238+
pushK8SLogs("$testName")
223239
return true
224240
}
225241
retryCount++

e2e-tests/functions

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ wait_pod() {
158158
| grep -v 'Getting tasks for pod' \
159159
| grep -v 'Getting pods from source' \
160160
| tail -100
161+
collect_k8s_logs
162+
161163
echo max retry count $retry reached. something went wrong with operator or kubernetes cluster
162164
exit 1
163165
fi
@@ -183,6 +185,8 @@ wait_cron() {
183185
| grep -v 'Getting tasks for pod' \
184186
| grep -v 'Getting pods from source' \
185187
| tail -100
188+
collect_k8s_logs
189+
186190
echo max retry count $retry reached. something went wrong with operator or kubernetes cluster
187191
exit 1
188192
fi
@@ -203,6 +207,8 @@ wait_backup_agent() {
203207
if [ $retry -ge 360 ]; then
204208
kubectl_bin logs $agent_pod -c backup-agent \
205209
| tail -100
210+
collect_k8s_logs
211+
206212
echo max retry count $retry reached. something went wrong with operator or kubernetes cluster
207213
exit 1
208214
fi
@@ -230,6 +236,8 @@ wait_backup() {
230236
| grep -v 'Getting tasks for pod' \
231237
| grep -v 'Getting pods from source' \
232238
| tail -100
239+
collect_k8s_logs
240+
233241
echo "Backup object psmdb-backup/${backup_name} is in ${current_state} state."
234242
echo something went wrong with operator or kubernetes cluster
235243
exit 1
@@ -289,6 +297,8 @@ wait_deployment() {
289297
| grep -v 'Getting tasks for pod' \
290298
| grep -v 'Getting pods from source' \
291299
| tail -100
300+
collect_k8s_logs
301+
292302
echo max retry count $retry reached. something went wrong with operator or kubernetes cluster
293303
exit 1
294304
fi
@@ -334,6 +344,7 @@ wait_restore() {
334344
| grep -v 'Getting tasks for pod' \
335345
| grep -v 'Getting pods from source' \
336346
| tail -100
347+
collect_k8s_logs
337348

338349
echo "Restore object restore-${backup_name} is in ${current_state} state."
339350
echo something went wrong with operator or kubernetes cluster
@@ -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
@@ -594,6 +607,8 @@ wait_for_delete() {
594607
| grep -v 'Getting tasks for pod' \
595608
| grep -v 'Getting pods from source' \
596609
| tail -100
610+
collect_k8s_logs
611+
597612
echo max retry count $retry reached. something went wrong with operator or kubernetes cluster
598613
exit 1
599614
fi
@@ -609,6 +624,8 @@ compare_generation() {
609624

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

981+
collect_k8s_logs
964982
exit 1
965983
}
966984

@@ -1135,6 +1153,7 @@ wait_cluster_consistency() {
11351153
until [[ "$(kubectl_bin get psmdb "${cluster_name}" -o jsonpath='{.status.state}')" == "ready" ]]; do
11361154
let retry+=1
11371155
if [ $retry -ge 32 ]; then
1156+
collect_k8s_logs
11381157
echo max retry count $retry reached. something went wrong with operator or kubernetes cluster
11391158
exit 1
11401159
fi
@@ -1161,6 +1180,7 @@ check_backup_deletion() {
11611180
retry=0
11621181
until [[ $(curl -sw '%{http_code}' -o /dev/null $path) -eq 403 ]] || [[ $(curl -sw '%{http_code}' -o /dev/null $path) -eq 404 ]]; do
11631182
if [ $retry -ge 10 ]; then
1183+
collect_k8s_logs
11641184
echo max retry count $retry reached. something went wrong with operator or kubernetes cluster
11651185
echo "Backup was not removed from bucket -- $storage_name"
11661186
exit 1
@@ -1222,6 +1242,7 @@ function get_mongod_ver_from_image() {
12221242
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')
12231243

12241244
if [[ ! ${version_info} =~ ^([0-9]+\.){2}[0-9]+-[0-9]+$ ]]; then
1245+
collect_k8s_logs
12251246
printf "No mongod version obtained from %s. Exiting" ${image}
12261247
exit 1
12271248
fi
@@ -1234,6 +1255,7 @@ function get_pbm_version() {
12341255
local version_info=$(run_simple_cli_inside_image ${image} 'pbm-agent version' | $sed -r 's/^Version:\ (([0-9]+\.){2}[0-9]+)\ .*/\1/g')
12351256

12361257
if [[ ! ${version_info} =~ ^([0-9]+\.){2}[0-9]+$ ]]; then
1258+
collect_k8s_logs
12371259
printf "No pbm version obtained from %s. Exiting" ${image}
12381260
exit 1
12391261
fi
@@ -1274,6 +1296,24 @@ function generate_vs_json() {
12741296
echo ${version_service_source} | jq '.' >${target_path}
12751297
}
12761298

1299+
collect_k8s_logs() {
1300+
local check_namespaces="$namespace${OPERATOR_NS:+ $OPERATOR_NS}"
1301+
1302+
for ns in $check_namespaces; do
1303+
local pods=$(kubectl_bin get pods -o name | awk -F "/" '{print $2}')
1304+
for p in $pods; do
1305+
local containers=$(kubectl_bin -n "$ns" get pod $p -o jsonpath='{.spec.containers[*].name}')
1306+
for c in $containers; do
1307+
kubectl_bin -n "$ns" logs $p -c $c > ${logs_dir}/logs_${test_name}_$p_$c.txt
1308+
echo logs saved in: ${logs_dir}/logs_${test_name}_$p_$c.txt
1309+
done
1310+
done
1311+
done
1312+
kubectl_bin get pods --all-namespaces > ${logs_dir}/logs_${test_name}_pods.txt
1313+
kubectl_bin get services --all-namespaces > ${logs_dir}/logs_${test_name}_services.txt
1314+
kubectl_bin get events --all-namespaces > ${logs_dir}/logs_${test_name}_k8s_events.txt
1315+
}
1316+
12771317
check_passwords_leak() {
12781318
secrets=$(kubectl_bin get secrets -o json | jq -r '.items[].data | to_entries | .[] | select(.key | (contains("_PASSWORD"))) | .value')
12791319
echo secrets=$secrets

0 commit comments

Comments
 (0)