-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathentrypointer.go
889 lines (800 loc) · 28.3 KB
/
entrypointer.go
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
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package entrypoint
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"
"time"
"github.com/tektoncd/pipeline/internal/artifactref"
"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/internal/resultref"
"github.com/tektoncd/pipeline/pkg/pod"
"github.com/tektoncd/pipeline/pkg/result"
"github.com/tektoncd/pipeline/pkg/termination"
"github.com/google/cel-go/cel"
"go.uber.org/zap"
)
// RFC3339 with millisecond
const (
timeFormat = "2006-01-02T15:04:05.000Z07:00"
ContinueOnError = "continue"
StopAndFailOnError = "stopAndFail"
ContinueAndFailOnError = "continueAndFail"
)
const (
breakpointExitSuffix = ".breakpointexit"
breakpointBeforeStepSuffix = ".beforestepexit"
ResultExtractionMethodTerminationMessage = "termination-message"
)
// DebugBeforeStepError is an error means mark before step breakpoint failure
type DebugBeforeStepError string
func (e DebugBeforeStepError) Error() string {
return string(e)
}
var (
errDebugBeforeStep = DebugBeforeStepError("before step breakpoint error file, user decided to skip the current step execution")
)
// ScriptDir for testing
var ScriptDir = pipeline.ScriptDir
// ContextError context error type
type ContextError string
// Error implements error interface
func (e ContextError) Error() string {
return string(e)
}
type SkipError string
func (e SkipError) Error() string {
return string(e)
}
var (
// ErrContextDeadlineExceeded is the error returned when the context deadline is exceeded
ErrContextDeadlineExceeded = ContextError(context.DeadlineExceeded.Error())
// ErrContextCanceled is the error returned when the context is canceled
ErrContextCanceled = ContextError(context.Canceled.Error())
// ErrSkipPreviousStepFailed is the error returned when the step is skipped due to previous step error
ErrSkipPreviousStepFailed = SkipError("error file present, bail and skip the step")
)
// IsContextDeadlineError determine whether the error is context deadline
func IsContextDeadlineError(err error) bool {
return errors.Is(err, ErrContextDeadlineExceeded)
}
// IsContextCanceledError determine whether the error is context canceled
func IsContextCanceledError(err error) bool {
return errors.Is(err, ErrContextCanceled)
}
// Entrypointer holds fields for running commands with redirected
// entrypoints.
type Entrypointer struct {
// Command is the original specified command and args.
Command []string
// WaitFiles is the set of files to wait for. If empty, execution
// begins immediately.
WaitFiles []string
// WaitFileContent indicates the WaitFile should have non-zero size
// before continuing with execution.
WaitFileContent bool
// PostFile is the file to write when complete. If not specified, no
// file is written.
PostFile string
// Termination path is the path of a file to write the starting time of this endpopint
TerminationPath string
// Waiter encapsulates waiting for files to exist.
Waiter Waiter
// Runner encapsulates running commands.
Runner Runner
// PostWriter encapsulates writing files when complete.
PostWriter PostWriter
// StepResults is the set of files that might contain step results
StepResults []string
// Results is the set of files that might contain task results
Results []string
// Timeout is an optional user-specified duration within which the Step must complete
Timeout *time.Duration
// BreakpointOnFailure helps determine if entrypoint execution needs to adapt debugging requirements
BreakpointOnFailure bool
// DebugBeforeStep help user attach container before execution
DebugBeforeStep bool
// OnError defines exiting behavior of the entrypoint
// set it to "stopAndFail" to indicate the entrypoint to exit the taskRun if the container exits with non zero exit code
// set it to "continue" to indicate the entrypoint to continue executing the rest of the steps irrespective of the container exit code
// set it to "continueAndFail" to indicate the entrypoint to continue executing the rest of the steps irrespective of the container exit code and exit the taskRun if the container exits with non zero exit code
OnError string
// StepMetadataDir is the directory for a step where the step related metadata can be stored
StepMetadataDir string
// SpireWorkloadAPI connects to spire and does obtains SVID based on taskrun
SpireWorkloadAPI EntrypointerAPIClient
// ResultsDirectory is the directory to find results, defaults to pipeline.DefaultResultPath
ResultsDirectory string
// ResultExtractionMethod is the method using which the controller extracts the results from the task pod.
ResultExtractionMethod string
// StepWhenExpressions a list of when expression to decide if the step should be skipped
StepWhenExpressions v1.StepWhenExpressions
// ArtifactsDirectory is the directory to find artifacts, defaults to pipeline.ArtifactsDir
ArtifactsDirectory string
}
// Waiter encapsulates waiting for files to exist.
type Waiter interface {
// Wait blocks until the specified file exists or the context is done.
Wait(ctx context.Context, file string, expectContent bool, breakpointOnFailure bool) error
}
// Runner encapsulates running commands.
type Runner interface {
Run(ctx context.Context, args ...string) error
}
// PostWriter encapsulates writing a file when complete.
type PostWriter interface {
// Write writes to the path when complete.
Write(file, content string)
}
// Go optionally waits for a file, runs the command, and writes a
// post file.
func (e Entrypointer) Go() error {
prod, _ := zap.NewProduction()
logger := prod.Sugar()
output := []result.RunResult{}
defer func() {
if wErr := termination.WriteMessage(e.TerminationPath, output); wErr != nil {
logger.Fatalf("Error while writing message: %s", wErr)
}
_ = logger.Sync()
}()
if err := os.MkdirAll(filepath.Join(e.StepMetadataDir, "results"), os.ModePerm); err != nil {
return err
}
if err := os.MkdirAll(filepath.Join(e.StepMetadataDir, "artifacts"), os.ModePerm); err != nil {
return err
}
for _, f := range e.WaitFiles {
if err := e.Waiter.Wait(context.Background(), f, e.WaitFileContent, e.BreakpointOnFailure); err != nil {
// An error happened while waiting, so we bail
// *but* we write postfile to make next steps bail too.
// In case of breakpoint on failure do not write post file.
if !e.BreakpointOnFailure {
e.WritePostFile(e.PostFile, err)
}
output = append(output, result.RunResult{
Key: "StartedAt",
Value: time.Now().Format(timeFormat),
ResultType: result.InternalTektonResultType,
})
if errors.Is(err, ErrSkipPreviousStepFailed) {
output = append(output, e.outputRunResult(pod.TerminationReasonSkipped))
}
return err
}
}
var err error
if e.DebugBeforeStep {
err = e.waitBeforeStepDebug()
}
output = append(output, result.RunResult{
Key: "StartedAt",
Value: time.Now().Format(timeFormat),
ResultType: result.InternalTektonResultType,
})
if e.Timeout != nil && *e.Timeout < time.Duration(0) {
err = errors.New("negative timeout specified")
}
ctx := context.Background()
var cancel context.CancelFunc
if err == nil {
if err := e.applyStepResultSubstitutions(pipeline.StepsDir); err != nil {
logger.Error("Error while substituting step results: ", err)
}
if err := e.applyStepArtifactSubstitutions(pipeline.StepsDir); err != nil {
logger.Error("Error while substituting step artifacts: ", err)
}
ctx, cancel = context.WithCancel(ctx)
if e.Timeout != nil && *e.Timeout > time.Duration(0) {
ctx, cancel = context.WithTimeout(ctx, *e.Timeout)
}
defer cancel()
// start a goroutine to listen for cancellation file
go func() {
if err := e.waitingCancellation(ctx, cancel); err != nil && (!IsContextCanceledError(err) && !IsContextDeadlineError(err)) {
logger.Error("Error while waiting for cancellation", zap.Error(err))
}
}()
allowExec, err1 := e.allowExec()
switch {
case err1 != nil:
err = err1
case allowExec:
err = e.Runner.Run(ctx, e.Command...)
default:
logger.Info("Step was skipped due to when expressions were evaluated to false.")
output = append(output, e.outputRunResult(pod.TerminationReasonSkipped))
e.WritePostFile(e.PostFile, nil)
e.WriteExitCodeFile(e.StepMetadataDir, "0")
return nil
}
}
var ee *exec.ExitError
switch {
case err != nil && errors.Is(err, errDebugBeforeStep):
e.WritePostFile(e.PostFile, err)
case err != nil && errors.Is(err, ErrContextCanceled):
logger.Info("Step was canceling")
output = append(output, e.outputRunResult(pod.TerminationReasonCancelled))
e.WritePostFile(e.PostFile, ErrContextCanceled)
e.WriteExitCodeFile(e.StepMetadataDir, syscall.SIGKILL.String())
case errors.Is(err, ErrContextDeadlineExceeded):
e.WritePostFile(e.PostFile, err)
output = append(output, e.outputRunResult(pod.TerminationReasonTimeoutExceeded))
case err != nil && e.BreakpointOnFailure:
logger.Info("Skipping writing to PostFile")
case e.OnError == ContinueOnError && errors.As(err, &ee):
// with continue on error and an ExitError, write non-zero exit code and a post file
exitCode := strconv.Itoa(ee.ExitCode())
output = append(output, result.RunResult{
Key: "ExitCode",
Value: exitCode,
ResultType: result.InternalTektonResultType,
})
e.WritePostFile(e.PostFile, nil)
e.WriteExitCodeFile(e.StepMetadataDir, exitCode)
case e.OnError == ContinueAndFailOnError && errors.As(err, &ee):
// with continueAndFail on error and an ExitError, write non-zero exit code and a post file with the error
exitCode := strconv.Itoa(ee.ExitCode())
output = append(output, result.RunResult{
Key: "ExitCode",
Value: exitCode,
ResultType: result.InternalTektonResultType,
})
e.WritePostFile(e.PostFile, err)
e.WriteExitCodeFile(e.StepMetadataDir, exitCode)
case err == nil:
// if err is nil, write zero exit code and a post file
e.WritePostFile(e.PostFile, nil)
e.WriteExitCodeFile(e.StepMetadataDir, "0")
default:
// for a step without continue on error and any error, write a post file with .err
e.WritePostFile(e.PostFile, err)
}
// strings.Split(..) with an empty string returns an array that contains one element, an empty string.
// This creates an error when trying to open the result folder as a file.
if len(e.Results) >= 1 && e.Results[0] != "" {
resultPath := pipeline.DefaultResultPath
if e.ResultsDirectory != "" {
resultPath = e.ResultsDirectory
}
if err := e.readResultsFromDisk(ctx, resultPath, result.TaskRunResultType); err != nil {
logger.Fatalf("Error while handling results: %s", err)
}
}
if len(e.StepResults) >= 1 && e.StepResults[0] != "" {
stepResultPath := filepath.Join(e.StepMetadataDir, "results")
if e.ResultsDirectory != "" {
stepResultPath = e.ResultsDirectory
}
if err := e.readResultsFromDisk(ctx, stepResultPath, result.StepResultType); err != nil {
logger.Fatalf("Error while handling step results: %s", err)
}
}
if e.ResultExtractionMethod == config.ResultExtractionMethodTerminationMessage {
e.appendArtifactOutputs(&output, logger)
}
return err
}
func readArtifacts(fp string, resultType result.ResultType) ([]result.RunResult, error) {
file, err := os.ReadFile(fp)
if os.IsNotExist(err) {
return []result.RunResult{}, nil
}
if err != nil {
return nil, err
}
return []result.RunResult{{Key: fp, Value: string(file), ResultType: resultType}}, nil
}
func (e Entrypointer) appendArtifactOutputs(output *[]result.RunResult, logger *zap.SugaredLogger) {
// step artifacts
fp := filepath.Join(e.StepMetadataDir, "artifacts", "provenance.json")
artifacts, err := readArtifacts(fp, result.StepArtifactsResultType)
if err != nil {
logger.Fatalf("Error while handling step artifacts: %s", err)
}
*output = append(*output, artifacts...)
artifactsDir := pipeline.ArtifactsDir
// task artifacts
if e.ArtifactsDirectory != "" {
artifactsDir = e.ArtifactsDirectory
}
fp = filepath.Join(artifactsDir, "provenance.json")
artifacts, err = readArtifacts(fp, result.TaskRunArtifactsResultType)
if err != nil {
logger.Fatalf("Error while handling task artifacts: %s", err)
}
*output = append(*output, artifacts...)
}
func (e Entrypointer) allowExec() (bool, error) {
when := e.StepWhenExpressions
m := map[string]bool{}
for _, we := range when {
if we.CEL == "" {
continue
}
b, ok := m[we.CEL]
if ok && !b {
return false, nil
}
env, err := cel.NewEnv()
if err != nil {
return false, err
}
ast, iss := env.Compile(we.CEL)
if iss.Err() != nil {
return false, iss.Err()
}
// Generate an evaluable instance of the Ast within the environment
prg, err := env.Program(ast)
if err != nil {
return false, err
}
// Evaluate the CEL expression
out, _, err := prg.Eval(map[string]interface{}{})
if err != nil {
return false, err
}
b, ok = out.Value().(bool)
if !ok {
return false, fmt.Errorf("the CEL expression %s is not evaluated to a boolean", we.CEL)
}
if !b {
return false, err
}
m[we.CEL] = true
}
return when.AllowsExecution(m), nil
}
func (e Entrypointer) waitBeforeStepDebug() error {
log.Println(`debug before step breakpoint has taken effect, waiting for user's decision:
1) continue, use cmd: /tekton/debug/scripts/debug-beforestep-continue
2) fail-continue, use cmd: /tekton/debug/scripts/debug-beforestep-fail-continue`)
breakpointBeforeStepPostFile := e.PostFile + breakpointBeforeStepSuffix
if waitErr := e.Waiter.Wait(context.Background(), breakpointBeforeStepPostFile, false, false); waitErr != nil {
log.Println("error occurred while waiting for " + breakpointBeforeStepPostFile + " : " + errDebugBeforeStep.Error())
return errDebugBeforeStep
}
return nil
}
func (e Entrypointer) readResultsFromDisk(ctx context.Context, resultDir string, resultType result.ResultType) error {
output := []result.RunResult{}
results := e.Results
if resultType == result.StepResultType {
results = e.StepResults
}
for _, resultFile := range results {
if resultFile == "" {
continue
}
fileContents, err := os.ReadFile(filepath.Join(resultDir, resultFile))
if os.IsNotExist(err) {
continue
} else if err != nil {
return err
}
// if the file doesn't exist, ignore it
output = append(output, result.RunResult{
Key: resultFile,
Value: string(fileContents),
ResultType: resultType,
})
}
signed, err := signResults(ctx, e.SpireWorkloadAPI, output)
if err != nil {
return err
}
output = append(output, signed...)
// push output to termination path
if e.ResultExtractionMethod == config.ResultExtractionMethodTerminationMessage && len(output) != 0 {
if err := termination.WriteMessage(e.TerminationPath, output); err != nil {
return err
}
}
return nil
}
// BreakpointExitCode reads the post file and returns the exit code it contains
func (e Entrypointer) BreakpointExitCode(breakpointExitPostFile string) (int, error) {
exitCode, err := os.ReadFile(breakpointExitPostFile)
if os.IsNotExist(err) {
return 0, fmt.Errorf("breakpoint postfile %s not found", breakpointExitPostFile)
}
strExitCode := strings.TrimSuffix(string(exitCode), "\n")
log.Println("Breakpoint exiting with exit code " + strExitCode)
return strconv.Atoi(strExitCode)
}
// WritePostFile write the postfile
func (e Entrypointer) WritePostFile(postFile string, err error) {
if err != nil && postFile != "" {
postFile += ".err"
}
if postFile != "" {
e.PostWriter.Write(postFile, "")
}
}
// WriteExitCodeFile write the exitCodeFile
func (e Entrypointer) WriteExitCodeFile(stepPath, content string) {
exitCodeFile := filepath.Join(stepPath, "exitCode")
e.PostWriter.Write(exitCodeFile, content)
}
// waitingCancellation waiting cancellation file, if no error occurs, call cancelFunc to cancel the context
func (e Entrypointer) waitingCancellation(ctx context.Context, cancel context.CancelFunc) error {
if err := e.Waiter.Wait(ctx, pod.DownwardMountCancelFile, true, false); err != nil {
return err
}
cancel()
return nil
}
// CheckForBreakpointOnFailure if step up breakpoint on failure
// waiting breakpointExitPostFile to be written
func (e Entrypointer) CheckForBreakpointOnFailure() {
if e.BreakpointOnFailure {
log.Println(`debug onFailure breakpoint has taken effect, waiting for user's decision:
1) continue, use cmd: /tekton/debug/scripts/debug-continue
2) fail-continue, use cmd: /tekton/debug/scripts/debug-fail-continue`)
breakpointExitPostFile := e.PostFile + breakpointExitSuffix
if waitErr := e.Waiter.Wait(context.Background(), breakpointExitPostFile, false, false); waitErr != nil {
log.Println("error occurred while waiting for " + breakpointExitPostFile + " : " + waitErr.Error())
}
// get exitcode from .breakpointexit
exitCode, readErr := e.BreakpointExitCode(breakpointExitPostFile)
// if readErr exists, the exitcode with default to 0 as we would like
// to encourage to continue running the next steps in the taskRun
if readErr != nil {
log.Println("error occurred while reading breakpoint exit code : " + readErr.Error())
}
os.Exit(exitCode)
}
}
// loadStepResult reads the step result file and returns the string, array or object result value.
func loadStepResult(stepDir string, stepName string, resultName string) (v1.ResultValue, error) {
v := v1.ResultValue{}
fp := getStepResultPath(stepDir, pod.GetContainerName(stepName), resultName)
fileContents, err := os.ReadFile(fp)
if err != nil {
return v, err
}
err = v.UnmarshalJSON(fileContents)
if err != nil {
return v, err
}
return v, nil
}
// getStepResultPath gets the path to the step result
func getStepResultPath(stepDir string, stepName string, resultName string) string {
return filepath.Join(stepDir, stepName, "results", resultName)
}
// findReplacement looks for any usage of step results in an input string.
// If found, it loads the results from the previous steps and provides the replacement value.
func findReplacement(stepDir string, s string) (string, []string, error) {
value := strings.TrimSuffix(strings.TrimPrefix(s, "$("), ")")
pr, err := resultref.ParseStepExpression(value)
if err != nil {
return "", nil, err
}
result, err := loadStepResult(stepDir, pr.ResourceName, pr.ResultName)
if err != nil {
return "", nil, err
}
replaceWithArray := []string{}
replaceWithString := ""
switch pr.ResultType {
case "object":
if pr.ObjectKey != "" {
replaceWithString = result.ObjectVal[pr.ObjectKey]
}
case "array":
if pr.ArrayIdx != nil {
replaceWithString = result.ArrayVal[*pr.ArrayIdx]
} else {
replaceWithArray = append(replaceWithArray, result.ArrayVal...)
}
// "string"
default:
replaceWithString = result.StringVal
}
return replaceWithString, replaceWithArray, nil
}
// replaceEnv performs replacements for step results in environment variables.
func replaceEnv(stepDir string) error {
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
matches := resultref.StepResultRegex.FindAllStringSubmatch(pair[1], -1)
v := pair[1]
for _, m := range matches {
replaceWith, _, err := findReplacement(stepDir, m[0])
if err != nil {
return err
}
v = strings.ReplaceAll(v, m[0], replaceWith)
}
os.Setenv(pair[0], v)
}
return nil
}
// replaceCommandAndArgs performs replacements for step results in e.Command
func replaceCommandAndArgs(command []string, stepDir string) ([]string, error) {
var newCommand []string
for _, c := range command {
matches := resultref.StepResultRegex.FindAllStringSubmatch(c, -1)
newC := []string{c}
for _, m := range matches {
replaceWithString, replaceWithArray, err := findReplacement(stepDir, m[0])
if err != nil {
return []string{}, fmt.Errorf("failed to find replacement for %s to replace %s", m[0], c)
}
// replaceWithString and replaceWithArray are mutually exclusive
if len(replaceWithArray) > 0 {
if c != m[0] {
// it has to be exact in "$(steps.<step-name>.results.<result-name>[*])" format, without anything else in the original string
return nil, errors.New("value must be in \"$(steps.<step-name>.results.<result-name>[*])\" format, when using array results")
}
newC = replaceWithArray
} else {
newC[0] = strings.ReplaceAll(newC[0], m[0], replaceWithString)
}
}
newCommand = append(newCommand, newC...)
}
return newCommand, nil
}
// applyStepResultSubstitutions applies the runtime step result substitutions in env, args and command.
func (e *Entrypointer) applyStepResultSubstitutions(stepDir string) error {
// env
if err := replaceEnv(stepDir); err != nil {
return err
}
// replace when
newWhen, err := replaceWhen(stepDir, e.StepWhenExpressions)
if err != nil {
return err
}
e.StepWhenExpressions = newWhen
// command + args
newCommand, err := replaceCommandAndArgs(e.Command, stepDir)
if err != nil {
return err
}
e.Command = newCommand
return nil
}
func replaceWhen(stepDir string, when v1.StepWhenExpressions) (v1.StepWhenExpressions, error) {
for i, w := range when {
var newValues []string
flag:
for _, v := range when[i].Values {
matches := resultref.StepResultRegex.FindAllStringSubmatch(v, -1)
newV := v
for _, m := range matches {
replaceWithString, replaceWithArray, err := findReplacement(stepDir, m[0])
if err != nil {
return v1.WhenExpressions{}, err
}
// replaceWithString and replaceWithArray are mutually exclusive
if len(replaceWithArray) > 0 {
if v != m[0] {
// it has to be exact in "$(steps.<step-name>.results.<result-name>[*])" format, without anything else in the original string
return nil, errors.New("value must be in \"$(steps.<step-name>.results.<result-name>[*])\" format, when using array results")
}
newValues = append(newValues, replaceWithArray...)
continue flag
}
newV = strings.ReplaceAll(newV, m[0], replaceWithString)
}
newValues = append(newValues, newV)
}
when[i].Values = newValues
matches := resultref.StepResultRegex.FindAllStringSubmatch(w.Input, -1)
v := when[i].Input
for _, m := range matches {
replaceWith, _, err := findReplacement(stepDir, m[0])
if err != nil {
return v1.StepWhenExpressions{}, err
}
v = strings.ReplaceAll(v, m[0], replaceWith)
}
when[i].Input = v
matches = resultref.StepResultRegex.FindAllStringSubmatch(w.CEL, -1)
c := when[i].CEL
for _, m := range matches {
replaceWith, _, err := findReplacement(stepDir, m[0])
if err != nil {
return v1.StepWhenExpressions{}, err
}
c = strings.ReplaceAll(c, m[0], replaceWith)
}
when[i].CEL = c
}
return when, nil
}
// outputRunResult returns the run reason for a termination
func (e Entrypointer) outputRunResult(terminationReason string) result.RunResult {
return result.RunResult{
Key: "Reason",
Value: terminationReason,
ResultType: result.InternalTektonResultType,
}
}
// getStepArtifactsPath gets the path to the step artifacts
func getStepArtifactsPath(stepDir string, containerName string) string {
return filepath.Join(stepDir, containerName, "artifacts", "provenance.json")
}
// loadStepArtifacts loads and parses the artifacts file for a specified step.
func loadStepArtifacts(stepDir string, containerName string) (v1.Artifacts, error) {
v := v1.Artifacts{}
fp := getStepArtifactsPath(stepDir, containerName)
fileContents, err := os.ReadFile(fp)
if err != nil {
return v, err
}
err = json.Unmarshal(fileContents, &v)
if err != nil {
return v, err
}
return v, nil
}
// getArtifactValues retrieves the values associated with a specified artifact reference.
// It parses the provided artifact template, loads the corresponding step's artifacts, and extracts the relevant values.
// If the artifact name is not specified in the template, the values of the first output are returned.
func getArtifactValues(dir string, template string) (string, error) {
artifactTemplate, err := parseArtifactTemplate(template)
if err != nil {
return "", err
}
artifacts, err := loadStepArtifacts(dir, artifactTemplate.ContainerName)
if err != nil {
return "", err
}
// $(steps.stepName.outputs.artifactName) <- artifacts.Output[artifactName].Values
var t []v1.Artifact
if artifactTemplate.Type == "outputs" {
t = artifacts.Outputs
} else {
t = artifacts.Inputs
}
for _, ar := range t {
if ar.Name == artifactTemplate.ArtifactName {
marshal, err := json.Marshal(ar.Values)
if err != nil {
return "", err
}
return string(marshal), err
}
}
return "", fmt.Errorf("values for template %s not found", template)
}
// parseArtifactTemplate parses an artifact template string and extracts relevant information into an ArtifactTemplate struct.
// The artifact template is expected to be in the format "$(steps.<step-name>.outputs.<artifact-category-name>)".
func parseArtifactTemplate(template string) (ArtifactTemplate, error) {
if template == "" {
return ArtifactTemplate{}, errors.New("template is empty")
}
if artifactref.StepArtifactRegex.FindString(template) != template {
return ArtifactTemplate{}, fmt.Errorf("invalid artifact template %s", template)
}
template = strings.TrimSuffix(strings.TrimPrefix(template, "$("), ")")
split := strings.Split(template, ".")
at := ArtifactTemplate{
ContainerName: "step-" + split[1],
Type: split[2],
}
if len(split) == 4 {
at.ArtifactName = split[3]
}
return at, nil
}
// ArtifactTemplate holds steps artifacts metadata parsed from step artifacts interpolation
type ArtifactTemplate struct {
ContainerName string
Type string // inputs or outputs
ArtifactName string
}
// applyStepArtifactSubstitutions replaces artifact references within a step's command and environment variables with their corresponding values.
//
// This function is designed to handle artifact substitutions in a script file, inline command, or environment variables.
//
// Args:
//
// stepDir: The directory of the executing step.
//
// Returns:
//
// An error object if any issues occur during substitution.
func (e *Entrypointer) applyStepArtifactSubstitutions(stepDir string) error {
// Script was re-written into a file, we need to read the file to and substitute the content
// and re-write the command.
// While param substitution cannot be used in Script from StepAction, allowing artifact substitution doesn't seem bad as
// artifacts are unmarshalled, should be safe.
if len(e.Command) == 1 && filepath.Dir(e.Command[0]) == filepath.Clean(ScriptDir) {
dataBytes, err := os.ReadFile(e.Command[0])
if err != nil {
return err
}
fileContent := string(dataBytes)
v, err := replaceValue(artifactref.StepArtifactRegex, fileContent, stepDir, getArtifactValues)
if err != nil {
return err
}
if v != fileContent {
temp, err := writeToTempFile(v)
if err != nil {
return err
}
e.Command = []string{temp.Name()}
}
} else {
command := e.Command
var newCmd []string
for _, c := range command {
v, err := replaceValue(artifactref.StepArtifactRegex, c, stepDir, getArtifactValues)
if err != nil {
return err
}
newCmd = append(newCmd, v)
}
e.Command = newCmd
}
// substitute env
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
v, err := replaceValue(artifactref.StepArtifactRegex, pair[1], stepDir, getArtifactValues)
if err != nil {
return err
}
os.Setenv(pair[0], v)
}
return nil
}
func writeToTempFile(v string) (*os.File, error) {
tmp, err := os.CreateTemp("", "script-*")
if err != nil {
return nil, err
}
err = os.Chmod(tmp.Name(), 0o755)
if err != nil {
return nil, err
}
_, err = tmp.WriteString(v)
if err != nil {
return nil, err
}
err = tmp.Close()
if err != nil {
return nil, err
}
return tmp, nil
}
func replaceValue(regex *regexp.Regexp, src string, stepDir string, getValue func(string, string) (string, error)) (string, error) {
matches := regex.FindAllStringSubmatch(src, -1)
t := src
for _, m := range matches {
v, err := getValue(stepDir, m[0])
if err != nil {
return "", err
}
t = strings.ReplaceAll(t, m[0], v)
}
return t, nil
}