From 3af9ba147ef25d34ed20a4e5af0795fb252e8053 Mon Sep 17 00:00:00 2001 From: Vasek - Tom C Date: Wed, 22 Dec 2021 14:59:26 +0100 Subject: [PATCH 1/4] Add `#Platform` field to `#Plan` Signed-off-by: Vasek - Tom C --- stdlib/europa/dagger/engine/plan.cue | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/stdlib/europa/dagger/engine/plan.cue b/stdlib/europa/dagger/engine/plan.cue index 99f242e3..187d71cf 100644 --- a/stdlib/europa/dagger/engine/plan.cue +++ b/stdlib/europa/dagger/engine/plan.cue @@ -27,6 +27,9 @@ package engine // Forward network services to and from the client proxy: [endpoint=string]: _#proxyEndpoint + // Configure platform execution + platform?: #Platform + // Execute actions in containers actions: { ... @@ -139,3 +142,7 @@ _#proxyEndpoint: { // A network service address #Address: string & =~"^(tcp://|unix://|udp://).*" + +// Platform supported by buildkit daemon +#Platform: "linux/amd64" | "linux/arm64" | "linux/arm/v7" | "linux/arm/v6" | + "linux/s390x" | "linux/ppc64le" | "darwin/amd64" | "windows/amd64" From c08f262ef5f25396a66e886360cf013c1dd5cc03 Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Thu, 23 Dec 2021 15:27:03 +0100 Subject: [PATCH 2/4] Add platform configuration in `plan.go` to set platform in context. Signed-off-by: Vasek - Tom C --- plan/plan.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/plan/plan.go b/plan/plan.go index 920f5ac7..74ad9207 100644 --- a/plan/plan.go +++ b/plan/plan.go @@ -48,6 +48,10 @@ func Load(ctx context.Context, args ...string) (*Plan, error) { return nil, err } + if err := p.configPlatform(); err != nil { + return nil, err + } + return p, nil } @@ -59,6 +63,31 @@ func (p *Plan) Source() *compiler.Value { return p.source } +// configPlatform load the platform specified in the context +// Buildkit will then run every operation using that platform +// If platform is not define, context keep default platform +func (p *Plan) configPlatform() error { + platformField := p.source.Lookup("platform") + + // Ignore if platform is not set in `#Plan` + if !platformField.Exists() { + return nil + } + + // Convert platform to string + platform, err := platformField.String() + if err != nil { + return err + } + + // Set platform to context + err = p.context.Platform.Set(platform) + if err != nil { + return err + } + return nil +} + // registerLocalDirectories scans the context for local imports. // BuildKit requires to known the list of directories ahead of time. func (p *Plan) registerLocalDirs() error { From 01414f80c97826539d5637d37e733f827906059a Mon Sep 17 00:00:00 2001 From: Vasek - Tom C Date: Thu, 23 Dec 2021 15:28:36 +0100 Subject: [PATCH 3/4] Add tests on plan platform configuration Signed-off-by: Vasek - Tom C --- tests/plan.bats | 14 ++++++++ ...nfig_platform_failure_invalid_platform.cue | 32 +++++++++++++++++++ .../platform/config_platform_linux_amd64.cue | 32 +++++++++++++++++++ .../platform/config_platform_linux_arm64.cue | 32 +++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 tests/plan/platform/config_platform_failure_invalid_platform.cue create mode 100644 tests/plan/platform/config_platform_linux_amd64.cue create mode 100644 tests/plan/platform/config_platform_linux_arm64.cue diff --git a/tests/plan.bats b/tests/plan.bats index c5a92ce1..ef320499 100644 --- a/tests/plan.bats +++ b/tests/plan.bats @@ -77,3 +77,17 @@ setup() { "$DAGGER" --europa up ./outputs.cue assert [ -f "./out/test" ] } + +@test "plan/platform" { + cd "$TESTDIR" + + # Run with amd64 platform + run "$DAGGER" --europa up ./plan/platform/config_platform_linux_amd64.cue + + # Run with arm64 platform + run "$DAGGER" --europa up ./plan/platform/config_platform_linux_arm64.cue + + # Run with invalid platform + run "$DAGGER" --europa up ./plan/platform/config_platform_failure_invalid_platform.cue + assert_failure +} \ No newline at end of file diff --git a/tests/plan/platform/config_platform_failure_invalid_platform.cue b/tests/plan/platform/config_platform_failure_invalid_platform.cue new file mode 100644 index 00000000..457a3cf6 --- /dev/null +++ b/tests/plan/platform/config_platform_failure_invalid_platform.cue @@ -0,0 +1,32 @@ +package main + +import ( + "alpha.dagger.io/europa/dagger/engine" +) + +engine.#Plan & { + platform: "linux/unknown" + + actions: { + image: engine.#Pull & { + source: "alpine:3.15.0" + } + + writeArch: engine.#Exec & { + input: image.output + always: true + args: [ + "sh", "-c", #""" + echo -n $(uname -m) >> /arch.txt + """#, + ] + } + + verify: engine.#ReadFile & { + input: writeArch.output + path: "/arch.txt" + } & { + contents: "s390x" + } + } +} diff --git a/tests/plan/platform/config_platform_linux_amd64.cue b/tests/plan/platform/config_platform_linux_amd64.cue new file mode 100644 index 00000000..e1902bb3 --- /dev/null +++ b/tests/plan/platform/config_platform_linux_amd64.cue @@ -0,0 +1,32 @@ +package main + +import ( + "alpha.dagger.io/europa/dagger/engine" +) + +engine.#Plan & { + platform: "linux/amd64" + + actions: { + image: engine.#Pull & { + source: "alpine:3.15.0" + } + + writeArch: engine.#Exec & { + input: image.output + always: true + args: [ + "sh", "-c", #""" + echo -n $(uname -m) >> /arch.txt + """#, + ] + } + + verify: engine.#ReadFile & { + input: writeArch.output + path: "/arch.txt" + } & { + contents: "x86_64" + } + } +} diff --git a/tests/plan/platform/config_platform_linux_arm64.cue b/tests/plan/platform/config_platform_linux_arm64.cue new file mode 100644 index 00000000..eb497405 --- /dev/null +++ b/tests/plan/platform/config_platform_linux_arm64.cue @@ -0,0 +1,32 @@ +package main + +import ( + "alpha.dagger.io/europa/dagger/engine" +) + +engine.#Plan & { + platform: "linux/arm64" + + actions: { + image: engine.#Pull & { + source: "alpine:3.15.0" + } + + writeArch: engine.#Exec & { + input: image.output + always: true + args: [ + "sh", "-c", #""" + echo -n $(uname -m) >> /arch.txt + """#, + ] + } + + verify: engine.#ReadFile & { + input: writeArch.output + path: "/arch.txt" + } & { + contents: "aarch64" + } + } +} From 9927b8d94d88abf48109574a4087874ca0f401fe Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Thu, 23 Dec 2021 16:21:32 +0100 Subject: [PATCH 4/4] Refactor platform type to `string` to support any kind of platform Signed-off-by: Vasek - Tom C --- stdlib/europa/dagger/engine/plan.cue | 6 +----- tests/plan/outputs/outputs.cue | 8 ++++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/stdlib/europa/dagger/engine/plan.cue b/stdlib/europa/dagger/engine/plan.cue index 187d71cf..2d444b33 100644 --- a/stdlib/europa/dagger/engine/plan.cue +++ b/stdlib/europa/dagger/engine/plan.cue @@ -28,7 +28,7 @@ package engine proxy: [endpoint=string]: _#proxyEndpoint // Configure platform execution - platform?: #Platform + platform?: string // Execute actions in containers actions: { @@ -142,7 +142,3 @@ _#proxyEndpoint: { // A network service address #Address: string & =~"^(tcp://|unix://|udp://).*" - -// Platform supported by buildkit daemon -#Platform: "linux/amd64" | "linux/arm64" | "linux/arm/v7" | "linux/arm/v6" | - "linux/s390x" | "linux/ppc64le" | "darwin/amd64" | "windows/amd64" diff --git a/tests/plan/outputs/outputs.cue b/tests/plan/outputs/outputs.cue index 086b6239..30101e8f 100644 --- a/tests/plan/outputs/outputs.cue +++ b/tests/plan/outputs/outputs.cue @@ -7,10 +7,10 @@ engine.#Plan & { scratch: engine.#Scratch data: engine.#WriteFile & { - input: scratch.output - path: "/test" - mode: 0o600 - contents: "foobar" + input: scratch.output + path: "/test" + permissions: 0o600 + contents: "foobar" } }