Skip to content

Commit 44a7e35

Browse files
PuneetPunamiyatekton-robot
authored andcommitted
cleanup: remove zap and dependency where k8s api are included
Signed-off-by: PuneetPunamiya <[email protected]>
1 parent 00ed658 commit 44a7e35

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

cmd/entrypoint/runner.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ import (
3131
"syscall"
3232

3333
"github.com/tektoncd/pipeline/pkg/entrypoint"
34-
"github.com/tektoncd/pipeline/pkg/pod"
34+
)
35+
36+
const (
37+
TektonHermeticEnvVar = "TEKTON_HERMETIC"
3538
)
3639

3740
// TODO(jasonhall): Test that original exit code is propagated and that
@@ -111,7 +114,7 @@ func (rr *realRunner) Run(ctx context.Context, args ...string) error {
111114
// main process and all children
112115
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
113116

114-
if os.Getenv("TEKTON_RESOURCE_NAME") == "" && os.Getenv(pod.TektonHermeticEnvVar) == "1" {
117+
if os.Getenv("TEKTON_RESOURCE_NAME") == "" && os.Getenv(TektonHermeticEnvVar) == "1" {
115118
dropNetworking(cmd)
116119
}
117120

pkg/entrypoint/entrypointer.go

+44-30
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"encoding/json"
2222
"errors"
2323
"fmt"
24+
"log/slog"
25+
2426
"log"
2527
"os"
2628
"os/exec"
@@ -31,17 +33,13 @@ import (
3133
"syscall"
3234
"time"
3335

36+
"github.com/google/cel-go/cel"
3437
"github.com/tektoncd/pipeline/internal/artifactref"
35-
"github.com/tektoncd/pipeline/pkg/apis/config"
3638
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
3739
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
3840
"github.com/tektoncd/pipeline/pkg/internal/resultref"
39-
"github.com/tektoncd/pipeline/pkg/pod"
4041
"github.com/tektoncd/pipeline/pkg/result"
4142
"github.com/tektoncd/pipeline/pkg/termination"
42-
43-
"github.com/google/cel-go/cel"
44-
"go.uber.org/zap"
4543
)
4644

4745
// RFC3339 with millisecond
@@ -55,8 +53,21 @@ const (
5553
breakpointExitSuffix = ".breakpointexit"
5654
breakpointBeforeStepSuffix = ".beforestepexit"
5755
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-"
5863
)
5964

65+
var DownwardMountCancelFile string
66+
67+
func init() {
68+
DownwardMountCancelFile = filepath.Join(downwardMountPoint, downwardMountCancelFile)
69+
}
70+
6071
// DebugBeforeStepError is an error means mark before step breakpoint failure
6172
type DebugBeforeStepError string
6273

@@ -180,15 +191,11 @@ type PostWriter interface {
180191
// Go optionally waits for a file, runs the command, and writes a
181192
// post file.
182193
func (e Entrypointer) Go() error {
183-
prod, _ := zap.NewProduction()
184-
logger := prod.Sugar()
185-
186194
output := []result.RunResult{}
187195
defer func() {
188196
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)
190198
}
191-
_ = logger.Sync()
192199
}()
193200

194201
if err := os.MkdirAll(filepath.Join(e.StepMetadataDir, "results"), os.ModePerm); err != nil {
@@ -212,7 +219,7 @@ func (e Entrypointer) Go() error {
212219
})
213220

214221
if errors.Is(err, ErrSkipPreviousStepFailed) {
215-
output = append(output, e.outputRunResult(pod.TerminationReasonSkipped))
222+
output = append(output, e.outputRunResult(TerminationReasonSkipped))
216223
}
217224

218225
return err
@@ -237,10 +244,10 @@ func (e Entrypointer) Go() error {
237244
var cancel context.CancelFunc
238245
if err == nil {
239246
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))
241248
}
242249
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))
244251
}
245252

