Skip to content

Commit 9ac4f69

Browse files
committed
move image digests resolution to backend
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 2bef976 commit 9ac4f69

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

cmd/compose/convert.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ import (
2626
"sort"
2727
"strings"
2828

29-
"github.com/cnabio/cnab-to-oci/remotes"
3029
"github.com/compose-spec/compose-go/cli"
3130
"github.com/compose-spec/compose-go/types"
32-
"github.com/distribution/distribution/v3/reference"
33-
cliconfig "github.com/docker/cli/cli/config"
34-
"github.com/opencontainers/go-digest"
3531
"github.com/spf13/cobra"
3632

3733
"github.com/docker/compose/v2/pkg/api"
@@ -127,22 +123,10 @@ func runConvert(ctx context.Context, backend api.Service, opts convertOptions, s
127123
return err
128124
}
129125

130-
if opts.resolveImageDigests {
131-
configFile := cliconfig.LoadDefaultConfigFile(os.Stderr)
132-
133-
resolver := remotes.CreateResolver(configFile)
134-
err = project.ResolveImages(func(named reference.Named) (digest.Digest, error) {
135-
_, desc, err := resolver.Resolve(ctx, named.String())
136-
return desc.Digest, err
137-
})
138-
if err != nil {
139-
return err
140-
}
141-
}
142-
143126
content, err = backend.Convert(ctx, project, api.ConvertOptions{
144-
Format: opts.Format,
145-
Output: opts.Output,
127+
Format: opts.Format,
128+
Output: opts.Output,
129+
ResolveImageDigests: opts.resolveImageDigests,
146130
})
147131
if err != nil {
148132
return err

pkg/api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ type ConvertOptions struct {
179179
Format string
180180
// Output defines the path to save the application model
181181
Output string
182+
// Resolve image reference to digests
183+
ResolveImageDigests bool
182184
}
183185

184186
// PushOptions group options of the Push API

pkg/compose/compose.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,19 @@ import (
2323
"io"
2424
"strings"
2525

26-
"gopkg.in/yaml.v2"
27-
26+
"github.com/cnabio/cnab-to-oci/remotes"
2827
"github.com/compose-spec/compose-go/types"
28+
"github.com/distribution/distribution/v3/reference"
2929
"github.com/docker/cli/cli/command"
3030
"github.com/docker/cli/cli/config/configfile"
31+
registry "github.com/docker/cli/cli/registry/client"
3132
"github.com/docker/cli/cli/streams"
3233
moby "github.com/docker/docker/api/types"
3334
"github.com/docker/docker/api/types/filters"
3435
"github.com/docker/docker/client"
36+
"github.com/opencontainers/go-digest"
3537
"github.com/pkg/errors"
38+
"gopkg.in/yaml.v2"
3639

3740
"github.com/docker/compose/v2/pkg/api"
3841
)
@@ -52,6 +55,10 @@ func (s *composeService) apiClient() client.APIClient {
5255
return s.dockerCli.Client()
5356
}
5457

58+
func (s *composeService) registryClient() registry.RegistryClient {
59+
return s.dockerCli.RegistryClient(false)
60+
}
61+
5562
func (s *composeService) configFile() *configfile.ConfigFile {
5663
return s.dockerCli.ConfigFile()
5764
}
@@ -93,6 +100,18 @@ func getContainerNameWithoutProject(c moby.Container) string {
93100
}
94101

95102
func (s *composeService) Convert(ctx context.Context, project *types.Project, options api.ConvertOptions) ([]byte, error) {
103+
if options.ResolveImageDigests {
104+
// TODO use dockercli.RegistryClient instead
105+
resolver := remotes.CreateResolver(s.configFile())
106+
err := project.ResolveImages(func(named reference.Named) (digest.Digest, error) {
107+
_, desc, err := resolver.Resolve(ctx, named.String())
108+
return desc.Digest, err
109+
})
110+
if err != nil {
111+
return nil, err
112+
}
113+
}
114+
96115
switch options.Format {
97116
case "json":
98117
return json.MarshalIndent(project, "", " ")

0 commit comments

Comments
 (0)