Skip to content

Unpacks should check and always do fetch if content not found - #8878

Closed
skaegi wants to merge 2 commits into
containerd:mainfrom
skaegi:incomplete_image_fix
Closed

skaegi wants to merge 2 commits into
containerd:mainfrom
skaegi:incomplete_image_fix

Conversation

@skaegi

@skaegi skaegi commented Jul 26, 2023 •

Copy link
Copy Markdown
Contributor

This patch adds an additional check in the unpack logic to "always" fetch the content if not present. This handles a case that can lead to incomplete images (images that have the RootFs in the snapshot but have a different digest in their oci config) and prevent snapshotters other than the default from working.

In terms of code changes...
This patch moves the content check and fetch to the start of doUnpackFn. This means that we "always" will check for content even if the snapshot diffid is unpacked already and download the content if missing.

Fixes #8674

Signed-off-by: Simon Kaegi [email protected]

@k8s-ci-robot

Copy link
Copy Markdown

Hi @skaegi. Thanks for your PR.

I'm waiting for a containerd member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@skaegi

skaegi commented Jul 26, 2023 •

Copy link
Copy Markdown
Contributor Author

Update: This is the approach I'm using in my branch -- change is much smaller and more just moving logic to do content check upfront vs. halfway through. I also removed doFetchFn and am just moving the existing logic to again make the change smaller.


Another possibility here is to always do the content check and fetch before the sn.Stat check and then remove the later fetch.

e.g.

if _, err := cs.Info(ctx, desc.Digest); err != nil {
	if errdefs.IsNotFound(err) {
		return doFetchFn(i)
	} else {
		return fmt.Errorf("failed to get content info %s: %w", desc.Digest.String(), err)
	}
}

if _, err := sn.Stat(ctx, chainID); err == nil {
	// no need to handle
	return nil
} else if !errdefs.IsNotFound(err) {
	return fmt.Errorf("failed to stat snapshot %s: %w", chainID, err)
}

@skaegi

skaegi commented Jul 26, 2023 •

Copy link
Copy Markdown
Contributor Author

Here's a quick problem repeater...

crictl pull uk.icr.io/armada-master/ingress-alpine:3.18_249
crictl pull alpine:3.18.0
ctr -n k8s.io image export /tmp/img docker.io/library/alpine:3.18.0

This shows a bit more of the problem...

ctr -n k8s.io i check | grep incomplete
ctr -n k8s.io content ls | grep 8a49

--

We have also validated this fix in our clusters where we overlayfs as the default and devmapper being used by a runtimeclass.

@skaegi
skaegi force-pushed the incomplete_image_fix branch 2 times, most recently from 421d4ec to cc5e3ca Compare August 10, 2023 16:43
This patch moves the content check and fetch to the start of doUnpackFn. This means that we "always" will check for content even if the snapshot diffid is unpacked already and download the content if missing.

This handles a real world case where image content has a different digest but the same diff id, which result in an incomplete image and then problems exporting images or using the image with a different snapshotter.

Signed-off-by: Simon Kaegi <[email protected]>
@henry118

Copy link
Copy Markdown
Member

Makes sense. Just want to understand any potential impact to GC, since we may end up with multiple contents referencing the same snapshot. If we remove one of the images, will the snapshot remain intact? Is it still possible to start a container for the other image?

@dmcgowan

Copy link
Copy Markdown
Member

images that have the RootFs in the snapshot but have a different digest in their oci config

What is this referring to and where is the difference?

In your case , is discard_unpacked_layers set?

The transfer service configuration is intended to solve the multiple snapshotter unpack issue. In the case here, is the devmapper snapshotter specified during the pull and it still fails? It seems that case would have a better solution than pulling the content even when it is not needed for the configured unpack and the content is to be discarded.

@skaegi

skaegi commented Sep 5, 2023 •

Copy link
Copy Markdown
Contributor Author

Makes sense. Just want to understand any potential impact to GC, since we may end up with multiple contents referencing the same snapshot. If we remove one of the images, will the snapshot remain intact? Is it still possible to start a container for the other image?

@henry118 Great question. With this PR patch applied this is what I saw. I first applied the two pods... (the kata pod is using devmapper)

apiVersion: v1
kind: Pod
metadata:
  name: trusted-alpine-ingress
spec:
  containers:
    - name: alpine
      image: uk.icr.io/armada-master/ingress-alpine:3.18_249
      imagePullPolicy: IfNotPresent
      command:
      - sleep
      - "50000000"
