|
| 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 |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + "github.com/containerd/containerd/v2/core/images" |
| 23 | + "github.com/containerd/containerd/v2/core/images/converter" |
| 24 | +) |
| 25 | + |
| 26 | +// It looks like containerd converter.Convert is faulty. |
| 27 | +// When dstRef != srcRef, convert will first forcefully delete dstRef, |
| 28 | +// apparently *asynchronously*, then create the image. |
| 29 | +// This is racy, and the deletion may kick in after the creation. |
| 30 | +// This here is to workaround the bug, by manually creating the image first, |
| 31 | +// then converting it in place (which avoid the problematic code-path). |
| 32 | + |
| 33 | +func Convert(ctx context.Context, client converter.Client, dstRef, srcRef string, opts ...converter.Opt) (*images.Image, error) { |
| 34 | + imageService := client.ImageService() |
| 35 | + |
| 36 | + img, err := imageService.Get(ctx, srcRef) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + img.Name = dstRef |
| 42 | + |
| 43 | + _ = imageService.Delete(ctx, img.Name, images.SynchronousDelete()) |
| 44 | + |
| 45 | + if _, err = imageService.Create(ctx, img); err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + return converter.Convert(ctx, client, dstRef, dstRef, opts...) |
| 50 | +} |
0 commit comments