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

Made the digest exporter report image digest if there is only one image. #1237

Merged
merged 3 commits into from
Sep 6, 2019
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
6 changes: 5 additions & 1 deletion Gopkg.lock

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

18 changes: 18 additions & 0 deletions cmd/imagedigestexporter/digest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
v1 "github.com/google/go-containerregistry/pkg/v1"
)

// GetDigest returns the digest of an OCI image index. If there is only one image in the index, the
// digest of the image is returned; otherwise, the digest of the whole index is returned.
func GetDigest(ii v1.ImageIndex) (v1.Hash, error) {
im, err := ii.IndexManifest()
if err != nil {
return v1.Hash{}, err
}
if len(im.Manifests) == 1 {
return im.Manifests[0].Digest, nil
}
return ii.Digest()
}
65 changes: 65 additions & 0 deletions cmd/imagedigestexporter/digest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"testing"

"github.com/google/go-cmp/cmp"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/random"
)

func TestGetDigest(t *testing.T) {
mustGetIndex := func(ii v1.ImageIndex, err error) v1.ImageIndex {
if err != nil {
t.Fatalf("must get image: %s", err)
}
return ii
}
mustGetManifest := func(im *v1.IndexManifest, err error) *v1.IndexManifest {
if err != nil {
t.Fatalf("must get manifest: %s", err)
}
return im
}
mustGetDigest := func(h v1.Hash, err error) v1.Hash {
if err != nil {
t.Fatalf("must get digest: %s", err)
}
return h
}
indexSingleImage := mustGetIndex(random.Index(1024, 4, 1))
indexMultipleImages := mustGetIndex(random.Index(1024, 4, 4))
tests := []struct {
name string
index v1.ImageIndex
expected v1.Hash
}{
{
name: "empty index",
index: empty.Index,
expected: mustGetDigest(empty.Index.Digest()),
},
{
name: "index with single image",
index: indexSingleImage,
expected: mustGetManifest(indexSingleImage.IndexManifest()).Manifests[0].Digest,
},
{
name: "index with multiple images",
index: indexMultipleImages,
expected: mustGetDigest(indexMultipleImages.Digest()),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
digest, err := GetDigest(test.index)
if err != nil {
t.Fatalf("cannot get digest: %s", err)
}
if diff := cmp.Diff(digest, test.expected); diff != "" {
t.Errorf("get digest: -want +got: %s", diff)
}
})
}
}
4 changes: 2 additions & 2 deletions cmd/imagedigestexporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func main() {
log.Printf("ImageResource %s doesn't have an index.json file: %s", imageResource.Name, err)
continue
}
digest, err := ii.Digest()
digest, err := GetDigest(ii)
if err != nil {
log.Fatalf("Unexpected error getting image digest %v: %v", imageResource, err)
log.Fatalf("Unexpected error getting image digest for %s: %v", imageResource.Name, err)
}
output = append(output, v1alpha1.PipelineResourceResult{Name: imageResource.Name, Digest: digest.String()})
}
Expand Down
6 changes: 3 additions & 3 deletions docs/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ 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.
task definition under the resource `outputImageDir`. If there is only one image
in the `index.json` file, the digest of that image is exported; otherwise, the
digest of the whole image index would be exported.

For example this build-push task defines the `outputImageDir` for the
`builtImage` resource in `/workspace/buildImage`
Expand Down
2 changes: 1 addition & 1 deletion examples/taskruns/task-multiple-output-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ spec:
}
]
}
EOF
EOF
---
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
Expand Down
2 changes: 1 addition & 1 deletion examples/taskruns/taskrun-cloud-event.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ spec:
{
"mediaType": "application/vnd.oci.image.index.v1+json",
"size": 314,
"digest": "sha256:NOTAREALDIGEST"
"digest": "sha256:deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
}
]
}
Expand Down
16 changes: 16 additions & 0 deletions vendor/github.com/google/go-containerregistry/pkg/v1/empty/doc.go

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

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

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

16 changes: 16 additions & 0 deletions vendor/github.com/google/go-containerregistry/pkg/v1/random/doc.go

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

Loading