---
apiVersion: v1
kind: Pod
metadata:
  name: untrusted-alpine
spec:
  runtimeClassName: kata
  containers:
    - name: alpine
      image: alpine:3.18.0
      imagePullPolicy: IfNotPresent
      command:
      - sleep
      - "50000000"

nsenter-ing on to the node I see...

root@kube-cf431inl06scgp3lmpt0-gen2lon-seconda-00001c53:/# alias c="ctr -n k8s.io"
root@kube-cf431inl06scgp3lmpt0-gen2lon-seconda-00001c53:/# c snapshots --snapshotter devmapper ls | grep bb01
37a0b1f65024713b470adf0ca38d7a2ba19303497c70cab0bdc1756894fc4225        sha256:bb01bd7e32b58b6694c8c3622c230171f1cec24001a82068a8d30d338f420d6c Active    
sha256:bb01bd7e32b58b6694c8c3622c230171f1cec24001a82068a8d30d338f420d6c                                                                         Committed 
root@kube-cf431inl06scgp3lmpt0-gen2lon-seconda-00001c53:/# c snapshots --snapshotter overlayfs ls | grep bb01
sha256:538b3bf128118798072d800141631b0524e0c5b8739b872f264e4b999480b17e sha256:bb01bd7e32b58b6694c8c3622c230171f1cec24001a82068a8d30d338f420d6c Committed 
sha256:bb01bd7e32b58b6694c8c3622c230171f1cec24001a82068a8d30d338f420d6c                                                                         Committed 
sha256:c0669ef34cdc14332c0f1ab0c2c01acb91d96014b172f1a76f3a39e63d1f0bda	528B	About a minute	containerd.io/distribution.source.docker.io=library/alpine,containerd.io/gc.ref.content.config=sha256:5e2b554c1c45d22c9d1aa836828828e320a26011b76c08631ac896cbc3625e3e,containerd.io/gc.ref.content.l.0=sha256:8a49fdb3b6a5ff2bd8ec6a86c05b2922a0f7454579ecc07637e94dfd1d0639b6
root@kube-cf431inl06scgp3lmpt0-gen2lon-seconda-00001c53:/# ctr -n k8s.io content ls | grep bb01
sha256:5e2b554c1c45d22c9d1aa836828828e320a26011b76c08631ac896cbc3625e3e	1.472kB	2 minutes	containerd.io/distribution.source.docker.io=library/alpine,containerd.io/gc.ref.snapshot.devmapper=sha256:bb01bd7e32b58b6694c8c3622c230171f1cec24001a82068a8d30d338f420d6c,containerd.io/gc.ref.snapshot.overlayfs=sha256:bb01bd7e32b58b6694c8c3622c230171f1cec24001a82068a8d30d338f420d6c
sha256:8a49fdb3b6a5ff2bd8ec6a86c05b2922a0f7454579ecc07637e94dfd1d0639b6	3.397MB	2 minutes	containerd.io/distribution.source.docker.io=library/alpine,containerd.io/uncompressed=sha256:bb01bd7e32b58b6694c8c3622c230171f1cec24001a82068a8d30d338f420d6c
sha256:bc01fbd705408cb3b447eb0ad4059bf08f686442be80e65dd951ce884b64a595	3.511MB	2 minutes	containerd.io/distribution.source.uk.icr.io=armada-master/ingress-alpine,containerd.io/uncompressed=sha256:bb01bd7e32b58b6694c8c3622c230171f1cec24001a82068a8d30d338f420d6c

I then deleted the kata pod -- kubectl delete pod untrusted-alpine and nsenter-ed the node again and ran...

