diff --git a/client/client.go b/client/client.go index 7d109113..975a9078 100644 --- a/client/client.go +++ b/client/client.go @@ -44,6 +44,8 @@ type Config struct { CacheExports []bk.CacheOptionsEntry CacheImports []bk.CacheOptionsEntry + + TargetPlatform *specs.Platform } func New(ctx context.Context, host string, cfg Config) (*Client, error) { @@ -81,8 +83,9 @@ func (c *Client) Do(ctx context.Context, pctx *plancontext.Context, fn DoFunc) e lg := log.Ctx(ctx) eg, gctx := errgroup.WithContext(ctx) - // if platform is set through plan config, skip detection. - if !pctx.Platform.IsSet() { + if c.cfg.TargetPlatform != nil { + pctx.Platform.Set(*c.cfg.TargetPlatform) + } else { p, err := c.detectPlatform(ctx) if err != nil { return err diff --git a/cmd/dagger/cmd/common/common.go b/cmd/dagger/cmd/common/common.go index b68e16db..114f262d 100644 --- a/cmd/dagger/cmd/common/common.go +++ b/cmd/dagger/cmd/common/common.go @@ -6,7 +6,9 @@ import ( "strings" "cuelang.org/go/cue" + "github.com/containerd/containerd/platforms" "github.com/docker/buildx/util/buildflags" + specs "github.com/opencontainers/image-spec/specs-go/v1" "github.com/rs/zerolog/log" "github.com/spf13/viper" "go.dagger.io/dagger/client" @@ -95,10 +97,21 @@ func NewClient(ctx context.Context) *client.Client { lg.Fatal().Err(err).Msg("unable to parse --cache-from options") } + ep := viper.GetString("experimental-platform") + var p *specs.Platform + if len(ep) > 0 { + pp, err := platforms.Parse(ep) + if err != nil { + lg.Fatal().Err(err).Msg("invalid value for --experimental-platform") + } + p = &pp + } + cl, err := client.New(ctx, "", client.Config{ - CacheExports: cacheExports, - CacheImports: cacheImports, - NoCache: viper.GetBool("no-cache"), + CacheExports: cacheExports, + CacheImports: cacheImports, + NoCache: viper.GetBool("no-cache"), + TargetPlatform: p, }) if err != nil { lg.Fatal().Err(err).Msg("unable to create client") diff --git a/cmd/dagger/cmd/do.go b/cmd/dagger/cmd/do.go index c11c2313..bced8559 100644 --- a/cmd/dagger/cmd/do.go +++ b/cmd/dagger/cmd/do.go @@ -157,6 +157,7 @@ func init() { doCmd.Flags().StringArrayP("with", "w", []string{}, "") doCmd.Flags().StringP("plan", "p", ".", "Path to plan (defaults to current directory)") doCmd.Flags().Bool("no-cache", false, "Disable caching") + doCmd.Flags().String("experimental-platform", "", "Set target build platform (experimental)") doCmd.Flags().StringArray("cache-to", []string{}, "Cache export destinations (eg. user/app:cache, type=local,dest=path/to/dir)") doCmd.Flags().StringArray("cache-from", []string{}, diff --git a/plancontext/platform.go b/plancontext/platform.go index 4ee46b82..47815f8f 100644 --- a/plancontext/platform.go +++ b/plancontext/platform.go @@ -25,7 +25,3 @@ func (c *platformContext) SetString(platform string) error { func (c *platformContext) Set(p specs.Platform) { c.platform = &p } - -func (c *platformContext) IsSet() bool { - return c.platform != nil -} diff --git a/tests/plan.bats b/tests/plan.bats index 69692a71..97b6d894 100644 --- a/tests/plan.bats +++ b/tests/plan.bats @@ -228,15 +228,17 @@ setup() { } @test "plan/platform" { + cd "$TESTDIR" - # Run with amd64 platform - run "$DAGGER" "do" -p./plan/platform/config_platform_linux_amd64.cue verify - - # Run with arm64 platform - run "$DAGGER" "do" -p./plan/platform/config_platform_linux_arm64.cue verify - - # Run with invalid platform - run "$DAGGER" "do" -p./plan/platform/config_platform_failure_invalid_platform.cue verify + # Run with invalid platform format + run "$DAGGER" "do" --experimental-platform invalid -p./plan/platform/platform.cue test assert_failure + assert_output --partial "unknown operating system or architecture: invalid argument" + + + # Run with non-existing platform + run "$DAGGER" "do" --experimental-platform invalid/invalid -p./plan/platform/platform.cue test + assert_failure + assert_output --partial "no match for platform in manifest" } diff --git a/tests/plan/platform/config_platform_failure_invalid_platform.cue b/tests/plan/platform/config_platform_failure_invalid_platform.cue deleted file mode 100644 index 6de027d8..00000000 --- a/tests/plan/platform/config_platform_failure_invalid_platform.cue +++ /dev/null @@ -1,33 +0,0 @@ -package main - -import ( - "dagger.io/dagger" - "dagger.io/dagger/core" -) - -dagger.#Plan & { - platform: "linux/unknown" - - actions: { - image: core.#Pull & { - source: "alpine:3.15.0" - } - - writeArch: core.#Exec & { - input: image.output - always: true - args: [ - "sh", "-c", #""" - echo -n $(uname -m) >> /arch.txt - """#, - ] - } - - verify: core.#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 deleted file mode 100644 index 366482ec..00000000 --- a/tests/plan/platform/config_platform_linux_amd64.cue +++ /dev/null @@ -1,33 +0,0 @@ -package main - -import ( - "dagger.io/dagger" - "dagger.io/dagger/core" -) - -dagger.#Plan & { - platform: "linux/amd64" - - actions: { - image: core.#Pull & { - source: "alpine:3.15.0" - } - - writeArch: core.#Exec & { - input: image.output - always: true - args: [ - "sh", "-c", #""" - echo -n $(uname -m) >> /arch.txt - """#, - ] - } - - verify: core.#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 deleted file mode 100644 index 9ffc213e..00000000 --- a/tests/plan/platform/config_platform_linux_arm64.cue +++ /dev/null @@ -1,33 +0,0 @@ -package main - -import ( - "dagger.io/dagger" - "dagger.io/dagger/core" -) - -dagger.#Plan & { - platform: "linux/arm64" - - actions: { - image: core.#Pull & { - source: "alpine:3.15.0" - } - - writeArch: core.#Exec & { - input: image.output - always: true - args: [ - "sh", "-c", #""" - echo -n $(uname -m) >> /arch.txt - """#, - ] - } - - verify: core.#ReadFile & { - input: writeArch.output - path: "/arch.txt" - } & { - contents: "aarch64" - } - } -} diff --git a/tests/plan/platform/platform.cue b/tests/plan/platform/platform.cue new file mode 100644 index 00000000..7865414b --- /dev/null +++ b/tests/plan/platform/platform.cue @@ -0,0 +1,12 @@ +package main + +import ( + "dagger.io/dagger" + "dagger.io/dagger/core" +) + +dagger.#Plan & { + actions: test: image: core.#Pull & { + source: "alpine:3.15.0" + } +}