@@ -21,6 +21,8 @@ import (
21
21
"encoding/json"
22
22
"errors"
23
23
"fmt"
24
+ "log/slog"
25
+
24
26
"log"
25
27
"os"
26
28
"os/exec"
@@ -31,17 +33,13 @@ import (
31
33
"syscall"
32
34
"time"
33
35
36
+ "github.com/google/cel-go/cel"
34
37
"github.com/tektoncd/pipeline/internal/artifactref"
35
- "github.com/tektoncd/pipeline/pkg/apis/config"
36
38
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
37
39
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
38
40
"github.com/tektoncd/pipeline/pkg/internal/resultref"
39
- "github.com/tektoncd/pipeline/pkg/pod"
40
41
"github.com/tektoncd/pipeline/pkg/result"
41
42
"github.com/tektoncd/pipeline/pkg/termination"
42
-
43
- "github.com/google/cel-go/cel"
44
- "go.uber.org/zap"
45
43
)
46
44
47
45
// RFC3339 with millisecond
@@ -55,8 +53,21 @@ const (
55
53
breakpointExitSuffix = ".breakpointexit"
56
54
breakpointBeforeStepSuffix = ".beforestepexit"
57
55
ResultExtractionMethodTerminationMessage = "termination-message"
56
+ TerminationReasonSkipped = "Skipped"
57
+ TerminationReasonCancelled = "Cancelled"
58
+ TerminationReasonTimeoutExceeded = "TimeoutExceeded"
59
+ // DownwardMountCancelFile is cancellation file mount to step, entrypoint will check this file to cancel the step.
60
+ downwardMountPoint = "/tekton/downward"
61
+ downwardMountCancelFile = "cancel"
62
+ stepPrefix = "step-"
58
63
)
59
64
65
+ var DownwardMountCancelFile string
66
+
67
+ func init () {
68
+ DownwardMountCancelFile = filepath .Join (downwardMountPoint , downwardMountCancelFile )
69
+ }
70
+
60
71
// DebugBeforeStepError is an error means mark before step breakpoint failure
61
72
type DebugBeforeStepError string
62
73
@@ -180,15 +191,11 @@ type PostWriter interface {
180
191
// Go optionally waits for a file, runs the command, and writes a
181
192
// post file.
182
193
func (e Entrypointer ) Go () error {
183
- prod , _ := zap .NewProduction ()
184
- logger := prod .Sugar ()
185
-
186
194
output := []result.RunResult {}
187
195
defer func () {
188
196
if wErr := termination .WriteMessage (e .TerminationPath , output ); wErr != nil {
189
- logger .Fatalf ("Error while writing message: %s" , wErr )
197
+ log .Fatalf ("Error while writing message: %s" , wErr )
190
198
}
191
- _ = logger .Sync ()
192
199
}()
193
200
194
201
if err := os .MkdirAll (filepath .Join (e .StepMetadataDir , "results" ), os .ModePerm ); err != nil {
@@ -212,7 +219,7 @@ func (e Entrypointer) Go() error {
212
219
})
213
220
214
221
if errors .Is (err , ErrSkipPreviousStepFailed ) {
215
- output = append (output , e .outputRunResult (pod . TerminationReasonSkipped ))
222
+ output = append (output , e .outputRunResult (TerminationReasonSkipped ))
216
223
}
217
224
218
225
return err
@@ -237,10 +244,10 @@ func (e Entrypointer) Go() error {
237
244
var cancel context.CancelFunc
238
245
if err == nil {
239
246
if err := e .applyStepResultSubstitutions (pipeline .StepsDir ); err != nil {
240
- logger .Error ("Error while substituting step results: " , err )
247
+ slog .Error ("Error while substituting step results:" , slog . Any ( "error" , err ) )
241
248
}
242
249
if err := e .applyStepArtifactSubstitutions (pipeline .StepsDir ); err != nil {
243
- logger .Error ("Error while substituting step artifacts: " , err )
250
+ slog .Error ("Error while substituting step artifacts:" , slog . Any ( "error" , err ) )
244
251
}
245
252
246
253
ctx , cancel = context .WithCancel (ctx )
@@ -251,7 +258,7 @@ func (e Entrypointer) Go() error {
251
258
// start a goroutine to listen for cancellation file
252
259
go func () {
253
260
if err := e .waitingCancellation (ctx , cancel ); err != nil && (! IsContextCanceledError (err ) && ! IsContextDeadlineError (err )) {
254
- logger .Error ("Error while waiting for cancellation" , zap . Error ( err ))
261
+ slog .Error ("Error while waiting for cancellation" , slog . Any ( "error" , err ))
255
262
}
256
263
}()
257
264
allowExec , err1 := e .allowExec ()
@@ -262,8 +269,8 @@ func (e Entrypointer) Go() error {
262
269
case allowExec :
263
270
err = e .Runner .Run (ctx , e .Command ... )
264
271
default :
265
- logger .Info ("Step was skipped due to when expressions were evaluated to false." )
266
- output = append (output , e .outputRunResult (pod . TerminationReasonSkipped ))
272
+ slog .Info ("Step was skipped due to when expressions were evaluated to false." )
273
+ output = append (output , e .outputRunResult (TerminationReasonSkipped ))
267
274
e .WritePostFile (e .PostFile , nil )
268
275
e .WriteExitCodeFile (e .StepMetadataDir , "0" )
269
276
return nil
@@ -275,15 +282,15 @@ func (e Entrypointer) Go() error {
275
282
case err != nil && errors .Is (err , errDebugBeforeStep ):
276
283
e .WritePostFile (e .PostFile , err )
277
284
case err != nil && errors .Is (err , ErrContextCanceled ):
278
- logger .Info ("Step was canceling" )
279
- output = append (output , e .outputRunResult (pod . TerminationReasonCancelled ))
285
+ slog .Info ("Step was canceling" )
286
+ output = append (output , e .outputRunResult (TerminationReasonCancelled ))
280
287
e .WritePostFile (e .PostFile , ErrContextCanceled )
281
288
e .WriteExitCodeFile (e .StepMetadataDir , syscall .SIGKILL .String ())
282
289
case errors .Is (err , ErrContextDeadlineExceeded ):
283
290
e .WritePostFile (e .PostFile , err )
284
- output = append (output , e .outputRunResult (pod . TerminationReasonTimeoutExceeded ))
291
+ output = append (output , e .outputRunResult (TerminationReasonTimeoutExceeded ))
285
292
case err != nil && e .BreakpointOnFailure :
286
- logger .Info ("Skipping writing to PostFile" )
293
+ slog .Info ("Skipping writing to PostFile" )
287
294
case e .OnError == ContinueOnError && errors .As (err , & ee ):
288
295
// with continue on error and an ExitError, write non-zero exit code and a post file
289
296
exitCode := strconv .Itoa (ee .ExitCode ())
@@ -311,7 +318,8 @@ func (e Entrypointer) Go() error {
311
318
resultPath = e .ResultsDirectory
312
319
}
313
320
if err := e .readResultsFromDisk (ctx , resultPath , result .TaskRunResultType ); err != nil {
314
- logger .Fatalf ("Error while handling results: %s" , err )
321
+ slog .Error ("Error while substituting step artifacts:" , slog .Any ("error" , err ))
322
+ return err
315
323
}
316
324
}
317
325
if len (e .StepResults ) >= 1 && e .StepResults [0 ] != "" {
@@ -320,12 +328,13 @@ func (e Entrypointer) Go() error {
320
328
stepResultPath = e .ResultsDirectory
321
329
}
322
330
if err := e .readResultsFromDisk (ctx , stepResultPath , result .StepResultType ); err != nil {
323
- logger .Fatalf ("Error while handling step results: %s" , err )
331
+ slog .Error ("Error while substituting step artifacts:" , slog .Any ("error" , err ))
332
+ return err
324
333
}
325
334
}
326
335
327
- if e .ResultExtractionMethod == config . ResultExtractionMethodTerminationMessage {
328
- e .appendArtifactOutputs (& output , logger )
336
+ if e .ResultExtractionMethod == ResultExtractionMethodTerminationMessage {
337
+ e .appendArtifactOutputs (& output )
329
338
}
330
339
331
340
return err
@@ -342,12 +351,12 @@ func readArtifacts(fp string, resultType result.ResultType) ([]result.RunResult,
342
351
return []result.RunResult {{Key : fp , Value : string (file ), ResultType : resultType }}, nil
343
352
}
344
353
345
- func (e Entrypointer ) appendArtifactOutputs (output * []result.RunResult , logger * zap. SugaredLogger ) {
354
+ func (e Entrypointer ) appendArtifactOutputs (output * []result.RunResult ) {
346
355
// step artifacts
347
356
fp := filepath .Join (e .StepMetadataDir , "artifacts" , "provenance.json" )
348
357
artifacts , err := readArtifacts (fp , result .StepArtifactsResultType )
349
358
if err != nil {
350
- logger .Fatalf ("Error while handling step artifacts: %s" , err )
359
+ log .Fatalf ("Error while handling step artifacts: %s" , err )
351
360
}
352
361
* output = append (* output , artifacts ... )
353
362
@@ -359,7 +368,7 @@ func (e Entrypointer) appendArtifactOutputs(output *[]result.RunResult, logger *
359
368
fp = filepath .Join (artifactsDir , "provenance.json" )
360
369
artifacts , err = readArtifacts (fp , result .TaskRunArtifactsResultType )
361
370
if err != nil {
362
- logger .Fatalf ("Error while handling task artifacts: %s" , err )
371
+ log .Fatalf ("Error while handling task artifacts: %s" , err )
363
372
}
364
373
* output = append (* output , artifacts ... )
365
374
}
@@ -451,7 +460,7 @@ func (e Entrypointer) readResultsFromDisk(ctx context.Context, resultDir string,
451
460
output = append (output , signed ... )
452
461
453
462
// push output to termination path
454
- if e .ResultExtractionMethod == config . ResultExtractionMethodTerminationMessage && len (output ) != 0 {
463
+ if e .ResultExtractionMethod == ResultExtractionMethodTerminationMessage && len (output ) != 0 {
455
464
if err := termination .WriteMessage (e .TerminationPath , output ); err != nil {
456
465
return err
457
466
}
@@ -489,7 +498,7 @@ func (e Entrypointer) WriteExitCodeFile(stepPath, content string) {
489
498
490
499
// waitingCancellation waiting cancellation file, if no error occurs, call cancelFunc to cancel the context
491
500
func (e Entrypointer ) waitingCancellation (ctx context.Context , cancel context.CancelFunc ) error {
492
- if err := e .Waiter .Wait (ctx , pod . DownwardMountCancelFile , true , false ); err != nil {
501
+ if err := e .Waiter .Wait (ctx , DownwardMountCancelFile , true , false ); err != nil {
493
502
return err
494
503
}
495
504
cancel ()
@@ -518,10 +527,15 @@ func (e Entrypointer) CheckForBreakpointOnFailure() {
518
527
}
519
528
}
520
529
530
+ // GetContainerName prefixes the input name with "step-"
531
+ func GetContainerName (name string ) string {
532
+ return fmt .Sprintf ("%s%s" , stepPrefix , name )
533
+ }
534
+
521
535
// loadStepResult reads the step result file and returns the string, array or object result value.
522
536
func loadStepResult (stepDir string , stepName string , resultName string ) (v1.ResultValue , error ) {
523
537
v := v1.ResultValue {}
524
- fp := getStepResultPath (stepDir , pod . GetContainerName (stepName ), resultName )
538
+ fp := getStepResultPath (stepDir , GetContainerName (stepName ), resultName )
525
539
fileContents , err := os .ReadFile (fp )
526
540
if err != nil {
527
541
return v , err
0 commit comments