root@kube-cf431inl06scgp3lmpt0-gen2lon-seconda-00001c53:/# alias c="ctr -n k8s.io"
root@kube-cf431inl06scgp3lmpt0-gen2lon-seconda-00001c53:/# c i prune --all
INFO[0000] deleted image: docker.io/library/alpine:3.18.0 
INFO[0000] deleted image: docker.io/library/alpine@sha256:02bb6f428431fbc2809c5d1b41eab5a68350194fb508869a33cb1af4444c9b11 
INFO[0000] deleted image: sha256:5e2b554c1c45d22c9d1aa836828828e320a26011b76c08631ac896cbc3625e3e 
INFO[0000] deleted image: sha256:8de5ed13778b0186780b382dd34d0f9ba7eee9d46f584d6441e90321f657f3ab 
INFO[0000] deleted image: uk.icr.io/armada-master/ingress-alpine@sha256:2f1ad34e0a7030a8f9a030a1ec99d8ebaa0a9ee4e9cb59ee04e244337d94726c 
root@kube-cf431inl06scgp3lmpt0-gen2lon-seconda-00001c53:/# c snapshots --snapshotter devmapper ls | grep bb01
root@kube-cf431inl06scgp3lmpt0-gen2lon-seconda-00001c53:/# c snapshots --snapshotter overlayfs ls | grep bb01
sha256:538b3bf128118798072d800141631b0524e0c5b8739b872f264e4b999480b17e sha256:bb01bd7e32b58b6694c8c3622c230171f1cec24001a82068a8d30d338f420d6c Committed 
sha256:bb01bd7e32b58b6694c8c3622c230171f1cec24001a82068a8d30d338f420d6c                                                                         Committed 
root@kube-cf431inl06scgp3lmpt0-gen2lon-seconda-00001c53:/# ctr -n k8s.io content ls | grep bb01
sha256:bc01fbd705408cb3b447eb0ad4059bf08f686442be80e65dd951ce884b64a595	3.511MB	7 minutes	containerd.io/distribution.source.uk.icr.io=armada-master/ingress-alpine,containerd.io/uncompressed=sha256:bb01bd7e32b58b6694c8c3622c230171f1cec24001a82068a8d30d338f420d6c

From what I saw removing the image did not clean up anything until I also did a prune --all. After the ctr i prune --all though what I saw looked correct and both the content and devmapper snapshotter cleaneup correctly and the snapshotter for the running container or the content for the other image were left as is as one would expect. Not shown... but if I delete the remaing pod and c i prune --all everything is cleaned up completely. (I hope this description makes sense!)

@skaegi

skaegi commented Sep 5, 2023 •

Copy link
Copy Markdown
Contributor Author

images that have the RootFs in the snapshot but have a different digest in their oci config

What is this referring to and where is the difference?

@dmcgowan When using uk.icr.io/armada-master/ingress-alpine:3.18_249 and alpine:3.18.0 we have a case where two blobs of content from the remote repositories unzip to the same bits.
sha256:8a49fdb3b6a5ff2bd8ec6a86c05b2922a0f7454579ecc07637e94dfd1d0639b6 from alpine:3.18.0 and sha256:bc01fbd705408cb3b447eb0ad4059bf08f686442be80e65dd951ce884b64a595 from uk.icr.io/armada-master/ingress-alpine:3.18_249 unzip to the same bits ... sha256:bb01bd7e32b58b6694c8c3622c230171f1cec24001a82068a8d30d338f420d6c

In your case , is discard_unpacked_layers set?

This is set to false in our environment

The transfer service configuration is intended to solve the multiple snapshotter unpack issue. In the case here, is the devmapper snapshotter specified during the pull and it still fails?

The unpack via CRI currently does not let us control this (I believe @mikebrow) and so uses the matching platform unpacker which pulls the content and unzips it to overlayfs by default. This just adds overhead!

It seems that case would have a better solution than pulling the content even when it is not needed for the configured unpack and the content is to be discarded.

Definitely! We currently see the content first downloaded and unzipped to overlayfs and then again unzipped to devmapper.

@mikebrow

mikebrow commented Sep 6, 2023

Copy link
Copy Markdown
Member

nod.. we are fixing the disconnect in k8s where we lost the annotation that specified the pod runtime handler(from class) on image status and pull from kubelet ..

@Kern--

Kern-- commented Sep 14, 2023

Copy link
Copy Markdown

This change breaks remote snapshotters which rely on the snapshotter being able to return errdef.ErrAlreadyExists from Prepare to skip content fetching. More details in the remote snapshotter doc.

@skaegi

skaegi commented Oct 10, 2023

Copy link
Copy Markdown
Contributor Author

This change breaks remote snapshotters which rely on the snapshotter being able to return errdef.ErrAlreadyExists from Prepare to skip content fetching.

Yep. This is why the CRI needs to pass down the snapshotter vs. just using the default snapshotter. If the correct snapshotter had been passed down then the existing logic would have worked. The patch above is a workaround (apparently for people not using a remote snapshotter) for using two snapshotters in 1.7.x.

@imeoer

imeoer commented Nov 9, 2023

