-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathmocks.sh
More file actions
155 lines (140 loc) · 5 KB
/
Copy pathmocks.sh
File metadata and controls
155 lines (140 loc) · 5 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
#!/usr/bin/env bash
set -eux
# mocks to be injected into task step scripts
function internal-request() {
printf '%s\n' "$*" >> $(params.dataDir)/mock_internal-request.txt
# Extract unique identifier from the task context that we can use for labeling
PIPELINE_UID=""
for arg in "$@"; do
if [[ "$arg" == *"pipelinerun-uid="* ]]; then
PIPELINE_UID=$(echo "$arg" | sed 's/.*pipelinerun-uid=//')
break
fi
done
# set to async and capture output
/home/utils/internal-request "$@" -s false | tee $(params.dataDir)/ir-output-${PIPELINE_UID:-default}.tmp
sleep 1
# Extract the IR name from the captured output specific to this pipeline
IR_NAME=$(awk -F"'" '/created/ { print $2 }' $(params.dataDir)/ir-output-${PIPELINE_UID:-default}.tmp)
if [ -z "$IR_NAME" ]; then
# Fallback: try to find IR with matching pipeline UID label
if [ -n "$PIPELINE_UID" ]; then
IR_NAME=$(kubectl get internalrequest -l "internal-services.appstudio.openshift.io/pipelinerun-uid=${PIPELINE_UID}" --no-headers -o custom-columns=":metadata.name" --sort-by=.metadata.creationTimestamp | tail -1)
fi
# Final fallback to the original method
if [ -z "$IR_NAME" ]; then
IR_NAME=$(kubectl get internalrequest --no-headers -o custom-columns=":metadata.name" \
--sort-by=.metadata.creationTimestamp | tail -1)
fi
fi
if [ -z "$IR_NAME" ]; then
echo "Error: Unable to get IR name for pipeline UID: ${PIPELINE_UID:-none}"
echo "Internal requests:"
kubectl get internalrequest --no-headers -o custom-columns=":metadata.name" \
--sort-by=.metadata.creationTimestamp
exit 1
fi
if [[ "$*" == *"fbcFragments="*"fail.io"* ]]; then
set_ir_status $IR_NAME 1
else
set_ir_status $IR_NAME 0
fi
}
function set_ir_status() {
NAME=$1
EXITCODE=$2
PATCH_FILE=$(params.dataDir)/${NAME}-patch.json
# Determine condition status based on exit code - matches internal-services behavior
if [ "${EXITCODE}" -eq 0 ]; then
CONDITION_STATUS="True"
CONDITION_REASON="Succeeded"
CONDITION_MESSAGE=""
else
CONDITION_STATUS="False"
CONDITION_REASON="Failed"
CONDITION_MESSAGE="Internal request failed with exit code ${EXITCODE}"
fi
# Match real internal-services behavior: results are extracted from PipelineRun and stored as map[string]string
# For failures (exitCode != 0), do not provide results as they would be empty in real scenarios
if [ "${EXITCODE}" -eq 0 ]; then
cat > $PATCH_FILE << EOF
{
"status": {
"results": {
"jsonBuildInfo": "$(echo '{"updated":"2024-03-06T16:39:11.314092Z", "index_image": "redhat.com/rh-stage/iib:01", "index_image_resolved": "redhat.com/rh-stage/iib@sha256:abcdefghijk"}' | gzip -c | base64 -w0)",
"indexImageDigests": "quay.io/a quay.io/b",
"iibLog": "Dummy IIB Log",
"exitCode": "${EXITCODE}"
},
"conditions": [
{
"type": "Succeeded",
"status": "${CONDITION_STATUS}",
"reason": "${CONDITION_REASON}",
"message": "${CONDITION_MESSAGE}",
"lastTransitionTime": "$(/usr/bin/date -u +%Y-%m-%dT%H:%M:%SZ)"
}
]
}
}
EOF
else
# For failures, only provide conditions without results (matches real behavior)
cat > $PATCH_FILE << EOF
{
"status": {
"conditions": [
{
"type": "Succeeded",
"status": "${CONDITION_STATUS}",
"reason": "${CONDITION_REASON}",
"message": "${CONDITION_MESSAGE}",
"lastTransitionTime": "$(/usr/bin/date -u +%Y-%m-%dT%H:%M:%SZ)"
}
]
}
}
EOF
fi
# Add retry logic for the patch operation to handle timing issues
for attempt in 1 2 3 4 5; do
if kubectl patch internalrequest $NAME --type=merge --subresource status --patch-file $PATCH_FILE; then
echo "Successfully patched InternalRequest $NAME on attempt $attempt"
break
else
echo "Patch attempt $attempt failed for InternalRequest $NAME, retrying in 2 seconds..."
if [ $attempt -eq 5 ]; then
echo "ERROR: Failed to patch InternalRequest $NAME after 5 attempts"
echo "Available InternalRequests:"
kubectl get internalrequest --no-headers -o custom-columns=":metadata.name"
exit 1
fi
sleep 2
fi
done
}
function date() {
echo $* >> $(params.dataDir)/mock_date.txt
case "$*" in
"+%Y-%m-%dT%H:%M:%SZ")
echo "2023-10-10T15:00:00Z" |tee $(params.dataDir)/mock_date_iso_format.txt
;;
"+%s"*)
echo "1696946200" | tee $(params.dataDir)/mock_date_epoch.txt
;;
"-u +%Hh%Mm%Ss -d @"*)
/usr/bin/date $*
;;
"-u +%Hh%Mm%Ss -d @"*)
usr/bin/date $*
;;
"*")
echo Error: Unexpected call
exit 1
;;
esac
}
# Export functions so they're available to the task scripts
export -f internal-request
export -f set_ir_status
export -f date