Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit f52fac5

Browse files
committed
Store repo digest as app image is pushed
also fix app image list --digests so it doesn't show "app build ID" as digest Signed-off-by: Nicolas De Loof <[email protected]>
1 parent e18898a commit f52fac5

File tree

14 files changed

+146
-117
lines changed

14 files changed

+146
-117
lines changed

Gopkg.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/cnab/cnab.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func PullBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, tagRef
123123
return nil, err
124124
}
125125
relocatedBundle := &relocated.Bundle{Bundle: bndl, RelocationMap: relocationMap}
126-
if _, err := bundleStore.Store(tagRef, relocatedBundle); err != nil {
126+
if _, err := bundleStore.Store(relocatedBundle, tagRef); err != nil {
127127
return nil, err
128128
}
129129
return relocatedBundle, nil

internal/commands/build/build.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,12 @@ func runBuild(dockerCli command.Cli, contextPath string, opt buildOptions) error
133133
return err
134134
}
135135

136-
var ref reference.Reference
137-
ref, err = packager.GetNamedTagged(opt.tag)
136+
ref, err := packager.GetNamedTagged(opt.tag)
138137
if err != nil {
139138
return err
140139
}
141140

142-
id, err := packager.PersistInBundleStore(ref, bundle)
141+
id, err := packager.PersistInBundleStore(bundle, ref)
143142
if err != nil {
144143
return err
145144
}

