From bc4fce95c84d1050e4c405712b9837c8d57b2543 Mon Sep 17 00:00:00 2001 From: Jerome Ju Date: Wed, 12 Apr 2023 20:59:30 -0400 Subject: [PATCH] Migrate v1beta1 RunResult to Unversioned Package This commit migrates the v1beta1 RunResult to the unversioned result package since it is no longer a struct in api used for v1beta1 resource types. The RunResult struct was previously PipelineResourceResult and it has been renamed and moved to an unversioned package because it is no longer used in apis. The old struct PipelineResourceResult and its ResultType is aliased and kept in v1beta1 for backward compatibility. --- docs/pipeline-api.md | 79 +--------------- .../sidecarlogresults/sidecarlogresults.go | 22 ++--- .../sidecarlogresults_test.go | 22 ++--- .../pipeline/v1beta1/openapi_generated.go | 50 +--------- pkg/apis/pipeline/v1beta1/resource_types.go | 63 ++----------- .../pipeline/v1beta1/resource_types_test.go | 38 -------- pkg/apis/pipeline/v1beta1/swagger.json | 30 +----- pkg/apis/pipeline/v1beta1/task_types.go | 12 --- pkg/apis/pipeline/v1beta1/taskrun_types.go | 2 +- .../pipeline/v1beta1/zz_generated.deepcopy.go | 19 +--- pkg/entrypoint/entrypointer.go | 26 +++--- pkg/entrypoint/entrypointer_test.go | 13 +-- pkg/pod/status.go | 19 ++-- pkg/result/result.go | 91 +++++++++++++++++++ pkg/result/result_test.go | 58 ++++++++++++ pkg/spire/sign.go | 27 +++--- pkg/spire/spire.go | 5 +- pkg/spire/spire_mock.go | 33 +++---- pkg/spire/spire_mock_test.go | 59 ++++++------ pkg/spire/spire_test.go | 37 ++++---- pkg/spire/verify.go | 15 +-- pkg/termination/parse.go | 12 +-- pkg/termination/parse_test.go | 12 +-- pkg/termination/write.go | 6 +- pkg/termination/write_test.go | 8 +- 25 files changed, 328 insertions(+), 430 deletions(-) create mode 100644 pkg/result/result.go create mode 100644 pkg/result/result_test.go diff --git a/docs/pipeline-api.md b/docs/pipeline-api.md index 05f3a24eb0d..73a08c06dfe 100644 --- a/docs/pipeline-api.md +++ b/docs/pipeline-api.md @@ -11428,16 +11428,6 @@ string -

ResultType -(int alias)

-

-(Appears on:RunResult) -

-
-

ResultType used to find out whether a RunResult is from a task result or not -Note that ResultsType is another type which is used to define the data type -(e.g. string, array, etc) we used for Results

-

ResultsType (string alias)

@@ -11455,71 +11445,6 @@ this ResultsType.

RunObject is implemented by CustomRun and Run

-

RunResult -

-

-(Appears on:TaskRunStatusFields) -

-
-

RunResult is used to write key/value pairs to TaskRun pod termination messages. -The key/value pairs may come from the entrypoint binary, or represent a TaskRunResult. -If they represent a TaskRunResult, the key is the name of the result and the value is the -JSON-serialized value of the result.

-
- - - - - - - - - - - - - - - - - - - - - - - - - -
FieldDescription
-key
- -string - -
-
-value
- -string - -
-
-resourceName
- -string - -
-

ResourceName may be used in tests, but it is not populated in termination messages. -It is preserved here for backwards compatibility and will not be ported to v1.

-
-type
- - -ResultType - - -
-

Sidecar

