X Tutup
Skip to content

Commit f0d3ea9

Browse files
committed
converter: Allow hooks during image conversion
This commit allows hook callbacks during image conversion. This enbles the caller additional modification for each blob descriptor. Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
1 parent aa65fae commit f0d3ea9

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

images/converter/default.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,35 @@ func DefaultIndexConvertFunc(layerConvertFunc ConvertFunc, docker2oci bool, plat
4848
return c.convert
4949
}
5050

51+
// ConvertHookFunc is a callback function called during conversion of a blob.
52+
// orgDesc is the target descriptor to convert. newDesc is passed if conversion happens.
53+
type ConvertHookFunc func(ctx context.Context, cs content.Store, orgDesc ocispec.Descriptor, newDesc *ocispec.Descriptor) (*ocispec.Descriptor, error)
54+
55+
// ConvertHooks is a configuration for hook callbacks called during blob conversion.
56+
type ConvertHooks struct {
57+
// PostConvertHook is a callback function called for each blob after conversion is done.
58+
PostConvertHook ConvertHookFunc
59+
}
60+
61+
// IndexConvertFuncWithHook is the convert func used by Convert with hook functions support.
62+
func IndexConvertFuncWithHook(layerConvertFunc ConvertFunc, docker2oci bool, platformMC platforms.MatchComparer, hooks ConvertHooks) ConvertFunc {
63+
c := &defaultConverter{
64+
layerConvertFunc: layerConvertFunc,
65+
docker2oci: docker2oci,
66+
platformMC: platformMC,
67+
diffIDMap: make(map[digest.Digest]digest.Digest),
68+
hooks: hooks,
69+
}
70+
return c.convert
71+
}
72+
5173
type defaultConverter struct {
5274
layerConvertFunc ConvertFunc
5375
docker2oci bool
5476
platformMC platforms.MatchComparer
5577
diffIDMap map[digest.Digest]digest.Digest // key: old diffID, value: new diffID
5678
diffIDMapMu sync.RWMutex
79+
hooks ConvertHooks
5780
}
5881

5982
// convert dispatches desc.MediaType and calls c.convert{Layer,Manifest,Index,Config}.
@@ -76,6 +99,15 @@ func (c *defaultConverter) convert(ctx context.Context, cs content.Store, desc o
7699
if err != nil {
77100
return nil, err
78101
}
102+
103+
if c.hooks.PostConvertHook != nil {
104+
if newDescPost, err := c.hooks.PostConvertHook(ctx, cs, desc, newDesc); err != nil {
105+
return nil, err
106+
} else if newDescPost != nil {
107+
newDesc = newDescPost
108+
}
109+
}
110+
79111
if images.IsDockerType(desc.MediaType) {
80112
if c.docker2oci {
81113
if newDesc == nil {

0 commit comments

Comments
 (0)
X Tutup