internal/commands/image/list.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ func getImageDesc(bundle *relocated.Bundle, ref reference.Reference) imageDesc {
126126
if t, ok := ref.(reference.Tagged); ok {
127127
tag = t.Tag()
128128
}
129-
var digest string
130-
if t, ok := ref.(reference.Digested); ok {
131-
digest = t.Digest().String()
132-
}
129+
digest := bundle.RepoDigest
133130
var created time.Time
134131
if payload, err := packager.CustomPayload(bundle.Bundle); err == nil {
135132
if createdPayload, ok := payload.(packager.CustomPayloadCreated); ok {
@@ -141,7 +138,7 @@ func getImageDesc(bundle *relocated.Bundle, ref reference.Reference) imageDesc {
141138
Name: bundle.Name,
142139
Repository: repository,
143140
Tag: tag,
144-
Digest: digest,
141+
Digest: digest.String(),
145142
Created: created,
146143
}
147144
}

internal/commands/image/list_test.go

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,24 @@ package image
33
import (
44
"bufio"
55
"bytes"
6-
"fmt"
76
"testing"
87
"time"
98

10-
"github.com/docker/app/internal/relocated"
11-
12-
"gotest.tools/assert"
9+
"github.com/docker/app/internal/store"
10+
"gotest.tools/fs"
1311

1412
"github.com/deislabs/cnab-go/bundle"
15-
"github.com/docker/app/internal/store"
13+
"github.com/docker/app/internal/relocated"
1614
"github.com/docker/cli/cli/command"
1715
"github.com/docker/distribution/reference"
16+
"gotest.tools/assert"
1817
)
1918

20-
type bundleStoreStubForListCmd struct {
21-
refMap map[reference.Reference]*relocated.Bundle
22-
// in order to keep the reference in the same order between tests
23-
refList []reference.Reference
24-
}
25-
26-
func (b *bundleStoreStubForListCmd) Store(ref reference.Reference, bndl *relocated.Bundle) (reference.Digested, error) {
27-
b.refMap[ref] = bndl
28-
b.refList = append(b.refList, ref)
29-
return store.FromBundle(bndl)
30-
}
31-
32-
func (b *bundleStoreStubForListCmd) Read(ref reference.Reference) (*relocated.Bundle, error) {
33-
bndl, ok := b.refMap[ref]
34-
if ok {
35-
return bndl, nil
36-
}
37-
return nil, fmt.Errorf("Bundle not found")
38-
}
39-
40-
func (b *bundleStoreStubForListCmd) List() ([]reference.Reference, error) {
41-
return b.refList, nil
42-
}
43-
44-
func (b *bundleStoreStubForListCmd) Remove(ref reference.Reference, force bool) error {
45-
return nil
46-
}
47-
48-
func (b *bundleStoreStubForListCmd) LookUp(refOrID string) (reference.Reference, error) {
49-
return nil, nil
50-
}
51-
5219
func TestListCmd(t *testing.T) {
53-
ref, err := store.FromString("a855ac937f2ed375ba4396bbc49c4093e124da933acd2713fb9bc17d7562a087")
54-
assert.NilError(t, err)
55-
refs := []reference.Reference{
20+
refs := []reference.Named{
5621
parseReference(t, "foo/bar@sha256:b59492bb814012ca3d2ce0b6728242d96b4af41687cc82166a4b5d7f2d9fb865"),
5722
parseReference(t, "foo/bar:1.0"),
58-
ref,
23+
nil,
5924
}
6025
bundles := []relocated.Bundle{
6126
{
@@ -85,36 +50,36 @@ func TestListCmd(t *testing.T) {
8550
{
8651
name: "TestList",
8752
expectedOutput: `REPOSITORY TAG APP IMAGE ID APP NAME CREATED
88-
foo/bar <none> 3f825b2d0657 Digested App N/A
53+
<none> <none> ad2828ea5653 Quiet App N/A
8954
foo/bar 1.0 9aae408ee04f Foo App N/A
90-
<none> <none> a855ac937f2e Quiet App N/A
55+
foo/bar <none> 3f825b2d0657 Digested App N/A
9156
`,
9257
options: imageListOption{format: "table"},
9358
},
9459
{
9560
name: "TestTemplate",
9661
expectedOutput: `APP IMAGE ID DIGEST
97-
3f825b2d0657 sha256:b59492bb814012ca3d2ce0b6728242d96b4af41687cc82166a4b5d7f2d9fb865
62+
ad2828ea5653 <none>
9863
9aae408ee04f <none>
99-
a855ac937f2e sha256:a855ac937f2ed375ba4396bbc49c4093e124da933acd2713fb9bc17d7562a087
64+
3f825b2d0657 sha256:b59492bb814012ca3d2ce0b6728242d96b4af41687cc82166a4b5d7f2d9fb865
10065
`,
10166
options: imageListOption{format: "table {{.ID}}", digests: true},
10267
},
10368
{
10469
name: "TestListWithDigests",
10570
//nolint:lll
10671
expectedOutput: `REPOSITORY TAG DIGEST APP IMAGE ID APP NAME CREATED
107-
foo/bar <none> sha256:b59492bb814012ca3d2ce0b6728242d96b4af41687cc82166a4b5d7f2d9fb865 3f825b2d0657 Digested App N/A
72+
<none> <none> <none> ad2828ea5653 Quiet App N/A
10873
foo/bar 1.0 <none> 9aae408ee04f Foo App N/A
109-
<none> <none> sha256:a855ac937f2ed375ba4396bbc49c4093e124da933acd2713fb9bc17d7562a087 a855ac937f2e Quiet App N/A
74+
foo/bar <none> sha256:b59492bb814012ca3d2ce0b6728242d96b4af41687cc82166a4b5d7f2d9fb865 3f825b2d0657 Digested App N/A
11075
`,
11176
options: imageListOption{format: "table", digests: true},
11277
},
11378
{
11479
name: "TestListWithQuiet",
115-
expectedOutput: `3f825b2d0657
80+
expectedOutput: `ad2828ea5653
11681
9aae408ee04f
117-
a855ac937f2e
82+
3f825b2d0657
11883
`,
11984
options: imageListOption{format: "table", quiet: true},
12085
},
@@ -143,27 +108,26 @@ func TestSortImages(t *testing.T) {
143108
assert.Equal(t, "3", images[4].ID)
144109
}
145110

146-
func parseReference(t *testing.T, s string) reference.Reference {
147-
ref, err := reference.Parse(s)
111+
func parseReference(t *testing.T, s string) reference.Named {
112+
ref, err := reference.ParseNormalizedNamed(s)
148113
assert.NilError(t, err)
149114
return ref
150115
}
151116

152-
func testRunList(t *testing.T, refs []reference.Reference, bundles []relocated.Bundle, options imageListOption, expectedOutput string) {
117+
func testRunList(t *testing.T, refs []reference.Named, bundles []relocated.Bundle, options imageListOption, expectedOutput string) {
153118
var buf bytes.Buffer
154119
w := bufio.NewWriter(&buf)
155120
dockerCli, err := command.NewDockerCli(command.WithOutputStream(w))
156121
assert.NilError(t, err)
157-
bundleStore := &bundleStoreStubForListCmd{
158-
refMap: make(map[reference.Reference]*relocated.Bundle),
159-
refList: []reference.Reference{},
160-
}
122+
bundleStore, err := store.NewBundleStore(fs.NewDir(t, "store").Path())
123+
assert.NilError(t, err)
161124
for i, ref := range refs {
162-
_, err = bundleStore.Store(ref, &bundles[i])
125+
_, err = bundleStore.Store(&bundles[i], ref)
163126
assert.NilError(t, err)
164127
}
165128
err = runList(dockerCli, options, bundleStore)
166129
assert.NilError(t, err)
167130
w.Flush()
168-
assert.Equal(t, buf.String(), expectedOutput)
131+
actualOutput := buf.String()
132+
assert.Equal(t, actualOutput, expectedOutput)
169133
}

internal/commands/image/tag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ func storeBundle(bundle *relocated.Bundle, name string, bundleStore store.Bundle
7373
if err != nil {
7474
return err
7575
}
76-
_, err = bundleStore.Store(cnabRef, bundle)
76+
_, err = bundleStore.Store(bundle, cnabRef)
7777
return err
7878
}

internal/commands/image/tag_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"testing"
66

7+
"github.com/docker/app/internal/store"
8+
79
"github.com/docker/app/internal/relocated"
810

911
"gotest.tools/assert"
@@ -23,12 +25,12 @@ type bundleStoreStub struct {
2325
ReadError error
2426
StoredBundle string
2527
StoredError error
26-
StoredID reference.Digested
28+
StoredID store.ID
2729
LookUpRef reference.Reference
2830
LookUpError error
2931
}
3032

31-
func (b *bundleStoreStub) Store(ref reference.Reference, bndle *relocated.Bundle) (reference.Digested, error) {
33+
func (b *bundleStoreStub) Store(bndl *relocated.Bundle, ref reference.Named) (store.ID, error) {
3234
defer func() {
3335
b.StoredError = nil
3436
}()

internal/commands/push.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"os"
99
"strings"
1010

11+
"github.com/opencontainers/go-digest"
12+
1113
"github.com/docker/app/internal/relocated"
1214
"github.com/docker/app/internal/store"
1315

@@ -72,7 +74,13 @@ func runPush(dockerCli command.Cli, name string) error {
7274
cnabRef := reference.TagNameOnly(ref)
7375

7476
// Push the bundle
75-
return pushBundle(dockerCli, bndl, cnabRef)
77+
dg, err := pushBundle(dockerCli, bndl, cnabRef)
78+
if err != nil {
79+
return errors.Wrapf(err, "could not push %q", cnabRef)
80+
}
81+
bndl.RepoDigest = dg
82+
_, err = bundleStore.Store(bndl, cnabRef)
83+
return err
7684
}
7785

7886
func resolveReferenceAndBundle(bundleStore store.BundleStore, ref reference.Reference) (*relocated.Bundle, error) {
@@ -88,10 +96,10 @@ func resolveReferenceAndBundle(bundleStore store.BundleStore, ref reference.Refe
8896
return bndl, err
8997
}
9098

91-
func pushBundle(dockerCli command.Cli, bndl *relocated.Bundle, cnabRef reference.Named) error {
99+
func pushBundle(dockerCli command.Cli, bndl *relocated.Bundle, cnabRef reference.Named) (digest.Digest, error) {
92100
insecureRegistries, err := internal.InsecureRegistriesFromEngine(dockerCli)
93101
if err != nil {
94-
return errors.Wrap(err, "could not retrieve insecure registries")
102+
return "", errors.Wrap(err, "could not retrieve insecure registries")
95103
}
96104
resolver := remotes.CreateResolver(dockerCli.ConfigFile(), insecureRegistries...)
97105
var display fixupDisplay = &plainDisplay{out: os.Stdout}
@@ -107,17 +115,17 @@ func pushBundle(dockerCli command.Cli, bndl *relocated.Bundle, cnabRef reference
107115
// bundle fixup
108116
relocationMap, err := remotes.FixupBundle(context.Background(), bndl.Bundle, cnabRef, resolver, fixupOptions...)
109117
if err != nil {
110-
return errors.Wrapf(err, "fixing up %q for push", cnabRef)
118+
return "", errors.Wrapf(err, "fixing up %q for push", cnabRef)
111119
}
112120
bndl.RelocationMap = relocationMap
113121
// push bundle manifest
114122
logrus.Debugf("Pushing the bundle %q", cnabRef)
115123
descriptor, err := remotes.Push(log.WithLogContext(context.Background()), bndl.Bundle, bndl.RelocationMap, cnabRef, resolver, true, withAppAnnotations)
116124
if err != nil {
117-
return errors.Wrapf(err, "pushing to %q", cnabRef)
125+
return "", errors.Wrapf(err, "pushing to %q", cnabRef)
118126
}
119127
fmt.Fprintf(os.Stdout, "Successfully pushed bundle to %s. Digest is %s.\n", cnabRef, descriptor.Digest)
120-
return nil
128+
return descriptor.Digest, nil
121129
}
122130

123131
func withAppAnnotations(index *ocischemav1.Index) error {

internal/packager/bundle.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ func MakeCNABImageName(appName, appVersion, suffix string) (string, error) {
7373
}
7474

7575
// PersistInBundleStore do store a bundle with optional reference and return it's ID
76-
func PersistInBundleStore(ref reference.Reference, bndl *bundle.Bundle) (reference.Digested, error) {
76+
func PersistInBundleStore(bndl *bundle.Bundle, ref reference.Named) (store.ID, error) {
7777
appstore, err := store.NewApplicationStore(config.Dir())
7878
if err != nil {
79-
return nil, err
79+
return store.ID(""), err
8080
}
8181
bundleStore, err := appstore.BundleStore()
8282
if err != nil {
83-
return nil, err
83+
return store.ID(""), err
8484
}
85-
return bundleStore.Store(ref, relocated.FromBundle(bndl))
85+
return bundleStore.Store(relocated.FromBundle(bndl), ref)
8686
}
8787

8888
func GetNamedTagged(tag string) (reference.NamedTagged, error) {

0 commit comments

Comments
 (0)