Skip to content

Commit

Permalink
Merge pull request #11051 from ndeloof/warn_remote_disabled
Browse files Browse the repository at this point in the history
warn user remote resource is disabled
  • Loading branch information
glours authored Oct 18, 2023
2 parents 818bc3c + 3d0207e commit 5e1d3f2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 54 deletions.
20 changes: 4 additions & 16 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,29 +254,17 @@ func (o *ProjectOptions) ToProject(dockerCli command.Cli, services []string, po
}

func (o *ProjectOptions) configureRemoteLoaders(dockerCli command.Cli, po []cli.ProjectOptionsFn) ([]cli.ProjectOptionsFn, error) {
enabled, err := remote.GitRemoteLoaderEnabled()
git, err := remote.NewGitRemoteLoader(o.Offline)
if err != nil {
return nil, err
}
if enabled {
git, err := remote.NewGitRemoteLoader(o.Offline)
if err != nil {
return nil, err
}
po = append(po, cli.WithResourceLoader(git))
}

enabled, err = remote.OCIRemoteLoaderEnabled()
oci, err := remote.NewOCIRemoteLoader(dockerCli, o.Offline)
if err != nil {
return nil, err
}
if enabled {
git, err := remote.NewOCIRemoteLoader(dockerCli, o.Offline)
if err != nil {
return nil, err
}
po = append(po, cli.WithResourceLoader(git))
}

po = append(po, cli.WithResourceLoader(git), cli.WithResourceLoader(oci))
return po, nil
}

Expand Down
15 changes: 12 additions & 3 deletions pkg/remote/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ import (
"strconv"

"github.com/adrg/xdg"

"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/types"
"github.com/docker/compose/v2/pkg/api"
"github.com/moby/buildkit/util/gitutil"
)

func GitRemoteLoaderEnabled() (bool, error) {
if v := os.Getenv("COMPOSE_EXPERIMENTAL_GIT_REMOTE"); v != "" {
const GIT_REMOTE_ENABLED = "COMPOSE_EXPERIMENTAL_GIT_REMOTE"

func gitRemoteLoaderEnabled() (bool, error) {
if v := os.Getenv(GIT_REMOTE_ENABLED); v != "" {
enabled, err := strconv.ParseBool(v)
if err != nil {
return false, fmt.Errorf("COMPOSE_EXPERIMENTAL_GIT_REMOTE environment variable expects boolean value: %w", err)
Expand Down Expand Up @@ -74,6 +75,14 @@ func (g gitRemoteLoader) Accept(path string) bool {
var commitSHA = regexp.MustCompile(`^[a-f0-9]{40}$`)

func (g gitRemoteLoader) Load(ctx context.Context, path string) (string, error) {
enabled, err := gitRemoteLoaderEnabled()
if err != nil {
return "", err
}
if !enabled {
return "", fmt.Errorf("experimental git remote resource is disabled. %q must be set", GIT_REMOTE_ENABLED)
}

ref, err := gitutil.ParseGitRef(path)
if err != nil {
return "", err
Expand Down
86 changes: 51 additions & 35 deletions pkg/remote/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ import (
"strings"

"github.com/adrg/xdg"
"github.com/compose-spec/compose-go/loader"
"github.com/distribution/reference"
"github.com/docker/buildx/store/storeutil"
"github.com/docker/buildx/util/imagetools"
"github.com/docker/cli/cli/command"
v1 "github.com/opencontainers/image-spec/specs-go/v1"

"github.com/compose-spec/compose-go/loader"
)

func OCIRemoteLoaderEnabled() (bool, error) {
if v := os.Getenv("COMPOSE_EXPERIMENTAL_OCI_REMOTE"); v != "" {
const OCI_REMOTE_ENABLED = "COMPOSE_EXPERIMENTAL_OCI_REMOTE"

func ociRemoteLoaderEnabled() (bool, error) {
if v := os.Getenv(OCI_REMOTE_ENABLED); v != "" {
enabled, err := strconv.ParseBool(v)
if err != nil {
return false, fmt.Errorf("COMPOSE_EXPERIMENTAL_OCI_REMOTE environment variable expects boolean value: %w", err)
Expand Down Expand Up @@ -76,6 +77,14 @@ func (g ociRemoteLoader) Accept(path string) bool {
}

func (g ociRemoteLoader) Load(ctx context.Context, path string) (string, error) {
enabled, err := ociRemoteLoaderEnabled()
if err != nil {
return "", err
}
if !enabled {
return "", fmt.Errorf("experimental OCI remote resource is disabled. %q must be set", OCI_REMOTE_ENABLED)
}

if g.offline {
return "", nil
}
Expand All @@ -99,50 +108,57 @@ func (g ociRemoteLoader) Load(ctx context.Context, path string) (string, error)
local := filepath.Join(g.cache, descriptor.Digest.Hex())
composeFile := filepath.Join(local, "compose.yaml")
if _, err = os.Stat(local); os.IsNotExist(err) {

err = os.MkdirAll(local, 0o700)
var manifest v1.Manifest
err = json.Unmarshal(content, &manifest)
if err != nil {
return "", err
}

f, err := os.Create(composeFile)
if err != nil {
return "", err
err2 := g.pullComposeFiles(ctx, local, composeFile, manifest, ref, resolver)
if err2 != nil {
return "", err2
}
defer f.Close() //nolint:errcheck
}
return composeFile, nil
}

var manifest v1.Manifest
err = json.Unmarshal(content, &manifest)
func (g ociRemoteLoader) pullComposeFiles(ctx context.Context, local string, composeFile string, manifest v1.Manifest, ref reference.Named, resolver *imagetools.Resolver) error {
err := os.MkdirAll(local, 0o700)
if err != nil {
return err
}

f, err := os.Create(composeFile)
if err != nil {
return err
}
defer f.Close() //nolint:errcheck

if manifest.ArtifactType != "application/vnd.docker.compose.project" {
return fmt.Errorf("%s is not a compose project OCI artifact, but %s", ref.String(), manifest.ArtifactType)
}

for i, layer := range manifest.Layers {
digested, err := reference.WithDigest(ref, layer.Digest)
if err != nil {
return "", err
return err
}

if manifest.ArtifactType != "application/vnd.docker.compose.project" {
return "", fmt.Errorf("%s is not a compose project OCI artifact, but %s", ref.String(), manifest.ArtifactType)
content, _, err := resolver.Get(ctx, digested.String())
if err != nil {
return err
}

for i, layer := range manifest.Layers {
digested, err := reference.WithDigest(ref, layer.Digest)
if err != nil {
return "", err
}
content, _, err := resolver.Get(ctx, digested.String())
if i > 0 {
_, err = f.Write([]byte("\n---\n"))
if err != nil {
return "", err
}
if i > 0 {
_, err = f.Write([]byte("\n---\n"))
if err != nil {
return "", err
}
}
_, err = f.Write(content)
if err != nil {
return "", err
return err
}
}
_, err = f.Write(content)
if err != nil {
return err
}
}
return composeFile, nil
return nil
}

var _ loader.ResourceLoader = ociRemoteLoader{}

0 comments on commit 5e1d3f2

Please sign in to comment.