Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IPFS missing layer issue #3490

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions cmd/nerdctl/issues/issues_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ package issues

import (
"fmt"
"os"
"path/filepath"
"testing"

"gotest.tools/v3/assert"

"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
Expand All @@ -37,6 +42,16 @@ func TestIssue3425(t *testing.T) {

var registry *testregistry.RegistryServer

var ipfsPath string
if rootlessutil.IsRootless() {
var err error
ipfsPath, err = rootlessutil.XDGDataHome()
ipfsPath = filepath.Join(ipfsPath, "ipfs")
assert.NilError(t, err)
} else {
ipfsPath = filepath.Join(os.Getenv("HOME"), ".ipfs")
}

testCase := &test.Case{
Description: "TestIssue3425",
Setup: func(data test.Data, helpers test.Helpers) {
Expand Down Expand Up @@ -126,6 +141,32 @@ func TestIssue3425(t *testing.T) {
},
Expected: test.Expects(0, nil, nil),
},
{
Description: "with ipfs",
Require: test.Require(
nerdtest.Private,
test.Not(test.Windows),
test.Not(nerdtest.Docker),
test.Binary("ipfs"),
),
Env: map[string]string{
"IPFS_PATH": ipfsPath,
},
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("image", "pull", testutil.CommonImage)
helpers.Ensure("run", "-d", "--name", data.Identifier(), testutil.CommonImage)
helpers.Ensure("image", "rm", "-f", testutil.CommonImage)
helpers.Ensure("image", "pull", testutil.CommonImage)
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
helpers.Anyhow("rmi", "-f", data.Identifier())
},
Command: func(data test.Data, helpers test.Helpers) test.Command {
return helpers.Command("image", "push", "ipfs://"+testutil.CommonImage)
},
Expected: test.Expects(0, nil, nil),
},
},
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/cmd/image/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package image
import (
"context"
"errors"
"fmt"
"net/http"
"os"

Expand All @@ -43,7 +42,6 @@ func EnsureAllContent(ctx context.Context, client *containerd.Client, srcName st
imageService := client.ImageService()
img, err := imageService.Get(ctx, srcName)
if err != nil {
fmt.Println("Failed getting imageservice")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Leftovers from initial implementation.

return err
}

Expand Down
13 changes: 12 additions & 1 deletion pkg/cmd/image/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ func Push(ctx context.Context, client *containerd.Client, rawRef string, options
}
log.G(ctx).Infof("pushing image %q to IPFS", ref)

parsedRef, err := distributionref.ParseDockerRef(ref)
if err != nil {
return err
}

// Ensure all the layers are here: https://github.com/containerd/nerdctl/issues/3489
err = EnsureAllContent(ctx, client, parsedRef.String(), options.GOptions)
if err != nil {
return err
}

var ipfsPath string
if options.IpfsAddress != "" {
dir, err := os.MkdirTemp("", "apidirtmp")
Expand All @@ -78,7 +89,7 @@ func Push(ctx context.Context, client *containerd.Client, rawRef string, options
if options.Estargz {
layerConvert = eStargzConvertFunc()
}
c, err := ipfs.Push(ctx, client, ref, layerConvert, options.AllPlatforms, options.Platforms, options.IpfsEnsureImage, ipfsPath)
c, err := ipfs.Push(ctx, client, parsedRef.String(), layerConvert, options.AllPlatforms, options.Platforms, options.IpfsEnsureImage, ipfsPath)
if err != nil {
log.G(ctx).WithError(err).Warnf("ipfs push failed")
return err
Expand Down
7 changes: 1 addition & 6 deletions pkg/ipfs/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/containerd/nerdctl/v2/pkg/idutil/imagewalker"
"github.com/containerd/nerdctl/v2/pkg/imgutil"
"github.com/containerd/nerdctl/v2/pkg/platformutil"
"github.com/containerd/nerdctl/v2/pkg/referenceutil"
)

const ipfsPathEnv = "IPFS_PATH"
Expand Down Expand Up @@ -93,11 +92,7 @@ func Push(ctx context.Context, client *containerd.Client, rawRef string, layerCo
log.G(ctx).WithError(err).Warnf("failed to ensure the existence of image %q", rawRef)
}
}
ref, err := referenceutil.ParseAny(rawRef)
if err != nil {
return "", err
}
return ipfs.PushWithIPFSPath(ctx, client, ref.String(), layerConvert, platMC, &ipath)
return ipfs.PushWithIPFSPath(ctx, client, rawRef, layerConvert, platMC, &ipath)
}

// ensureContentsOfIPFSImage ensures that the entire contents of an existing IPFS image are fully downloaded to containerd.
Expand Down
Loading