Skip to content

Commit

Permalink
Migrate v1beta1 RunResult to Unversioned Package
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JeromeJu committed Apr 13, 2023
1 parent fb38679 commit bc4fce9
Show file tree
Hide file tree
Showing 25 changed files with 328 additions and 430 deletions.
79 changes: 1 addition & 78 deletions docs/pipeline-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -11428,16 +11428,6 @@ string
</tr>
</tbody>
</table>
<h3 id="tekton.dev/v1beta1.ResultType">ResultType
(<code>int</code> alias)</h3>
<p>
(<em>Appears on:</em><a href="#tekton.dev/v1beta1.RunResult">RunResult</a>)
</p>
<div>
<p>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</p>
</div>
<h3 id="tekton.dev/v1beta1.ResultsType">ResultsType
(<code>string</code> alias)</h3>
<p>
Expand All @@ -11455,71 +11445,6 @@ this ResultsType.</p>
<div>
<p>RunObject is implemented by CustomRun and Run</p>
</div>
<h3 id="tekton.dev/v1beta1.RunResult">RunResult
</h3>
<p>
(<em>Appears on:</em><a href="#tekton.dev/v1beta1.TaskRunStatusFields">TaskRunStatusFields</a>)
</p>
<div>
<p>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.</p>
</div>
<table>
<thead>
<tr>
<th>Field</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>key</code><br/>
<em>
string
</em>
</td>
<td>
</td>
</tr>
<tr>
<td>
<code>value</code><br/>
<em>
string
</em>
</td>
<td>
</td>
</tr>
<tr>
<td>
<code>resourceName</code><br/>
<em>
string
</em>
</td>
<td>
<p>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.</p>
</td>
</tr>
<tr>
<td>
<code>type</code><br/>
<em>
<a href="#tekton.dev/v1beta1.ResultType">
ResultType
</a>
</em>
</td>
<td>
</td>
</tr>
</tbody>
</table>
<h3 id="tekton.dev/v1beta1.Sidecar">Sidecar
</h3>
<p>
Expand Down Expand Up @@ -13966,9 +13891,7 @@ All TaskRunStatus stored in RetriesStatus will have no date within the RetriesSt
<td>
<code>resourcesResult</code><br/>
<em>
<a href="#tekton.dev/v1beta1.RunResult">
[]RunResult
</a>
[]github.com/tektoncd/pipeline/pkg/result.RunResult
</em>
</td>
<td>
Expand Down
22 changes: 11 additions & 11 deletions internal/sidecarlogresults/sidecarlogresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}
22 changes: 11 additions & 11 deletions internal/sidecarlogresults/sidecarlogresults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down
50 changes: 4 additions & 46 deletions pkg/apis/pipeline/v1beta1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 7 additions & 56 deletions pkg/apis/pipeline/v1beta1/resource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading

0 comments on commit bc4fce9

Please sign in to comment.