Copy link
Copy Markdown

This change breaks remote snapshotters which rely on the snapshotter being able to return errdef.ErrAlreadyExists from Prepare to skip content fetching. More details in the remote snapshotter doc.

Indeed, it seems the PR is not a complete fixup, one possible situation in the real world:

  1. Some images in the pod (including the pause image) have used containerd's default snapshotter (such as the overlayfs snapshotter);
  2. The discard_unpacked_layers option was previously set to true in containerd config;
  3. Containerd has already deleted the blobs from the content store;
  4. The user switches to another snapshotter (for example nydus/estargz lazy loading implementation) to pull the image.

A possible workaround: ensure that discard_unpacked_layers=false, then use the following command to restore the image:

ctr -n k8s.io content fetch pause:3.8

The pause:3.8 is just an example image, we should also fetch all images used by the Pod to ensure that there are no issues.

cc @skaegi @dmcgowan @Kern--

@AkihiroSuda AkihiroSuda left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but needs rebase to pass the CI.

I'd also like to see a test

@AkihiroSuda AkihiroSuda added cherry-pick/1.6.x cherry-pick/1.7.x Change to be cherry picked to release/1.7 branch labels Dec 6, 2023
@AkihiroSuda
AkihiroSuda requested a review from dmcgowan December 6, 2023 03:42
@estesp

estesp commented Dec 6, 2023

Copy link
Copy Markdown
Member

Has @Kern--'s comment been resolved regarding this being a breaking change for remote snapshotters? I see a few follow-on comments but no further changes/discussion. I don't want us to merge this before that concern is resolved.

@dmcgowan

dmcgowan commented Dec 6, 2023

Copy link
Copy Markdown
Member

I agree with @imeoer that the correct solution for the specific error is discard_unpacked_layers=false. Long term, we really need to know the target snapshotter the pull is for if discard_unpacked_layers=true is used.

@dmcgowan

dmcgowan commented Dec 6, 2023

Copy link
Copy Markdown
Member

When using uk.icr.io/armada-master/ingress-alpine:3.18_249 and alpine:3.18.0 we have a case where two blobs of content from the remote repositories unzip to the same bits.
sha256:8a49fdb3b6a5ff2bd8ec6a86c05b2922a0f7454579ecc07637e94dfd1d0639b6 from alpine:3.18.0 and sha256:bc01fbd705408cb3b447eb0ad4059bf08f686442be80e65dd951ce884b64a595 from uk.icr.io/armada-master/ingress-alpine:3.18_249 unzip to the same bits ... sha256:bb01bd7e32b58b6694c8c3622c230171f1cec24001a82068a8d30d338f420d6c

This is a pretty uncommon case that we definitely should have a solution for. Always pulling doesn't make sense for the unpacker though as it would degrade performance for the vast majority of pulls which would not have this issue. I think this was mentioned before but the most ideal solution would be to have the pull target both snapshotters for unpack. This optimized unpacker logic was not designed for breaking apart pull and unpack like ctr does today.

@skaegi

skaegi commented Dec 31, 2023 •

Copy link
Copy Markdown
Contributor Author

Sorry guys, I left this here as for whatever reason a number of teams started using it. I do not think this is mergeable for both the remote snapshotter and unpack reasons. Although this patch kind of works for our use-case, we still get the extra unpack to the "default" overlay snaphotter which takes both extra disk space and time.

We really need the snapshotter passed down and have been using the io.containerd.cri.runtime-handler annotation and its associated logic to do what we need as a stop-gap while we wait for KEP 4216 and associated changes in containerd.

@dims dims added the area/snapshotters Snapshotters label Jan 22, 2024
The chain ID lock is valuable to prevent parallel fetch and extracts for the same content. In practice without this lock we saw conflicting parallel extracts that greatly slowed image pulls when multiple pods/containers were pulling the same image.
@k8s-ci-robot

Copy link
Copy Markdown

PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open 90 days with no activity. This PR will be closed in 7 days unless new comments are made or the stale label is removed.

@github-actions github-actions Bot added the Stale label May 12, 2024
@github-actions

Copy link
Copy Markdown

This PR was closed because it has been stalled for 7 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/snapshotters Snapshotters cherry-pick/1.7.x Change to be cherry picked to release/1.7 branch needs-ok-to-test needs-rebase size/M Stale

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

devmapper snapshotter image pulls result in error unpacking image: failed to extract layer

10 participants