Skip to content

Multiplatform support - #3119

Merged
sipsma merged 9 commits into
dagger:mainfrom
sipsma:platform-dagger
Oct 31, 2022
Merged

sipsma merged 9 commits into
dagger:mainfrom
sipsma:platform-dagger

Conversation

@sipsma

@sipsma sipsma commented Sep 16, 2022 •

Copy link
Copy Markdown
Contributor

Signed-off-by: Erik Sipsma [email protected]

See comment on simplified design based on new API here: #3119 (comment)

Followups ups (after PR)

  • Consider making Platform an Enum. Tempting to do right now but integrating that w/ codegen is too much for this PR. Will make follow up issue if there's agreement
  • Update getting started guide to show multiplatform image push
  • Add multiplatform binary builds and image push to CI

@sipsma
sipsma force-pushed the platform-dagger branch 7 times, most recently from 77df16c to 376587d Compare September 27, 2022 03:27
@sipsma sipsma changed the title WIP: multiplatform support Multiplatform support Sep 27, 2022
@sipsma
sipsma marked this pull request as ready for review September 27, 2022 03:37
@sipsma
sipsma requested review from aluzzardi and vito September 27, 2022 03:37
@netlify

netlify Bot commented Sep 27, 2022 •

Copy link
Copy Markdown

👷 Deploy request for cloak-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 62c33f8

@shykes shykes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Amazing that we can get this feature so quickly :) As we know from 0.2, this is one of the most consistently requested features.

I left a couple comments below.

Comment thread core/core.schema.go Resolved
@sipsma

sipsma commented Oct 5, 2022

Copy link
Copy Markdown
Contributor Author

Just to update on this, focusing intensely on preview launch items, so this PR is suffering as a result. I think it will make the most sense to rebase this on top of the new API (Container/Directory/etc.). As part of that we can also get resolution on the issues discussed here: #3119 (comment)

@aluzzardi
aluzzardi changed the base branch from cloak to main October 14, 2022 18:27
@sipsma

This comment was marked as outdated.

@sipsma

sipsma commented Oct 28, 2022 •

Copy link
Copy Markdown
Contributor Author

After writing the implementation more I'm resigning to an even simpler design.

The problem with the previous one described in this older comment is that supporting WithPlatform implies either:

A) The new platform only applies going forward for new steps in the container

This just isn't very useful and is confusing. E.g. this would either fail or output the original default platform

c.Container().From("alpine").WithPlatform("non-default-platform").Exec("uname -a").Stdout()

B) The platform specified applies "retroactively" to the LLB DAG of the previous container steps.

E.g. the following would result in the output of "non-default-platform" even though the non default platform was specified after you pulled the image and defined the exec:

c.Container().From("alpine").Exec("uname -a").WithPlatform("non-default-platform").Stdout()

This is more intuitive in some ways but ends up being an enormous rabbit hole for a number of different reasons both in terms of UX and implementation details. Mostly things involving mounts and requirements around cross-compilation where a directory/file needs to initially be the buildkit host platform but then converted into a different platform while not triggering a re-run of its history using emulation. I'm happy to explain more for anyone interested, not writing out in detail yet for sake of time.


New even simpler solution:

scalar Platform

extend type Query {
  """
  The default platform of the builder.
  """
  defaultPlatform: Platform!

  """
  Load a container from ID.
  Null ID returns an empty container (scratch).
  Optional platform argument initializes new containers to execute and publish as that platform. Platform defaults to that of the builder's host.
  """
  container(id: ContainerID, platform: Platform): Container!
}

type Container {
  "the platform associated with the Container"
  platform: Platform!

  "same publish as before, but now with the option to include extra Containers that will be used as different platform variants in a multi-arch image."
  publish(address: String!, platformVariants: [ContainerID!]) String
}

Use Cases

Emulated Build + Push

var variants []dagger.ContainerID
for _, plat := range []string{"linux/amd64", "linux/arm64"} {
  builtDir, _ := c.Container(plat).
    From("builder:latest").
    WithMountedDirectory("/src", srcDir).
    WithMountedDirectory("/output", c.Directory()).
    Exec("build /src -o /output").
    Directory("/output")

  variants = append(variants, c.Container(play).WithFS(builtDir).ID())
}

c.Container().Publish("my-multiarch-image:latest", variants)