246253
ctx, cancel = context.WithCancel(ctx)
@@ -251,7 +258,7 @@ func (e Entrypointer) Go() error {
251258
// start a goroutine to listen for cancellation file
252259
go func() {
253260
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))
255262
}
256263
}()
257264
allowExec, err1 := e.allowExec()
@@ -262,8 +269,8 @@ func (e Entrypointer) Go() error {
262269
case allowExec:
263270
err = e.Runner.Run(ctx, e.Command...)
264271
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))
267274
e.WritePostFile(e.PostFile, nil)
268275
e.WriteExitCodeFile(e.StepMetadataDir, "0")
269276
return nil
@@ -275,15 +282,15 @@ func (e Entrypointer) Go() error {
275282
case err != nil && errors.Is(err, errDebugBeforeStep):
276283
e.WritePostFile(e.PostFile, err)
277284
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))
280287
e.WritePostFile(e.PostFile, ErrContextCanceled)
281288
e.WriteExitCodeFile(e.StepMetadataDir, syscall.SIGKILL.String())
282289
case errors.Is(err, ErrContextDeadlineExceeded):
283290
e.WritePostFile(e.PostFile, err)
284-
output = append(output, e.outputRunResult(pod.TerminationReasonTimeoutExceeded))
291+
output = append(output, e.outputRunResult(TerminationReasonTimeoutExceeded))
285292
case err != nil && e.BreakpointOnFailure:
286-
logger.Info("Skipping writing to PostFile")
293+
slog.Info("Skipping writing to PostFile")
287294
case e.OnError == ContinueOnError && errors.As(err, &ee):
288295
// with continue on error and an ExitError, write non-zero exit code and a post file
289296
exitCode := strconv.Itoa(ee.ExitCode())
@@ -311,7 +318,8 @@ func (e Entrypointer) Go() error {
311318
resultPath = e.ResultsDirectory
312319
}
313320
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
315323
}
316324
}
317325
if len(e.StepResults) >= 1 && e.StepResults[0] != "" {
@@ -320,12 +328,13 @@ func (e Entrypointer) Go() error {
320328
stepResultPath = e.ResultsDirectory
321329
}
322330
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
324333
}
325334
}
326335

327-
if e.ResultExtractionMethod == config.ResultExtractionMethodTerminationMessage {
328-
e.appendArtifactOutputs(&output, logger)
336+
if e.ResultExtractionMethod == ResultExtractionMethodTerminationMessage {
337+
e.appendArtifactOutputs(&output)
329338
}
330339

331340
return err
@@ -342,12 +351,12 @@ func readArtifacts(fp string, resultType result.ResultType) ([]result.RunResult,
342351
return []result.RunResult{{Key: fp, Value: string(file), ResultType: resultType}}, nil
343352
}
344353

345-
func (e Entrypointer) appendArtifactOutputs(output *[]result.RunResult, logger *zap.SugaredLogger) {
354+
func (e Entrypointer) appendArtifactOutputs(output *[]result.RunResult) {
346355
// step artifacts
347356
fp := filepath.Join(e.StepMetadataDir, "artifacts", "provenance.json")
348357
artifacts, err := readArtifacts(fp, result.StepArtifactsResultType)
349358
if err != nil {
350-
logger.Fatalf("Error while handling step artifacts: %s", err)
359+
log.Fatalf("Error while handling step artifacts: %s", err)
351360
}
352361
*output = append(*output, artifacts...)
353362

@@ -359,7 +368,7 @@ func (e Entrypointer) appendArtifactOutputs(output *[]result.RunResult, logger *
359368
fp = filepath.Join(artifactsDir, "provenance.json")
360369
artifacts, err = readArtifacts(fp, result.TaskRunArtifactsResultType)
361370
if err != nil {
362-
logger.Fatalf("Error while handling task artifacts: %s", err)
371+
log.Fatalf("Error while handling task artifacts: %s", err)
363372
}
364373
*output = append(*output, artifacts...)
365374
}
@@ -451,7 +460,7 @@ func (e Entrypointer) readResultsFromDisk(ctx context.Context, resultDir string,
451460
output = append(output, signed...)
452461

453462
// push output to termination path
454-
if e.ResultExtractionMethod == config.ResultExtractionMethodTerminationMessage && len(output) != 0 {
463+
if e.ResultExtractionMethod == ResultExtractionMethodTerminationMessage && len(output) != 0 {
455464
if err := termination.WriteMessage(e.TerminationPath, output); err != nil {
456465
return err
457466
}
@@ -489,7 +498,7 @@ func (e Entrypointer) WriteExitCodeFile(stepPath, content string) {
489498

490499
// waitingCancellation waiting cancellation file, if no error occurs, call cancelFunc to cancel the context
491500
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 {
493502
return err
494503
}
495504
cancel()
@@ -518,10 +527,15 @@ func (e Entrypointer) CheckForBreakpointOnFailure() {
518527
}
519528
}
520529

530+
// GetContainerName prefixes the input name with "step-"
531+
func GetContainerName(name string) string {
532+
return fmt.Sprintf("%s%s", stepPrefix, name)
533+
}
534+
521535
// loadStepResult reads the step result file and returns the string, array or object result value.
522536
func loadStepResult(stepDir string, stepName string, resultName string) (v1.ResultValue, error) {
523537
v := v1.ResultValue{}
524-
fp := getStepResultPath(stepDir, pod.GetContainerName(stepName), resultName)
538+
fp := getStepResultPath(stepDir, GetContainerName(stepName), resultName)
525539
fileContents, err := os.ReadFile(fp)
526540
if err != nil {
527541
return v, err

0 commit comments

Comments
 (0)