Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement publish #11008

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 115 additions & 3 deletions pkg/compose/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,136 @@

import (
"context"
"encoding/json"
"os"

"github.com/compose-spec/compose-go/types"
"github.com/distribution/reference"
"github.com/docker/buildx/util/imagetools"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/progress"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

func (s *composeService) Publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error {
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
return s.publish(ctx, project, repository, options)
}, s.stdinfo(), "Publishing")

Check warning on line 37 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L35-L37

Added lines #L35 - L37 were not covered by tests
}

func (s *composeService) publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error {

Check warning on line 40 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L40

Added line #L40 was not covered by tests
err := s.Push(ctx, project, api.PushOptions{})
if err != nil {
return err
}

_, err = reference.ParseDockerRef(repository)
w := progress.ContextWriter(ctx)

named, err := reference.ParseDockerRef(repository)
if err != nil {
return err
}

Check warning on line 51 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L46-L51

Added lines #L46 - L51 were not covered by tests

resolver := imagetools.New(imagetools.Opt{
Auth: s.configFile(),
})

var layers []v1.Descriptor
for _, file := range project.ComposeFiles {
f, err := os.ReadFile(file)
if err != nil {
return err
}

Check warning on line 62 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L53-L62

Added lines #L53 - L62 were not covered by tests

w.Event(progress.Event{
ID: file,
Text: "publishing",
Status: progress.Working,
})
layer := v1.Descriptor{
MediaType: "application/vnd.docker.compose.file+yaml",
Digest: digest.FromString(string(f)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Digest: digest.FromString(string(f)),
Digest: digest.FromBytes(f),

Internally, digest.FromString will just convert back to bytes so might as well pass it directly

Size: int64(len(f)),
Annotations: map[string]string{
"com.docker.compose": api.ComposeVersion,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

com.docker.compose.version perhaps?

},
}
layers = append(layers, layer)
err = resolver.Push(ctx, named, layer, f)
if err != nil {
w.Event(progress.Event{
ID: file,
Text: "publishing",
Status: progress.Error,
})

return err
}

Check warning on line 87 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L64-L87

Added lines #L64 - L87 were not covered by tests

w.Event(progress.Event{
ID: file,
Text: "published",
Status: progress.Done,
})

Check warning on line 93 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L89-L93

Added lines #L89 - L93 were not covered by tests
}

emptyConfig, err := json.Marshal(v1.ImageConfig{})
if err != nil {
return err
}
configDescriptor := v1.Descriptor{
MediaType: "application/vnd.docker.compose.project",
Digest: digest.FromBytes(emptyConfig),
Size: int64(len(emptyConfig)),
Annotations: map[string]string{
"com.docker.compose.version": api.ComposeVersion,
},
}
err = resolver.Push(ctx, named, configDescriptor, emptyConfig)
if err != nil {
return err
}

Check warning on line 111 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L96-L111

Added lines #L96 - L111 were not covered by tests

imageManifest, err := json.Marshal(v1.Manifest{
Versioned: specs.Versioned{SchemaVersion: 2},
MediaType: v1.MediaTypeImageManifest,
ArtifactType: "application/vnd.docker.compose.project",
Config: configDescriptor,
Layers: layers,
})

Check warning on line 119 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L113-L119

Added lines #L113 - L119 were not covered by tests
if err != nil {
return err
}

// TODO publish project.ComposeFiles
w.Event(progress.Event{
ID: repository,
Text: "publishing",
Status: progress.Working,
})

Check warning on line 128 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L124-L128

Added lines #L124 - L128 were not covered by tests

return api.ErrNotImplemented
err = resolver.Push(ctx, named, v1.Descriptor{
MediaType: v1.MediaTypeImageManifest,
Digest: digest.FromString(string(imageManifest)),
Size: int64(len(imageManifest)),
Annotations: map[string]string{
"com.docker.compose.version": api.ComposeVersion,
},
ArtifactType: "application/vnd.docker.compose.project",
}, imageManifest)
if err != nil {
w.Event(progress.Event{
ID: repository,
Text: "publishing",
Status: progress.Error,
})
return err
}
w.Event(progress.Event{
ID: repository,
Text: "published",
Status: progress.Done,
})
return nil

Check warning on line 152 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L130-L152

Added lines #L130 - L152 were not covered by tests
}
7 changes: 6 additions & 1 deletion pkg/remote/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
offline bool
}

const prefix = "oci:"
const prefix = "oci://"

func (g ociRemoteLoader) Accept(path string) bool {
return strings.HasPrefix(path, prefix)
Expand Down Expand Up @@ -117,6 +117,11 @@
if err != nil {
return "", err
}

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

Check warning on line 123 in pkg/remote/oci.go

View check run for this annotation

Codecov / codecov/patch

pkg/remote/oci.go#L121-L123

Added lines #L121 - L123 were not covered by tests

for i, layer := range descriptor.Layers {
digested, err := reference.WithDigest(ref, layer.Digest)
if err != nil {
Expand Down