Emulated Test

for _, plat := range []string{"linux/amd64", "linux/arm64"} {
  _, err := c.Container(plat).
    From("builder:latest").
    WithMountedDirectory("/src", srcDir).
    Exec("build /src -o /output").
    Exec("test /output/bin").
    ExitCode(ctx)
  if err != nil {
    panic(err)
  }
}

Cross Compilation + Push

var variants []dagger.ContainerID
for _, plat := range []string{"linux/amd64", "linux/arm64"} {
    // NOTE: container has no platform override, so it is using the host platform
  builtDir, _ := c.Container().From("builder:latest").
    WithMountedDirectory("/src", srcDir).
    WithMountedDirectory("/output", c.Directory()).
    WithEnv("PLATFORM", plat).
    Exec("build /src --platform $PLATFORM -o /output").
    Directory("/output")

  variants = append(variants, c.Container(plat).WithFS(builtDir).ID())
}

c.Container().Publish("my-multiarch-image:latest", variants)

Extensions

The main reason I was avoiding this solution previously was that it puts extensions in a corner.

This approach forces all extensions that output container/directory/file/etc. to always accept platform as an arg for their APIs and explicitly+correctly handle them. For instance, Alpine Build would have to accept platform because it returns Container. And every user extension ever written that returns or otherwise uses container will have to accept platform as an arg and handle it correctly.

  • This becomes a huge source of boilerplate and potential for bugs forever without any tangible user benefit.

The only viable solution to the above in the longer term that I can think of is the previous approach where we allow changing the default platform with a field right under Query.

However, we can defer that for now. We can safely add it in the future and call it WithDefaultPlatform and make it change the default value of platform in the API when platform is not set. This would be backwards compatible I believe.

We can also consider just reverting back to that approach now too, though I agree it's more one-way-door-y than the approach of putting platform into Container+Directory APIs.

@sipsma
sipsma force-pushed the platform-dagger branch 3 times, most recently from c1eef42 to 6f23011 Compare October 29, 2022 02:33
@sipsma

sipsma commented Oct 29, 2022 •

Copy link
Copy Markdown
Contributor Author

Pushed code implementing the highly simplified design. Unsurprisingly, the code is highly simplified too. Small TODOs/follow-ups listed in the description but it's working and ready to review.

Please let me know if there's concerns with the API ASAP so any adjustments can be made early. The tests are good examples of how to us it for emulation, cross-compilation and for multiplatform image push: https://github.com/dagger/dagger/blob/11accbf00512cb890005c0d3bcbfe47349176b1b/core/integration/platform_test.go

@sipsma
sipsma requested review from aluzzardi, shykes and vito and removed request for aluzzardi and vito October 29, 2022 02:36
@shykes

shykes commented Oct 29, 2022

Copy link
Copy Markdown
Contributor

Sorry for being late to the party. Will try to sneak out and check it out over the weekend, or monday at the latest.

@aluzzardi

Copy link
Copy Markdown
Contributor

However, we can defer that for now. We can safely add it in the future and call it WithDefaultPlatform and make it change the default value of platform in the API when platform is not set. This would be backwards compatible I believe.

Agreed. We can find solutions for this later on. I think WithDefaultPlatform work, but if for some reason we don't want to put that in the schema itself we could make it a dagger.Connect(ctx, dagger.WithPlatform(...))) and pass it as a header or something?

@aluzzardi aluzzardi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I really like the simplicity of this one.

/cc @vito for API review

@shykes shykes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Makes sense, bummer that we can't use withPlatform but I understand the tradeoff. This is nice and simple.

Just a reminder that this is one of the most requested Dagger features since 0.1, and we never managed to ship it until now... So 1) this is an accomplishment to celebrate :) and 2) we should highlight it in the docs, for example with a guide. We should not assume users will find it on their own.

@vito vito left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm!

Comment thread core/shim/shim.go Outdated
Comment on lines +42 to +44
// It's not incredibly efficient to call shimSource every time, but llb.State in general
// is not designed to handle being marshalled multiple times with different args (e.g. platform)
// and will fail in surprising ways.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

😆 I was wondering if we would run into this, I remember hitting it too.

Includes emulation support, support for cross-compilation (converting
host dirs into other platforms) and multiplatform image push.

Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants