Skip to content

Commit

Permalink
IPFS: use ipfs binary instead of library
Browse files Browse the repository at this point in the history
Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
  • Loading branch information
ktock committed Dec 22, 2022
1 parent 49c0e57 commit 234ed05
Show file tree
Hide file tree
Showing 25 changed files with 336 additions and 1,302 deletions.
25 changes: 5 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,6 @@ It does not necessarily mean that the corresponding features are missing in cont
- [:whale: nerdctl compose top](#whale-nerdctl-compose-top)
- [:whale: nerdctl compose version](#whale-nerdctl-compose-version)
- [IPFS management](#ipfs-management)
- [:nerd_face: nerdctl ipfs registry up](#nerd_face-nerdctl-ipfs-registry-up)
- [:nerd_face: nerdctl ipfs registry down](#nerd_face-nerdctl-ipfs-registry-down)
- [:nerd_face: nerdctl ipfs registry serve](#nerd_face-nerdctl-ipfs-registry-serve)
- [Global flags](#global-flags)
- [Unimplemented Docker commands](#unimplemented-docker-commands)
Expand Down Expand Up @@ -1637,26 +1635,13 @@ Flags:

P2P image distribution (IPFS) is completely optional. Your host is NOT connected to any P2P network, unless you opt in to [install and run IPFS daemon](https://docs.ipfs.io/install/).

### :nerd_face: nerdctl ipfs registry up
Start read-only local registry backed by IPFS.
See [`./docs/ipfs.md`](./docs/ipfs.md) for details.

Usage: `nerdctl ipfs registry up [OPTIONS]`

Flags:
- :nerd_face: `--listen-registry`: Address to listen (default `localhost:5050`)
- :nerd_face: `--read-retry-num`: Times to retry query on IPFS (default 0 (no retry))
- :nerd_face: `--read-timeout`: Timeout duration of a read request to IPFS (default 0 (no timeout))

### :nerd_face: nerdctl ipfs registry down
Stop and remove read-only local registry backed by IPFS.
See [`./docs/ipfs.md`](./docs/ipfs.md) for details.

Usage: `nerdctl ipfs registry down`

### :nerd_face: nerdctl ipfs registry serve
Serve read-only registry backed by IPFS on localhost.
Use `nerdctl ipfs registry up`.
This is needed to run `nerdctl build` with pulling base images from IPFS.
Other commands (e.g. `nerdctl push ipfs://<image-name>` and `nerdctl pull ipfs://<CID>`) don't require this.

You need to install `ipfs` command on the host.
See [`./docs/ipfs.md`](./docs/ipfs.md) for details.

Usage: `nerdctl ipfs registry serve [OPTIONS]`

Expand Down
15 changes: 0 additions & 15 deletions cmd/nerdctl/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ If Dockerfile is not present and -f is not specified, it will look for Container
buildCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms)
// #endregion

buildCommand.Flags().Bool("ipfs", false, "Allow pulling base images from IPFS")
buildCommand.Flags().String("iidfile", "", "Write the image ID to the file")
buildCommand.Flags().StringArray("label", nil, "Set metadata for an image")

Expand Down Expand Up @@ -145,20 +144,6 @@ func buildAction(cmd *cobra.Command, args []string) error {
defer cleanup()
}

runIPFSRegistry, err := cmd.Flags().GetBool("ipfs")
if err != nil {
return err
}
if runIPFSRegistry {
logrus.Infof("Ensuring IPFS registry is running")
nerdctlCmd, nerdctlArgs := globalFlags(cmd)
out, err := exec.Command(nerdctlCmd, append(nerdctlArgs, "ipfs", "registry", "up")...).CombinedOutput()
if err != nil {
return fmt.Errorf("failed to start IPFS registry: %v: %v", string(out), err)
}
logrus.Infof("IPFS registry is running: %v", string(out))
}

quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
return err
Expand Down
7 changes: 1 addition & 6 deletions cmd/nerdctl/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/containerd/nerdctl/pkg/ipfs"
"github.com/containerd/nerdctl/pkg/netutil"
"github.com/containerd/nerdctl/pkg/referenceutil"
httpapi "github.com/ipfs/go-ipfs-http-client"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -199,11 +198,7 @@ func getComposer(cmd *cobra.Command, client *containerd.Client) (*composer.Compo

// IPFS reference
if scheme, ref, err := referenceutil.ParseIPFSRefWithScheme(imageName); err == nil {
ipfsClient, err := httpapi.NewLocalApi()
if err != nil {
return err
}
_, err = ipfs.EnsureImage(ctx, client, ipfsClient, cmd.OutOrStdout(), cmd.ErrOrStderr(), snapshotter, scheme, ref,
_, err = ipfs.EnsureImage(ctx, client, cmd.OutOrStdout(), cmd.ErrOrStderr(), snapshotter, scheme, ref,
pullMode, ocispecPlatforms, nil, quiet)
return err
}
Expand Down
7 changes: 0 additions & 7 deletions cmd/nerdctl/compose_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ func newComposeBuildCommand() *cobra.Command {
composeBuildCommand.Flags().Bool("no-cache", false, "Do not use cache when building the image.")
composeBuildCommand.Flags().String("progress", "", "Set type of progress output (auto, plain, tty). Use plain to show container output")

composeBuildCommand.Flags().Bool("ipfs", false, "Allow pulling base images from IPFS during build")

return composeBuildCommand
}

Expand All @@ -51,10 +49,6 @@ func composeBuildAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
enableIPFS, err := cmd.Flags().GetBool("ipfs")
if err != nil {
return err
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
Expand All @@ -70,7 +64,6 @@ func composeBuildAction(cmd *cobra.Command, args []string) error {
Args: buildArg,
NoCache: noCache,
Progress: progress,
IPFS: enableIPFS,
}
return c.Build(ctx, bo, args)
}
6 changes: 0 additions & 6 deletions cmd/nerdctl/compose_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func newComposeRunCommand() *cobra.Command {
composeRunCommand.Flags().Bool("build", false, "Build images before starting containers.")
composeRunCommand.Flags().Bool("quiet-pull", false, "Pull without printing progress information")
composeRunCommand.Flags().Bool("remove-orphans", false, "Remove containers for services not defined in the Compose file.")
composeRunCommand.Flags().Bool("ipfs", false, "Allow pulling base images from IPFS during build")

composeRunCommand.Flags().String("name", "", "Assign a name to the container")
composeRunCommand.Flags().Bool("no-deps", false, "Don't start dependencies")
Expand Down Expand Up @@ -90,10 +89,6 @@ func composeRunAction(cmd *cobra.Command, args []string) error {
if build && noBuild {
return errors.New("--build and --no-build can not be combined")
}
enableIPFS, err := cmd.Flags().GetBool("ipfs")
if err != nil {
return err
}
quietPull, err := cmd.Flags().GetBool("quiet-pull")
if err != nil {
return err
Expand Down Expand Up @@ -185,7 +180,6 @@ func composeRunAction(cmd *cobra.Command, args []string) error {
NoColor: noColor,
NoLogPrefix: noLogPrefix,
ForceBuild: build,
IPFS: enableIPFS,
QuietPull: quietPull,
RemoveOrphans: removeOrphans,

Expand Down
23 changes: 19 additions & 4 deletions cmd/nerdctl/ipfs_build_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/containerd/nerdctl/pkg/testutil"

"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
)

func TestIPFSBuild(t *testing.T) {
testutil.DockerIncompatible(t)
requiresIPFS(t)
testutil.RequireExecutable(t, "ipfs")
testutil.RequiresBuild(t)
base := testutil.NewBase(t)
defer base.Cmd("builder", "prune").Run()
Expand All @@ -47,9 +49,22 @@ CMD ["echo", "nerdctl-build-test-string"]
assert.NilError(t, err)
defer os.RemoveAll(buildCtx)

defer base.Cmd("ipfs", "registry", "down").AssertOK()
base.Cmd("build", "--ipfs", "-t", imageName, buildCtx).AssertOK()
base.Cmd("build", buildCtx, "--ipfs", "-t", imageName).AssertOK()
done := ipfsRegistryUp(t, base)
defer done()
base.Cmd("build", "-t", imageName, buildCtx).AssertOK()
base.Cmd("build", buildCtx, "-t", imageName).AssertOK()

base.Cmd("run", "--rm", imageName).AssertOutContains("nerdctl-build-test-string")
}

func ipfsRegistryUp(t *testing.T, base *testutil.Base, args ...string) (done func() error) {
res := icmd.StartCmd(base.Cmd(append([]string{"ipfs", "registry", "serve"}, args...)...).Cmd)
time.Sleep(time.Second)
assert.Assert(t, res.Cmd.Process != nil)
assert.NilError(t, res.Error)
return func() error {
res.Cmd.Process.Kill()
icmd.WaitOnCmd(3*time.Second, res)
return nil
}
}
9 changes: 5 additions & 4 deletions cmd/nerdctl/ipfs_compose_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func TestIPFSComposeUp(t *testing.T) {
requiresIPFS(t)
testutil.RequireExecutable(t, "ipfs")
testutil.DockerIncompatible(t)
tests := []struct {
name string
Expand Down Expand Up @@ -101,7 +101,7 @@ volumes:
}

func TestIPFSComposeUpBuild(t *testing.T) {
requiresIPFS(t)
testutil.RequireExecutable(t, "ipfs")
testutil.DockerIncompatible(t)
testutil.RequiresBuild(t)
base := testutil.NewBase(t)
Expand All @@ -127,8 +127,9 @@ COPY index.html /usr/share/nginx/html/index.html
comp.WriteFile("Dockerfile", dockerfile)
comp.WriteFile("index.html", indexHTML)

base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d", "--build", "--ipfs").AssertOK()
defer base.Cmd("ipfs", "registry", "down").AssertOK()
done := ipfsRegistryUp(t, base)
defer done()
base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d", "--build").AssertOK()
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run()

resp, err := nettestutil.HTTPGet("http://127.0.0.1:8080", 50, false)
Expand Down
20 changes: 5 additions & 15 deletions cmd/nerdctl/ipfs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ import (
"github.com/containerd/nerdctl/pkg/infoutil"
"github.com/containerd/nerdctl/pkg/rootlessutil"
"github.com/containerd/nerdctl/pkg/testutil"
"github.com/ipfs/go-cid"
httpapi "github.com/ipfs/go-ipfs-http-client"

"gotest.tools/v3/assert"
)

func TestIPFS(t *testing.T) {
requiresIPFS(t)
testutil.RequireExecutable(t, "ipfs")
testutil.DockerIncompatible(t)
base := testutil.NewBase(t)
ipfsCID := pushImageToIPFS(t, base, testutil.AlpineImage)
Expand Down Expand Up @@ -61,7 +59,7 @@ func TestIPFS(t *testing.T) {
}

func TestIPFSCommit(t *testing.T) {
requiresIPFS(t)
testutil.RequireExecutable(t, "ipfs")
// cgroup is required for nerdctl commit
if rootlessutil.IsRootless() && infoutil.CgroupsVersion() == "1" {
t.Skip("test skipped for rootless containers on cgroup v1")
Expand All @@ -86,7 +84,7 @@ func TestIPFSCommit(t *testing.T) {
}

func TestIPFSWithLazyPulling(t *testing.T) {
requiresIPFS(t)
testutil.RequireExecutable(t, "ipfs")
testutil.DockerIncompatible(t)
base := testutil.NewBase(t)
requiresStargz(base)
Expand All @@ -98,7 +96,7 @@ func TestIPFSWithLazyPulling(t *testing.T) {
}

func TestIPFSWithLazyPullingCommit(t *testing.T) {
requiresIPFS(t)
testutil.RequireExecutable(t, "ipfs")
// cgroup is required for nerdctl commit
if rootlessutil.IsRootless() && infoutil.CgroupsVersion() == "1" {
t.Skip("test skipped for rootless containers on cgroup v1")
Expand Down Expand Up @@ -134,13 +132,5 @@ func pushImageToIPFS(t *testing.T, base *testutil.Base, name string, opts ...str

func cidOf(t *testing.T, lines []string) string {
assert.Equal(t, len(lines) >= 2, true)
c, err := cid.Decode(lines[len(lines)-2])
assert.NilError(t, err)
return "ipfs://" + c.String()
}

func requiresIPFS(t *testing.T) {
if _, err := httpapi.NewLocalApi(); err != nil {
t.Skipf("test requires ipfs daemon, but got: %v", err)
}
return "ipfs://" + lines[len(lines)-2]
}
2 changes: 0 additions & 2 deletions cmd/nerdctl/ipfs_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ func newIPFSRegistryCommand() *cobra.Command {
}
cmd.AddCommand(
newIPFSRegistryServeCommand(),
newIPFSRegistryUpCommand(),
newIPFSRegistryDownCommand(),
)
return cmd
}
67 changes: 0 additions & 67 deletions cmd/nerdctl/ipfs_registry_down.go

This file was deleted.

12 changes: 6 additions & 6 deletions cmd/nerdctl/ipfs_registry_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

func TestIPFSRegistry(t *testing.T) {
requiresIPFS(t)
testutil.RequireExecutable(t, "ipfs")
testutil.DockerIncompatible(t)

base := testutil.NewBase(t)
Expand All @@ -34,14 +34,14 @@ func TestIPFSRegistry(t *testing.T) {
ipfsRegistryAddr := "localhost:5555"
ipfsRegistryRef := ipfsRegistryReference(ipfsRegistryAddr, ipfsCID)

base.Cmd("ipfs", "registry", "up", "--listen-registry", ipfsRegistryAddr).AssertOK()
done := ipfsRegistryUp(t, base, "--listen-registry", ipfsRegistryAddr)
defer done()
base.Cmd("pull", ipfsRegistryRef).AssertOK()
base.Cmd("run", "--rm", ipfsRegistryRef, "echo", "hello").AssertOK()
base.Cmd("ipfs", "registry", "down").AssertOK()
}

func TestIPFSRegistryWithLazyPulling(t *testing.T) {
requiresIPFS(t)
testutil.RequireExecutable(t, "ipfs")
testutil.DockerIncompatible(t)

base := testutil.NewBase(t)
Expand All @@ -51,10 +51,10 @@ func TestIPFSRegistryWithLazyPulling(t *testing.T) {
ipfsRegistryAddr := "localhost:5555"
ipfsRegistryRef := ipfsRegistryReference(ipfsRegistryAddr, ipfsCID)

base.Cmd("ipfs", "registry", "up", "--listen-registry", ipfsRegistryAddr).AssertOK()
done := ipfsRegistryUp(t, base, "--listen-registry", ipfsRegistryAddr)
defer done()
base.Cmd("pull", ipfsRegistryRef).AssertOK()
base.Cmd("run", "--rm", ipfsRegistryRef, "ls", "/.stargz-snapshotter").AssertOK()
base.Cmd("ipfs", "registry", "down").AssertOK()
}

func ipfsRegistryReference(addr string, c string) string {
Expand Down
Loading

0 comments on commit 234ed05

Please sign in to comment.