From eb4249ece4886de93d7c9ee54723aad77d77e560 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Mon, 22 Jan 2024 12:27:38 +0100 Subject: [PATCH] bump compose-go to v2.0.0-rc.2 Signed-off-by: Nicolas De Loof --- go.mod | 2 +- go.sum | 4 +-- pkg/remote/git.go | 49 +++++++++++++++++++------------- pkg/remote/oci.go | 71 +++++++++++++++++++++++++++-------------------- 4 files changed, 73 insertions(+), 53 deletions(-) diff --git a/go.mod b/go.mod index 15b45f49f..5fb8b34b7 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/AlecAivazis/survey/v2 v2.3.7 github.com/Microsoft/go-winio v0.6.1 github.com/buger/goterm v1.0.4 - github.com/compose-spec/compose-go/v2 v2.0.0-rc.1 + github.com/compose-spec/compose-go/v2 v2.0.0-rc.2 github.com/containerd/console v1.0.3 github.com/containerd/containerd v1.7.12 github.com/davecgh/go-spew v1.1.1 diff --git a/go.sum b/go.sum index 8515dfec7..5ebce7fc8 100644 --- a/go.sum +++ b/go.sum @@ -86,8 +86,8 @@ github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+g github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE= github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4= -github.com/compose-spec/compose-go/v2 v2.0.0-rc.1 h1:0nnSpdYg29uaQOU/YJ1YSgYgwkQr/XNZ0QAFtEPTtIA= -github.com/compose-spec/compose-go/v2 v2.0.0-rc.1/go.mod h1:IVsvFyGVhw4FASzUtlWNVaAOhYmakXAFY9IlZ7LAuD8= +github.com/compose-spec/compose-go/v2 v2.0.0-rc.2 h1:eJ01FpliL/02KvsaPyH1bSLbM1S70yWQUojHVRbyvy4= +github.com/compose-spec/compose-go/v2 v2.0.0-rc.2/go.mod h1:IVsvFyGVhw4FASzUtlWNVaAOhYmakXAFY9IlZ7LAuD8= github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM= github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw= github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw= diff --git a/pkg/remote/git.go b/pkg/remote/git.go index 4d42007cc..869165372 100644 --- a/pkg/remote/git.go +++ b/pkg/remote/git.go @@ -48,11 +48,13 @@ func gitRemoteLoaderEnabled() (bool, error) { func NewGitRemoteLoader(offline bool) loader.ResourceLoader { return gitRemoteLoader{ offline: offline, + known: map[string]string{}, } } type gitRemoteLoader struct { offline bool + known map[string]string } func (g gitRemoteLoader) Accept(path string) bool { @@ -76,31 +78,34 @@ func (g gitRemoteLoader) Load(ctx context.Context, path string) (string, error) return "", err } - if ref.Commit == "" { - ref.Commit = "HEAD" // default branch - } - - err = g.resolveGitRef(ctx, path, ref) - if err != nil { - return "", err - } - - cache, err := cacheDir() - if err != nil { - return "", fmt.Errorf("initializing remote resource cache: %w", err) - } - - local := filepath.Join(cache, ref.Commit) - if _, err := os.Stat(local); os.IsNotExist(err) { - if g.offline { - return "", nil + local, ok := g.known[path] + if !ok { + if ref.Commit == "" { + ref.Commit = "HEAD" // default branch } - err = g.checkout(ctx, local, ref) + + err = g.resolveGitRef(ctx, path, ref) if err != nil { return "", err } - } + cache, err := cacheDir() + if err != nil { + return "", fmt.Errorf("initializing remote resource cache: %w", err) + } + + local = filepath.Join(cache, ref.Commit) + if _, err := os.Stat(local); os.IsNotExist(err) { + if g.offline { + return "", nil + } + err = g.checkout(ctx, local, ref) + if err != nil { + return "", err + } + } + g.known[path] = local + } if ref.SubDir != "" { local = filepath.Join(local, ref.SubDir) } @@ -114,6 +119,10 @@ func (g gitRemoteLoader) Load(ctx context.Context, path string) (string, error) return local, err } +func (g gitRemoteLoader) Dir(path string) string { + return g.known[path] +} + func (g gitRemoteLoader) resolveGitRef(ctx context.Context, path string, ref *gitutil.GitRef) error { if !commitSHA.MatchString(ref.Commit) { cmd := exec.CommandContext(ctx, "git", "ls-remote", "--exit-code", ref.Remote, ref.Commit) diff --git a/pkg/remote/oci.go b/pkg/remote/oci.go index d8e64509c..efd395919 100644 --- a/pkg/remote/oci.go +++ b/pkg/remote/oci.go @@ -50,12 +50,14 @@ func NewOCIRemoteLoader(dockerCli command.Cli, offline bool) loader.ResourceLoad return ociRemoteLoader{ dockerCli: dockerCli, offline: offline, + known: map[string]string{}, } } type ociRemoteLoader struct { dockerCli command.Cli offline bool + known map[string]string } const prefix = "oci://" @@ -77,42 +79,51 @@ func (g ociRemoteLoader) Load(ctx context.Context, path string) (string, error) return "", nil } - ref, err := reference.ParseDockerRef(path[len(prefix):]) - if err != nil { - return "", err - } - - opt, err := storeutil.GetImageConfig(g.dockerCli, nil) - if err != nil { - return "", err - } - resolver := imagetools.New(opt) - - content, descriptor, err := resolver.Get(ctx, ref.String()) - if err != nil { - return "", err - } - - cache, err := cacheDir() - if err != nil { - return "", fmt.Errorf("initializing remote resource cache: %w", err) - } - - local := filepath.Join(cache, descriptor.Digest.Hex()) - composeFile := filepath.Join(local, "compose.yaml") - if _, err = os.Stat(local); os.IsNotExist(err) { - var manifest v1.Manifest - err = json.Unmarshal(content, &manifest) + local, ok := g.known[path] + if !ok { + ref, err := reference.ParseDockerRef(path[len(prefix):]) if err != nil { return "", err } - err2 := g.pullComposeFiles(ctx, local, composeFile, manifest, ref, resolver) - if err2 != nil { - return "", err2 + opt, err := storeutil.GetImageConfig(g.dockerCli, nil) + if err != nil { + return "", err } + resolver := imagetools.New(opt) + + content, descriptor, err := resolver.Get(ctx, ref.String()) + if err != nil { + return "", err + } + + cache, err := cacheDir() + if err != nil { + return "", fmt.Errorf("initializing remote resource cache: %w", err) + } + + local = filepath.Join(cache, descriptor.Digest.Hex()) + composeFile := filepath.Join(local, "compose.yaml") + if _, err = os.Stat(local); os.IsNotExist(err) { + var manifest v1.Manifest + err = json.Unmarshal(content, &manifest) + if err != nil { + return "", err + } + + err2 := g.pullComposeFiles(ctx, local, composeFile, manifest, ref, resolver) + if err2 != nil { + return "", err2 + } + } + g.known[path] = local } - return composeFile, nil + + return filepath.Join(local, "compose.yaml"), nil +} + +func (g ociRemoteLoader) Dir(path string) string { + return g.known[path] } func (g ociRemoteLoader) pullComposeFiles(ctx context.Context, local string, composeFile string, manifest v1.Manifest, ref reference.Named, resolver *imagetools.Resolver) error {