Skip to content

Commit

Permalink
improve returned errors in outoutimageexporter + other small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nader-ziada committed May 10, 2019
1 parent 29eb21f commit 51a92ab
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 25 deletions.
4 changes: 2 additions & 2 deletions docs/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ spec:

#### Surfacing the image digest built in a task

To surface the image digest in the output of the `taskRun` the builder tool should produce this information in a [OCI Image Spec](https://github.com/opencontainers/image-spec/blob/master/image-layout.md) `index.json` file. This file should be placed on a location as specified in the task definition under the resource `outputImageDir`.
To surface the image digest in the output of the `taskRun` the builder tool should produce this information in a [OCI Image Spec](https://github.com/opencontainers/image-spec/blob/master/image-layout.md) `index.json` file. This file should be placed on a location as specified in the task definition under the resource `outputImageDir`. Annotations in `index.json` will be ignored, and if there are multiple versions of the image, the latest will be used.

For example this build-push task defines the `outputImageDir` for the `buildImage` resource in `/worksapce/buildImage`
For example this build-push task defines the `outputImageDir` for the `builtImage` resource in `/workspace/buildImage`
```yaml
apiVersion: tekton.dev/v1alpha1
kind: Task
Expand Down
11 changes: 7 additions & 4 deletions pkg/apis/pipeline/v1alpha1/image_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func NewImageResource(r *PipelineResource) (*ImageResource, error) {

// ImageResource defines an endpoint where artifacts can be stored, such as images.
type ImageResource struct {
Name string `json:"name"`
Type PipelineResourceType `json:"type"`
URL string `json:"url"`
Digest string `json:"digest"`
Name string `json:"name"`
Type PipelineResourceType `json:"type"`
URL string `json:"url"`
Digest string `json:"digest"`
OutputImageDir string
}

Expand Down Expand Up @@ -98,6 +98,9 @@ func (s *ImageResource) GetOutputImageDir() string {
}

func (s ImageResource) String() string {
// the String() func works as a toString func to return the contents as a string
// and has to follow the interface and therefore cannot return an error
// if the Marshal func gives an error, the returned string will be empty
json, _ := json.Marshal(s)
return string(json)
}
16 changes: 6 additions & 10 deletions pkg/reconciler/v1alpha1/taskrun/resources/image_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package resources
import (
"encoding/json"
"flag"
"fmt"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/names"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
)

Expand All @@ -35,28 +35,24 @@ func AddOutputImageDigestExporter(
tr *v1alpha1.TaskRun,
taskSpec *v1alpha1.TaskSpec,
gr GetResource,
logger *zap.SugaredLogger,
) error {

output := []*v1alpha1.ImageResource{}
if len(tr.Spec.Outputs.Resources) > 0 {
for _, trb := range tr.Spec.Outputs.Resources {
boundResource, err := getBoundResource(trb.Name, tr.Spec.Outputs.Resources)
if err != nil {
logger.Errorf("Failed to get bound resource: %s", err)
return err
return fmt.Errorf("Failed to get bound resource: %s while adding output image digest exporter", err)
}

resource, err := getResource(boundResource, gr)
if err != nil {
logger.Errorf("Failed to get output pipeline Resource for taskRun %q resource %v; error: %s", tr.Name, boundResource, err.Error())
return err
return fmt.Errorf("Failed to get output pipeline Resource for taskRun %q resource %v; error: %s while adding output image digest exporter", tr.Name, boundResource, err.Error())
}
if resource.Spec.Type == v1alpha1.PipelineResourceTypeImage {
imageResource, err := v1alpha1.NewImageResource(resource)
if err != nil {
logger.Errorf("Invalid Image Resource for taskRun %q resource %v; error: %s", tr.Name, boundResource, err.Error())
return err
return fmt.Errorf("Invalid Image Resource for taskRun %q resource %v; error: %s", tr.Name, boundResource, err.Error())
}
for _, o := range taskSpec.Outputs.Resources {
if o.Name == boundResource.Name {
Expand All @@ -74,7 +70,7 @@ func AddOutputImageDigestExporter(
augmentedSteps := []corev1.Container{}
imagesJSON, err := json.Marshal(output)
if err != nil {
return err
return fmt.Errorf("Failed to format image resource data for output image exporter: %s", err)
}

for _, s := range taskSpec.Steps {
Expand All @@ -93,7 +89,7 @@ func AddOutputImageDigestExporter(
func UpdateTaskRunStatusWithResourceResult(taskRun *v1alpha1.TaskRun, logContent []byte) error {
err := json.Unmarshal(logContent, &taskRun.Status.ResourcesResult)
if err != nil {
return err
return fmt.Errorf("Failed to unmarshal output image exporter JSON output: %s", err)
}
return nil
}
Expand Down
14 changes: 6 additions & 8 deletions pkg/reconciler/v1alpha1/taskrun/resources/image_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/knative/pkg/apis"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/logging"
"github.com/tektoncd/pipeline/test/names"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestExportingOutputImageResource(t *testing.T) {
func TestAddOutputImageDigestExporter(t *testing.T) {
currentDir, _ := os.Getwd()
for _, c := range []struct {
desc string
Expand All @@ -50,8 +49,8 @@ func TestExportingOutputImageResource(t *testing.T) {
},
Outputs: &v1alpha1.Outputs{
Resources: []v1alpha1.TaskResource{{
Name: "source-image",
Type: "image",
Name: "source-image",
Type: "image",
OutputImageDir: currentDir,
}},
},
Expand Down Expand Up @@ -111,8 +110,8 @@ func TestExportingOutputImageResource(t *testing.T) {
},
Outputs: &v1alpha1.Outputs{
Resources: []v1alpha1.TaskResource{{
Name: "source-image",
Type: "image",
Name: "source-image",
Type: "image",
OutputImageDir: currentDir,
}},
},
Expand Down Expand Up @@ -168,7 +167,6 @@ func TestExportingOutputImageResource(t *testing.T) {
}} {
t.Run(c.desc, func(t *testing.T) {
names.TestingSeed()
logger, _ := logging.NewLogger("", "")
gr := func(n string) (*v1alpha1.PipelineResource, error) {
return &v1alpha1.PipelineResource{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -190,7 +188,7 @@ func TestExportingOutputImageResource(t *testing.T) {
},
}, nil
}
err := AddOutputImageDigestExporter(c.taskRun, &c.task.Spec, gr, logger)
err := AddOutputImageDigestExporter(c.taskRun, &c.task.Spec, gr)
if err != nil {
t.Fatalf("Failed to declare output resources for test %q: error %v", c.desc, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/v1alpha1/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func (c *Reconciler) updateLabels(tr *v1alpha1.TaskRun) (*v1alpha1.TaskRun, erro
func (c *Reconciler) createPod(tr *v1alpha1.TaskRun, ts *v1alpha1.TaskSpec, taskName string) (*corev1.Pod, error) {
ts = ts.DeepCopy()

err := resources.AddOutputImageDigestExporter(tr, ts, c.resourceLister.PipelineResources(tr.Namespace).Get, c.Logger)
err := resources.AddOutputImageDigestExporter(tr, ts, c.resourceLister.PipelineResources(tr.Namespace).Get)
if err != nil {
c.Logger.Errorf("Failed to create a build for taskrun: %s due to output image resource error %v", tr.Name, err)
return nil, err
Expand Down

0 comments on commit 51a92ab

Please sign in to comment.