|
| 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 converter provides image converter |
| 18 | +package converter |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + |
| 23 | + "github.com/containerd/containerd/content" |
| 24 | + "github.com/containerd/containerd/images" |
| 25 | + "github.com/containerd/containerd/leases" |
| 26 | + "github.com/containerd/containerd/platforms" |
| 27 | +) |
| 28 | + |
| 29 | +type convertOpts struct { |
| 30 | + layerConvertFunc ConvertFunc |
| 31 | + docker2oci bool |
| 32 | + indexConvertFunc ConvertFunc |
| 33 | + platformMC platforms.MatchComparer |
| 34 | +} |
| 35 | + |
| 36 | +// Opt is an option for Convert() |
| 37 | +type Opt func(*convertOpts) error |
| 38 | + |
| 39 | +// WithLayerConvertFunc specifies the function that converts layers. |
| 40 | +func WithLayerConvertFunc(fn ConvertFunc) Opt { |
| 41 | + return func(copts *convertOpts) error { |
| 42 | + copts.layerConvertFunc = fn |
| 43 | + return nil |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// WithDockerToOCI converts Docker media types into OCI ones. |
| 48 | +func WithDockerToOCI(v bool) Opt { |
| 49 | + return func(copts *convertOpts) error { |
| 50 | + copts.docker2oci = true |
| 51 | + return nil |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// WithPlatform specifies the platform. |
| 56 | +// Defaults to all platforms. |
| 57 | +func WithPlatform(p platforms.MatchComparer) Opt { |
| 58 | + return func(copts *convertOpts) error { |
| 59 | + copts.platformMC = p |
| 60 | + return nil |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// WithIndexConvertFunc specifies the function that converts manifests and index (manifest lists). |
| 65 | +// Defaults to DefaultIndexConvertFunc. |
| 66 | +func WithIndexConvertFunc(fn ConvertFunc) Opt { |
| 67 | + return func(copts *convertOpts) error { |
| 68 | + copts.indexConvertFunc = fn |
| 69 | + return nil |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Client is implemented by *containerd.Client . |
| 74 | +type Client interface { |
| 75 | + WithLease(ctx context.Context, opts ...leases.Opt) (context.Context, func(context.Context) error, error) |
| 76 | + ContentStore() content.Store |
| 77 | + ImageService() images.Store |
| 78 | +} |
| 79 | + |
| 80 | +// Convert converts an image. |
| 81 | +func Convert(ctx context.Context, client Client, dstRef, srcRef string, opts ...Opt) (*images.Image, error) { |
| 82 | + var copts convertOpts |
| 83 | + for _, o := range opts { |
| 84 | + if err := o(&copts); err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + } |
| 88 | + if copts.platformMC == nil { |
| 89 | + copts.platformMC = platforms.All |
| 90 | + } |
| 91 | + if copts.indexConvertFunc == nil { |
| 92 | + copts.indexConvertFunc = DefaultIndexConvertFunc(copts.layerConvertFunc, copts.docker2oci, copts.platformMC) |
| 93 | + } |
| 94 | + |
| 95 | + ctx, done, err := client.WithLease(ctx) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + defer done(ctx) |
| 100 | + |
| 101 | + cs := client.ContentStore() |
| 102 | + is := client.ImageService() |
| 103 | + srcImg, err := is.Get(ctx, srcRef) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + dstDesc, err := copts.indexConvertFunc(ctx, cs, srcImg.Target) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + dstImg := srcImg |
| 114 | + dstImg.Name = dstRef |
| 115 | + if dstDesc != nil { |
| 116 | + dstImg.Target = *dstDesc |
| 117 | + } |
| 118 | + var res images.Image |
| 119 | + if dstRef != srcRef { |
| 120 | + _ = is.Delete(ctx, dstRef) |
| 121 | + res, err = is.Create(ctx, dstImg) |
| 122 | + } else { |
| 123 | + res, err = is.Update(ctx, dstImg) |
| 124 | + } |
| 125 | + return &res, err |
| 126 | +} |
0 commit comments