-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Surface error results from TaskRun to BuildRun
According to ship 0024, this commit adds a mechanism to surface errors from TaskRun steps. The errors are published under `status.failure` in BuildRun iff underlying TaskRun has failed. Adds `pkg/reconciler/buildrun/resources/failures.go`, `pkg/reconciler/buildrun/resources/failures_test.go` that contains logic for extracting error reason and message. Modifies `pkg/reconciler/buildrun/resources/taskrun.go` by adding two new generic results for errors.
- Loading branch information
Baran Dalgic
authored and
dalbar
committed
Nov 3, 2021
1 parent
4ed2f1e
commit 2d696d5
Showing
7 changed files
with
179 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
# Temporary Build Files | ||
.DS_Store | ||
git | ||
shipwright-build-controller | ||
build/_output | ||
build/_test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package resources | ||
|
||
import ( | ||
"encoding/json" | ||
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1" | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" | ||
"knative.dev/pkg/apis" | ||
) | ||
|
||
const ( | ||
prefixedResultErrorReason = prefixParamsResultsVolumes + "-" + resultErrorReason | ||
prefixedResultErrorMessage = prefixParamsResultsVolumes + "-" + resultErrorMessage | ||
) | ||
|
||
// UpdateBuildRunUsingTaskFailures is extracting failures from taskRun steps and adding them to buildRun (mutates) | ||
func UpdateBuildRunUsingTaskFailures(buildRun *buildv1alpha1.BuildRun, taskRun *v1beta1.TaskRun) { | ||
trCondition := taskRun.Status.GetCondition(apis.ConditionSucceeded) | ||
|
||
// only extract failures when failing condition is present | ||
if trCondition != nil && v1beta1.TaskRunReason(trCondition.Reason) == v1beta1.TaskRunReasonFailed { | ||
buildRun.Status.Failure = extractErrorFromTaskRun(taskRun) | ||
} | ||
} | ||
|
||
func extractErrorFromTaskRun(taskRun *v1beta1.TaskRun) *buildv1alpha1.Failure { | ||
shipError := buildv1alpha1.Failure{} | ||
|
||
for _, step := range taskRun.Status.Steps { | ||
message := step.Terminated.Message | ||
var taskRunResults []v1beta1.PipelineResourceResult | ||
|
||
if err := json.Unmarshal([]byte(message), &taskRunResults); err != nil { | ||
continue | ||
} | ||
|
||
for _, result := range taskRunResults { | ||
if result.Key == prefixedResultErrorMessage { | ||
shipError.Message = result.Value | ||
} | ||
|
||
if result.Key == prefixedResultErrorReason { | ||
shipError.Reason = result.Value | ||
} | ||
} | ||
} | ||
|
||
if len(shipError.Message) == 0 || len(shipError.Reason) == 0 { | ||
return nil | ||
} | ||
|
||
return &shipError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package resources | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/shipwright-io/build/pkg/apis/build/v1alpha1" | ||
v1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" | ||
v1 "k8s.io/api/core/v1" | ||
"knative.dev/pkg/apis" | ||
) | ||
|
||
var _ = Describe("Surfacing errors", func() { | ||
Context("resources.UpdateBuildRunUsingTaskFailures", func() { | ||
|
||
It("surfaces errors to BuildRun in failed TaskRun", func() { | ||
redTaskRun := v1beta1.TaskRun{} | ||
redTaskRun.Status.Conditions = append(redTaskRun.Status.Conditions, | ||
apis.Condition{Type: apis.ConditionSucceeded, Reason: v1beta1.TaskRunReasonFailed.String()}) | ||
failedStep := v1beta1.StepState{} | ||
|
||
errorReasonValue := "val1" | ||
errorMessageValue := "val2" | ||
errorReasonKey := fmt.Sprintf("%s-%s", prefixParamsResultsVolumes, resultErrorReason) | ||
errorMessageKey := fmt.Sprintf("%s-%s", prefixParamsResultsVolumes, resultErrorMessage) | ||
|
||
errorReason := v1beta1.PipelineResourceResult{Key: errorReasonKey, Value: errorReasonValue} | ||
errorMessage := v1beta1.PipelineResourceResult{Key: errorMessageKey, Value: errorMessageValue} | ||
unrelated := v1beta1.PipelineResourceResult{Key: "unrelated", Value: "unrelated"} | ||
|
||
message, _ := json.Marshal([]v1beta1.PipelineResourceResult{errorReason, errorMessage, unrelated}) | ||
|
||
failedStep.Terminated = &v1.ContainerStateTerminated{Message: string(message)} | ||
|
||
redTaskRun.Status.Steps = append(redTaskRun.Status.Steps, failedStep) | ||
redBuild := v1alpha1.BuildRun{} | ||
|
||
UpdateBuildRunUsingTaskFailures(&redBuild, &redTaskRun) | ||
|
||
Expect(redBuild.Status.Failure.Message).To(Equal(errorMessageValue)) | ||
Expect(redBuild.Status.Failure.Reason).To(Equal(errorReasonValue)) | ||
}) | ||
|
||
It("failed TaskRun without error reason and message", func() { | ||
redTaskRun := v1beta1.TaskRun{} | ||
redTaskRun.Status.Conditions = append(redTaskRun.Status.Conditions, | ||
apis.Condition{Type: apis.ConditionSucceeded, Reason: v1beta1.TaskRunReasonFailed.String()}) | ||
failedStep := v1beta1.StepState{} | ||
|
||
unrelated := v1beta1.PipelineResourceResult{Key: "unrelated", Value: "unrelated"} | ||
|
||
message, _ := json.Marshal([]v1beta1.PipelineResourceResult{unrelated}) | ||
|
||
failedStep.Terminated = &v1.ContainerStateTerminated{Message: string(message)} | ||
|
||
redTaskRun.Status.Steps = append(redTaskRun.Status.Steps, failedStep) | ||
redBuild := v1alpha1.BuildRun{} | ||
|
||
UpdateBuildRunUsingTaskFailures(&redBuild, &redTaskRun) | ||
|
||
Expect(redBuild.Status.Failure).To(BeNil()) | ||
}) | ||
|
||
It("no errors present in BuildRun for successful TaskRun", func() { | ||
greenTaskRun := v1beta1.TaskRun{} | ||
greenTaskRun.Status.Conditions = append(greenTaskRun.Status.Conditions, apis.Condition{Type: apis.ConditionSucceeded}) | ||
greenBuildRun := v1alpha1.BuildRun{} | ||
|
||
UpdateBuildRunUsingTaskFailures(&greenBuildRun, &greenTaskRun) | ||
|
||
Expect(greenBuildRun.Status.Failure).To(BeNil()) | ||
}) | ||
|
||
It("TaskRun has not condition succeeded so nothing to do", func() { | ||
unfinishedTaskRun := v1beta1.TaskRun{} | ||
unfinishedTaskRun.Status.Conditions = append(unfinishedTaskRun.Status.Conditions, apis.Condition{Type: apis.ConditionReady}) | ||
unfinishedBuildRun := v1alpha1.BuildRun{} | ||
|
||
UpdateBuildRunUsingTaskFailures(&unfinishedBuildRun, &unfinishedTaskRun) | ||
Expect(unfinishedBuildRun.Status.Failure).To(BeNil()) | ||
}) | ||
|
||
It("TaskRun has a unknown reason", func() { | ||
unknownTaskRun := v1beta1.TaskRun{} | ||
unknownTaskRun.Status.Conditions = append(unknownTaskRun.Status.Conditions, apis.Condition{Type: apis.ConditionSucceeded, Reason: "random"}) | ||
unknownBuildRun := v1alpha1.BuildRun{} | ||
|
||
UpdateBuildRunUsingTaskFailures(&unknownBuildRun, &unknownTaskRun) | ||
Expect(unknownBuildRun.Status.Failure).To(BeNil()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters