Skip to content

Commit

Permalink
Remove the use of go.uber.org/zap in entrypoint's dependent packages …
Browse files Browse the repository at this point in the history
…and uses log/slog

- This patch uses logging with the standard library instead of using zap
  to ensure that we are not pulling in too many indirect dependencies
  into the entrypoint through the uber-go/zap module

Signed-off-by: PuneetPunamiya <[email protected]>
  • Loading branch information
PuneetPunamiya committed Jan 31, 2025
1 parent cef86d1 commit d2c70d5
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions pkg/entrypoint/entrypointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"log"
"log/slog"
"os"
"os/exec"
"path/filepath"
Expand All @@ -42,7 +43,6 @@ import (
"github.com/tektoncd/pipeline/pkg/termination"

"github.com/google/cel-go/cel"
"go.uber.org/zap"
)

// RFC3339 with millisecond
Expand Down Expand Up @@ -180,15 +180,11 @@ type PostWriter interface {
// 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)
log.Fatalf("Error while writing message: %s", wErr)
}
_ = logger.Sync()
}()

if err := os.MkdirAll(filepath.Join(e.StepMetadataDir, "results"), os.ModePerm); err != nil {
Expand Down Expand Up @@ -237,10 +233,10 @@ func (e Entrypointer) Go() error {
var cancel context.CancelFunc
if err == nil {
if err := e.applyStepResultSubstitutions(pipeline.StepsDir); err != nil {
logger.Error("Error while substituting step results: ", err)
slog.Error("Error while substituting step results: ", slog.Any("error", err))
}
if err := e.applyStepArtifactSubstitutions(pipeline.StepsDir); err != nil {
logger.Error("Error while substituting step artifacts: ", err)
slog.Error("Error while substituting step artifacts: ", slog.Any("error", err))
}

ctx, cancel = context.WithCancel(ctx)
Expand All @@ -251,7 +247,7 @@ func (e Entrypointer) Go() error {
// 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))
slog.Error("Error while waiting for cancellation", slog.Any("error", err))
}
}()
allowExec, err1 := e.allowExec()
Expand All @@ -262,7 +258,7 @@ func (e Entrypointer) Go() error {
case allowExec:
err = e.Runner.Run(ctx, e.Command...)
default:
logger.Info("Step was skipped due to when expressions were evaluated to false.")
slog.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")
Expand All @@ -275,15 +271,15 @@ func (e Entrypointer) Go() error {
case err != nil && errors.Is(err, errDebugBeforeStep):
e.WritePostFile(e.PostFile, err)
case err != nil && errors.Is(err, ErrContextCanceled):
logger.Info("Step was canceling")
slog.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")
slog.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())
Expand Down Expand Up @@ -311,7 +307,8 @@ func (e Entrypointer) Go() error {
resultPath = e.ResultsDirectory
}
if err := e.readResultsFromDisk(ctx, resultPath, result.TaskRunResultType); err != nil {
logger.Fatalf("Error while handling results: %s", err)
slog.Error("Error while substituting step artifacts: ", slog.Any("error", err))
return err
}
}
if len(e.StepResults) >= 1 && e.StepResults[0] != "" {
Expand All @@ -320,12 +317,13 @@ func (e Entrypointer) Go() error {
stepResultPath = e.ResultsDirectory
}
if err := e.readResultsFromDisk(ctx, stepResultPath, result.StepResultType); err != nil {
logger.Fatalf("Error while handling step results: %s", err)
slog.Error("Error while substituting step artifacts: ", slog.Any("error", err))
return err
}
}

if e.ResultExtractionMethod == config.ResultExtractionMethodTerminationMessage {
e.appendArtifactOutputs(&output, logger)
e.appendArtifactOutputs(&output)
}

return err
Expand All @@ -342,12 +340,12 @@ func readArtifacts(fp string, resultType result.ResultType) ([]result.RunResult,
return []result.RunResult{{Key: fp, Value: string(file), ResultType: resultType}}, nil
}

func (e Entrypointer) appendArtifactOutputs(output *[]result.RunResult, logger *zap.SugaredLogger) {
func (e Entrypointer) appendArtifactOutputs(output *[]result.RunResult) {
// 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)
log.Fatalf("Error while handling step artifacts: %s", err)
}
*output = append(*output, artifacts...)

Expand All @@ -359,7 +357,7 @@ func (e Entrypointer) appendArtifactOutputs(output *[]result.RunResult, logger *
fp = filepath.Join(artifactsDir, "provenance.json")
artifacts, err = readArtifacts(fp, result.TaskRunArtifactsResultType)
if err != nil {
logger.Fatalf("Error while handling task artifacts: %s", err)
log.Fatalf("Error while handling task artifacts: %s", err)
}
*output = append(*output, artifacts...)
}
Expand Down

0 comments on commit d2c70d5

Please sign in to comment.