Skip to content

Commit

Permalink
Add back conversion for TaskRunStatus Resources
Browse files Browse the repository at this point in the history
This commit adds back the conversion for TaskrunStatus resources which
are used by chains to sign outputs of image resources type. It also adds
back the conversion for TaskRun Resources.

/kind misc
fixes: #7504
  • Loading branch information
JeromeJu authored and tekton-robot committed Dec 21, 2023
1 parent e59ee42 commit 891bda9
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 0 deletions.
57 changes: 57 additions & 0 deletions pkg/apis/pipeline/v1beta1/taskrun_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
const (
cloudEventsAnnotationKey = "tekton.dev/v1beta1CloudEvents"
resourcesResultAnnotationKey = "tekton.dev/v1beta1ResourcesResult"
resourcesStatusAnnotationKey = "tekton.dev/v1beta1ResourcesStatus"
)

var _ apis.Convertible = (*TaskRun)(nil)
Expand All @@ -41,12 +42,18 @@ func (tr *TaskRun) ConvertTo(ctx context.Context, to apis.Convertible) error {
switch sink := to.(type) {
case *v1.TaskRun:
sink.ObjectMeta = tr.ObjectMeta
if err := serializeTaskRunResources(&sink.ObjectMeta, &tr.Spec); err != nil {
return err
}
if err := serializeTaskRunCloudEvents(&sink.ObjectMeta, &tr.Status); err != nil {
return err
}
if err := serializeTaskRunResourcesResult(&sink.ObjectMeta, &tr.Status); err != nil {
return err
}
if err := serializeTaskRunResourcesStatus(&sink.ObjectMeta, &tr.Status); err != nil {
return err
}
if err := tr.Status.ConvertTo(ctx, &sink.Status, &sink.ObjectMeta); err != nil {
return err
}
Expand Down Expand Up @@ -115,6 +122,9 @@ func (tr *TaskRun) ConvertFrom(ctx context.Context, from apis.Convertible) error
switch source := from.(type) {
case *v1.TaskRun:
tr.ObjectMeta = source.ObjectMeta
if err := deserializeTaskRunResources(&tr.ObjectMeta, &tr.Spec); err != nil {
return err
}
if err := deserializeTaskRunCloudEvents(&tr.ObjectMeta, &tr.Status); err != nil {
return err
}
Expand All @@ -124,6 +134,9 @@ func (tr *TaskRun) ConvertFrom(ctx context.Context, from apis.Convertible) error
if err := tr.Status.ConvertFrom(ctx, source.Status, &tr.ObjectMeta); err != nil {
return err
}
if err := deserializeTaskRunResourcesStatus(&tr.ObjectMeta, &tr.Status); err != nil {
return err
}
return tr.Spec.ConvertFrom(ctx, &source.Spec, &tr.ObjectMeta)
default:
return fmt.Errorf("unknown version, got: %T", tr)
Expand Down Expand Up @@ -381,6 +394,25 @@ func (ss *SidecarState) convertFrom(ctx context.Context, source v1.SidecarState)
ss.ImageID = source.ImageID
}

func serializeTaskRunResources(meta *metav1.ObjectMeta, spec *TaskRunSpec) error {
if spec.Resources == nil {
return nil
}
return version.SerializeToMetadata(meta, spec.Resources, resourcesAnnotationKey)
}

func deserializeTaskRunResources(meta *metav1.ObjectMeta, spec *TaskRunSpec) error {
resources := &TaskRunResources{}
err := version.DeserializeFromMetadata(meta, resources, resourcesAnnotationKey)
if err != nil {
return err
}
if resources.Inputs != nil || resources.Outputs != nil {
spec.Resources = resources
}
return nil
}

func serializeTaskRunCloudEvents(meta *metav1.ObjectMeta, status *TaskRunStatus) error {
if status.CloudEvents == nil {
return nil
Expand Down Expand Up @@ -418,3 +450,28 @@ func deserializeTaskRunResourcesResult(meta *metav1.ObjectMeta, status *TaskRunS
}
return nil
}

func serializeTaskRunResourcesStatus(meta *metav1.ObjectMeta, status *TaskRunStatus) error {
if status.TaskSpec == nil {
return nil
}
if status.TaskSpec.Resources == nil {
return nil
}
return version.SerializeToMetadata(meta, status.TaskSpec.Resources, resourcesStatusAnnotationKey)
}

func deserializeTaskRunResourcesStatus(meta *metav1.ObjectMeta, status *TaskRunStatus) error {
resourcesStatus := &TaskResources{}
err := version.DeserializeFromMetadata(meta, resourcesStatus, resourcesStatusAnnotationKey)
if err != nil {
return err
}
if resourcesStatus.Inputs != nil || resourcesStatus.Outputs != nil {
if status.TaskRunStatusFields.TaskSpec == nil {
status.TaskSpec = &TaskSpec{}
}
status.TaskSpec.Resources = resourcesStatus
}
return nil
}
141 changes: 141 additions & 0 deletions pkg/apis/pipeline/v1beta1/taskrun_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,147 @@ func TestTaskRunConversionFromDeprecated(t *testing.T) {
in *v1beta1.TaskRun
want *v1beta1.TaskRun
}{{
name: "input resources",
in: &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
},
Spec: v1beta1.TaskRunSpec{
Resources: &v1beta1.TaskRunResources{
Inputs: []v1beta1.TaskResourceBinding{{
PipelineResourceBinding: v1beta1.PipelineResourceBinding{
ResourceRef: &v1beta1.PipelineResourceRef{
Name: "the-git-with-branch",
},
Name: "gitspace",
},
Paths: []string{"test-path"},
}},
},
},
},
want: &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
},
Spec: v1beta1.TaskRunSpec{
Resources: &v1beta1.TaskRunResources{
Inputs: []v1beta1.TaskResourceBinding{{
PipelineResourceBinding: v1beta1.PipelineResourceBinding{
ResourceRef: &v1beta1.PipelineResourceRef{
Name: "the-git-with-branch",
},
Name: "gitspace",
},
Paths: []string{"test-path"},
}},
},
},
},
}, {
name: "output resources",
in: &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
},
Spec: v1beta1.TaskRunSpec{
Resources: &v1beta1.TaskRunResources{
Outputs: []v1beta1.TaskResourceBinding{{
PipelineResourceBinding: v1beta1.PipelineResourceBinding{
ResourceRef: &v1beta1.PipelineResourceRef{
Name: "the-git-with-branch",
},
Name: "gitspace",
},
Paths: []string{"test-path"},
}},
},
},
},
want: &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
},
Spec: v1beta1.TaskRunSpec{
Resources: &v1beta1.TaskRunResources{
Outputs: []v1beta1.TaskResourceBinding{{
PipelineResourceBinding: v1beta1.PipelineResourceBinding{
ResourceRef: &v1beta1.PipelineResourceRef{
Name: "the-git-with-branch",
},
Name: "gitspace",
},
Paths: []string{"test-path"},
}},
},
},
},
}, {
name: "taskrun status task resources",
in: &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
},
Spec: v1beta1.TaskRunSpec{
TaskRef: &v1beta1.TaskRef{
Name: "test-resources-status",
},
},
Status: v1beta1.TaskRunStatus{
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
TaskSpec: &v1beta1.TaskSpec{
Resources: &v1beta1.TaskResources{
Inputs: []v1beta1.TaskResource{{
v1beta1.ResourceDeclaration{
Name: "input-resource",
},
}},
Outputs: []v1beta1.TaskResource{{
v1beta1.ResourceDeclaration{
Name: "input-resource",
Type: "image",
},
}},
},
},
},
},
},
want: &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
},
Spec: v1beta1.TaskRunSpec{
TaskRef: &v1beta1.TaskRef{
Name: "test-resources-status",
},
},
Status: v1beta1.TaskRunStatus{
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
TaskSpec: &v1beta1.TaskSpec{
Resources: &v1beta1.TaskResources{
Inputs: []v1beta1.TaskResource{{
v1beta1.ResourceDeclaration{
Name: "input-resource",
},
}},
Outputs: []v1beta1.TaskResource{{
v1beta1.ResourceDeclaration{
Name: "input-resource",
Type: "image",
},
}},
},
},
},
}},
}, {
name: "bundle",
in: &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit 891bda9

Please sign in to comment.