Browse code

Merge pull request #46590 from vvoland/c8d-tag-ctx-withoutcancel

daemon/c8d: Use WithoutCancel instead of context.Background

Sebastiaan van Stijn authored on 2023/10/11 23:55:16
Showing 6 changed files
... ...
@@ -13,6 +13,7 @@ import (
13 13
 	"github.com/docker/docker/api/types/events"
14 14
 	"github.com/docker/docker/container"
15 15
 	"github.com/docker/docker/image"
16
+	"github.com/docker/docker/internal/compatcontext"
16 17
 	"github.com/docker/docker/pkg/stringid"
17 18
 	"github.com/opencontainers/go-digest"
18 19
 	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
... ...
@@ -136,7 +137,7 @@ func (i *ImageService) deleteAll(ctx context.Context, img images.Image, force, p
136 136
 		return nil, err
137 137
 	}
138 138
 	defer func() {
139
-		if err := i.unleaseSnapshotsFromDeletedConfigs(context.Background(), possiblyDeletedConfigs); err != nil {
139
+		if err := i.unleaseSnapshotsFromDeletedConfigs(compatcontext.WithoutCancel(ctx), possiblyDeletedConfigs); err != nil {
140 140
 			log.G(ctx).WithError(err).Warn("failed to unlease snapshots")
141 141
 		}
142 142
 	}()
... ...
@@ -11,6 +11,7 @@ import (
11 11
 	"github.com/docker/docker/api/types"
12 12
 	"github.com/docker/docker/api/types/filters"
13 13
 	"github.com/docker/docker/errdefs"
14
+	"github.com/docker/docker/internal/compatcontext"
14 15
 	"github.com/hashicorp/go-multierror"
15 16
 	"github.com/opencontainers/go-digest"
16 17
 	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
... ...
@@ -147,7 +148,7 @@ func (i *ImageService) pruneUnused(ctx context.Context, filterFunc imageFilterFu
147 147
 
148 148
 	// Workaround for https://github.com/moby/buildkit/issues/3797
149 149
 	defer func() {
150
-		if err := i.unleaseSnapshotsFromDeletedConfigs(context.Background(), possiblyDeletedConfigs); err != nil {
150
+		if err := i.unleaseSnapshotsFromDeletedConfigs(compatcontext.WithoutCancel(ctx), possiblyDeletedConfigs); err != nil {
151 151
 			errs = multierror.Append(errs, err)
152 152
 		}
153 153
 	}()
... ...
@@ -15,6 +15,7 @@ import (
15 15
 	"github.com/docker/docker/api/types/registry"
16 16
 	"github.com/docker/docker/distribution"
17 17
 	"github.com/docker/docker/errdefs"
18
+	"github.com/docker/docker/internal/compatcontext"
18 19
 	"github.com/docker/docker/pkg/progress"
19 20
 	"github.com/docker/docker/pkg/streamformatter"
20 21
 	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
... ...
@@ -129,7 +130,7 @@ func (i *ImageService) PullImage(ctx context.Context, ref reference.Named, platf
129 129
 	logger.Info("image pulled")
130 130
 
131 131
 	// The pull succeeded, so try to remove any dangling image we have for this target
132
-	err = i.client.ImageService().Delete(context.Background(), danglingImageName(img.Target().Digest))
132
+	err = i.client.ImageService().Delete(compatcontext.WithoutCancel(ctx), danglingImageName(img.Target().Digest))
133 133
 	if err != nil && !cerrdefs.IsNotFound(err) {
134 134
 		// Image pull succeeded, but cleaning up the dangling image failed. Ignore the
135 135
 		// error to not mark the pull as failed.
... ...
@@ -10,6 +10,7 @@ import (
10 10
 	"github.com/docker/docker/api/types/events"
11 11
 	"github.com/docker/docker/errdefs"
12 12
 	"github.com/docker/docker/image"
13
+	"github.com/docker/docker/internal/compatcontext"
13 14
 	"github.com/pkg/errors"
14 15
 )
15 16
 
... ...
@@ -49,7 +50,7 @@ func (i *ImageService) TagImage(ctx context.Context, imageID image.ID, newTag re
49 49
 			return errors.Wrapf(err, "failed to delete previous image %s", replacedImg.Name)
50 50
 		}
51 51
 
52
-		if _, err = is.Create(context.Background(), newImg); err != nil {
52
+		if _, err = is.Create(compatcontext.WithoutCancel(ctx), newImg); err != nil {
53 53
 			return errdefs.System(errors.Wrapf(err, "failed to create an image %s with target %s after deleting the existing one",
54 54
 				newImg.Name, imageID.String()))
55 55
 		}
... ...
@@ -64,7 +65,7 @@ func (i *ImageService) TagImage(ctx context.Context, imageID image.ID, newTag re
64 64
 	defer i.LogImageEvent(imageID.String(), reference.FamiliarString(newTag), events.ActionTag)
65 65
 
66 66
 	// The tag succeeded, check if the source image is dangling
67
-	sourceDanglingImg, err := is.Get(context.Background(), danglingImageName(target.Digest))
67
+	sourceDanglingImg, err := is.Get(compatcontext.WithoutCancel(ctx), danglingImageName(target.Digest))
68 68
 	if err != nil {
69 69
 		if !cerrdefs.IsNotFound(err) {
70 70
 			logger.WithError(err).Warn("unexpected error when checking if source image is dangling")
... ...
@@ -79,13 +80,13 @@ func (i *ImageService) TagImage(ctx context.Context, imageID image.ID, newTag re
79 79
 			imageLabelClassicBuilderParent: builderLabel,
80 80
 		}
81 81
 
82
-		if _, err := is.Update(context.Background(), newImg, "labels"); err != nil {
82
+		if _, err := is.Update(compatcontext.WithoutCancel(ctx), newImg, "labels"); err != nil {
83 83
 			logger.WithError(err).Warnf("failed to set %s label on the newly tagged image", imageLabelClassicBuilderParent)
84 84
 		}
85 85
 	}
86 86
 
87 87
 	// Delete the source dangling image, as it's no longer dangling.
88
-	if err := is.Delete(context.Background(), sourceDanglingImg.Name); err != nil {
88
+	if err := is.Delete(compatcontext.WithoutCancel(ctx), sourceDanglingImg.Name); err != nil {
89 89
 		logger.WithError(err).Warn("unexpected error when deleting dangling image")
90 90
 	}
91 91
 
... ...
@@ -11,6 +11,7 @@ import (
11 11
 	"github.com/containerd/containerd/log"
12 12
 	"github.com/containerd/containerd/remotes"
13 13
 	"github.com/containerd/containerd/remotes/docker"
14
+	"github.com/docker/docker/internal/compatcontext"
14 15
 	"github.com/docker/docker/pkg/progress"
15 16
 	"github.com/docker/docker/pkg/stringid"
16 17
 	"github.com/opencontainers/go-digest"
... ...
@@ -52,7 +53,7 @@ func (j *jobs) showProgress(ctx context.Context, out progress.Output, updater pr
52 52
 					}
53 53
 				}
54 54
 			case <-ctx.Done():
55
-				ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500)
55
+				ctx, cancel := context.WithTimeout(compatcontext.WithoutCancel(ctx), time.Millisecond*500)
56 56
 				defer cancel()
57 57
 				updater.UpdateProgress(ctx, j, out, start)
58 58
 				close(lastUpdate)
... ...
@@ -6,6 +6,7 @@ import (
6 6
 	cerrdefs "github.com/containerd/containerd/errdefs"
7 7
 	containerdimages "github.com/containerd/containerd/images"
8 8
 	"github.com/docker/docker/errdefs"
9
+	"github.com/docker/docker/internal/compatcontext"
9 10
 	"github.com/opencontainers/go-digest"
10 11
 	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
11 12
 	"github.com/pkg/errors"
... ...
@@ -32,7 +33,7 @@ func (i *ImageService) softImageDelete(ctx context.Context, img containerdimages
32 32
 
33 33
 	// Create dangling image if this is the last image pointing to this target.
34 34
 	if len(imgs) == 1 {
35
-		err = i.ensureDanglingImage(context.Background(), img)
35
+		err = i.ensureDanglingImage(compatcontext.WithoutCancel(ctx), img)
36 36
 
37 37
 		// Error out in case we couldn't persist the old image.
38 38
 		if err != nil {
... ...
@@ -42,7 +43,7 @@ func (i *ImageService) softImageDelete(ctx context.Context, img containerdimages
42 42
 	}
43 43
 
44 44
 	// Free the target name.
45
-	err = is.Delete(context.Background(), img.Name)
45
+	err = is.Delete(compatcontext.WithoutCancel(ctx), img.Name)
46 46
 	if err != nil {
47 47
 		if !cerrdefs.IsNotFound(err) {
48 48
 			return errdefs.System(errors.Wrapf(err, "failed to delete image %s which existed a moment before", img.Name))
... ...
@@ -66,7 +67,7 @@ func (i *ImageService) ensureDanglingImage(ctx context.Context, from containerdi
66 66
 	}
67 67
 	danglingImage.Name = danglingImageName(from.Target.Digest)
68 68
 
69
-	_, err := i.client.ImageService().Create(context.Background(), danglingImage)
69
+	_, err := i.client.ImageService().Create(compatcontext.WithoutCancel(ctx), danglingImage)
70 70
 	// If it already exists, then just continue.
71 71
 	if cerrdefs.IsAlreadyExists(err) {
72 72
 		return nil