-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathlib.sh
More file actions
executable file
·2533 lines (2143 loc) · 82.3 KB
/
lib.sh
File metadata and controls
executable file
·2533 lines (2143 loc) · 82.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# A library of CI related reusable bash functions
SCRIPTS_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd)"
# shellcheck source=../../scripts/lib.sh
source "$SCRIPTS_ROOT/scripts/lib.sh"
# shellcheck source=../../scripts/ci/metrics.sh
source "$SCRIPTS_ROOT/scripts/ci/metrics.sh"
# shellcheck source=../../scripts/ci/test_state.sh
source "$SCRIPTS_ROOT/scripts/ci/test_state.sh"
# shellcheck source=../../scripts/ci/gcp.sh
source "$SCRIPTS_ROOT/scripts/ci/gcp.sh"
set -euo pipefail
ensure_CI() {
if ! is_CI; then
die "A CI environment is required."
fi
}
ci_export() {
if [[ "$#" -ne 2 ]]; then
die "missing args. usage: ci_export <env-name> <env-value>"
fi
local env_name="$1"
local env_value="$2"
if command -v cci-export >/dev/null; then
cci-export "$env_name" "$env_value"
else
export "$env_name"="$env_value"
fi
}
# set_ci_shared_export() - for openshift-ci and GHA this is state shared between steps.
set_ci_shared_export() {
if [[ "$#" -ne 2 ]]; then
die "missing args. usage: set_ci_shared_export <env-name> <env-value>"
fi
ci_export "$@"
local env_name="$1"
local env_value="$2"
printf 'export %s=%q\n' "${env_name}" "${env_value}" >> "${SHARED_DIR:-/tmp}/shared_env"
printf '%s=%q\n' "${env_name}" "${env_value}" >> "${GITHUB_ENV:-/dev/null}"
}
ci_exit_trap() {
local exit_code="$?"
info "Executing a general purpose exit trap for CI"
echo "Exit code is: ${exit_code}"
if [[ "${exit_code}" == "0" ]]; then
set_ci_shared_export JOB_DISPATCH_OUTCOME "${OUTCOME_PASSED}"
elif [[ "${exit_code}" == "130" ]]; then
set_ci_shared_export JOB_DISPATCH_OUTCOME "${OUTCOME_CANCELED}"
else
set_ci_shared_export JOB_DISPATCH_OUTCOME "${OUTCOME_FAILED}"
fi
post_process_test_results "${JOB_SLACK_FAILURE_ATTACHMENTS}" "${JOB_JUNIT2JIRA_SUMMARY_FILE}"
while [[ -e /tmp/hold ]]; do
info "Holding this job for debug"
sleep 60
done
handle_dangling_processes
gate_flaky_tests "${exit_code}"
}
# handle_dangling_processes() - The OpenShift CI ci-operator will not complete a
# test job if there are processes remaining that were started by the job. While
# processes _should_ be cleaned up by their creators it is common that some are
# not, so this exists as a fail safe.
handle_dangling_processes() {
info "Process state at exit:"
ps -e -O ppid
local psline pid ppid
ps -e -O ppid | while read -r pid ppid psline; do
# Example output:
# PID PPID S TTY TIME COMMAND
# 1 0 S ? 00:00:00 /tmp/entrypoint-wrapper/entrypoint-wrapper /tools/entrypoint
# [...]
# 179283 25 R ? 00:00:00 ps -e -O ppid
# trim leading whitespace
psline="$pid $ppid $psline"
if [[ "$pid" == "PID" ]]; then
# Ignoring header
continue
fi
if [[ "$pid" == "$$" ]]; then
echo "Ignoring self: $psline"
continue
fi
if [[ "$ppid" == "$$" ]]; then
echo "Ignoring child: $psline"
continue
fi
if [[ "$psline" =~ entrypoint|defunct ]]; then
echo "Ignoring ci-operator entrypoint or defunct process: $psline"
continue
fi
echo "A candidate to kill: $psline"
echo "Will kill $pid"
kill "$pid" || {
echo "Error killing $pid"
}
done
}
create_exit_trap() {
trap ci_exit_trap EXIT
}
setup_deployment_env() {
info "Setting up the deployment environment"
if [[ "$#" -ne 2 ]]; then
die "missing args. usage: setup_deployment_env <docker-login> <use-websocket>"
fi
local docker_login="$1"
local use_websocket="$2"
if [[ "$docker_login" == "true" ]]; then
registry_ro_login "quay.io/rhacs-eng"
fi
if [[ "$use_websocket" == "true" ]]; then
ci_export CLUSTER_API_ENDPOINT "wss://central.stackrox:443"
fi
ci_export REGISTRY_USERNAME "$QUAY_RHACS_ENG_RO_USERNAME"
ci_export REGISTRY_PASSWORD "$QUAY_RHACS_ENG_RO_PASSWORD"
if [[ -z "${MAIN_IMAGE_TAG:-}" ]]; then
ci_export MAIN_IMAGE_TAG "$(make --quiet --no-print-directory tag)"
fi
ci_export ROX_PRODUCT_BRANDING "RHACS_BRANDING"
}
get_central_debug_dump() {
info "Getting a central debug dump"
if [[ "$#" -ne 1 ]]; then
die "missing arg. usage: get_central_debug_dump <output_dir>"
fi
local output_dir="$1"
require_environment "API_ENDPOINT"
require_environment "ROX_ADMIN_PASSWORD"
roxctl -e "${API_ENDPOINT}" --ca "" --insecure-skip-tls-verify \
central debug dump --output-dir "${output_dir}"
ls -l "${output_dir}"
}
process_central_metrics() {
info "Processing metrics from central debug dump"
if [[ "$#" -ne 1 ]]; then
die "missing arg. usage: process_central_metrics <debug_dump_dir>"
fi
local output_dir="$1"
local metrics_output
local csv_output
local debug_dump_zip
metrics_output="$(mktemp --suffix=.prom)"
csv_output="$(mktemp --suffix=.csv)"
# shellcheck disable=SC2012
debug_dump_zip="$(ls -t "${output_dir}"/*.zip | head -1)"
unzip -p "${debug_dump_zip}" metrics-1 > "${metrics_output}"
get_prometheus_metrics_parser
# We need a link to repository. In case it's not part of job spec (e.g., periodic`s)
# we will fallback to short commit
base_link="$(echo "$JOB_SPEC" | jq ".refs.base_link | select( . != null )" -r)"
calculated_base_link="https://github.com/stackrox/stackrox/commit/$(make --quiet --no-print-directory shortcommit)"
local metadata="build_tag=${STACKROX_BUILD_TAG:-none},build_id=${BUILD_ID:-none},orchestrator_flavor=${ORCHESTRATOR_FLAVOR:-PROW},job_name=${JOB_NAME:-missing},base_link=${base_link:-$calculated_base_link}"
prometheus-metric-parser single \
--format csv \
--file "${metrics_output}" \
--labels "${metadata}" \
> "${csv_output}"
setup_gcp
save_central_metrics "${csv_output}"
}
get_prometheus_metrics_parser() {
local parserBin
local parserDir
parserBin=$(make prometheus-metric-parser -C "$ROOT" --silent | tail -1)
parserDir=$(dirname "${parserBin}")
export PATH="$parserDir":$PATH
prometheus-metric-parser help
}
get_central_diagnostics() {
info "Getting central diagnostics"
if [[ "$#" -ne 1 ]]; then
die "missing arg. usage: get_central_diagnostics <output_dir>"
fi
local output_dir="$1"
require_environment "API_ENDPOINT"
require_environment "ROX_ADMIN_PASSWORD"
roxctl -e "${API_ENDPOINT}" --ca "" --insecure-skip-tls-verify \
central debug download-diagnostics --output-dir "${output_dir}"
ls -l "${output_dir}"
}
push_image_manifest_lists() {
info "Pushing main, roxctl and central-db images as manifest lists"
if [[ "$#" -ne 3 ]]; then
die "missing arg. usage: push_image_manifest_lists <push_context> <brand> <architectures (CSV)>"
fi
local push_context="$1"
local brand="$2"
local architectures="$3"
local main_image_set=("main" "roxctl" "central-db")
local registry
registry="$(registry_from_branding "$brand")"
local tag
tag="$(make --quiet --no-print-directory tag)"
registry_rw_login "$registry"
for image in "${main_image_set[@]}"; do
retry 5 true \
"$SCRIPTS_ROOT/scripts/ci/push-as-multiarch-manifest-list.sh" "${registry}/${image}:${tag}" "$architectures" | cat
if [[ "$push_context" == "merge-to-master" ]]; then
retry 5 true \
"$SCRIPTS_ROOT/scripts/ci/push-as-multiarch-manifest-list.sh" "${registry}/${image}:latest" "$architectures" | cat
fi
done
# Push manifest lists for scanner and collector for amd64 only
local amd64_image_set=("scanner" "scanner-db" "scanner-slim" "scanner-db-slim" "collector")
for image in "${amd64_image_set[@]}"; do
retry 5 true \
"$SCRIPTS_ROOT/scripts/ci/push-as-multiarch-manifest-list.sh" "${registry}/${image}:${tag}" "amd64" | cat
done
}
registry_from_branding() {
local branding="$1"
if [[ "$branding" == "STACKROX_BRANDING" ]]; then
registry="quay.io/stackrox-io"
elif [[ "$branding" == "RHACS_BRANDING" ]]; then
registry="quay.io/rhacs-eng"
else
die "$branding is not a supported branding"
fi
echo "$registry"
}
push_main_image_set() {
info "Pushing main, roxctl and central-db images"
if [[ "$#" -ne 3 ]]; then
die "missing arg. usage: push_main_image_set <push_context> <brand> <arch>"
fi
local push_context="$1"
local brand="$2"
local arch="$3"
local main_image_set=("main" "roxctl" "central-db")
_push_main_image_set() {
local registry="$1"
local tag="$2"
for image in "${main_image_set[@]}"; do
retry 5 true \
docker push "${registry}/${image}:${tag}" | cat
done
}
_tag_main_image_set() {
local local_tag="$1"
local registry="$2"
local remote_tag="$3"
for image in "${main_image_set[@]}"; do
docker tag "stackrox/${image}:${local_tag}" "${registry}/${image}:${remote_tag}"
done
}
local registry
registry="$(registry_from_branding "$brand")"
local tag
tag="$(make --quiet --no-print-directory tag)"
registry_rw_login "$registry"
_tag_main_image_set "$tag" "$registry" "$tag-$arch"
_push_main_image_set "$registry" "$tag-$arch"
if [[ "$push_context" == "merge-to-master" ]]; then
_tag_main_image_set "$tag" "$registry" "latest-${arch}"
_push_main_image_set "$registry" "latest-${arch}"
fi
}
push_operator_image() {
info "Pushing stackrox-operator image"
if [[ "$#" -ne 3 ]]; then
die "Missing parameter. Usage: push_operator_image <push_context> <brand> <arch>"
fi
local push_context="$1" # Allowed to be empty.
local brand="$2"
local arch="$3"
if [[ "$brand" == "" ]]; then
die "Brand must be non-empty"
fi
if [[ "$arch" == "" ]]; then
die "Arch must be non-empty"
fi
_push_operator_image() {
local registry="$1"
local tag="$2"
local arch="$3"
retry 5 true \
docker push "${registry}/stackrox-operator:${tag}-${arch}" | cat
}
local registry
registry="$(registry_from_branding "$brand")"
local tag
tag="$(make -C operator/ --quiet --no-print-directory tag)"
registry_rw_login "$registry"
docker tag "${registry}/stackrox-operator:${tag}" "${registry}/stackrox-operator:${tag}-${arch}"
_push_operator_image "$registry" "$tag" "$arch"
if [[ "$push_context" == "merge-to-master" ]]; then
docker tag "${registry}/stackrox-operator:${tag}" "${registry}/stackrox-operator:latest-${arch}"
_push_operator_image "$registry" "latest" "$arch"
fi
if [[ $tag =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
# For release builds, also push a major.minor tag, see operator/install/README.md
local major_minor="${tag%.*}"
docker tag "${registry}/stackrox-operator:${tag}" "${registry}/stackrox-operator:${major_minor}-${arch}"
_push_operator_image "$registry" "$major_minor" "$arch"
fi
}
push_scanner_image_manifest_lists() {
info "Pushing scanner-v4 and scanner-v4-db images as manifest lists"
if [[ "$#" -ne 2 ]]; then
die "missing arg. usage: push_scanner_image_manifest_lists <registry> <architectures (CSV)>"
fi
local registry="$1"
local architectures="$2"
local scanner_image_set=("scanner-v4" "scanner-v4-db")
local tag
tag="$(make --quiet --no-print-directory -C scanner tag)"
registry_rw_login "$registry"
for image in "${scanner_image_set[@]}"; do
retry 5 true \
"$SCRIPTS_ROOT/scripts/ci/push-as-multiarch-manifest-list.sh" "${registry}/${image}:${tag}" "$architectures" | cat
done
}
push_scanner_image_set() {
info "Pushing scanner-v4 and scanner-v4-db images"
if [[ "$#" -ne 2 ]]; then
die "missing arg. usage: push_scanner_image_set <registry> <arch>"
fi
local registry="$1"
local arch="$2"
local scanner_image_set=("scanner-v4" "scanner-v4-db")
_push_scanner_image_set() {
local registry="$1"
local tag="$2"
for image in "${scanner_image_set[@]}"; do
retry 5 true \
docker push "${registry}/${image}:${tag}" | cat
done
}
_tag_scanner_image_set() {
local local_tag="$1"
local registry="$2"
local remote_tag="$3"
for image in "${scanner_image_set[@]}"; do
docker tag "stackrox/${image}:${local_tag}" "${registry}/${image}:${remote_tag}"
done
}
local tag
tag="$(make --quiet --no-print-directory -C scanner tag)"
registry_rw_login "$registry"
_tag_scanner_image_set "$tag" "$registry" "$tag-$arch"
_push_scanner_image_set "$registry" "$tag-$arch"
}
push_operator_manifest_lists() {
info "Pushing stackrox-operator images as manifest lists"
if [[ "$#" -ne 3 ]]; then
die "missing arg. usage: push_image_manifest_lists <push_context> <brand> <architectures (CSV)>"
fi
local push_context="$1"
local brand="$2"
local architectures="$3"
local registry
registry="$(registry_from_branding "$brand")"
local tag
tag="$(make -C operator --quiet --no-print-directory tag)"
registry_rw_login "$registry"
retry 5 true \
"$SCRIPTS_ROOT/scripts/ci/push-as-multiarch-manifest-list.sh" "${registry}/stackrox-operator:${tag}" "$architectures" | cat
if [[ "$push_context" == "merge-to-master" ]]; then
retry 5 true \
"$SCRIPTS_ROOT/scripts/ci/push-as-multiarch-manifest-list.sh" "${registry}/stackrox-operator:latest" "$architectures" | cat
fi
if [[ $tag =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
# For release builds, also push a major.minor tag, see operator/install/README.md
local major_minor="${tag%.*}"
retry 5 true \
"$SCRIPTS_ROOT/scripts/ci/push-as-multiarch-manifest-list.sh" "${registry}/stackrox-operator:${major_minor}" "$architectures" | cat
fi
}
registry_rw_login() {
if [[ "$#" -ne 1 ]]; then
die "missing arg. usage: registry_rw_login <registry>"
fi
local registry="$1"
case "$registry" in
quay.io/rhacs-eng)
_login() {
# shellcheck disable=SC2317
docker login -u "$QUAY_RHACS_ENG_RW_USERNAME" --password-stdin <<<"$QUAY_RHACS_ENG_RW_PASSWORD" quay.io
}
retry 5 true _login
;;
quay.io/stackrox-io)
_login() {
# shellcheck disable=SC2317
docker login -u "$QUAY_STACKROX_IO_RW_USERNAME" --password-stdin <<<"$QUAY_STACKROX_IO_RW_PASSWORD" quay.io
}
retry 5 true _login
;;
*)
die "Unsupported registry login: $registry"
esac
}
registry_ro_login() {
if [[ "$#" -ne 1 ]]; then
die "missing arg. usage: registry_ro_login <registry>"
fi
local registry="$1"
case "$registry" in
quay.io/rhacs-eng)
_login() {
# shellcheck disable=SC2317
docker login -u "$QUAY_RHACS_ENG_RO_USERNAME" --password-stdin <<<"$QUAY_RHACS_ENG_RO_PASSWORD" quay.io
}
retry 5 true _login
;;
*)
die "Unsupported registry login: $registry"
esac
}
push_matching_collector_scanner_images() {
info "Pushing collector & scanner images tagged with main-version to quay.io/rhacs-eng"
if [[ "$#" -ne 2 ]]; then
die "missing arg. usage: push_matching_collector_scanner_images <brand> <arch>"
fi
local brand="$1"
local arch="$2"
local registry
registry="$(registry_from_branding "$brand")"
_retag() {
retry 5 true "$SCRIPTS_ROOT/scripts/ci/pull-retag-push.sh" "$1" "$2"
}
if [[ "$arch" != "amd64" ]]; then
echo "Skipping rebundling for non-amd64 arch"
exit 0
fi
local main_tag
main_tag="$(make --quiet --no-print-directory tag)"
local scanner_version
scanner_version="$(make --quiet --no-print-directory scanner-tag)"
local collector_version
collector_version="$(make --quiet --no-print-directory collector-tag)"
local fact_version
fact_version="$(make --quiet --no-print-directory fact-tag)"
registry_rw_login "${registry}"
_retag "${registry}/scanner:${scanner_version}" "${registry}/scanner:${main_tag}-${arch}"
_retag "${registry}/scanner-db:${scanner_version}" "${registry}/scanner-db:${main_tag}-${arch}"
_retag "${registry}/scanner-slim:${scanner_version}" "${registry}/scanner-slim:${main_tag}-${arch}"
_retag "${registry}/scanner-db-slim:${scanner_version}" "${registry}/scanner-db-slim:${main_tag}-${arch}"
_retag "${registry}/collector:${collector_version}" "${registry}/collector:${main_tag}-${arch}"
_retag "${registry}/fact:${fact_version}" "${registry}/fact:${main_tag}-${arch}"
}
poll_for_system_test_images() {
info "Polling for images required for system tests"
if [[ "$#" -ne 1 ]]; then
die "missing arg. usage: poll_for_system_test_images <seconds to wait>"
fi
local time_limit="$1"
require_environment "QUAY_RHACS_ENG_BEARER_TOKEN"
local image_list
image_list="$(mktemp)"
populate_stackrox_image_list "${image_list}"
info "Will poll for: $(awk '{print $1}' "${image_list}")"
local start_time
start_time="$(date '+%s')"
local tag
local image
while read -r image tag
do
while ! check_rhacs_eng_image_exists "$image" "$tag"
do
info "$image does not exist"
if (( $(date '+%s') - start_time > time_limit )); then
check_build_workflows "$(get_commit_sha)"
die "ERROR: Timed out waiting for images after ${time_limit} seconds"
fi
sleep 60
done
done < "$image_list"
info "All images exist."
touch "${STATE_IMAGES_AVAILABLE}"
}
# Image prefetch is broken into two sets:
# - prebuilt: test fixture images that already exist prior to CI job execution.
# Prefetch for these images can start as soon as the cluster is ready.
# - system: the product images under test 'stackrox-images'. Prefetch for these
# should not start until the images are built to avoid backoff noise in the
# prefetcher logs.
image_prefetcher_await_msg_prefix="Waiting for pre-fetcher"
image_prefetcher_prebuilt_start() {
info "Starting image pre-fetcher of pre-built images (NOT built for current commit)..."
junit_wrap image-prefetcher-prebuilt-start \
"Start image pre-fetcher for pre-built images (NOT built for current commit)." \
"See log for error details." \
_image_prefetcher_prebuilt_start
}
image_prefetcher_system_start() {
info "Starting image pre-fetcher of system images (built for current commit)..."
junit_wrap image-prefetcher-system-start \
"Start image pre-fetcher for system images (built for current commit)." \
"See log for error details." \
_image_prefetcher_system_start
}
_image_prefetcher_prebuilt_start() {
# NOTE: when changing this function, make corresponding changes to
# _image_prefetcher_prebuilt_await
case "$CI_JOB_NAME" in
*qa-e2e-tests)
image_prefetcher_start_set qa-e2e
# Override the default image pull policy for containers with quay.io
# images to rely on prefetched images. This helps ensure that the static
# prefect list stays up to date with additions.
ci_export "IMAGE_PULL_POLICY_FOR_QUAY_IO" "Never"
;;
*nongroovy-e2e-tests)
image_prefetcher_start_set qa-nongroovy-e2e
# Override the default image pull policy for containers with quay.io
# images to rely on prefetched images. This helps ensure that the static
# prefect list stays up to date with additions.
ci_export "IMAGE_PULL_POLICY_FOR_QUAY_IO" "Never"
;;
*-operator-e2e-tests)
image_prefetcher_start_set operator-e2e
# TODO(ROX-20508): pre-fetch images of the release from which operator upgrade test starts as well.
;;
*)
info "No pre-built image prefetching is currently performed for: ${CI_JOB_NAME}."
;;
esac
}
_image_prefetcher_system_start() {
# NOTE: when changing this function, make corresponding changes to
# _image_prefetcher_system_await
case "$CI_JOB_NAME" in
# ROX-24818: GKE is excluded from system image prefetch as it causes
# flakes in test.
*-operator-e2e-tests|*ocp*qa-e2e-tests)
image_prefetcher_start_set stackrox-images
;;
# Enabling scanner V4 installation tests as well, even though they also run on GKE,
# for gathering some more data points for CI reliability.
*-scanner-v4-install-tests)
image_prefetcher_start_set stackrox-images
;;
*)
info "No system image prefetching is performed for: ${CI_JOB_NAME}."
;;
esac
}
image_prefetcher_start_set() {
local ns="prefetch-images"
local name="$1"
make image-prefetcher-deploy-bin
local image_prefetcher_deploy_bin
image_prefetcher_deploy_bin="$(make print-image-prefetcher-deploy-bin)"
local image_prefetcher_version
image_prefetcher_version="$(go -C tools/test list -m -f '{{.Version}}' github.com/stackrox/image-prefetcher/deploy)"
info "Using ${image_prefetcher_deploy_bin} ${image_prefetcher_version} for image prefetch deployment"
local manifest
manifest=$(mktemp)
case "${ORCHESTRATOR_FLAVOR}" in
k8s)
flavor=vanilla
;;
openshift)
flavor=ocp
;;
*)
die "unsupported ORCHESTRATOR: ${ORCHESTRATOR_FLAVOR}"
;;
esac
# daemonset, etc
${image_prefetcher_deploy_bin} \
--version="${image_prefetcher_version}" \
--k8s-flavor="$flavor" \
--secret=stackrox \
--collect-metrics \
--namespace="$ns" \
"$name" > "$manifest"
# image list
local image_list
image_list=$(mktemp)
populate_prefetcher_image_list "$name" "${image_list}"
echo "---" >> "$manifest"
kubectl create --dry-run=client -o yaml configmap "$name" --from-file="images.txt=$image_list" >> "$manifest"
# pull secret
REGISTRY_PASSWORD="${QUAY_RHACS_ENG_RO_PASSWORD}" \
REGISTRY_USERNAME="${QUAY_RHACS_ENG_RO_USERNAME}" \
NAMESPACE=$ns \
make -C operator stackrox-image-pull-secret
# apply configmap, daemonset etc
retry 5 true kubectl apply --namespace=$ns -f "$manifest"
info "Image pre-fetcher is now running in the background. Its status will be checked later (look for message starting with ${image_prefetcher_await_msg_prefix}). Proceeding with other tasks in the meantime."
rm -f "$image_list" "$manifest"
}
image_prefetcher_prebuilt_await() {
if [[ "${IMAGE_PREFETCH_DISABLED:-false}" == "true" ]]; then
return
fi
info "${image_prefetcher_await_msg_prefix} of pre-built images to complete..."
junit_wrap image-prefetcher-prebuilt-await \
"Waiting for pre-fetcher of pre-built images to complete." \
"See log for error details." \
_image_prefetcher_prebuilt_await
}
image_prefetcher_system_await() {
if [[ "${IMAGE_PREFETCH_DISABLED:-false}" == "true" ]]; then
return
fi
info "${image_prefetcher_await_msg_prefix} of system images to complete..."
junit_wrap image-prefetcher-system-await \
"Waiting for pre-fetcher of system images to complete." \
"See log for error details." \
_image_prefetcher_system_await
}
_image_prefetcher_prebuilt_await() {
case "$CI_JOB_NAME" in
# Note: when adding a case for a new test job below, add a image_prefetcher_prebuilt_await call
# at the last moment before any of the prebuilt images is used. (See other existing examples.)
# This way we save time since prefetching can happen in parallel with whatever other setup the test job needs.
*qa-e2e-tests)
image_prefetcher_await_set qa-e2e
;;
*nongroovy-e2e-tests)
image_prefetcher_await_set qa-nongroovy-e2e
;;
*-operator-e2e-tests)
image_prefetcher_await_set operator-e2e
# TODO(ROX-20508): pre-fetch images of the release from which operator upgrade test starts as well.
;;
*)
info "No pre-built image prefetching is currently performed for: ${CI_JOB_NAME}. Nothing to wait for."
;;
esac
}
_image_prefetcher_system_await() {
case "$CI_JOB_NAME" in
# ROX-24818: GKE is excluded from system image prefetch as it causes
# flakes in test.
*-operator-e2e-tests|*ocp*qa-e2e-tests)
image_prefetcher_await_set stackrox-images
;;
# Enabling scanner V4 installation tests as well, even though they also run on GKE,
# for gathering some more data points for CI reliability.
*-scanner-v4-install-tests)
image_prefetcher_await_set stackrox-images
;;
*)
info "No system image prefetching is performed for: ${CI_JOB_NAME}. Nothing to wait for."
;;
esac
}
image_prefetcher_await_set() {
local ns="prefetch-images"
local name="$1"
local extra_fields='{"build_id": "'"${BUILD_ID:-}"'", "job_name": "'"${JOB_NAME:-}"'", "orchestrator": "'"${ORCHESTRATOR_FLAVOR:-}"'", "build_tag": "'"${STACKROX_BUILD_TAG:-}"'"}'
info "Waiting for image prefetcher set ${name} to complete..."
if kubectl rollout status daemonset "$name" -n "$ns" --timeout 15m; then
info "All images in the set are now pre-fetched."
else
info "WARNING: Pre-fetching failed to complete in time."
info "To investigate closer, go to https://console.cloud.google.com/bigquery and run a query such as:"
local query
query=$(mktemp)
cat > "${query}" <<- EOM
SELECT started_at, duration_ms, image, error
FROM \`acs-san-stackroxci.ci_metrics.stackrox_image_prefetches\`
WHERE error IS NOT NULL AND
$(echo "${extra_fields}" | jq -r '[to_entries | .[] | select(.value != "") | (.key + "=\"" + .value + "\"")] | join(" AND ")')
ORDER BY started_at DESC LIMIT 1000
EOM
cat "${query}"
info "Note: The data is imported into the table periodically: https://github.com/stackrox/stackrox/actions/workflows/batch-load-test-metrics.yml"
if [[ -n ${ARTIFACT_DIR:-} ]]; then
local prefetcher_help="$ARTIFACT_DIR/image-pre-fetcher-${name}-failure-summary.html"
cat > "${prefetcher_help}" <<- EOM
<html>
<head>
<title>Image pre-fetcher ${name} failure</title>
<style>
body { color: #e8e8e8; background-color: #424242; font-family: "Roboto", "Helvetica", "Arial", sans-serif }
a { color: #ff8caa }
a:visited { color: #ff8caa }
</style>
</head>
<body>
Waiting for image prefetcher set ${name} to complete timed out.<br>
To investigate closer, go to <a target="_blank" href="https://console.cloud.google.com/bigquery">BigQuery</a> and run a query such as the following:
<br>
<pre>
EOM
cat >> "${prefetcher_help}" "${query}"
cat >> "${prefetcher_help}" <<- EOM
</pre>
Note: The data is imported into the table <a target="_blank" href="https://github.com/stackrox/stackrox/actions/workflows/batch-load-test-metrics.yml">periodically</a>.
<br><br>
</body>
</html>
EOM
fi
rm -f "${query}"
fi
info "Now retrieving prefetcher metrics..."
local attempt=0
local service="service/${name}-metrics"
while [[ -z $(kubectl -n "${ns}" get "${service}" -o jsonpath="{.status.loadBalancer.ingress}" 2>/dev/null) ]]; do
if [ "$attempt" -lt "60" ]; then
info "Waiting for ${service} to obtain endpoint ..."
((attempt++))
sleep 10
else
info "Something is wrong with the ${service} service. See the following 'describe' output."
kubectl -n "${ns}" describe "${service}" || true
die "Timeout waiting for ${service} to obtain endpoint!"
fi
done
local endpoint
endpoint="$(kubectl -n "${ns}" get "${service}" -o json | service_get_endpoint)"
local fetcher_metrics
fetcher_metrics="$(mktemp --suffix=.csv)"
local fetcher_metrics_json
fetcher_metrics_json="$(mktemp --suffix=.json)"
local metrics_url="http://${endpoint}:8080/metrics"
if ! curl --silent --show-error --fail --retry 3 --retry-connrefused "${metrics_url}" > "${fetcher_metrics_json}"; then
die "Failed to fetch prefetcher metrics from ${metrics_url}"
fi
# See the stackrox_image_prefetches table definition in https://github.com/stackrox/automation-iac/blob/main/resources/testing/stackrox-ci/metrics.tf
# for the order of columns.
if ! jq --raw-output \
--argjson cols '["attempt_id", "started_at", "image", "duration_ms", "node", "size_bytes", "error", "build_id", "job_name", "orchestrator", "build_tag"]' \
--argjson extra "${extra_fields}" \
'map(.started_at = (.started_at | todate) | ($extra+.) as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' \
"${fetcher_metrics_json}" > "${fetcher_metrics}"; then
info "WARNING: Failed to convert image prefetcher metrics to CSV with extra fields ${extra_fields}"
info "Dumping the input JSON file:"
jq . < "${fetcher_metrics_json}"
die "Failed to convert image prefetcher metrics to CSV, aborting."
fi
rm -f "${fetcher_metrics_json}"
setup_gcp
if save_image_prefetches_metrics "${fetcher_metrics}"; then
info "Image pre-fetcher metrics retrieved and saved."
else
info "WARNING: failed to save image pre-fetcher metrics."
fi
rm -f "${fetcher_metrics}"
}
service_get_endpoint() {
jq -r '.status.loadBalancer.ingress // error("List of ingress points of LB " + .metadata.name + " is empty.") | .[0] | .hostname // .ip'
}
populate_prefetcher_image_list() {
local name="$1"
local image_list="$2"
case "$name" in
stackrox-images)
local image_tag_list
image_tag_list=$(mktemp)
populate_stackrox_image_list "${image_tag_list}"
# convert format from "basename tag" to "quay.io/.../basename:tag" expected by pre-fetcher
awk '{print "quay.io/rhacs-eng/" $1 ":" $2}' "$image_tag_list" > "$image_list"
rm -f "$image_tag_list"
;;
operator-e2e)
# shellcheck disable=SC2046
grep PREFETCH-THIS-IMAGE -h -A 1 $(find operator/tests/ -type f) | awk '$1 == "image:" {print $2}' | sort -u > "$image_list"
;;
qa-e2e)
cp "$SCRIPTS_ROOT/qa-tests-backend/scripts/images-to-prefetch.txt" "$image_list"
;;
qa-nongroovy-e2e)
cp "$SCRIPTS_ROOT/tests/images-to-prefetch.txt" "$image_list"
;;
*)
die "ERROR: An unsupported image prefetcher target was requested: $name"
;;
esac
}
populate_stackrox_image_list() {
local image_list="$1"
local tag
tag="$(make --quiet --no-print-directory tag)"
local operator_metadata_tag
operator_metadata_tag="$(echo "v${tag}" | sed 's,x,0,')"
local operator_controller_tag="${tag//x/0}"
# Require images based on the job
case "$CI_JOB_NAME" in
*-operator-e2e-tests)
cat >> "${image_list}" << END
stackrox-operator ${operator_controller_tag}
stackrox-operator-bundle ${operator_metadata_tag}
stackrox-operator-index ${operator_metadata_tag}
main ${tag}
central-db ${tag}
collector ${tag}
scanner ${tag}
scanner-db ${tag}
scanner-v4 ${tag}
scanner-v4-db ${tag}
END
;;
*-race-condition-qa-e2e-tests)
cat >> "${image_list}" << END
central-db ${tag}
main ${tag}-rcd
roxctl ${tag}
END
if is_in_PR_context && ! pr_has_label "ci-build-race-condition-debug"; then
echo "ERROR: Your PR is missing the \"ci-build-race-condition-debug\" label."
echo "ERROR: This label is required to build the images for $CI_JOB_NAME."
# Quietly continue to allow labels added after tests start.
# Otherwise this message will surface in the Prow log when
# images timeout out below.
fi
;;
*-scanner-v4-install-tests)
cat >> "${image_list}" << END
stackrox-operator ${operator_controller_tag}
stackrox-operator-bundle ${operator_metadata_tag}
stackrox-operator-index ${operator_metadata_tag}
main ${tag}
central-db ${tag}
collector ${tag}
scanner ${tag}
scanner-db ${tag}
scanner-v4 ${tag}
scanner-v4-db ${tag}
roxctl ${tag}
END
;;
*)
cat >> "${image_list}" << END
central-db ${tag}
main ${tag}
roxctl ${tag}
END
;;
esac
if [[ "${DEPLOY_STACKROX_VIA_OPERATOR:-}" == "true" ]]; then
cat >> "${image_list}" << END
stackrox-operator ${operator_controller_tag}
stackrox-operator-bundle ${operator_metadata_tag}
stackrox-operator-index ${operator_metadata_tag}
END
fi
# Remove duplicates.