Skip to content

Commit 297233b

Browse files
committed
Ensure all layers are here when committing
For context: #3425 #3439 Signed-off-by: apostasie <[email protected]>
1 parent 9d2effa commit 297233b

File tree

6 files changed

+64
-19
lines changed

6 files changed

+64
-19
lines changed

cmd/nerdctl/container/container_commit_linux_test.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"strings"
2121
"testing"
2222

23-
"gotest.tools/v3/icmd"
24-
2523
"github.com/containerd/nerdctl/v2/pkg/testutil"
2624
)
2725

@@ -35,12 +33,11 @@ It will regularly succeed or fail, making random PR fail the Kube check.
3533
func TestKubeCommitPush(t *testing.T) {
3634
t.Parallel()
3735

38-
t.Skip("Test that confirm that #827 is still broken is too flaky")
39-
4036
base := testutil.NewBaseForKubernetes(t)
4137
tID := testutil.Identifier(t)
4238

4339
var containerID string
40+
// var registryIP string
4441

4542
setup := func() {
4643
testutil.KubectlHelper(base, "run", "--image", testutil.CommonImage, tID, "--", "sleep", "Inf").
@@ -55,26 +52,45 @@ func TestKubeCommitPush(t *testing.T) {
5552
cmd := testutil.KubectlHelper(base, "get", "pods", tID, "-o", "jsonpath={ .status.containerStatuses[0].containerID }")
5653
cmd.Run()
5754
containerID = strings.TrimPrefix(cmd.Out(), "containerd://")
55+
56+
// This below is missing configuration to allow for plain http communication
57+
// This is left here for future work to successfully start a registry usable in the cluster
58+
/*
59+
// Start a registry
60+
testutil.KubectlHelper(base, "run", "--port", "5000", "--image", testutil.RegistryImageStable, "testregistry").
61+
AssertOK()
62+
63+
testutil.KubectlHelper(base, "wait", "pod", "testregistry", "--for=condition=ready", "--timeout=1m").
64+
AssertOK()
65+
66+
cmd = testutil.KubectlHelper(base, "get", "pods", tID, "-o", "jsonpath={ .status.hostIPs[0].ip }")
67+
cmd.Run()
68+
registryIP = cmd.Out()
69+
70+
cmd = testutil.KubectlHelper(base, "apply", "-f", "-", fmt.Sprintf(`apiVersion: v1
71+
kind: ConfigMap
72+
metadata:
73+
name: local-registry
74+
namespace: nerdctl-test
75+
data:
76+
localRegistryHosting.v1: |
77+
host: "%s:5000"
78+
help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
79+
`, registryIP))
80+
*/
81+
5882
}
5983

6084
tearDown := func() {
61-
testutil.KubectlHelper(base, "delete", "pod", "-f", tID).Run()
85+
testutil.KubectlHelper(base, "delete", "pod", "--all").Run()
6286
}
6387

6488
tearDown()
6589
t.Cleanup(tearDown)
6690
setup()
6791

6892
t.Run("test commit / push on Kube (https://github.com/containerd/nerdctl/issues/827)", func(t *testing.T) {
69-
t.Log("This test is meant to verify that we can commit / push an image from a pod." +
70-
"Currently, this is broken, hence the test assumes it will fail. Once the problem is fixed, we should just" +
71-
"change the expectation to 'success'.")
72-
73-
base.Cmd("commit", containerID, "registry.example.com/my-app:v1").AssertOK()
74-
// See note above.
75-
base.Cmd("push", "registry.example.com/my-app:v1").Assert(icmd.Expected{
76-
ExitCode: 1,
77-
Err: "failed to create a tmp single-platform image",
78-
})
93+
base.Cmd("commit", containerID, "testcommitsave").AssertOK()
94+
base.Cmd("save", "testcommitsave").AssertOK()
7995
})
8096
}

hack/build-integration-kubernetes.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ readonly root
2323

2424
GO_VERSION=1.23
2525
KIND_VERSION=v0.24.0
26+
CNI_PLUGINS_VERSION=v1.5.1
2627

2728
[ "$(uname -m)" == "aarch64" ] && GOARCH=arm64 || GOARCH=amd64
2829

@@ -53,6 +54,19 @@ install::kubectl(){
5354
host::install "$temp"/kubectl
5455
}
5556

57+
install::cni(){
58+
local version="$1"
59+
local temp
60+
temp="$(fs::mktemp "install")"
61+
62+
http::get "$temp"/cni.tgz "https://github.com/containernetworking/plugins/releases/download/$version/cni-plugins-${GOOS:-linux}-${GOARCH:-amd64}-$version.tgz"
63+
sudo mkdir -p /opt/cni/bin
64+
sudo tar xzf "$temp"/cni.tgz -C /opt/cni/bin
65+
mkdir -p ~/opt/cni/bin
66+
tar xzf "$temp"/cni.tgz -C ~/opt/cni/bin
67+
rm "$temp"/cni.tgz
68+
}
69+
5670
exec::kind(){
5771
local args=()
5872
[ ! "$_rootful" ] || args=(sudo env PATH="$PATH" KIND_EXPERIMENTAL_PROVIDER="$KIND_EXPERIMENTAL_PROVIDER")
@@ -85,6 +99,9 @@ main(){
8599
PATH=$(pwd)/_output:"$PATH"
86100
export PATH
87101

102+
# Add CNI plugins
103+
install::cni "$CNI_PLUGINS_VERSION"
104+
88105
# Hack to get go into kind control plane
89106
exec::nerdctl rm -f go-kind 2>/dev/null || true
90107
exec::nerdctl run -d --name go-kind golang:"$GO_VERSION" sleep Inf
@@ -97,4 +114,4 @@ main(){
97114
exec::kind create cluster --name nerdctl-test --config=./hack/kind.yaml
98115
}
99116

100-
main "$@"
117+
main "$@"

hack/kind.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ nodes:
1010
containerPath: /usr/local/go
1111
- hostPath: .
1212
containerPath: /nerdctl-source
13+
- hostPath: /opt/cni
14+
containerPath: /opt/cni

pkg/cmd/container/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func Commit(ctx context.Context, client *containerd.Client, rawRef string, req s
5757
if found.MatchCount > 1 {
5858
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
5959
}
60-
imageID, err := commit.Commit(ctx, client, found.Container, opts)
60+
imageID, err := commit.Commit(ctx, client, found.Container, opts, options.GOptions)
6161
if err != nil {
6262
return err
6363
}

pkg/cmd/image/tag.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
containerd "github.com/containerd/containerd/v2/client"
2424
"github.com/containerd/errdefs"
25+
"github.com/containerd/log"
2526

2627
"github.com/containerd/nerdctl/v2/pkg/api/types"
2728
"github.com/containerd/nerdctl/v2/pkg/idutil/imagewalker"
@@ -68,7 +69,8 @@ func Tag(ctx context.Context, client *containerd.Client, options types.ImageTagO
6869

6970
err = imgutil.EnsureAllContent(ctx, client, srcName, img.Target, "", options.GOptions)
7071
if err != nil {
71-
return err
72+
log.G(ctx).Warn("Unable to fetch missing layers before tagging. " +
73+
"If you try to save or push this image, it might fail. See https://github.com/containerd/nerdctl/issues/3439.")
7274
}
7375

7476
if _, err = imageService.Create(ctx, img); err != nil {

pkg/imgutil/commit/commit.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
"github.com/containerd/log"
4545
"github.com/containerd/platforms"
4646

47+
"github.com/containerd/nerdctl/v2/pkg/api/types"
4748
imgutil "github.com/containerd/nerdctl/v2/pkg/imgutil"
4849
"github.com/containerd/nerdctl/v2/pkg/labels"
4950
)
@@ -65,7 +66,7 @@ var (
6566
emptyDigest = digest.Digest("")
6667
)
6768

68-
func Commit(ctx context.Context, client *containerd.Client, container containerd.Container, opts *Opts) (digest.Digest, error) {
69+
func Commit(ctx context.Context, client *containerd.Client, container containerd.Container, opts *Opts, options types.GlobalCommandOptions) (digest.Digest, error) {
6970
id := container.ID()
7071
info, err := container.Info(ctx)
7172
if err != nil {
@@ -96,6 +97,13 @@ func Commit(ctx context.Context, client *containerd.Client, container containerd
9697
return emptyDigest, err
9798
}
9899

100+
// Ensure all the layers are here: https://github.com/containerd/nerdctl/issues/3425
101+
err = imgutil.EnsureAllContent(ctx, client, baseImg.Name(), baseImg.Target(), "", options)
102+
if err != nil {
103+
log.G(ctx).Warn("Unable to fetch missing layers before tagging. " +
104+
"If you try to save or push this image, it might fail. See https://github.com/containerd/nerdctl/issues/3439.")
105+
}
106+
99107
if opts.Pause {
100108
task, err := container.Task(ctx, cio.Load)
101109
if err != nil {

0 commit comments

Comments
 (0)