From d771699df91b326b10149fe8979826231c372901 Mon Sep 17 00:00:00 2001 From: Helder Correia <174525+helderco@users.noreply.github.com> Date: Mon, 28 Mar 2022 18:06:46 +0000 Subject: [PATCH] Move connecting socket to `client: network` Signed-off-by: Helder Correia <174525+helderco@users.noreply.github.com> --- docs/drafts/plans/docker-cli-load/local.cue | 4 +- docs/drafts/plans/docker-cli-run/local.cue | 4 +- .../tests/core-concepts/client/plans/unix.cue | 4 +- .../core-concepts/client/plans/windows.cue | 7 +- pkg/dagger.io/dagger/plan.cue | 28 ++++++-- pkg/dagger.io/dagger/types.cue | 3 +- .../docker/cli/test/load.cue | 6 +- .../docker/cli/test/run.cue | 6 +- plan/task/clientfilesystemread.go | 26 -------- plan/task/clientnetwork.go | 64 +++++++++++++++++++ tests/plan.bats | 16 ++--- .../read/service => network}/invalid.cue | 4 +- .../read/service => network}/valid.cue | 4 +- .../read/service => network}/windows.cue | 4 +- tests/tasks/exec/mount_service.cue | 4 +- 15 files changed, 117 insertions(+), 67 deletions(-) create mode 100644 plan/task/clientnetwork.go rename tests/plan/client/{filesystem/read/service => network}/invalid.cue (77%) rename tests/plan/client/{filesystem/read/service => network}/valid.cue (77%) rename tests/plan/client/{filesystem/read/service => network}/windows.cue (77%) diff --git a/docs/drafts/plans/docker-cli-load/local.cue b/docs/drafts/plans/docker-cli-load/local.cue index 16078868..e92fde82 100644 --- a/docs/drafts/plans/docker-cli-load/local.cue +++ b/docs/drafts/plans/docker-cli-load/local.cue @@ -7,7 +7,7 @@ import ( ) dagger.#Plan & { - client: filesystem: "/var/run/docker.sock": read: contents: dagger.#Socket + client: network: "unix:///var/run/docker.sock": connect: dagger.#Socket actions: { build: docker.#Build & { @@ -16,7 +16,7 @@ dagger.#Plan & { load: cli.#Load & { image: build.output - host: client.filesystem."/var/run/docker.sock".read.contents + host: client.network."unix:///var/run/docker.sock".connect tag: "myimage" } } diff --git a/docs/drafts/plans/docker-cli-run/local.cue b/docs/drafts/plans/docker-cli-run/local.cue index 756624b4..c8da6d8a 100644 --- a/docs/drafts/plans/docker-cli-run/local.cue +++ b/docs/drafts/plans/docker-cli-run/local.cue @@ -6,10 +6,10 @@ import ( ) dagger.#Plan & { - client: filesystem: "/var/run/docker.sock": read: contents: dagger.#Socket + client: network: "unix:///var/run/docker.sock": connect: dagger.#Socket actions: run: cli.#Run & { - host: client.filesystem."/var/run/docker.sock".read.contents + host: client.network."unix:///var/run/docker.sock".connect command: name: "info" } } diff --git a/docs/tests/core-concepts/client/plans/unix.cue b/docs/tests/core-concepts/client/plans/unix.cue index 7a808673..d8c0f483 100644 --- a/docs/tests/core-concepts/client/plans/unix.cue +++ b/docs/tests/core-concepts/client/plans/unix.cue @@ -1,5 +1,5 @@ dagger.#Plan & { - client: filesystem: "/var/run/docker.sock": read: contents: dagger.#Socket + client: network: "unix:///var/run/docker.sock": connect: dagger.#Socket actions: { image: alpine.#Build & { @@ -9,7 +9,7 @@ dagger.#Plan & { input: image.output mounts: docker: { dest: "/var/run/docker.sock" - contents: client.filesystem."/var/run/docker.sock".read.contents + contents: client.network."unix:///var/run/docker.sock".connect } command: { name: "docker" diff --git a/docs/tests/core-concepts/client/plans/windows.cue b/docs/tests/core-concepts/client/plans/windows.cue index bfb25842..42807d09 100644 --- a/docs/tests/core-concepts/client/plans/windows.cue +++ b/docs/tests/core-concepts/client/plans/windows.cue @@ -1,8 +1,5 @@ dagger.#Plan & { - client: filesystem: "//./pipe/docker_engine": read: { - contents: dagger.#Socket - type: "npipe" - } + client: network: "npipe:////./pipe/docker_engine": connect: dagger.#Socket actions: { image: alpine.#Build & { @@ -12,7 +9,7 @@ dagger.#Plan & { input: image.output mounts: docker: { dest: "/var/run/docker.sock" - contents: client.filesystem."//./pipe/docker_engine".read.contents + contents: client.network."npipe:////./pipe/docker_engine".connect } command: { name: "docker" diff --git a/pkg/dagger.io/dagger/plan.cue b/pkg/dagger.io/dagger/plan.cue index f678e0f8..55020e03 100644 --- a/pkg/dagger.io/dagger/plan.cue +++ b/pkg/dagger.io/dagger/plan.cue @@ -23,6 +23,11 @@ package dagger } } + // Access client network endpoints + network: [address=#Address]: _#clientNetwork & { + "address": address + } + // Access client environment variables env: _#clientEnv @@ -65,13 +70,6 @@ _#clientFilesystemRead: { // Filename patterns to exclude // Example: ["node_modules"] exclude?: [...string] - } | { - // CUE type defines expected content: - // #Socket: unix socket or npipe - contents: #Socket - - // Type of service - type: *"unix" | "npipe" } } @@ -93,6 +91,22 @@ _#clientFilesystemWrite: { } } +_#clientNetwork: { + $dagger: task: _name: "ClientNetwork" + + // URL to the socket + // Example: unix:///var/run/docker.sock + address: #Address + + { + // unix socket or npipe + connect: #Socket + // } | { + // // FIXME: not yet implemented + // listen: #Socket + } +} + _#clientEnv: { $dagger: task: _name: "ClientEnv" diff --git a/pkg/dagger.io/dagger/types.cue b/pkg/dagger.io/dagger/types.cue index ba9f992b..db9fec2d 100644 --- a/pkg/dagger.io/dagger/types.cue +++ b/pkg/dagger.io/dagger/types.cue @@ -34,4 +34,5 @@ package dagger } // A network service address -#Address: string & =~"^(tcp://|unix://|udp://).*" +#Address: string & =~"^(unix://|npipe://).+" +// TODO: #Address: string & =~"^(tcp://|unix://|npipe://|udp://).+" diff --git a/pkg/universe.dagger.io/docker/cli/test/load.cue b/pkg/universe.dagger.io/docker/cli/test/load.cue index 43eeb301..1b247167 100644 --- a/pkg/universe.dagger.io/docker/cli/test/load.cue +++ b/pkg/universe.dagger.io/docker/cli/test/load.cue @@ -10,7 +10,7 @@ import ( ) dagger.#Plan & { - client: filesystem: "/var/run/docker.sock": read: contents: dagger.#Socket + client: network: "unix:///var/run/docker.sock": connect: dagger.#Socket actions: test: { _cli: alpine.#Build & { @@ -30,14 +30,14 @@ dagger.#Plan & { load: cli.#Load & { image: _image.output - host: client.filesystem."/var/run/docker.sock".read.contents + host: client.network."unix:///var/run/docker.sock".connect tag: "dagger:load" } verify: bash.#Run & { input: _cli.output mounts: docker: { - contents: client.filesystem."/var/run/docker.sock".read.contents + contents: client.network."unix:///var/run/docker.sock".connect dest: "/var/run/docker.sock" } env: { diff --git a/pkg/universe.dagger.io/docker/cli/test/run.cue b/pkg/universe.dagger.io/docker/cli/test/run.cue index 71fa536c..4f9dc163 100644 --- a/pkg/universe.dagger.io/docker/cli/test/run.cue +++ b/pkg/universe.dagger.io/docker/cli/test/run.cue @@ -9,11 +9,11 @@ import ( ) dagger.#Plan & { - client: filesystem: "/var/run/docker.sock": read: contents: dagger.#Socket + client: network: "unix:///var/run/docker.sock": connect: dagger.#Socket actions: test: { run: cli.#Run & { - host: client.filesystem."/var/run/docker.sock".read.contents + host: client.network."unix:///var/run/docker.sock".connect command: name: "info" } @@ -33,7 +33,7 @@ dagger.#Plan & { } run: cli.#Run & { input: _cli.output - host: client.filesystem."/var/run/docker.sock".read.contents + host: client.network."unix:///var/run/docker.sock".connect command: { name: "docker" args: ["info"] diff --git a/plan/task/clientfilesystemread.go b/plan/task/clientfilesystemread.go index bbc0f931..792a217b 100644 --- a/plan/task/clientfilesystemread.go +++ b/plan/task/clientfilesystemread.go @@ -79,11 +79,6 @@ func (t clientFilesystemReadTask) readContents(ctx context.Context, pctx *planco return t.readFS(ctx, pctx, s, v, path) } - if plancontext.IsServiceValue(contents) { - lg.Debug().Str("path", path).Msg("loading local service") - return t.readService(pctx, v, path) - } - if plancontext.IsSecretValue(contents) { lg.Debug().Str("path", path).Msg("loading local secret file") return t.readSecret(pctx, path) @@ -156,27 +151,6 @@ func (t clientFilesystemReadTask) readFS(ctx context.Context, pctx *plancontext. return fs.MarshalCUE(), nil } -func (t clientFilesystemReadTask) readService(pctx *plancontext.Context, v *compiler.Value, path string) (*compiler.Value, error) { - typ, err := v.Lookup("type").String() - if err != nil { - return nil, err - } - - var unix, npipe string - - switch typ { - case "unix": - unix = path - case "npipe": - npipe = path - default: - return nil, fmt.Errorf("invalid socket type %q", typ) - } - - service := pctx.Services.New(unix, npipe) - return service.MarshalCUE(), nil -} - func (t clientFilesystemReadTask) readSecret(pctx *plancontext.Context, path string) (*compiler.Value, error) { contents, err := t.readString(path) if err != nil { diff --git a/plan/task/clientnetwork.go b/plan/task/clientnetwork.go new file mode 100644 index 00000000..7b1330b8 --- /dev/null +++ b/plan/task/clientnetwork.go @@ -0,0 +1,64 @@ +package task + +import ( + "context" + "errors" + "fmt" + "net/url" + "os" + + "github.com/rs/zerolog/log" + "go.dagger.io/dagger/compiler" + "go.dagger.io/dagger/plancontext" + "go.dagger.io/dagger/solver" +) + +func init() { + Register("ClientNetwork", func() Task { return &clientNetwork{} }) +} + +type clientNetwork struct { +} + +func (t clientNetwork) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) { + lg := log.Ctx(ctx) + + addr, err := v.Lookup("address").String() + if err != nil { + return nil, err + } + + u, err := url.Parse(addr) + if err != nil { + return nil, err + } + + lg.Debug().Str("type", u.Scheme).Str("path", u.Path).Msg("loading local socket") + + if _, err := os.Stat(u.Path); errors.Is(err, os.ErrNotExist) { + return nil, fmt.Errorf("path %q does not exist", u.Path) + } + + var unix, npipe string + + switch u.Scheme { + case "unix": + unix = u.Path + case "npipe": + npipe = u.Path + default: + return nil, fmt.Errorf("invalid service type %q", u.Scheme) + } + + connect := v.Lookup("connect") + + if !plancontext.IsServiceValue(connect) { + return nil, fmt.Errorf("wrong type %q", connect.Kind()) + } + + service := pctx.Services.New(unix, npipe) + + return compiler.NewValue().FillFields(map[string]interface{}{ + "connect": service.MarshalCUE(), + }) +} diff --git a/tests/plan.bats b/tests/plan.bats index 90260316..8af74b72 100644 --- a/tests/plan.bats +++ b/tests/plan.bats @@ -104,14 +104,6 @@ setup() { assert_output --partial "unexpected concrete value" } -@test "plan/client/filesystem/read/service" { - cd "$TESTDIR" - "$DAGGER" "do" -p ./plan/client/filesystem/read/service/valid.cue test - - run "$DAGGER" "do" -p ./plan/client/filesystem/read/service/invalid.cue test - assert_failure -} - @test "plan/client/filesystem/write fs" { cd "$TESTDIR/plan/client/filesystem/write" @@ -158,6 +150,14 @@ setup() { rm -f test.txt } +@test "plan/client/network" { + cd "$TESTDIR" + "$DAGGER" "do" -p ./plan/client/network/valid.cue test + + run "$DAGGER" "do" -p ./plan/client/network/invalid.cue test + assert_failure +} + @test "plan/client/env usage" { cd "${TESTDIR}" diff --git a/tests/plan/client/filesystem/read/service/invalid.cue b/tests/plan/client/network/invalid.cue similarity index 77% rename from tests/plan/client/filesystem/read/service/invalid.cue rename to tests/plan/client/network/invalid.cue index 40e14d1d..4adc1ee2 100644 --- a/tests/plan/client/filesystem/read/service/invalid.cue +++ b/tests/plan/client/network/invalid.cue @@ -6,7 +6,7 @@ import ( ) dagger.#Plan & { - client: filesystem: "/var/run/docker.soc": read: contents: dagger.#Socket + client: network: "unix:///var/run/docker.soc": connect: dagger.#Socket actions: { image: core.#Pull & { @@ -22,7 +22,7 @@ dagger.#Plan & { input: imageWithDocker.output mounts: docker: { dest: "/var/run/docker.sock" - contents: client.filesystem."/var/run/docker.soc".read.contents + contents: client.network."unix:///var/run/docker.soc".connect } args: ["docker", "info"] } diff --git a/tests/plan/client/filesystem/read/service/valid.cue b/tests/plan/client/network/valid.cue similarity index 77% rename from tests/plan/client/filesystem/read/service/valid.cue rename to tests/plan/client/network/valid.cue index f2d8982f..4ad990ba 100644 --- a/tests/plan/client/filesystem/read/service/valid.cue +++ b/tests/plan/client/network/valid.cue @@ -6,7 +6,7 @@ import ( ) dagger.#Plan & { - client: filesystem: "/var/run/docker.sock": read: contents: dagger.#Socket + client: network: "unix:///var/run/docker.sock": connect: dagger.#Socket actions: { image: core.#Pull & { @@ -22,7 +22,7 @@ dagger.#Plan & { input: imageWithDocker.output mounts: docker: { dest: "/var/run/docker.sock" - contents: client.filesystem."/var/run/docker.sock".read.contents + contents: client.network."unix:///var/run/docker.sock".connect } args: ["docker", "info"] } diff --git a/tests/plan/client/filesystem/read/service/windows.cue b/tests/plan/client/network/windows.cue similarity index 77% rename from tests/plan/client/filesystem/read/service/windows.cue rename to tests/plan/client/network/windows.cue index 0c47e368..63e4451f 100644 --- a/tests/plan/client/filesystem/read/service/windows.cue +++ b/tests/plan/client/network/windows.cue @@ -6,7 +6,7 @@ import ( ) dagger.#Plan & { - client: filesystem: "//./pipe/docker_engine": read: contents: dagger.#Socket + client: network: "npipe:////./pipe/docker_engine": connect: dagger.#Socket actions: { image: core.#Pull & { @@ -22,7 +22,7 @@ dagger.#Plan & { input: imageWithDocker.output mounts: docker: { dest: "/var/run/docker.sock" - contents: client.filesystem."//./pipe/docker_engine".read.contents + contents: client.network."npipe:////./pipe/docker_engine".connect } args: ["docker", "info"] } diff --git a/tests/tasks/exec/mount_service.cue b/tests/tasks/exec/mount_service.cue index 85c39483..ba99a3fd 100644 --- a/tests/tasks/exec/mount_service.cue +++ b/tests/tasks/exec/mount_service.cue @@ -6,7 +6,7 @@ import ( ) dagger.#Plan & { - client: filesystem: "/var/run/docker.sock": read: contents: dagger.#Socket + client: network: "unix:///var/run/docker.sock": connect: dagger.#Socket actions: { image: core.#Pull & { @@ -22,7 +22,7 @@ dagger.#Plan & { input: imageWithDocker.output mounts: docker: { dest: "/var/run/docker.sock" - contents: client.filesystem."/var/run/docker.sock".read.contents + contents: client.network."unix:///var/run/docker.sock".connect } args: ["docker", "info"] }