Skip to content

Commit bca374a

Browse files
committed
Get media type key from context
Adds a method for setting a custom media type key prefix used by the fetch handler. This allows both overwriting a built-in prefix (for reasons?) as well as supplying a custom media type. I added this because I was getting an error on `FetchHandler` when pulling docker plugin images which have their own media type. Signed-off-by: Brian Goff <[email protected]>
1 parent e4dc2f6 commit bca374a

2 files changed

Lines changed: 98 additions & 3 deletions

File tree

remotes/handlers.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,35 @@ import (
3333
"github.com/sirupsen/logrus"
3434
)
3535

36+
type refKeyPrefix struct{}
37+
38+
// WithMediaTypeKeyPrefix adds a custom key prefix for a media type which is used when storing
39+
// data in the content store from the FetchHandler.
40+
//
41+
// Used in `MakeRefKey` to determine what the key prefix should be.
42+
func WithMediaTypeKeyPrefix(ctx context.Context, mediaType, prefix string) context.Context {
43+
var values map[string]string
44+
if v := ctx.Value(refKeyPrefix{}); v != nil {
45+
values = v.(map[string]string)
46+
} else {
47+
values = make(map[string]string)
48+
}
49+
50+
values[mediaType] = prefix
51+
return context.WithValue(ctx, refKeyPrefix{}, values)
52+
}
53+
3654
// MakeRefKey returns a unique reference for the descriptor. This reference can be
3755
// used to lookup ongoing processes related to the descriptor. This function
3856
// may look to the context to namespace the reference appropriately.
3957
func MakeRefKey(ctx context.Context, desc ocispec.Descriptor) string {
40-
// TODO(stevvooe): Need better remote key selection here. Should be a
41-
// product of the context, which may include information about the ongoing
42-
// fetch process.
58+
if v := ctx.Value(refKeyPrefix{}); v != nil {
59+
values := v.(map[string]string)
60+
if prefix := values[desc.MediaType]; prefix != "" {
61+
return prefix + "-" + desc.Digest.String()
62+
}
63+
}
64+
4365
switch desc.MediaType {
4466
case images.MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest:
4567
return "manifest-" + desc.Digest.String()

remotes/handlers_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright The containerd 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 remotes
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/containerd/containerd/images"
24+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
25+
)
26+
27+
func TestContextCustomKeyPrefix(t *testing.T) {
28+
ctx := context.Background()
29+
cmt := "testing/custom.media.type"
30+
ctx = WithMediaTypeKeyPrefix(ctx, images.MediaTypeDockerSchema2Layer, "bananas")
31+
ctx = WithMediaTypeKeyPrefix(ctx, cmt, "apples")
32+
33+
// makes sure tha even though we've supplied some custom handling, the built-in still works
34+
t.Run("normal supported case", func(t *testing.T) {
35+
desc := ocispec.Descriptor{MediaType: ocispec.MediaTypeImageLayer}
36+
expected := "layer-"
37+
38+
actual := MakeRefKey(ctx, desc)
39+
if actual != expected {
40+
t.Fatalf("unexpected ref key, expected %s, got: %s", expected, actual)
41+
}
42+
})
43+
44+
t.Run("unknown media type", func(t *testing.T) {
45+
desc := ocispec.Descriptor{MediaType: "we.dont.know.what.this.is"}
46+
expected := "unknown-"
47+
48+
actual := MakeRefKey(ctx, desc)
49+
if actual != expected {
50+
t.Fatalf("unexpected ref key, expected %s, got: %s", expected, actual)
51+
}
52+
})
53+
54+
t.Run("overwrite supported media type", func(t *testing.T) {
55+
desc := ocispec.Descriptor{MediaType: images.MediaTypeDockerSchema2Layer}
56+
expected := "bananas-"
57+
58+
actual := MakeRefKey(ctx, desc)
59+
if actual != expected {
60+
t.Fatalf("unexpected ref key, expected %s, got: %s", expected, actual)
61+
}
62+
})
63+
64+
t.Run("custom media type", func(t *testing.T) {
65+
desc := ocispec.Descriptor{MediaType: cmt}
66+
expected := "apples-"
67+
68+
actual := MakeRefKey(ctx, desc)
69+
if actual != expected {
70+
t.Fatalf("unexpected ref key, expected %s, got: %s", expected, actual)
71+
}
72+
})
73+
}

0 commit comments

Comments
 (0)