@@ -13966,9 +13891,7 @@ All TaskRunStatus stored in RetriesStatus will have no date within the RetriesSt resourcesResult
- -[]RunResult - +[]github.com/tektoncd/pipeline/pkg/result.RunResult diff --git a/internal/sidecarlogresults/sidecarlogresults.go b/internal/sidecarlogresults/sidecarlogresults.go index de5965db9dd..f9e776ad5af 100644 --- a/internal/sidecarlogresults/sidecarlogresults.go +++ b/internal/sidecarlogresults/sidecarlogresults.go @@ -27,7 +27,7 @@ import ( "path/filepath" "github.com/tektoncd/pipeline/pkg/apis/config" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/result" "golang.org/x/sync/errgroup" corev1 "k8s.io/api/core/v1" "k8s.io/client-go/kubernetes" @@ -137,8 +137,8 @@ func LookForResults(w io.Writer, runDir string, resultsDir string, resultNames [ } // GetResultsFromSidecarLogs extracts results from the logs of the results sidecar -func GetResultsFromSidecarLogs(ctx context.Context, clientset kubernetes.Interface, namespace string, name string, container string, podPhase corev1.PodPhase) ([]v1beta1.RunResult, error) { - sidecarLogResults := []v1beta1.RunResult{} +func GetResultsFromSidecarLogs(ctx context.Context, clientset kubernetes.Interface, namespace string, name string, container string, podPhase corev1.PodPhase) ([]result.RunResult, error) { + sidecarLogResults := []result.RunResult{} if podPhase == corev1.PodPending { return sidecarLogResults, nil } @@ -153,7 +153,7 @@ func GetResultsFromSidecarLogs(ctx context.Context, clientset kubernetes.Interfa return extractResultsFromLogs(sidecarLogs, sidecarLogResults, maxResultLimit) } -func extractResultsFromLogs(logs io.Reader, sidecarLogResults []v1beta1.RunResult, maxResultLimit int) ([]v1beta1.RunResult, error) { +func extractResultsFromLogs(logs io.Reader, sidecarLogResults []result.RunResult, maxResultLimit int) ([]result.RunResult, error) { scanner := bufio.NewScanner(logs) buf := make([]byte, maxResultLimit) scanner.Buffer(buf, maxResultLimit) @@ -174,20 +174,20 @@ func extractResultsFromLogs(logs io.Reader, sidecarLogResults []v1beta1.RunResul return sidecarLogResults, nil } -func parseResults(resultBytes []byte, maxResultLimit int) (v1beta1.RunResult, error) { - result := v1beta1.RunResult{} +func parseResults(resultBytes []byte, maxResultLimit int) (result.RunResult, error) { + runResult := result.RunResult{} if len(resultBytes) > maxResultLimit { - return result, ErrSizeExceeded + return runResult, ErrSizeExceeded } var res SidecarLogResult if err := json.Unmarshal(resultBytes, &res); err != nil { - return result, fmt.Errorf("Invalid result %w", err) + return runResult, fmt.Errorf("Invalid result %w", err) } - result = v1beta1.RunResult{ + runResult = result.RunResult{ Key: res.Name, Value: res.Value, - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, } - return result, nil + return runResult, nil } diff --git a/internal/sidecarlogresults/sidecarlogresults_test.go b/internal/sidecarlogresults/sidecarlogresults_test.go index f945a3186a2..9a5c85f1b42 100644 --- a/internal/sidecarlogresults/sidecarlogresults_test.go +++ b/internal/sidecarlogresults/sidecarlogresults_test.go @@ -29,7 +29,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/result" "github.com/tektoncd/pipeline/test/diff" corev1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1" @@ -163,19 +163,19 @@ func TestExtractResultsFromLogs(t *testing.T) { } logs := strings.NewReader(podLogs) - results, err := extractResultsFromLogs(logs, []v1beta1.RunResult{}, 4096) + results, err := extractResultsFromLogs(logs, []result.RunResult{}, 4096) if err != nil { t.Error(err) } - want := []v1beta1.RunResult{ + want := []result.RunResult{ { Key: "result1", Value: "foo", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, { Key: "result2", Value: "bar", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, } if d := cmp.Diff(want, results); d != "" { @@ -197,7 +197,7 @@ func TestExtractResultsFromLogs_Failure(t *testing.T) { } logs := strings.NewReader(podLogs) - _, err := extractResultsFromLogs(logs, []v1beta1.RunResult{}, 4096) + _, err := extractResultsFromLogs(logs, []result.RunResult{}, 4096) if !errors.Is(err, ErrSizeExceeded) { t.Fatalf("Expected error %v but got %v", ErrSizeExceeded, err) } @@ -221,20 +221,20 @@ func TestParseResults(t *testing.T) { res, _ := json.Marshal(&r) podLogs = append(podLogs, string(res)) } - want := []v1beta1.RunResult{{ + want := []result.RunResult{{ Key: "result1", Value: "foo", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, { Key: "result2", Value: `{"IMAGE_URL":"ar.com", "IMAGE_DIGEST":"sha234"}`, - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, { Key: "result3", Value: `["hello","world"]`, - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }} - stepResults := []v1beta1.RunResult{} + stepResults := []result.RunResult{} for _, plog := range podLogs { res, err := parseResults([]byte(plog), 4096) if err != nil { diff --git a/pkg/apis/pipeline/v1beta1/openapi_generated.go b/pkg/apis/pipeline/v1beta1/openapi_generated.go index 83f0b51d338..df879cd588a 100644 --- a/pkg/apis/pipeline/v1beta1/openapi_generated.go +++ b/pkg/apis/pipeline/v1beta1/openapi_generated.go @@ -79,7 +79,6 @@ func GetOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenA "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.RefSource": schema_pkg_apis_pipeline_v1beta1_RefSource(ref), "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.ResolverRef": schema_pkg_apis_pipeline_v1beta1_ResolverRef(ref), "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.ResultRef": schema_pkg_apis_pipeline_v1beta1_ResultRef(ref), - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.RunResult": schema_pkg_apis_pipeline_v1beta1_RunResult(ref), "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.Sidecar": schema_pkg_apis_pipeline_v1beta1_Sidecar(ref), "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.SidecarState": schema_pkg_apis_pipeline_v1beta1_SidecarState(ref), "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.SkippedTask": schema_pkg_apis_pipeline_v1beta1_SkippedTask(ref), @@ -3161,47 +3160,6 @@ func schema_pkg_apis_pipeline_v1beta1_ResultRef(ref common.ReferenceCallback) co } } -func schema_pkg_apis_pipeline_v1beta1_RunResult(ref common.ReferenceCallback) common.OpenAPIDefinition { - return common.OpenAPIDefinition{ - Schema: spec.Schema{ - SchemaProps: spec.SchemaProps{ - Description: "RunResult is used to write key/value pairs to TaskRun pod termination messages. The key/value pairs may come from the entrypoint binary, or represent a TaskRunResult. If they represent a TaskRunResult, the key is the name of the result and the value is the JSON-serialized value of the result.", - Type: []string{"object"}, - Properties: map[string]spec.Schema{ - "key": { - SchemaProps: spec.SchemaProps{ - Default: "", - Type: []string{"string"}, - Format: "", - }, - }, - "value": { - SchemaProps: spec.SchemaProps{ - Default: "", - Type: []string{"string"}, - Format: "", - }, - }, - "resourceName": { - SchemaProps: spec.SchemaProps{ - Description: "ResourceName may be used in tests, but it is not populated in termination messages. It is preserved here for backwards compatibility and will not be ported to v1.", - Type: []string{"string"}, - Format: "", - }, - }, - "type": { - SchemaProps: spec.SchemaProps{ - Type: []string{"integer"}, - Format: "int32", - }, - }, - }, - Required: []string{"key", "value"}, - }, - }, - } -} - func schema_pkg_apis_pipeline_v1beta1_Sidecar(ref common.ReferenceCallback) common.OpenAPIDefinition { return common.OpenAPIDefinition{ Schema: spec.Schema{ @@ -5219,7 +5177,7 @@ func schema_pkg_apis_pipeline_v1beta1_TaskRunStatus(ref common.ReferenceCallback Schema: &spec.Schema{ SchemaProps: spec.SchemaProps{ Default: map[string]interface{}{}, - Ref: ref("github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.RunResult"), + Ref: ref("github.com/tektoncd/pipeline/pkg/result.RunResult"), }, }, }, @@ -5296,7 +5254,7 @@ func schema_pkg_apis_pipeline_v1beta1_TaskRunStatus(ref common.ReferenceCallback }, }, Dependencies: []string{ - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.CloudEventDelivery", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.Provenance", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.RunResult", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.SidecarState", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.StepState", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.TaskRunResult", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.TaskRunStatus", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.TaskSpec", "k8s.io/apimachinery/pkg/apis/meta/v1.Time", "knative.dev/pkg/apis.Condition"}, + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.CloudEventDelivery", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.Provenance", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.SidecarState", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.StepState", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.TaskRunResult", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.TaskRunStatus", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.TaskSpec", "github.com/tektoncd/pipeline/pkg/result.RunResult", "k8s.io/apimachinery/pkg/apis/meta/v1.Time", "knative.dev/pkg/apis.Condition"}, } } @@ -5397,7 +5355,7 @@ func schema_pkg_apis_pipeline_v1beta1_TaskRunStatusFields(ref common.ReferenceCa Schema: &spec.Schema{ SchemaProps: spec.SchemaProps{ Default: map[string]interface{}{}, - Ref: ref("github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.RunResult"), + Ref: ref("github.com/tektoncd/pipeline/pkg/result.RunResult"), }, }, }, @@ -5474,7 +5432,7 @@ func schema_pkg_apis_pipeline_v1beta1_TaskRunStatusFields(ref common.ReferenceCa }, }, Dependencies: []string{ - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.CloudEventDelivery", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.Provenance", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.RunResult", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.SidecarState", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.StepState", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.TaskRunResult", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.TaskRunStatus", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.TaskSpec", "k8s.io/apimachinery/pkg/apis/meta/v1.Time"}, + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.CloudEventDelivery", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.Provenance", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.SidecarState", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.StepState", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.TaskRunResult", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.TaskRunStatus", "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1.TaskSpec", "github.com/tektoncd/pipeline/pkg/result.RunResult", "k8s.io/apimachinery/pkg/apis/meta/v1.Time"}, } } diff --git a/pkg/apis/pipeline/v1beta1/resource_types.go b/pkg/apis/pipeline/v1beta1/resource_types.go index 9162a5e9d95..0e5ec62de3a 100644 --- a/pkg/apis/pipeline/v1beta1/resource_types.go +++ b/pkg/apis/pipeline/v1beta1/resource_types.go @@ -17,71 +17,22 @@ limitations under the License. package v1beta1 import ( - "encoding/json" - "fmt" - - "github.com/hashicorp/go-multierror" resource "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1" + "github.com/tektoncd/pipeline/pkg/result" v1 "k8s.io/api/core/v1" ) // RunResult is used to write key/value pairs to TaskRun pod termination messages. -// The key/value pairs may come from the entrypoint binary, or represent a TaskRunResult. -// If they represent a TaskRunResult, the key is the name of the result and the value is the -// JSON-serialized value of the result. -type RunResult struct { - Key string `json:"key"` - Value string `json:"value"` - // ResourceName may be used in tests, but it is not populated in termination messages. - // It is preserved here for backwards compatibility and will not be ported to v1. - ResourceName string `json:"resourceName,omitempty"` - ResultType ResultType `json:"type,omitempty"` -} +// It has been migrated to the result package and kept for backward compatibility +type RunResult = result.RunResult // PipelineResourceResult has been deprecated with the migration of PipelineResources // Deprecated: Use RunResult instead -type PipelineResourceResult = RunResult - -// ResultType used to find out whether a RunResult is from a task result or not -// Note that ResultsType is another type which is used to define the data type -// (e.g. string, array, etc) we used for Results -type ResultType int - -// UnmarshalJSON unmarshals either an int or a string into a ResultType. String -// ResultTypes were removed because they made JSON messages bigger, which in -// turn limited the amount of space in termination messages for task results. String -// support is maintained for backwards compatibility - the Pipelines controller could -// be stopped midway through TaskRun execution, updated with support for int in place -// of string, and then fail the running TaskRun because it doesn't know how to interpret -// the string value that the TaskRun's entrypoint will emit when it completes. -func (r *ResultType) UnmarshalJSON(data []byte) error { - var asInt int - var intErr error - - if err := json.Unmarshal(data, &asInt); err != nil { - intErr = err - } else { - *r = ResultType(asInt) - return nil - } +type PipelineResourceResult = result.RunResult - var asString string - - if err := json.Unmarshal(data, &asString); err != nil { - return fmt.Errorf("unsupported value type, neither int nor string: %w", multierror.Append(intErr, err).ErrorOrNil()) - } - - switch asString { - case "TaskRunResult": - *r = TaskRunResultType - case "InternalTektonResult": - *r = InternalTektonResultType - default: - *r = UnknownResultType - } - - return nil -} +// ResultType of PipelineResourceResult has been deprecated with the migration of PipelineResources +// Deprecated: v1beta1.ResultType is only kept for backward compatibility +type ResultType = result.ResultType // ResourceParam declares a string value to use for the parameter called Name, and is used in // the specific context of PipelineResources. diff --git a/pkg/apis/pipeline/v1beta1/resource_types_test.go b/pkg/apis/pipeline/v1beta1/resource_types_test.go index 32b501ddfc9..acb4a623ea8 100644 --- a/pkg/apis/pipeline/v1beta1/resource_types_test.go +++ b/pkg/apis/pipeline/v1beta1/resource_types_test.go @@ -14,12 +14,7 @@ limitations under the License. package v1beta1_test import ( - "encoding/json" - "testing" - - "github.com/google/go-cmp/cmp" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - "github.com/tektoncd/pipeline/test/diff" corev1 "k8s.io/api/core/v1" ) @@ -57,36 +52,3 @@ func (tm *TestTaskModifier) GetStepsToAppend() []v1beta1.Step { func (tm *TestTaskModifier) GetVolumes() []corev1.Volume { return []corev1.Volume{volume} } - -func TestRunResult_UnmarshalJSON(t *testing.T) { - testcases := []struct { - name string - data string - pr v1beta1.RunResult - }{{ - name: "type defined as string - TaskRunResult", - data: "{\"key\":\"resultName\",\"value\":\"resultValue\", \"type\": \"TaskRunResult\"}", - pr: v1beta1.RunResult{Key: "resultName", Value: "resultValue", ResultType: v1beta1.TaskRunResultType}, - }, - { - name: "type defined as string - InternalTektonResult", - data: "{\"key\":\"resultName\",\"value\":\"\", \"type\": \"InternalTektonResult\"}", - pr: v1beta1.RunResult{Key: "resultName", Value: "", ResultType: v1beta1.InternalTektonResultType}, - }, { - name: "type defined as int", - data: "{\"key\":\"resultName\",\"value\":\"\", \"type\": 1}", - pr: v1beta1.RunResult{Key: "resultName", Value: "", ResultType: v1beta1.TaskRunResultType}, - }} - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - pipRes := v1beta1.RunResult{} - if err := json.Unmarshal([]byte(tc.data), &pipRes); err != nil { - t.Errorf("Unexpected error when unmarshalling the json into RunResult") - } - if d := cmp.Diff(tc.pr, pipRes); d != "" { - t.Errorf(diff.PrintWantGot(d)) - } - }) - } -} diff --git a/pkg/apis/pipeline/v1beta1/swagger.json b/pkg/apis/pipeline/v1beta1/swagger.json index 172ee8b3741..c0b8e2770cf 100644 --- a/pkg/apis/pipeline/v1beta1/swagger.json +++ b/pkg/apis/pipeline/v1beta1/swagger.json @@ -1755,32 +1755,6 @@ } } }, - "v1beta1.RunResult": { - "description": "RunResult is used to write key/value pairs to TaskRun pod termination messages. The key/value pairs may come from the entrypoint binary, or represent a TaskRunResult. If they represent a TaskRunResult, the key is the name of the result and the value is the JSON-serialized value of the result.", - "type": "object", - "required": [ - "key", - "value" - ], - "properties": { - "key": { - "type": "string", - "default": "" - }, - "resourceName": { - "description": "ResourceName may be used in tests, but it is not populated in termination messages. It is preserved here for backwards compatibility and will not be ported to v1.", - "type": "string" - }, - "type": { - "type": "integer", - "format": "int32" - }, - "value": { - "type": "string", - "default": "" - } - } - }, "v1beta1.Sidecar": { "description": "Sidecar has nearly the same data structure as Step but does not have the ability to timeout.", "type": "object", @@ -2853,7 +2827,7 @@ "type": "array", "items": { "default": {}, - "$ref": "#/definitions/v1beta1.RunResult" + "$ref": "#/definitions/github.aaakk.us.kg.tektoncd.pipeline.pkg.result.RunResult" }, "x-kubernetes-list-type": "atomic" }, @@ -2945,7 +2919,7 @@ "type": "array", "items": { "default": {}, - "$ref": "#/definitions/v1beta1.RunResult" + "$ref": "#/definitions/github.aaakk.us.kg.tektoncd.pipeline.pkg.result.RunResult" }, "x-kubernetes-list-type": "atomic" }, diff --git a/pkg/apis/pipeline/v1beta1/task_types.go b/pkg/apis/pipeline/v1beta1/task_types.go index d4208b73e9c..850929d015a 100644 --- a/pkg/apis/pipeline/v1beta1/task_types.go +++ b/pkg/apis/pipeline/v1beta1/task_types.go @@ -24,18 +24,6 @@ import ( "knative.dev/pkg/kmeta" ) -const ( - // TaskRunResultType default task run result value - TaskRunResultType ResultType = 1 - // reserved: 2 - // was RunResultType - - // InternalTektonResultType default internal tekton result value - InternalTektonResultType = 3 - // UnknownResultType default unknown result type value - UnknownResultType = 10 -) - // +genclient // +genclient:noStatus // +genreconciler:krshapedlogic=false diff --git a/pkg/apis/pipeline/v1beta1/taskrun_types.go b/pkg/apis/pipeline/v1beta1/taskrun_types.go index bfda99667c2..572dffd9b98 100644 --- a/pkg/apis/pipeline/v1beta1/taskrun_types.go +++ b/pkg/apis/pipeline/v1beta1/taskrun_types.go @@ -255,7 +255,7 @@ type TaskRunStatusFields struct { // Deprecated: this field is not populated and is preserved only for backwards compatibility // +optional // +listType=atomic - ResourcesResult []RunResult `json:"resourcesResult,omitempty"` + ResourcesResult []PipelineResourceResult `json:"resourcesResult,omitempty"` // TaskRunResults are the list of results written out by the task's containers // +optional diff --git a/pkg/apis/pipeline/v1beta1/zz_generated.deepcopy.go b/pkg/apis/pipeline/v1beta1/zz_generated.deepcopy.go index 9d938264ecb..fdd02825788 100644 --- a/pkg/apis/pipeline/v1beta1/zz_generated.deepcopy.go +++ b/pkg/apis/pipeline/v1beta1/zz_generated.deepcopy.go @@ -26,6 +26,7 @@ import ( pod "github.com/tektoncd/pipeline/pkg/apis/pipeline/pod" v1alpha1 "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1" runv1beta1 "github.com/tektoncd/pipeline/pkg/apis/run/v1beta1" + result "github.com/tektoncd/pipeline/pkg/result" corev1 "k8s.io/api/core/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" @@ -1506,22 +1507,6 @@ func (in *ResultRef) DeepCopy() *ResultRef { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *RunResult) DeepCopyInto(out *RunResult) { - *out = *in - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RunResult. -func (in *RunResult) DeepCopy() *RunResult { - if in == nil { - return nil - } - out := new(RunResult) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Sidecar) DeepCopyInto(out *Sidecar) { *out = *in @@ -2364,7 +2349,7 @@ func (in *TaskRunStatusFields) DeepCopyInto(out *TaskRunStatusFields) { } if in.ResourcesResult != nil { in, out := &in.ResourcesResult, &out.ResourcesResult - *out = make([]RunResult, len(*in)) + *out = make([]result.RunResult, len(*in)) copy(*out, *in) } if in.TaskRunResults != nil { diff --git a/pkg/entrypoint/entrypointer.go b/pkg/entrypoint/entrypointer.go index 6d7273b5a14..1f1e63c1ed4 100644 --- a/pkg/entrypoint/entrypointer.go +++ b/pkg/entrypoint/entrypointer.go @@ -30,7 +30,7 @@ import ( "github.com/tektoncd/pipeline/pkg/apis/config" "github.com/tektoncd/pipeline/pkg/apis/pipeline" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/result" "github.com/tektoncd/pipeline/pkg/spire" "github.com/tektoncd/pipeline/pkg/termination" "go.uber.org/zap" @@ -112,7 +112,7 @@ func (e Entrypointer) Go() error { prod, _ := zap.NewProduction() logger := prod.Sugar() - output := []v1beta1.RunResult{} + output := []result.RunResult{} defer func() { if wErr := termination.WriteMessage(e.TerminationPath, output); wErr != nil { logger.Fatalf("Error while writing message: %s", wErr) @@ -128,19 +128,19 @@ func (e Entrypointer) Go() error { if !e.BreakpointOnFailure { e.WritePostFile(e.PostFile, err) } - output = append(output, v1beta1.RunResult{ + output = append(output, result.RunResult{ Key: "StartedAt", Value: time.Now().Format(timeFormat), - ResultType: v1beta1.InternalTektonResultType, + ResultType: result.InternalTektonResultType, }) return err } } - output = append(output, v1beta1.RunResult{ + output = append(output, result.RunResult{ Key: "StartedAt", Value: time.Now().Format(timeFormat), - ResultType: v1beta1.InternalTektonResultType, + ResultType: result.InternalTektonResultType, }) ctx := context.Background() @@ -158,10 +158,10 @@ func (e Entrypointer) Go() error { } err = e.Runner.Run(ctx, e.Command...) if errors.Is(err, context.DeadlineExceeded) { - output = append(output, v1beta1.RunResult{ + output = append(output, result.RunResult{ Key: "Reason", Value: "TimeoutExceeded", - ResultType: v1beta1.InternalTektonResultType, + ResultType: result.InternalTektonResultType, }) } } @@ -173,10 +173,10 @@ func (e Entrypointer) Go() error { 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, v1beta1.RunResult{ + output = append(output, result.RunResult{ Key: "ExitCode", Value: exitCode, - ResultType: v1beta1.InternalTektonResultType, + ResultType: result.InternalTektonResultType, }) e.WritePostFile(e.PostFile, nil) e.WriteExitCodeFile(e.StepMetadataDir, exitCode) @@ -205,7 +205,7 @@ func (e Entrypointer) Go() error { } func (e Entrypointer) readResultsFromDisk(ctx context.Context, resultDir string) error { - output := []v1beta1.RunResult{} + output := []result.RunResult{} for _, resultFile := range e.Results { if resultFile == "" { continue @@ -217,10 +217,10 @@ func (e Entrypointer) readResultsFromDisk(ctx context.Context, resultDir string) return err } // if the file doesn't exist, ignore it - output = append(output, v1beta1.RunResult{ + output = append(output, result.RunResult{ Key: resultFile, Value: string(fileContents), - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }) } if e.SpireWorkloadAPI != nil { diff --git a/pkg/entrypoint/entrypointer_test.go b/pkg/entrypoint/entrypointer_test.go index 2d6bcfa5275..f451593f839 100644 --- a/pkg/entrypoint/entrypointer_test.go +++ b/pkg/entrypoint/entrypointer_test.go @@ -32,6 +32,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/tektoncd/pipeline/pkg/apis/config" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/result" "github.com/tektoncd/pipeline/pkg/spire" "github.com/tektoncd/pipeline/pkg/termination" "github.com/tektoncd/pipeline/test/diff" @@ -231,7 +232,7 @@ func TestEntrypointer(t *testing.T) { } fileContents, err := os.ReadFile(terminationPath) if err == nil { - var entries []v1beta1.RunResult + var entries []result.RunResult if err := json.Unmarshal(fileContents, &entries); err == nil { var found = false for _, result := range entries { @@ -259,26 +260,26 @@ func TestReadResultsFromDisk(t *testing.T) { desc string results []string resultContent []v1beta1.ResultValue - want []v1beta1.RunResult + want []result.RunResult }{{ desc: "read string result file", results: []string{"results"}, resultContent: []v1beta1.ResultValue{*v1beta1.NewStructuredValues("hello world")}, - want: []v1beta1.RunResult{ + want: []result.RunResult{ {Value: `"hello world"`, ResultType: 1}}, }, { desc: "read array result file", results: []string{"results"}, resultContent: []v1beta1.ResultValue{*v1beta1.NewStructuredValues("hello", "world")}, - want: []v1beta1.RunResult{ + want: []result.RunResult{ {Value: `["hello","world"]`, ResultType: 1}}, }, { desc: "read string and array result files", results: []string{"resultsArray", "resultsString"}, resultContent: []v1beta1.ResultValue{*v1beta1.NewStructuredValues("hello", "world"), *v1beta1.NewStructuredValues("hello world")}, - want: []v1beta1.RunResult{ + want: []result.RunResult{ {Value: `["hello","world"]`, ResultType: 1}, {Value: `"hello world"`, @@ -567,7 +568,7 @@ func TestEntrypointerResults(t *testing.T) { fileContents, err := os.ReadFile(terminationPath) if err == nil { resultCheck := map[string]bool{} - var entries []v1beta1.RunResult + var entries []result.RunResult if err := json.Unmarshal(fileContents, &entries); err != nil { t.Fatalf("failed to unmarshal results: %v", err) } diff --git a/pkg/pod/status.go b/pkg/pod/status.go index 87e4557f852..88959bc20a3 100644 --- a/pkg/pod/status.go +++ b/pkg/pod/status.go @@ -29,6 +29,7 @@ import ( "github.com/tektoncd/pipeline/pkg/apis/config" "github.com/tektoncd/pipeline/pkg/apis/pipeline" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/result" "github.com/tektoncd/pipeline/pkg/termination" "go.uber.org/zap" corev1 "k8s.io/api/core/v1" @@ -248,7 +249,7 @@ func setTaskRunStatusBasedOnSidecarStatus(sidecarStatuses []corev1.ContainerStat } } -func createMessageFromResults(results []v1beta1.RunResult) (string, error) { +func createMessageFromResults(results []result.RunResult) (string, error) { if len(results) == 0 { return "", nil } @@ -262,16 +263,16 @@ func createMessageFromResults(results []v1beta1.RunResult) (string, error) { // filterResults filters the RunResults and TaskResults based on the results declared in the task spec. // It returns a slice of any of the input results that are defined in the task spec, converted to TaskRunResults, // and a slice of any of the RunResults that don't represent internal values (i.e. those that should not be displayed in the TaskRun status. -func filterResults(results []v1beta1.RunResult, specResults []v1beta1.TaskResult) ([]v1beta1.TaskRunResult, []v1beta1.RunResult) { +func filterResults(results []result.RunResult, specResults []v1beta1.TaskResult) ([]v1beta1.TaskRunResult, []result.RunResult) { var taskResults []v1beta1.TaskRunResult - var filteredResults []v1beta1.RunResult + var filteredResults []result.RunResult neededTypes := make(map[string]v1beta1.ResultsType) for _, r := range specResults { neededTypes[r.Name] = r.Type } for _, r := range results { switch r.ResultType { - case v1beta1.TaskRunResultType: + case result.TaskRunResultType: var taskRunResult v1beta1.TaskRunResult if neededTypes[r.Key] == v1beta1.ResultsTypeString { taskRunResult = v1beta1.TaskRunResult{ @@ -293,7 +294,7 @@ func filterResults(results []v1beta1.RunResult, specResults []v1beta1.TaskResult } taskResults = append(taskResults, taskRunResult) filteredResults = append(filteredResults, r) - case v1beta1.InternalTektonResultType: + case result.InternalTektonResultType: // Internal messages are ignored because they're not used as external result continue default: @@ -323,7 +324,7 @@ func removeDuplicateResults(taskRunResult []v1beta1.TaskRunResult) []v1beta1.Tas return uniq } -func extractStartedAtTimeFromResults(results []v1beta1.RunResult) (*metav1.Time, error) { +func extractStartedAtTimeFromResults(results []result.RunResult) (*metav1.Time, error) { for _, result := range results { if result.Key == "StartedAt" { t, err := time.Parse(timeFormat, result.Value) @@ -337,7 +338,7 @@ func extractStartedAtTimeFromResults(results []v1beta1.RunResult) (*metav1.Time, return nil, nil } -func extractExitCodeFromResults(results []v1beta1.RunResult) (*int32, error) { +func extractExitCodeFromResults(results []result.RunResult) (*int32, error) { for _, result := range results { if result.Key == "ExitCode" { // We could just pass the string through but this provides extra validation @@ -469,8 +470,8 @@ func extractContainerFailureMessage(logger *zap.SugaredLogger, status corev1.Con if term != nil { msg := status.State.Terminated.Message r, _ := termination.ParseMessage(logger, msg) - for _, result := range r { - if result.ResultType == v1beta1.InternalTektonResultType && result.Key == "Reason" && result.Value == "TimeoutExceeded" { + for _, runResult := range r { + if runResult.ResultType == result.InternalTektonResultType && runResult.Key == "Reason" && runResult.Value == "TimeoutExceeded" { // Newline required at end to prevent yaml parser from breaking the log help text at 80 chars return fmt.Sprintf("%q exited because the step exceeded the specified timeout limit; for logs run: kubectl -n %s logs %s -c %s\n", status.Name, diff --git a/pkg/result/result.go b/pkg/result/result.go new file mode 100644 index 00000000000..7bc1261659f --- /dev/null +++ b/pkg/result/result.go @@ -0,0 +1,91 @@ +/* +Copyright 2023 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 result + +import ( + "encoding/json" + "fmt" + + "github.com/hashicorp/go-multierror" +) + +const ( + // TaskRunResultType default task run result value + TaskRunResultType ResultType = 1 + // reserved: 2 + // was RunResultType + + // InternalTektonResultType default internal tekton result value + InternalTektonResultType = 3 + // UnknownResultType default unknown result type value + UnknownResultType = 10 +) + +// RunResult is used to write key/value pairs to TaskRun pod termination messages. +// The key/value pairs may come from the entrypoint binary, or represent a TaskRunResult. +// If they represent a TaskRunResult, the key is the name of the result and the value is the +// JSON-serialized value of the result. +type RunResult struct { + Key string `json:"key"` + Value string `json:"value"` + // ResourceName may be used in tests, but it is not populated in termination messages. + // It is preserved here for backwards compatibility and will not be ported to v1. + ResourceName string `json:"resourceName,omitempty"` + ResultType ResultType `json:"type,omitempty"` +} + +// ResultType used to find out whether a RunResult is from a task result or not +// Note that ResultsType is another type which is used to define the data type +// (e.g. string, array, etc) we used for Results +// nolint:revive // revive complains about stutter of `result.ResultType`. +type ResultType int + +// UnmarshalJSON unmarshals either an int or a string into a ResultType. String +// ResultTypes were removed because they made JSON messages bigger, which in +// turn limited the amount of space in termination messages for task results. String +// support is maintained for backwards compatibility - the Pipelines controller could +// be stopped midway through TaskRun execution, updated with support for int in place +// of string, and then fail the running TaskRun because it doesn't know how to interpret +// the string value that the TaskRun's entrypoint will emit when it completes. +func (r *ResultType) UnmarshalJSON(data []byte) error { + var asInt int + var intErr error + + if err := json.Unmarshal(data, &asInt); err != nil { + intErr = err + } else { + *r = ResultType(asInt) + return nil + } + + var asString string + + if err := json.Unmarshal(data, &asString); err != nil { + return fmt.Errorf("unsupported value type, neither int nor string: %w", multierror.Append(intErr, err).ErrorOrNil()) + } + + switch asString { + case "TaskRunResult": + *r = TaskRunResultType + case "InternalTektonResult": + *r = InternalTektonResultType + default: + *r = UnknownResultType + } + + return nil +} diff --git a/pkg/result/result_test.go b/pkg/result/result_test.go new file mode 100644 index 00000000000..ee2e5c366c4 --- /dev/null +++ b/pkg/result/result_test.go @@ -0,0 +1,58 @@ +/* +Copyright 2023 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 result + +import ( + "encoding/json" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/tektoncd/pipeline/test/diff" +) + +func TestRunResult_UnmarshalJSON(t *testing.T) { + testcases := []struct { + name string + data string + pr RunResult + }{{ + name: "type defined as string - TaskRunResult", + data: "{\"key\":\"resultName\",\"value\":\"resultValue\", \"type\": \"TaskRunResult\"}", + pr: RunResult{Key: "resultName", Value: "resultValue", ResultType: TaskRunResultType}, + }, + { + name: "type defined as string - InternalTektonResult", + data: "{\"key\":\"resultName\",\"value\":\"\", \"type\": \"InternalTektonResult\"}", + pr: RunResult{Key: "resultName", Value: "", ResultType: InternalTektonResultType}, + }, { + name: "type defined as int", + data: "{\"key\":\"resultName\",\"value\":\"\", \"type\": 1}", + pr: RunResult{Key: "resultName", Value: "", ResultType: TaskRunResultType}, + }} + + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + pipRes := RunResult{} + if err := json.Unmarshal([]byte(tc.data), &pipRes); err != nil { + t.Errorf("Unexpected error when unmarshalling the json into RunResult") + } + if d := cmp.Diff(tc.pr, pipRes); d != "" { + t.Errorf(diff.PrintWantGot(d)) + } + }) + } +} diff --git a/pkg/spire/sign.go b/pkg/spire/sign.go index 9391b9ee3b7..798eefbe805 100644 --- a/pkg/spire/sign.go +++ b/pkg/spire/sign.go @@ -28,10 +28,11 @@ import ( "github.com/spiffe/go-spiffe/v2/svid/x509svid" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/result" ) // Signs the TaskRun results with the TaskRun spire SVID and appends the results to RunResult -func (w *spireEntrypointerAPIClient) Sign(ctx context.Context, results []v1beta1.RunResult) ([]v1beta1.RunResult, error) { +func (w *spireEntrypointerAPIClient) Sign(ctx context.Context, results []result.RunResult) ([]result.RunResult, error) { err := w.setupClient(ctx) if err != nil { return nil, err @@ -46,19 +47,19 @@ func (w *spireEntrypointerAPIClient) Sign(ctx context.Context, results []v1beta1 return nil, errors.New("returned workload svid does not have certificates") } - output := []v1beta1.RunResult{} + output := []result.RunResult{} p := pem.EncodeToMemory(&pem.Block{ Bytes: xsvid.Certificates[0].Raw, Type: "CERTIFICATE", }) - output = append(output, v1beta1.RunResult{ + output = append(output, result.RunResult{ Key: KeySVID, Value: string(p), - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }) for _, r := range results { - if r.ResultType == v1beta1.TaskRunResultType { + if r.ResultType == result.TaskRunResultType { resultValue, err := getResultValue(r) if err != nil { return nil, err @@ -67,28 +68,28 @@ func (w *spireEntrypointerAPIClient) Sign(ctx context.Context, results []v1beta1 if err != nil { return nil, err } - output = append(output, v1beta1.RunResult{ + output = append(output, result.RunResult{ Key: r.Key + KeySignatureSuffix, Value: base64.StdEncoding.EncodeToString(s), - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }) } } // get complete manifest of keys such that it can be verified manifest := getManifest(results) - output = append(output, v1beta1.RunResult{ + output = append(output, result.RunResult{ Key: KeyResultManifest, Value: manifest, - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }) manifestSig, err := signWithKey(xsvid, manifest) if err != nil { return nil, err } - output = append(output, v1beta1.RunResult{ + output = append(output, result.RunResult{ Key: KeyResultManifest + KeySignatureSuffix, Value: base64.StdEncoding.EncodeToString(manifestSig), - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }) return output, nil @@ -103,10 +104,10 @@ func signWithKey(xsvid *x509svid.SVID, value string) ([]byte, error) { return s, nil } -func getManifest(results []v1beta1.RunResult) string { +func getManifest(results []result.RunResult) string { keys := []string{} for _, r := range results { - if strings.HasSuffix(r.Key, KeySignatureSuffix) || r.Key == KeySVID || r.ResultType != v1beta1.TaskRunResultType { + if strings.HasSuffix(r.Key, KeySignatureSuffix) || r.Key == KeySVID || r.ResultType != result.TaskRunResultType { continue } keys = append(keys, r.Key) diff --git a/pkg/spire/spire.go b/pkg/spire/spire.go index 3372e320c58..7ad3942aaa7 100644 --- a/pkg/spire/spire.go +++ b/pkg/spire/spire.go @@ -29,6 +29,7 @@ import ( "time" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/result" spireconfig "github.com/tektoncd/pipeline/pkg/spire/config" "go.uber.org/zap" corev1 "k8s.io/api/core/v1" @@ -64,7 +65,7 @@ type ControllerAPIClient interface { CreateEntries(ctx context.Context, tr *v1beta1.TaskRun, pod *corev1.Pod, ttl time.Duration) error DeleteEntry(ctx context.Context, tr *v1beta1.TaskRun, pod *corev1.Pod) error VerifyStatusInternalAnnotation(ctx context.Context, tr *v1beta1.TaskRun, logger *zap.SugaredLogger) error - VerifyTaskRunResults(ctx context.Context, prs []v1beta1.RunResult, tr *v1beta1.TaskRun) error + VerifyTaskRunResults(ctx context.Context, prs []result.RunResult, tr *v1beta1.TaskRun) error SetConfig(c spireconfig.SpireConfig) } @@ -72,5 +73,5 @@ type ControllerAPIClient interface { type EntrypointerAPIClient interface { Close() error // Sign returns the signature material to be put in the RunResult to append to the output results - Sign(ctx context.Context, results []v1beta1.RunResult) ([]v1beta1.RunResult, error) + Sign(ctx context.Context, results []result.RunResult) ([]result.RunResult, error) } diff --git a/pkg/spire/spire_mock.go b/pkg/spire/spire_mock.go index e6fe0977a01..49e0571fca2 100644 --- a/pkg/spire/spire_mock.go +++ b/pkg/spire/spire_mock.go @@ -25,6 +25,7 @@ import ( "github.com/pkg/errors" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/result" spireconfig "github.com/tektoncd/pipeline/pkg/spire/config" "go.uber.org/zap" corev1 "k8s.io/api/core/v1" @@ -64,7 +65,7 @@ type MockClient struct { VerifyStatusInternalAnnotationOverride func(ctx context.Context, tr *v1beta1.TaskRun, logger *zap.SugaredLogger) error // VerifyTaskRunResultsOverride contains the function to overwrite a call to VerifyTaskRunResults - VerifyTaskRunResultsOverride func(ctx context.Context, prs []v1beta1.RunResult, tr *v1beta1.TaskRun) error + VerifyTaskRunResultsOverride func(ctx context.Context, prs []result.RunResult, tr *v1beta1.TaskRun) error // AppendStatusInternalAnnotationOverride contains the function to overwrite a call to AppendStatusInternalAnnotation AppendStatusInternalAnnotationOverride func(ctx context.Context, tr *v1beta1.TaskRun) error @@ -73,7 +74,7 @@ type MockClient struct { CheckSpireVerifiedFlagOverride func(tr *v1beta1.TaskRun) bool // SignOverride contains the function to overwrite a call to Sign - SignOverride func(ctx context.Context, results []v1beta1.RunResult) ([]v1beta1.RunResult, error) + SignOverride func(ctx context.Context, results []result.RunResult) ([]result.RunResult, error) } const controllerSvid = "CONTROLLER_SVID_DATA" @@ -178,7 +179,7 @@ func (sc *MockClient) VerifyStatusInternalAnnotation(ctx context.Context, tr *v1 } // VerifyTaskRunResults checks that all the TaskRun results are valid by the mocked spire client -func (sc *MockClient) VerifyTaskRunResults(ctx context.Context, prs []v1beta1.RunResult, tr *v1beta1.TaskRun) error { +func (sc *MockClient) VerifyTaskRunResults(ctx context.Context, prs []result.RunResult, tr *v1beta1.TaskRun) error { if sc.VerifyTaskRunResultsOverride != nil { return sc.VerifyTaskRunResultsOverride(ctx, prs, tr) } @@ -190,9 +191,9 @@ func (sc *MockClient) VerifyTaskRunResults(ctx context.Context, prs []v1beta1.Ru return errors.New("failed to verify from mock VerifyAlwaysReturns") } - resultMap := map[string]v1beta1.RunResult{} + resultMap := map[string]result.RunResult{} for _, r := range prs { - if r.ResultType == v1beta1.TaskRunResultType { + if r.ResultType == result.TaskRunResultType { resultMap[r.Key] = r } } @@ -241,7 +242,7 @@ func (sc *MockClient) VerifyTaskRunResults(ctx context.Context, prs []v1beta1.Ru } // Sign signs and appends signatures to the RunResult based on the mocked spire client -func (sc *MockClient) Sign(ctx context.Context, results []v1beta1.RunResult) ([]v1beta1.RunResult, error) { +func (sc *MockClient) Sign(ctx context.Context, results []result.RunResult) ([]result.RunResult, error) { if sc.SignOverride != nil { return sc.SignOverride(ctx, results) } @@ -257,39 +258,39 @@ func (sc *MockClient) Sign(ctx context.Context, results []v1beta1.RunResult) ([] return nil, errors.Errorf("entry doesn't exist for identity: %v", identity) } - output := []v1beta1.RunResult{} - output = append(output, v1beta1.RunResult{ + output := []result.RunResult{} + output = append(output, result.RunResult{ Key: KeySVID, Value: identity, - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }) for _, r := range results { - if r.ResultType == v1beta1.TaskRunResultType { + if r.ResultType == result.TaskRunResultType { resultValue, err := getResultValue(r) if err != nil { return nil, err } s := sc.mockSign(resultValue, identity) - output = append(output, v1beta1.RunResult{ + output = append(output, result.RunResult{ Key: r.Key + KeySignatureSuffix, Value: s, - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }) } } // get complete manifest of keys such that it can be verified manifest := getManifest(results) - output = append(output, v1beta1.RunResult{ + output = append(output, result.RunResult{ Key: KeyResultManifest, Value: manifest, - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }) manifestSig := sc.mockSign(manifest, identity) - output = append(output, v1beta1.RunResult{ + output = append(output, result.RunResult{ Key: KeyResultManifest + KeySignatureSuffix, Value: manifestSig, - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }) return output, nil diff --git a/pkg/spire/spire_mock_test.go b/pkg/spire/spire_mock_test.go index d5fc417de38..73d9dc7b1d9 100644 --- a/pkg/spire/spire_mock_test.go +++ b/pkg/spire/spire_mock_test.go @@ -24,6 +24,7 @@ import ( "github.com/google/uuid" "github.com/tektoncd/pipeline/pkg/apis/config" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/result" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -322,19 +323,19 @@ func TestMock_TaskRunResultsSignTamper(t *testing.T) { ec EntrypointerAPIClient = spireMockClient ) - genPr := func() []v1beta1.RunResult { - return []v1beta1.RunResult{ + genPr := func() []result.RunResult { + return []result.RunResult{ { Key: "foo", Value: "foo-value", ResourceName: "source-image", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, { Key: "bar", Value: "bar-value", ResourceName: "source-image2", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, } } @@ -343,7 +344,7 @@ func TestMock_TaskRunResultsSignTamper(t *testing.T) { // description of test desc string // function to tamper - tamperFn func([]v1beta1.RunResult) []v1beta1.RunResult + tamperFn func([]result.RunResult) []result.RunResult // whether sign/verify procedure should succeed success bool }{ @@ -353,8 +354,8 @@ func TestMock_TaskRunResultsSignTamper(t *testing.T) { }, { desc: "non-intrusive tamper", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { - prs = append(prs, v1beta1.RunResult{ + tamperFn: func(prs []result.RunResult) []result.RunResult { + prs = append(prs, result.RunResult{ Key: "not-taskrun-result-type-add", Value: "abc:12345", }) @@ -364,7 +365,7 @@ func TestMock_TaskRunResultsSignTamper(t *testing.T) { }, { desc: "tamper SVID", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == KeySVID { prs[i].Value = "tamper-value" @@ -376,7 +377,7 @@ func TestMock_TaskRunResultsSignTamper(t *testing.T) { }, { desc: "tamper result manifest", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == KeyResultManifest { prs[i].Value = "tamper-value" @@ -388,7 +389,7 @@ func TestMock_TaskRunResultsSignTamper(t *testing.T) { }, { desc: "tamper result manifest signature", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == KeyResultManifest+KeySignatureSuffix { prs[i].Value = "tamper-value" @@ -400,7 +401,7 @@ func TestMock_TaskRunResultsSignTamper(t *testing.T) { }, { desc: "tamper result field", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == "foo" { prs[i].Value = "tamper-value" @@ -412,7 +413,7 @@ func TestMock_TaskRunResultsSignTamper(t *testing.T) { }, { desc: "tamper result field signature", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == "foo"+KeySignatureSuffix { prs[i].Value = "tamper-value" @@ -424,7 +425,7 @@ func TestMock_TaskRunResultsSignTamper(t *testing.T) { }, { desc: "delete SVID", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == KeySVID { return append(prs[:i], prs[i+1:]...) @@ -436,7 +437,7 @@ func TestMock_TaskRunResultsSignTamper(t *testing.T) { }, { desc: "delete result manifest", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == KeyResultManifest { return append(prs[:i], prs[i+1:]...) @@ -448,7 +449,7 @@ func TestMock_TaskRunResultsSignTamper(t *testing.T) { }, { desc: "delete result manifest signature", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == KeyResultManifest+KeySignatureSuffix { return append(prs[:i], prs[i+1:]...) @@ -460,7 +461,7 @@ func TestMock_TaskRunResultsSignTamper(t *testing.T) { }, { desc: "delete result field", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == "foo" { return append(prs[:i], prs[i+1:]...) @@ -472,7 +473,7 @@ func TestMock_TaskRunResultsSignTamper(t *testing.T) { }, { desc: "delete result field signature", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == "foo"+KeySignatureSuffix { return append(prs[:i], prs[i+1:]...) @@ -484,7 +485,7 @@ func TestMock_TaskRunResultsSignTamper(t *testing.T) { }, { desc: "add to result manifest", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == KeyResultManifest { prs[i].Value += ",xyz" @@ -631,15 +632,15 @@ func testTaskRuns() []*v1beta1.TaskRun { } } -func testRunResults() [][]v1beta1.RunResult { - return [][]v1beta1.RunResult{ +func testRunResults() [][]result.RunResult { + return [][]result.RunResult{ // Single result { { Key: "digest", Value: "sha256:12345", ResourceName: "source-image", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, }, // array result @@ -648,7 +649,7 @@ func testRunResults() [][]v1beta1.RunResult { Key: "resultName", Value: "[\"hello\",\"world\"]", ResourceName: "source-image", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, }, // array result @@ -657,7 +658,7 @@ func testRunResults() [][]v1beta1.RunResult { Key: "resultArray", Value: "{\"key1\":\"var1\",\"key2\":\"var2\"}", ResourceName: "source-image", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, }, // multi result @@ -666,13 +667,13 @@ func testRunResults() [][]v1beta1.RunResult { Key: "foo", Value: "abc", ResourceName: "source-image", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, { Key: "bar", Value: "xyz", ResourceName: "source-image2", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, }, // mix result type @@ -681,25 +682,25 @@ func testRunResults() [][]v1beta1.RunResult { Key: "foo", Value: "abc", ResourceName: "source-image", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, { Key: "bar", Value: "xyz", ResourceName: "source-image2", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, { Key: "resultName", Value: "[\"hello\",\"world\"]", ResourceName: "source-image3", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, { Key: "resultName2", Value: "{\"key1\":\"var1\",\"key2\":\"var2\"}", ResourceName: "source-image4", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, }, // not TaskRunResultType diff --git a/pkg/spire/spire_test.go b/pkg/spire/spire_test.go index a4359c857eb..42675d6d9f1 100644 --- a/pkg/spire/spire_test.go +++ b/pkg/spire/spire_test.go @@ -27,6 +27,7 @@ import ( "github.com/spiffe/go-spiffe/v2/svid/x509svid" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing" + "github.com/tektoncd/pipeline/pkg/result" "github.com/tektoncd/pipeline/pkg/spire/config" "github.com/tektoncd/pipeline/pkg/spire/test" "github.com/tektoncd/pipeline/pkg/spire/test/fakeworkloadapi" @@ -474,18 +475,18 @@ func TestTaskRunResultsSignTamper(t *testing.T) { testCases := []struct { desc string // function to tamper - tamperFn func([]v1beta1.RunResult) []v1beta1.RunResult + tamperFn func([]result.RunResult) []result.RunResult wantErr bool }{ { desc: "no tamper", wantErr: false, - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { return prs }, + tamperFn: func(prs []result.RunResult) []result.RunResult { return prs }, }, { desc: "non-intrusive tamper", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { - prs = append(prs, v1beta1.RunResult{ + tamperFn: func(prs []result.RunResult) []result.RunResult { + prs = append(prs, result.RunResult{ Key: "not-taskrun-result-type-add", Value: "abc:12345", }) @@ -495,7 +496,7 @@ func TestTaskRunResultsSignTamper(t *testing.T) { }, { desc: "tamper SVID", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == KeySVID { prs[i].Value = "tamper-value" @@ -507,7 +508,7 @@ func TestTaskRunResultsSignTamper(t *testing.T) { }, { desc: "tamper result manifest", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == KeyResultManifest { prs[i].Value = "tamper-value" @@ -519,7 +520,7 @@ func TestTaskRunResultsSignTamper(t *testing.T) { }, { desc: "tamper result manifest signature", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == KeyResultManifest+KeySignatureSuffix { prs[i].Value = "tamper-value" @@ -531,7 +532,7 @@ func TestTaskRunResultsSignTamper(t *testing.T) { }, { desc: "tamper result field", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == "foo" { prs[i].Value = "tamper-value" @@ -543,7 +544,7 @@ func TestTaskRunResultsSignTamper(t *testing.T) { }, { desc: "tamper result field signature", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == "foo"+KeySignatureSuffix { prs[i].Value = "tamper-value" @@ -555,7 +556,7 @@ func TestTaskRunResultsSignTamper(t *testing.T) { }, { desc: "delete SVID", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == KeySVID { return append(prs[:i], prs[i+1:]...) @@ -567,7 +568,7 @@ func TestTaskRunResultsSignTamper(t *testing.T) { }, { desc: "delete result manifest", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == KeyResultManifest { return append(prs[:i], prs[i+1:]...) @@ -579,7 +580,7 @@ func TestTaskRunResultsSignTamper(t *testing.T) { }, { desc: "delete result manifest signature", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == KeyResultManifest+KeySignatureSuffix { return append(prs[:i], prs[i+1:]...) @@ -591,7 +592,7 @@ func TestTaskRunResultsSignTamper(t *testing.T) { }, { desc: "delete result field", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == "foo" { return append(prs[:i], prs[i+1:]...) @@ -603,7 +604,7 @@ func TestTaskRunResultsSignTamper(t *testing.T) { }, { desc: "delete result field signature", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == "foo"+KeySignatureSuffix { return append(prs[:i], prs[i+1:]...) @@ -615,7 +616,7 @@ func TestTaskRunResultsSignTamper(t *testing.T) { }, { desc: "add to result manifest", - tamperFn: func(prs []v1beta1.RunResult) []v1beta1.RunResult { + tamperFn: func(prs []result.RunResult) []result.RunResult { for i, pr := range prs { if pr.Key == KeyResultManifest { prs[i].Value += ",xyz" @@ -631,16 +632,16 @@ func TestTaskRunResultsSignTamper(t *testing.T) { ctx := context.Background() for _, tr := range testTaskRuns() { t.Run(tt.desc+" "+tr.ObjectMeta.Name, func(t *testing.T) { - results := []v1beta1.RunResult{{ + results := []result.RunResult{{ Key: "foo", Value: "foo-value", ResourceName: "source-image", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }, { Key: "bar", Value: "bar-value", ResourceName: "source-image2", - ResultType: v1beta1.TaskRunResultType, + ResultType: result.TaskRunResultType, }} wl.SetX509SVIDResponse(&fakeworkloadapi.X509SVIDResponse{ diff --git a/pkg/spire/verify.go b/pkg/spire/verify.go index 3e6eb20ec46..5735e4d8722 100644 --- a/pkg/spire/verify.go +++ b/pkg/spire/verify.go @@ -34,19 +34,20 @@ import ( "github.com/pkg/errors" "github.com/spiffe/go-spiffe/v2/workloadapi" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/result" "go.uber.org/zap" ) // VerifyTaskRunResults ensures that the TaskRun results are valid and have not been tampered with -func (sc *spireControllerAPIClient) VerifyTaskRunResults(ctx context.Context, prs []v1beta1.RunResult, tr *v1beta1.TaskRun) error { +func (sc *spireControllerAPIClient) VerifyTaskRunResults(ctx context.Context, prs []result.RunResult, tr *v1beta1.TaskRun) error { err := sc.setupClient(ctx) if err != nil { return err } - resultMap := map[string]v1beta1.RunResult{} + resultMap := map[string]result.RunResult{} for _, r := range prs { - if r.ResultType == v1beta1.TaskRunResultType { + if r.ResultType == result.TaskRunResultType { resultMap[r.Key] = r } } @@ -177,7 +178,7 @@ func CheckStatusInternalAnnotation(tr *v1beta1.TaskRun) error { return nil } -func getSVID(resultMap map[string]v1beta1.RunResult) (*x509.Certificate, error) { +func getSVID(resultMap map[string]result.RunResult) (*x509.Certificate, error) { svid, ok := resultMap[KeySVID] if !ok { return nil, errors.New("no SVID found") @@ -252,7 +253,7 @@ func verifyCertificateTrust(cert *x509.Certificate, rootCertPool *x509.CertPool) return nil } -func verifyManifest(results map[string]v1beta1.RunResult) error { +func verifyManifest(results map[string]result.RunResult) error { manifest, ok := results[KeyResultManifest] if !ok { return errors.New("no manifest found in results") @@ -283,7 +284,7 @@ func verifyAnnotation(pub interface{}, annotations map[string]string) error { return verifySignature(pub, signature, hash) } -func verifyResult(pub crypto.PublicKey, key string, results map[string]v1beta1.RunResult) error { +func verifyResult(pub crypto.PublicKey, key string, results map[string]result.RunResult) error { signature, ok := results[key+KeySignatureSuffix] if !ok { return fmt.Errorf("no signature found for %s", key) @@ -324,7 +325,7 @@ func verifySignature(pub crypto.PublicKey, signature string, value string) error } } -func getResultValue(result v1beta1.RunResult) (string, error) { +func getResultValue(result result.RunResult) (string, error) { aos := v1beta1.ArrayOrString{} err := aos.UnmarshalJSON([]byte(result.Value)) valList := []string{} diff --git a/pkg/termination/parse.go b/pkg/termination/parse.go index eb3b1c2c98a..395f70039f8 100644 --- a/pkg/termination/parse.go +++ b/pkg/termination/parse.go @@ -21,7 +21,7 @@ import ( "fmt" "sort" - v1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/result" "go.uber.org/zap" ) @@ -29,18 +29,18 @@ import ( // // If more than one item has the same key, only the latest is returned. Items // are sorted by their key. -func ParseMessage(logger *zap.SugaredLogger, msg string) ([]v1beta1.RunResult, error) { +func ParseMessage(logger *zap.SugaredLogger, msg string) ([]result.RunResult, error) { if msg == "" { return nil, nil } - var r []v1beta1.RunResult + var r []result.RunResult if err := json.Unmarshal([]byte(msg), &r); err != nil { return nil, fmt.Errorf("parsing message json: %w, msg: %s", err, msg) } for i, rr := range r { - if rr == (v1beta1.RunResult{}) { + if rr == (result.RunResult{}) { // Erase incorrect result r[i] = r[len(r)-1] r = r[:len(r)-1] @@ -49,11 +49,11 @@ func ParseMessage(logger *zap.SugaredLogger, msg string) ([]v1beta1.RunResult, e } // Remove duplicates (last one wins) and sort by key. - m := map[string]v1beta1.RunResult{} + m := map[string]result.RunResult{} for _, rr := range r { m[rr.Key] = rr } - r2 := make([]v1beta1.RunResult, 0, len(m)) + r2 := make([]result.RunResult, 0, len(m)) for _, v := range m { r2 = append(r2, v) } diff --git a/pkg/termination/parse_test.go b/pkg/termination/parse_test.go index 813adaf32d6..3d701ea00be 100644 --- a/pkg/termination/parse_test.go +++ b/pkg/termination/parse_test.go @@ -20,7 +20,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" - v1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/result" "github.com/tektoncd/pipeline/test/diff" "knative.dev/pkg/logging" ) @@ -28,11 +28,11 @@ import ( func TestParseMessage(t *testing.T) { for _, c := range []struct { desc, msg string - want []v1beta1.RunResult + want []result.RunResult }{{ desc: "valid message", msg: `[{"key": "digest","value":"hereisthedigest"},{"key":"foo","value":"bar"}]`, - want: []v1beta1.RunResult{{ + want: []result.RunResult{{ Key: "digest", Value: "hereisthedigest", }, { @@ -42,7 +42,7 @@ func TestParseMessage(t *testing.T) { }, { desc: "invalid key in message", msg: `[{"invalid": "digest","value":"hereisthedigest"},{"key":"foo","value":"bar"}]`, - want: []v1beta1.RunResult{{ + want: []result.RunResult{{ Value: "hereisthedigest", }, { Key: "foo", @@ -58,7 +58,7 @@ func TestParseMessage(t *testing.T) { {"key":"foo","value":"first"}, {"key":"foo","value":"middle"}, {"key":"foo","value":"last"}]`, - want: []v1beta1.RunResult{{ + want: []result.RunResult{{ Key: "foo", Value: "last", }}, @@ -68,7 +68,7 @@ func TestParseMessage(t *testing.T) { {"key":"zzz","value":"last"}, {"key":"ddd","value":"middle"}, {"key":"aaa","value":"first"}]`, - want: []v1beta1.RunResult{{ + want: []result.RunResult{{ Key: "aaa", Value: "first", }, { diff --git a/pkg/termination/write.go b/pkg/termination/write.go index a04a2889bcd..e58b3070112 100644 --- a/pkg/termination/write.go +++ b/pkg/termination/write.go @@ -20,7 +20,7 @@ import ( "encoding/json" "os" - v1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/result" ) const ( @@ -30,12 +30,12 @@ const ( ) // WriteMessage writes the results to the termination message path. -func WriteMessage(path string, pro []v1beta1.RunResult) error { +func WriteMessage(path string, pro []result.RunResult) error { // if the file at path exists, concatenate the new values otherwise create it // file at path already exists fileContents, err := os.ReadFile(path) if err == nil { - var existingEntries []v1beta1.RunResult + var existingEntries []result.RunResult if err := json.Unmarshal(fileContents, &existingEntries); err == nil { // append new entries to existing entries pro = append(existingEntries, pro...) diff --git a/pkg/termination/write_test.go b/pkg/termination/write_test.go index 0075a873d6f..19deb600ad8 100644 --- a/pkg/termination/write_test.go +++ b/pkg/termination/write_test.go @@ -23,7 +23,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" - "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/result" "github.com/tektoncd/pipeline/test/diff" "knative.dev/pkg/logging" ) @@ -40,7 +40,7 @@ func TestExistingFile(t *testing.T) { defer func() { _ = logger.Sync() }() - output := []v1beta1.RunResult{{ + output := []result.RunResult{{ Key: "key1", Value: "hello", }} @@ -49,7 +49,7 @@ func TestExistingFile(t *testing.T) { logger.Fatalf("Errot while writing message: %s", err) } - output = []v1beta1.RunResult{{ + output = []result.RunResult{{ Key: "key2", Value: "world", }} @@ -77,7 +77,7 @@ func TestMaxSizeFile(t *testing.T) { // Remember to clean up the file afterwards defer os.Remove(tmpFile.Name()) - output := []v1beta1.RunResult{{ + output := []result.RunResult{{ Key: "key1", Value: value, }}