Skip to content

Commit

Permalink
move image digests resolution to backend
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Nov 30, 2022
1 parent fb5b90e commit 1f2f68f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
22 changes: 3 additions & 19 deletions cmd/compose/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions pkg/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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()
}
Expand Down Expand Up @@ -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, "", " ")
Expand Down

0 comments on commit 1f2f68f

Please sign in to comment.