From 8390bc56837f9259fb67b6e0158ddc94a2619d56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Tue, 16 Jan 2024 13:42:50 +0100 Subject: [PATCH] c8d/cache: Use ContainerConfig from content store MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Gronowski --- daemon/containerd/cache.go | 31 +++++++++++++++++++++++++++++-- daemon/containerd/image_commit.go | 3 +-- daemon/containerd/imagespec.go | 18 ++++++++---------- image/spec/specs-go/v1/image.go | 2 -- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/daemon/containerd/cache.go b/daemon/containerd/cache.go index 6133a0b89e..804f4c7926 100644 --- a/daemon/containerd/cache.go +++ b/daemon/containerd/cache.go @@ -5,10 +5,14 @@ import ( "reflect" "strings" + "github.com/containerd/log" "github.com/docker/docker/api/types/container" imagetype "github.com/docker/docker/api/types/image" "github.com/docker/docker/builder" + "github.com/docker/docker/errdefs" "github.com/docker/docker/image" + "github.com/opencontainers/go-digest" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) // MakeImageCache creates a stateful image cache. @@ -71,12 +75,35 @@ func (ic *localCache) GetCache(parentID string, cfg *container.Config) (imageID var match *image.Image for _, child := range children { - childImage, err := ic.imageService.GetImage(ctx, child.String(), imagetype.GetImageOpts{}) + ccDigestStr, err := ic.imageService.getImageLabelByDigest(ctx, child.Digest(), imageLabelClassicBuilderContainerConfig) if err != nil { return "", err } + if ccDigestStr == "" { + continue + } + + dgst, err := digest.Parse(ccDigestStr) + if err != nil { + log.G(ctx).WithError(err).Warnf("invalid container config digest: %q", ccDigestStr) + continue + } + + var cc container.Config + if err := readConfig(ctx, ic.imageService.content, ocispec.Descriptor{Digest: dgst}, &cc); err != nil { + if errdefs.IsNotFound(err) { + log.G(ctx).WithError(err).WithField("image", child).Warnf("missing container config: %q", ccDigestStr) + continue + } + return "", err + } + + if isMatch(&cc, cfg) { + childImage, err := ic.imageService.GetImage(ctx, child.String(), imagetype.GetImageOpts{}) + if err != nil { + return "", err + } - if isMatch(&childImage.ContainerConfig, cfg) { if childImage.Created != nil && (match == nil || match.Created.Before(*childImage.Created)) { match = childImage } diff --git a/daemon/containerd/image_commit.go b/daemon/containerd/image_commit.go index 8345fb5182..fe589846eb 100644 --- a/daemon/containerd/image_commit.go +++ b/daemon/containerd/image_commit.go @@ -144,8 +144,7 @@ func generateCommitImageConfig(baseConfig imagespec.DockerOCIImage, diffID diges EmptyLayer: diffID == "", }), }, - Config: containerConfigToDockerOCIImageConfig(opts.Config), - ContainerConfig: containerConfigToDockerOCIImageConfig(opts.ContainerConfig), + Config: containerConfigToDockerOCIImageConfig(opts.Config), } } diff --git a/daemon/containerd/imagespec.go b/daemon/containerd/imagespec.go index 1eea82a0ce..932119999b 100644 --- a/daemon/containerd/imagespec.go +++ b/daemon/containerd/imagespec.go @@ -18,14 +18,13 @@ import ( // - Details func dockerOciImageToDockerImagePartial(id image.ID, img imagespec.DockerOCIImage) *image.Image { v1Image := image.V1Image{ - DockerVersion: dockerversion.Version, - Config: dockerOCIImageConfigToContainerConfig(img.Config), - ContainerConfig: *dockerOCIImageConfigToContainerConfig(img.ContainerConfig), - Architecture: img.Platform.Architecture, - Variant: img.Platform.Variant, - OS: img.Platform.OS, - Author: img.Author, - Created: img.Created, + DockerVersion: dockerversion.Version, + Config: dockerOCIImageConfigToContainerConfig(img.Config), + Architecture: img.Platform.Architecture, + Variant: img.Platform.Variant, + OS: img.Platform.OS, + Author: img.Author, + Created: img.Created, } rootFS := &image.RootFS{ @@ -67,8 +66,7 @@ func dockerImageToDockerOCIImage(img image.Image) imagespec.DockerOCIImage { RootFS: rootfs, History: img.History, }, - Config: containerConfigToDockerOCIImageConfig(img.Config), - ContainerConfig: containerConfigToDockerOCIImageConfig(&img.ContainerConfig), + Config: containerConfigToDockerOCIImageConfig(img.Config), } } diff --git a/image/spec/specs-go/v1/image.go b/image/spec/specs-go/v1/image.go index 82740c5aa8..1672617635 100644 --- a/image/spec/specs-go/v1/image.go +++ b/image/spec/specs-go/v1/image.go @@ -14,8 +14,6 @@ type DockerOCIImage struct { // Shadow ocispec.Image.Config Config DockerOCIImageConfig `json:"config,omitempty"` - - ContainerConfig DockerOCIImageConfig `json:"container_config,omitempty"` } // DockerOCIImageConfig is a ocispec.ImageConfig extended with Docker specific fields.