Skip to content

Commit

Permalink
Merge pull request #11390 from ndeloof/compose-go-v2.0.0-rc.2
Browse files Browse the repository at this point in the history
bump compose-go to v2.0.0-rc.2
  • Loading branch information
glours authored Jan 22, 2024
2 parents 1e8241f + eb4249e commit 143ac0f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 51 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
49 changes: 29 additions & 20 deletions pkg/remote/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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)
Expand Down
67 changes: 39 additions & 28 deletions pkg/remote/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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://"
Expand All @@ -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
}
local, ok := g.known[path]
if !ok {
ref, err := reference.ParseDockerRef(path[len(prefix):])
if err != nil {
return "", err
}

cache, err := cacheDir()
if err != nil {
return "", fmt.Errorf("initializing remote resource cache: %w", err)
}
opt, err := storeutil.GetImageConfig(g.dockerCli, nil)
if err != nil {
return "", err
}
resolver := imagetools.New(opt)

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)
content, descriptor, err := resolver.Get(ctx, ref.String())
if err != nil {
return "", err
}

err2 := g.pullComposeFiles(ctx, local, composeFile, manifest, ref, resolver)
if err2 != nil {
return "", err2
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 {
Expand Down

0 comments on commit 143ac0f

Please sign in to comment.