Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate v1beta1 RunResult to Unversioned Package #6514

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
JeromeJu marked this conversation as resolved.
Show resolved Hide resolved
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