Skip to content

Commit 0519bb4

Browse files
committed
results using sidecar logs
Updating an example pipeline to include different types of a result. Signed-off-by: Priti Desai <[email protected]>
1 parent a6f78bb commit 0519bb4

File tree

5 files changed

+136
-26
lines changed

5 files changed

+136
-26
lines changed

examples/v1/pipelineruns/alpha/pipelinerun-large-results.yaml

Lines changed: 126 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,149 @@ spec:
77
- name: result1
88
- name: result2
99
- name: result3
10+
type: array
11+
description: The Array results
1012
- name: result4
1113
- name: result5
14+
type: object
15+
description: The object results
16+
properties:
17+
url:
18+
type: string
19+
digest:
20+
type: string
1221
steps:
1322
- name: step1
1423
image: alpine
1524
script: |
16-
cat /dev/urandom | head -c 2500 | base64 | tee $(results.result1.path);
17-
cat /dev/urandom | head -c 2500 | base64 | tee $(results.result2.path);
18-
cat /dev/urandom | head -c 2500 | base64 | tee $(results.result3.path);
19-
cat /dev/urandom | head -c 2500 | base64 | tee $(results.result4.path);
20-
cat /dev/urandom | head -c 2500 | base64 | tee $(results.result5.path);
25+
# produce a result - a random string with 2,500 characters - result1
26+
tr -dc A-Za-z0-9 </dev/urandom | head -c 2500 | tee $(results.result1.path);
27+
28+
# produce a result - a random string with 2,500 characters - result2
29+
tr -dc A-Za-z0-9 </dev/urandom | head -c 2500 | tee $(results.result2.path);
30+
31+
# produce a result - an array with four elements - result3
32+
URL1=`tr -dc A-Za-z0-9 </dev/urandom | head -c 600`
33+
URL2=`tr -dc A-Za-z0-9 </dev/urandom | head -c 700`
34+
URL3=`tr -dc A-Za-z0-9 </dev/urandom | head -c 800`
35+
echo -n "[\"$URL1\", \"\", \"$URL2\", \"$URL3\"]" | tee $(results.result3.path)
36+
37+
# produce a result - a random string with 2,500 characters - result4
38+
tr -dc A-Za-z0-9 </dev/urandom | head -c 2500 | tee $(results.result4.path);
39+
40+
# produce a result - a hash with two objects - result5
41+
URL=`tr -dc A-Za-z0-9 </dev/urandom | head -c 2000`
42+
DIGEST=`tr -dc A-Za-z0-9 </dev/urandom | head -c 200`
43+
echo -n "{\"url\":\"$URL\",\"digest\":\"$DIGEST\"}" | tee $(results.result5.path)
2144
---
45+
2246
apiVersion: tekton.dev/v1
2347
kind: Task
2448
metadata:
25-
name: print-large-results
49+
name: verify-large-results
2650
spec:
2751
params:
2852
- name: param1
2953
- name: param2
3054
- name: param3
55+
description: The array param
56+
type: array
3157
- name: param4
3258
- name: param5
59+
description: The object param
60+
properties:
61+
url:
62+
type: string
63+
digest:
64+
type: string
3365
steps:
3466
- name: step1
3567
image: alpine
68+
args: [
69+
"$(params.param3[*])"
70+
]
3671
script: |
37-
echo "$(params.param1)";
38-
echo "$(params.param2)";
39-
echo "$(params.param3)";
40-
echo "$(params.param4)";
41-
echo "$(params.param5)";
72+
#!/usr/bin/env sh
73+
echo "Validating the length of the param reading larger result - param1"
74+
echo "The string param, param1 must be 2500 characters long"
75+
p1=$(params.param1)
76+
if [ "${#p1}" != 2500 ]; then
77+
echo "Error: expected 2500 characters in param1 but has ${#p1} characters"
78+
exit 1
79+
fi
80+
echo "Done validating the length of the param reading larger result - param1"
81+
82+
echo "Validating the length of the param reading larger result - param2"
83+
echo "The string param, param2 must be 2500 characters long"
84+
p2=$(params.param2)
85+
if [ "${#p2}" != 2500 ]; then
86+
echo "Error: expected 2500 characters in param2 but has ${#p2} characters"
87+
exit 1
88+
fi
89+
echo "Done validating the length of the param reading larger result - param2"
90+
91+
echo "Validating the length of the array parameter - param3"
92+
echo "The array parameter, param3 must have 4 elements"
93+
if [[ $# != 4 ]]; then
94+
echo "Error: expected 4 elements in param3 but has $# characters"
95+
exit 1
96+
fi
97+
echo "Done validating the length of the array parameter - param3"
98+
99+
echo "Validating the first element"
100+
if [ "${#1}" != 600 ]; then
101+
echo "Error: expected 600 characters in the first array element but has ${#1} characters"
102+
exit 1
103+
fi
104+
echo "Done validating the first element"
105+
106+
echo "Validating the second element"
107+
if [ "${#2}" != 0 ]; then
108+
echo "Error: expected 0 characters in the second array element but has ${#2} characters"
109+
exit 1
110+
fi
111+
echo "Done validating the second element"
112+
113+
echo "Validating the third element"
114+
if [ "${#3}" != 700 ]; then
115+
echo "Error: expected 700 characters in the third array element but has ${#3} characters"
116+
exit 1
117+
fi
118+
echo "Done validating the third element"
119+
120+
echo "Validating the fourth element"
121+
if [ "${#4}" != 800 ]; then
122+
echo "Error: expected 800 characters in the fourth array element but has ${#4} characters"
123+
exit 1
124+
fi
125+
echo "Done validating the fourth element"
126+
127+
echo "Validating the length of the param reading larger result - param4"
128+
echo "The string param, param4 must be 2500 characters long"
129+
p4=$(params.param4)
130+
if [ "${#p4}" != 2500 ]; then
131+
echo "Error: expected 2500 characters in param4 but has ${#p4} characters"
132+
exit 1
133+
fi
134+
echo "Done validating the length of the param reading larger result - param4"
135+
136+
echo "validating param5.url"
137+
p51=$(params.param5.url)
138+
if [ "${#p51}" != 2000 ]; then
139+
echo "Error: expected 2000 characters in the url of the hash param \"param5\" but has ${#p51} characters"
140+
exit 1
141+
fi
142+
echo "Done validating param5.url"
143+
144+
echo "Validating param5.digest"
145+
p52=$(params.param5.digest)
146+
if [ "${#p52}" != 200 ]; then
147+
echo "Error: expected 200 characters in the digest of the hash param \"param5\" but has ${#p52} characters"
148+
exit 1
149+
fi
150+
echo "Done validating param5.digest"
42151
---
152+
43153
apiVersion: tekton.dev/v1
44154
kind: Pipeline
45155
metadata:
@@ -49,28 +159,28 @@ spec:
49159
- name: large-task
50160
taskRef:
51161
name: large-result-task
52-
- name: print-results
162+
- name: verify-results
53163
params:
54164
- name: param1
55165
value: "$(tasks.large-task.results.result1)"
56166
- name: param2
57167
value: "$(tasks.large-task.results.result2)"
58168
- name: param3
59-
value: "$(tasks.large-task.results.result3)"
169+
value: "$(tasks.large-task.results.result3[*])"
60170
- name: param4
61171
value: "$(tasks.large-task.results.result4)"
62172
- name: param5
63-
value: "$(tasks.large-task.results.result5)"
173+
value: "$(tasks.large-task.results.result5[*])"
64174
taskRef:
65-
name: print-large-results
175+
name: verify-large-results
66176
results:
67177
- name: large-result
68178
value: $(tasks.large-task.results.result1)
69179
---
70180
apiVersion: tekton.dev/v1
71181
kind: PipelineRun
72182
metadata:
73-
name: large-result-pipeline-run
183+
generateName: large-result-pipeline-run
74184
spec:
75185
pipelineRef:
76186
name: large-result-pipeline

internal/sidecarlogresults/sidecarlogresults.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,12 @@ func extractResultsFromLogs(logs io.Reader, sidecarLogResults []result.RunResult
176176

177177
func parseResults(resultBytes []byte, maxResultLimit int) (result.RunResult, error) {
178178
runResult := result.RunResult{}
179-
if len(resultBytes) > maxResultLimit {
180-
return runResult, ErrSizeExceeded
181-
}
182-
183179
var res SidecarLogResult
184180
if err := json.Unmarshal(resultBytes, &res); err != nil {
185-
return runResult, fmt.Errorf("Invalid result %w", err)
181+
return runResult, fmt.Errorf("invalid result \"%s\": %w", res.Name, err)
182+
}
183+
if len(resultBytes) > maxResultLimit {
184+
return runResult, fmt.Errorf("invalid result \"%s\": %w of %d", res.Name, ErrSizeExceeded, maxResultLimit)
186185
}
187186
runResult = result.RunResult{
188187
Key: res.Name,

internal/sidecarlogresults/sidecarlogresults_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ func TestParseResults(t *testing.T) {
248248
}
249249

250250
func TestParseResults_Failure(t *testing.T) {
251+
maxResultLimit := 4096
251252
result := SidecarLogResult{
252253
Name: "result2",
253254
Value: strings.Repeat("k", 4098),
@@ -256,12 +257,12 @@ func TestParseResults_Failure(t *testing.T) {
256257
res2, _ := json.Marshal(&result)
257258
podLogs := []string{string(res1), string(res2)}
258259
want := []string{
259-
"Invalid result json: cannot unmarshal string into Go value of type sidecarlogresults.SidecarLogResult",
260-
ErrSizeExceeded.Error(),
260+
"invalid result \"\": json: cannot unmarshal string into Go value of type sidecarlogresults.SidecarLogResult",
261+
fmt.Sprintf("invalid result \"%s\": %s of %d", result.Name, ErrSizeExceeded.Error(), maxResultLimit),
261262
}
262263
got := []string{}
263264
for _, plog := range podLogs {
264-
_, err := parseResults([]byte(plog), 4096)
265+
_, err := parseResults([]byte(plog), maxResultLimit)
265266
got = append(got, err.Error())
266267
}
267268
if d := cmp.Diff(want, got); d != "" {

pkg/pod/status_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func TestSetTaskRunStatusBasedOnStepStatus_sidecar_logs(t *testing.T) {
125125
}, {
126126
desc: "test result with sidecar logs bad format",
127127
maxResultSize: 4096,
128-
wantErr: fmt.Errorf("%s", "Invalid result invalid character 'k' in literal false (expecting 'l')"),
128+
wantErr: fmt.Errorf("%s", "invalid result \"\": invalid character 'k' in literal false (expecting 'l')"),
129129
}} {
130130
t.Run(c.desc, func(t *testing.T) {
131131
tr := v1.TaskRun{

pkg/reconciler/taskrun/taskrun.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (c *Reconciler) ReconcileKind(ctx context.Context, tr *v1.TaskRun) pkgrecon
196196
logger.Errorf("Reconcile: %v", err.Error())
197197
if errors.Is(err, sidecarlogresults.ErrSizeExceeded) {
198198
cfg := config.FromContextOrDefaults(ctx)
199-
message := fmt.Sprintf("TaskRun %q failed: results exceeded size limit %d bytes", tr.Name, cfg.FeatureFlags.MaxResultSize)
199+
message := fmt.Sprintf("TaskRun \"%q\" failed: results exceeded size limit %d bytes", tr.Name, cfg.FeatureFlags.MaxResultSize)
200200
err := c.failTaskRun(ctx, tr, v1.TaskRunReasonResultLargerThanAllowedLimit, message)
201201
return c.finishReconcileUpdateEmitEvents(ctx, tr, before, err)
202202
}

0 commit comments

Comments
 (0)