From 26064d4b60a720967d3247962b91779f730b2995 Mon Sep 17 00:00:00 2001 From: Guillaume Lours <705411+glours@users.noreply.github.com> Date: Fri, 18 Oct 2024 09:44:14 +0200 Subject: [PATCH] allow usage of -f flag with oci Compose artifact Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com> --- cmd/compose/publish.go | 2 +- docs/reference/docker_compose_alpha_publish.yaml | 2 +- internal/ocipush/push.go | 1 + pkg/api/api.go | 1 + pkg/compose/publish.go | 2 +- pkg/compose/push.go | 3 +++ pkg/remote/oci.go | 7 +++++-- 7 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cmd/compose/publish.go b/cmd/compose/publish.go index 42b0140b5..6740986a2 100644 --- a/cmd/compose/publish.go +++ b/cmd/compose/publish.go @@ -36,7 +36,7 @@ func publishCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Servic ProjectOptions: p, } cmd := &cobra.Command{ - Use: "publish [OPTIONS] [REPOSITORY]", + Use: "publish [OPTIONS] REPOSITORY[:TAG]", Short: "Publish compose application", RunE: Adapt(func(ctx context.Context, args []string) error { return runPublish(ctx, dockerCli, backend, opts, args[0]) diff --git a/docs/reference/docker_compose_alpha_publish.yaml b/docs/reference/docker_compose_alpha_publish.yaml index 38868104a..7a2da5ca9 100644 --- a/docs/reference/docker_compose_alpha_publish.yaml +++ b/docs/reference/docker_compose_alpha_publish.yaml @@ -1,7 +1,7 @@ command: docker compose alpha publish short: Publish compose application long: Publish compose application -usage: docker compose alpha publish [OPTIONS] [REPOSITORY] +usage: docker compose alpha publish [OPTIONS] REPOSITORY[:TAG] pname: docker compose alpha plink: docker_compose_alpha.yaml options: diff --git a/internal/ocipush/push.go b/internal/ocipush/push.go index 0296f67da..d9a7f6ec0 100644 --- a/internal/ocipush/push.go +++ b/internal/ocipush/push.go @@ -178,6 +178,7 @@ func generateManifest(layers []v1.Descriptor, ociCompat api.OCIVersion) ([]Pusha case api.OCIVersion1_1: config = v1.DescriptorEmptyJSON artifactType = ComposeProjectArtifactType + config.ArtifactType = artifactType // N.B. the descriptor has the data embedded in it toPush = append(toPush, Pushable{Descriptor: config, Data: make([]byte, len(config.Data))}) default: diff --git a/pkg/api/api.go b/pkg/api/api.go index e48cde46d..b829f139b 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -291,6 +291,7 @@ type ConfigOptions struct { type PushOptions struct { Quiet bool IgnoreFailures bool + ImageMandatory bool } // PullOptions group options of the Pull API diff --git a/pkg/compose/publish.go b/pkg/compose/publish.go index 3cab72d5b..d1c4cd103 100644 --- a/pkg/compose/publish.go +++ b/pkg/compose/publish.go @@ -35,7 +35,7 @@ func (s *composeService) Publish(ctx context.Context, project *types.Project, re } func (s *composeService) publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error { - err := s.Push(ctx, project, api.PushOptions{}) + err := s.Push(ctx, project, api.PushOptions{IgnoreFailures: true, ImageMandatory: true}) if err != nil { return err } diff --git a/pkg/compose/push.go b/pkg/compose/push.go index 63cc47526..86698194d 100644 --- a/pkg/compose/push.go +++ b/pkg/compose/push.go @@ -62,6 +62,9 @@ func (s *composeService) push(ctx context.Context, project *types.Project, optio w := progress.ContextWriter(ctx) for _, service := range project.Services { if service.Build == nil || service.Image == "" { + if options.ImageMandatory && service.Image == "" { + return fmt.Errorf("%q attribut is mandatory to push an image for service %q", "service.image", service.Name) + } w.Event(progress.Event{ ID: service.Name, Status: progress.Done, diff --git a/pkg/remote/oci.go b/pkg/remote/oci.go index efd395919..a9d0e0936 100644 --- a/pkg/remote/oci.go +++ b/pkg/remote/oci.go @@ -30,6 +30,7 @@ import ( "github.com/docker/buildx/store/storeutil" "github.com/docker/buildx/util/imagetools" "github.com/docker/cli/cli/command" + "github.com/docker/compose/v2/internal/ocipush" v1 "github.com/opencontainers/image-spec/specs-go/v1" ) @@ -113,6 +114,8 @@ func (g ociRemoteLoader) Load(ctx context.Context, path string) (string, error) err2 := g.pullComposeFiles(ctx, local, composeFile, manifest, ref, resolver) if err2 != nil { + // we need to clean up the directory to be sure we won't let empty files present + _ = os.RemoveAll(local) return "", err2 } } @@ -137,8 +140,8 @@ func (g ociRemoteLoader) pullComposeFiles(ctx context.Context, local string, com return err } defer f.Close() //nolint:errcheck - - if manifest.ArtifactType != "application/vnd.docker.compose.project" { + if (manifest.ArtifactType != "" && manifest.ArtifactType != ocipush.ComposeProjectArtifactType) || + (manifest.ArtifactType == "" && manifest.Config.MediaType != ocipush.ComposeEmptyConfigMediaType) { return fmt.Errorf("%s is not a compose project OCI artifact, but %s", ref.String(), manifest.ArtifactType) }