Skip to content

Commit 9c4a216

Browse files
committed
use containerd client for OCI operations
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 8978c10 commit 9c4a216

File tree

4 files changed

+92
-42
lines changed

4 files changed

+92
-42
lines changed

internal/ocipush/push.go renamed to internal/oci/push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
package ocipush
17+
package oci
1818

1919
import (
2020
"context"

internal/oci/resolver.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2023 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package oci
18+
19+
import (
20+
"context"
21+
"io"
22+
23+
"github.com/containerd/containerd/v2/core/remotes"
24+
"github.com/containerd/containerd/v2/core/remotes/docker"
25+
"github.com/distribution/reference"
26+
"github.com/docker/cli/cli/config/configfile"
27+
"github.com/docker/compose/v2/internal/registry"
28+
"github.com/opencontainers/image-spec/specs-go/v1"
29+
)
30+
31+
// NewResolver setup an OCI Resolver based on docker/cli config to provide registry credentials
32+
func NewResolver(config *configfile.ConfigFile) remotes.Resolver {
33+
return docker.NewResolver(docker.ResolverOptions{
34+
Hosts: docker.ConfigureDefaultRegistries(
35+
docker.WithAuthorizer(docker.NewDockerAuthorizer(
36+
docker.WithAuthCreds(func(host string) (string, string, error) {
37+
host = registry.GetAuthConfigKey(host)
38+
auth, err := config.GetAuthConfig(host)
39+
if err != nil {
40+
return "", "", err
41+
}
42+
if auth.IdentityToken != "" {
43+
return "", auth.IdentityToken, nil
44+
}
45+
return auth.Username, auth.Password, nil
46+
}),
47+
)),
48+
),
49+
})
50+
}
51+
52+
// Get retrieves a Named OCI resource and returns OCI Descriptor and Manifest
53+
func Get(ctx context.Context, resolver remotes.Resolver, ref reference.Named) (v1.Descriptor, []byte, error) {
54+
_, descriptor, err := resolver.Resolve(ctx, ref.String())
55+
if err != nil {
56+
return v1.Descriptor{}, nil, err
57+
}
58+
59+
fetcher, err := resolver.Fetcher(ctx, ref.String())
60+
if err != nil {
61+
return v1.Descriptor{}, nil, err
62+
}
63+
fetch, err := fetcher.Fetch(ctx, descriptor)
64+
if err != nil {
65+
return v1.Descriptor{}, nil, err
66+
}
67+
content, err := io.ReadAll(fetch)
68+
if err != nil {
69+
return v1.Descriptor{}, nil, err
70+
}
71+
return descriptor, content, nil
72+
}

pkg/compose/publish.go

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ import (
2929
"github.com/DefangLabs/secret-detector/pkg/secrets"
3030
"github.com/compose-spec/compose-go/v2/loader"
3131
"github.com/compose-spec/compose-go/v2/types"
32-
"github.com/containerd/containerd/v2/core/remotes/docker"
3332
"github.com/distribution/reference"
3433
"github.com/docker/cli/cli/command"
35-
"github.com/docker/compose/v2/internal/ocipush"
36-
"github.com/docker/compose/v2/internal/registry"
34+
"github.com/docker/compose/v2/internal/oci"
3735
"github.com/docker/compose/v2/pkg/api"
3836
"github.com/docker/compose/v2/pkg/compose/transform"
3937
"github.com/docker/compose/v2/pkg/progress"
@@ -67,23 +65,7 @@ func (s *composeService) publish(ctx context.Context, project *types.Project, re
6765

6866
config := s.dockerCli.ConfigFile()
6967

70-
resolver := docker.NewResolver(docker.ResolverOptions{
71-
Hosts: docker.ConfigureDefaultRegistries(
72-
docker.WithAuthorizer(docker.NewDockerAuthorizer(
73-
docker.WithAuthCreds(func(host string) (string, string, error) {
74-
host = registry.GetAuthConfigKey(host)
75-
auth, err := config.GetAuthConfig(host)
76-
if err != nil {
77-
return "", "", err
78-
}
79-
if auth.IdentityToken != "" {
80-
return "", auth.IdentityToken, nil
81-
}
82-
return auth.Username, auth.Password, nil
83-
}),
84-
)),
85-
),
86-
})
68+
resolver := oci.NewResolver(config)
8769

8870
var layers []v1.Descriptor
8971
extFiles := map[string]string{}
@@ -93,7 +75,7 @@ func (s *composeService) publish(ctx context.Context, project *types.Project, re
9375
return err
9476
}
9577

96-
layerDescriptor := ocipush.DescriptorForComposeFile(file, data)
78+
layerDescriptor := oci.DescriptorForComposeFile(file, data)
9779
layers = append(layers, layerDescriptor)
9880
}
9981

@@ -113,7 +95,7 @@ func (s *composeService) publish(ctx context.Context, project *types.Project, re
11395
return err
11496
}
11597

116-
layerDescriptor := ocipush.DescriptorForComposeFile("image-digests.yaml", yaml)
98+
layerDescriptor := oci.DescriptorForComposeFile("image-digests.yaml", yaml)
11799
layers = append(layers, layerDescriptor)
118100
}
119101

@@ -124,7 +106,7 @@ func (s *composeService) publish(ctx context.Context, project *types.Project, re
124106
Status: progress.Working,
125107
})
126108
if !s.dryRun {
127-
err = ocipush.PushManifest(ctx, resolver, named, layers, options.OCIVersion)
109+
err = oci.PushManifest(ctx, resolver, named, layers, options.OCIVersion)
128110
if err != nil {
129111
w.Event(progress.Event{
130112
ID: repository,
@@ -151,7 +133,7 @@ func processExtends(ctx context.Context, project *types.Project, extFiles map[st
151133
return nil, err
152134
}
153135

154-
layerDescriptor := ocipush.DescriptorForComposeFile(hash, data)
136+
layerDescriptor := oci.DescriptorForComposeFile(hash, data)
155137
layerDescriptor.Annotations["com.docker.compose.extends"] = "true"
156138
layers = append(layers, layerDescriptor)
157139
}
@@ -360,7 +342,7 @@ func envFileLayers(project *types.Project) []v1.Descriptor {
360342
// if we can't read the file, skip to the next one
361343
continue
362344
}
363-
layerDescriptor := ocipush.DescriptorForEnvFile(envFile.Path, f)
345+
layerDescriptor := oci.DescriptorForEnvFile(envFile.Path, f)
364346
layers = append(layers, layerDescriptor)
365347
}
366348
}

pkg/remote/oci.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ import (
2626
"strings"
2727

2828
"github.com/compose-spec/compose-go/v2/loader"
29+
"github.com/containerd/containerd/v2/core/remotes"
2930
"github.com/distribution/reference"
30-
"github.com/docker/buildx/store/storeutil"
31-
"github.com/docker/buildx/util/imagetools"
3231
"github.com/docker/cli/cli/command"
33-
"github.com/docker/compose/v2/internal/ocipush"
32+
"github.com/docker/compose/v2/internal/oci"
3433
v1 "github.com/opencontainers/image-spec/specs-go/v1"
3534
)
3635

@@ -88,13 +87,9 @@ func (g ociRemoteLoader) Load(ctx context.Context, path string) (string, error)
8887
return "", err
8988
}
9089

91-
opt, err := storeutil.GetImageConfig(g.dockerCli, nil)
92-
if err != nil {
93-
return "", err
94-
}
95-
resolver := imagetools.New(opt)
90+
resolver := oci.NewResolver(g.dockerCli.ConfigFile())
9691

97-
content, descriptor, err := resolver.Get(ctx, ref.String())
92+
descriptor, content, err := oci.Get(ctx, resolver, ref)
9893
if err != nil {
9994
return "", err
10095
}
@@ -128,7 +123,7 @@ func (g ociRemoteLoader) Dir(path string) string {
128123
return g.known[path]
129124
}
130125

131-
func (g ociRemoteLoader) pullComposeFiles(ctx context.Context, local string, manifest v1.Manifest, ref reference.Named, resolver *imagetools.Resolver) error { //nolint:gocyclo
126+
func (g ociRemoteLoader) pullComposeFiles(ctx context.Context, local string, manifest v1.Manifest, ref reference.Named, resolver remotes.Resolver) error { //nolint:gocyclo
132127
err := os.MkdirAll(local, 0o700)
133128
if err != nil {
134129
return err
@@ -139,8 +134,8 @@ func (g ociRemoteLoader) pullComposeFiles(ctx context.Context, local string, man
139134
return err
140135
}
141136
defer f.Close() //nolint:errcheck
142-
if (manifest.ArtifactType != "" && manifest.ArtifactType != ocipush.ComposeProjectArtifactType) ||
143-
(manifest.ArtifactType == "" && manifest.Config.MediaType != ocipush.ComposeEmptyConfigMediaType) {
137+
if (manifest.ArtifactType != "" && manifest.ArtifactType != oci.ComposeProjectArtifactType) ||
138+
(manifest.ArtifactType == "" && manifest.Config.MediaType != oci.ComposeEmptyConfigMediaType) {
144139
return fmt.Errorf("%s is not a compose project OCI artifact, but %s", ref.String(), manifest.ArtifactType)
145140
}
146141

@@ -149,13 +144,14 @@ func (g ociRemoteLoader) pullComposeFiles(ctx context.Context, local string, man
149144
if err != nil {
150145
return err
151146
}
152-
content, _, err := resolver.Get(ctx, digested.String())
147+
148+
_, content, err := oci.Get(ctx, resolver, digested)
153149
if err != nil {
154150
return err
155151
}
156152

157153
switch layer.MediaType {
158-
case ocipush.ComposeYAMLMediaType:
154+
case oci.ComposeYAMLMediaType:
159155
target := f
160156
_, extends := layer.Annotations["com.docker.compose.extends"]
161157
if extends {
@@ -167,11 +163,11 @@ func (g ociRemoteLoader) pullComposeFiles(ctx context.Context, local string, man
167163
if err := writeComposeFile(layer, i, target, content); err != nil {
168164
return err
169165
}
170-
case ocipush.ComposeEnvFileMediaType:
166+
case oci.ComposeEnvFileMediaType:
171167
if err := writeEnvFile(layer, local, content); err != nil {
172168
return err
173169
}
174-
case ocipush.ComposeEmptyConfigMediaType:
170+
case oci.ComposeEmptyConfigMediaType:
175171
}
176172
}
177173
return nil

0 commit comments

Comments
 (0)