move image digests resolution to backend

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2022-11-29 09:59:56 +01:00 committed by Nicolas De loof
parent 2bef9769e5
commit 9ac4f69918
3 changed files with 26 additions and 21 deletions

View File

@ -26,12 +26,8 @@ import (
"sort"
"strings"
"github.com/cnabio/cnab-to-oci/remotes"
"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/types"
"github.com/distribution/distribution/v3/reference"
cliconfig "github.com/docker/cli/cli/config"
"github.com/opencontainers/go-digest"
"github.com/spf13/cobra"
"github.com/docker/compose/v2/pkg/api"
@ -127,22 +123,10 @@ func runConvert(ctx context.Context, backend api.Service, opts convertOptions, s
return err
}
if opts.resolveImageDigests {
configFile := cliconfig.LoadDefaultConfigFile(os.Stderr)
resolver := remotes.CreateResolver(configFile)
err = project.ResolveImages(func(named reference.Named) (digest.Digest, error) {
_, desc, err := resolver.Resolve(ctx, named.String())
return desc.Digest, err
})
if err != nil {
return err
}
}
content, err = backend.Convert(ctx, project, api.ConvertOptions{
Format: opts.Format,
Output: opts.Output,
Format: opts.Format,
Output: opts.Output,
ResolveImageDigests: opts.resolveImageDigests,
})
if err != nil {
return err

View File

@ -179,6 +179,8 @@ type ConvertOptions struct {
Format string
// Output defines the path to save the application model
Output string
// Resolve image reference to digests
ResolveImageDigests bool
}
// PushOptions group options of the Push API

View File

@ -23,16 +23,19 @@ import (
"io"
"strings"
"gopkg.in/yaml.v2"
"github.com/cnabio/cnab-to-oci/remotes"
"github.com/compose-spec/compose-go/types"
"github.com/distribution/distribution/v3/reference"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/config/configfile"
registry "github.com/docker/cli/cli/registry/client"
"github.com/docker/cli/cli/streams"
moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"github.com/docker/compose/v2/pkg/api"
)
@ -52,6 +55,10 @@ func (s *composeService) apiClient() client.APIClient {
return s.dockerCli.Client()
}
func (s *composeService) registryClient() registry.RegistryClient {
return s.dockerCli.RegistryClient(false)
}
func (s *composeService) configFile() *configfile.ConfigFile {
return s.dockerCli.ConfigFile()
}
@ -93,6 +100,18 @@ func getContainerNameWithoutProject(c moby.Container) string {
}
func (s *composeService) Convert(ctx context.Context, project *types.Project, options api.ConvertOptions) ([]byte, error) {
if options.ResolveImageDigests {
// TODO use dockercli.RegistryClient instead
resolver := remotes.CreateResolver(s.configFile())
err := project.ResolveImages(func(named reference.Named) (digest.Digest, error) {
_, desc, err := resolver.Resolve(ctx, named.String())
return desc.Digest, err
})
if err != nil {
return nil, err
}
}
switch options.Format {
case "json":
return json.MarshalIndent(project, "", " ")