Skip to content

Commit

Permalink
Ensure all layers are here when committing
Browse files Browse the repository at this point in the history
Signed-off-by: apostasie <[email protected]>
  • Loading branch information
apostasie committed Sep 18, 2024
1 parent 009096d commit 6aa644c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
15 changes: 9 additions & 6 deletions cmd/nerdctl/container/container_commit_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package container

import (
"fmt"
"strings"
"testing"

"gotest.tools/v3/icmd"

"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/testregistry"
)

/*
Expand All @@ -35,13 +37,14 @@ It will regularly succeed or fail, making random PR fail the Kube check.
func TestKubeCommitPush(t *testing.T) {
t.Parallel()

t.Skip("Test that confirm that #827 is still broken is too flaky")

base := testutil.NewBaseForKubernetes(t)
tID := testutil.Identifier(t)

var containerID string

// Start a registry
reg := testregistry.NewWithNoAuth(base, 0, false)

setup := func() {
testutil.KubectlHelper(base, "run", "--image", testutil.CommonImage, tID, "--", "sleep", "Inf").
AssertOK()
Expand All @@ -59,6 +62,7 @@ func TestKubeCommitPush(t *testing.T) {

tearDown := func() {
testutil.KubectlHelper(base, "delete", "pod", "-f", tID).Run()
reg.Cleanup(nil)
}

tearDown()
Expand All @@ -70,11 +74,10 @@ func TestKubeCommitPush(t *testing.T) {
"Currently, this is broken, hence the test assumes it will fail. Once the problem is fixed, we should just" +
"change the expectation to 'success'.")

base.Cmd("commit", containerID, "registry.example.com/my-app:v1").AssertOK()
base.Cmd("commit", containerID, fmt.Sprintf("127.0.0.1:%d/my-app:v1", reg.Port)).AssertOK()
// See note above.
base.Cmd("push", "registry.example.com/my-app:v1").Assert(icmd.Expected{
ExitCode: 1,
Err: "failed to create a tmp single-platform image",
base.Cmd("push", fmt.Sprintf("127.0.0.1:%d/my-app:v1", reg.Port)).Assert(icmd.Expected{
ExitCode: 0,
})
})
}
2 changes: 1 addition & 1 deletion pkg/cmd/container/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Commit(ctx context.Context, client *containerd.Client, rawRef string, req s
if found.MatchCount > 1 {
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
}
imageID, err := commit.Commit(ctx, client, found.Container, opts)
imageID, err := commit.Commit(ctx, client, found.Container, opts, options.GOptions)
if err != nil {
return err
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/imgutil/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/containerd/log"
"github.com/containerd/platforms"

"github.com/containerd/nerdctl/v2/pkg/api/types"
imgutil "github.com/containerd/nerdctl/v2/pkg/imgutil"
"github.com/containerd/nerdctl/v2/pkg/labels"
)
Expand All @@ -65,7 +66,7 @@ var (
emptyDigest = digest.Digest("")
)

func Commit(ctx context.Context, client *containerd.Client, container containerd.Container, opts *Opts) (digest.Digest, error) {
func Commit(ctx context.Context, client *containerd.Client, container containerd.Container, opts *Opts, options types.GlobalCommandOptions) (digest.Digest, error) {
id := container.ID()
info, err := container.Info(ctx)
if err != nil {
Expand Down Expand Up @@ -96,6 +97,12 @@ func Commit(ctx context.Context, client *containerd.Client, container containerd
return emptyDigest, err
}

// Ensure all the layers are here: https://github.com/containerd/nerdctl/issues/3425
err = imgutil.EnsureAllContent(ctx, client, baseImg.Name(), "", options)
if err != nil {
return emptyDigest, err
}

if opts.Pause {
task, err := container.Task(ctx, cio.Load)
if err != nil {
Expand Down

0 comments on commit 6aa644c

Please sign in to comment.