Skip to content

Commit

Permalink
Support multi-arch builds via --remote-builder in publish
Browse files Browse the repository at this point in the history
The Function Builder API now supports passing in an array of
platforms for multi-arch builds.

Tested e2e with a K3s cluster running in non-root mode.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Jul 26, 2024
1 parent cf22c9e commit 8b55f87
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
4 changes: 2 additions & 2 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ func BuildImage(image string, handler string, functionName string, language stri

tarPath := path.Join(tempDir, "req.tar")

if err := makeTar(builderConfig{Image: imageName, BuildArgs: buildArgMap}, path.Join("build", functionName), tarPath); err != nil {
if err := makeTar(buildConfig{Image: imageName, BuildArgs: buildArgMap}, path.Join("build", functionName), tarPath); err != nil {
return fmt.Errorf("failed to create tar file for %s, error: %w", functionName, err)
}

res, err := callBuilder(tarPath, tempPath, remoteBuilder, functionName, payloadSecretPath)
res, err := callBuilder(tarPath, remoteBuilder, functionName, payloadSecretPath)
if err != nil {
return err
}
Expand Down
16 changes: 10 additions & 6 deletions builder/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import (
hmac "github.com/alexellis/hmac/v2"
)

type builderConfig struct {
type buildConfig struct {
Image string `json:"image"`
Frontend string `json:"frontend,omitempty"`
BuildArgs map[string]string `json:"buildArgs,omitempty"`
Platforms []string `json:"platforms,omitempty"`
}

type builderResult struct {
Expand Down Expand Up @@ -87,11 +89,13 @@ func PublishImage(image string, handler string, functionName string, language st

tarPath := path.Join(tempDir, "req.tar")

if err := makeTar(builderConfig{Image: imageName, BuildArgs: buildArgMap}, path.Join("build", functionName), tarPath); err != nil {
builderPlatforms := strings.Split(platforms, ",")

if err := makeTar(buildConfig{Image: imageName, BuildArgs: buildArgMap, Platforms: builderPlatforms}, path.Join("build", functionName), tarPath); err != nil {
return fmt.Errorf("failed to create tar file for %s, error: %w", functionName, err)
}

res, err := callBuilder(tarPath, tempPath, remoteBuilder, functionName, payloadSecretPath)
res, err := callBuilder(tarPath, remoteBuilder, functionName, payloadSecretPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -200,7 +204,7 @@ func applyTag(index int, baseImage, tag string) string {
return fmt.Sprintf("%s:%s", baseImage[:index], tag)
}

func makeTar(buildConfig builderConfig, base, tarPath string) error {
func makeTar(buildConfig buildConfig, base, tarPath string) error {
configBytes, _ := json.Marshal(buildConfig)
if err := os.WriteFile(path.Join(base, BuilderConfigFilename), configBytes, 0664); err != nil {
return err
Expand Down Expand Up @@ -251,7 +255,7 @@ func makeTar(buildConfig builderConfig, base, tarPath string) error {
return err
}

func callBuilder(tarPath, tempPath, builderAddress, functionName, payloadSecretPath string) (*http.Response, error) {
func callBuilder(tarPath, builderAddress, functionName, payloadSecretPath string) (*http.Response, error) {

payloadSecret, err := os.ReadFile(payloadSecretPath)
if err != nil {
Expand Down Expand Up @@ -280,7 +284,7 @@ func callBuilder(tarPath, tempPath, builderAddress, functionName, payloadSecretP
r.Header.Set("X-Build-Signature", "sha256="+hex.EncodeToString(digest))
r.Header.Set("Content-Type", "application/octet-stream")

log.Printf("%s invoking the API for build at %s ", functionName, builderAddress)
log.Printf("%s invoking the API for build at %s", functionName, builderAddress)
res, err := http.DefaultClient.Do(r)
if err != nil {
return nil, err
Expand Down
38 changes: 20 additions & 18 deletions commands/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,27 +171,29 @@ func runPublish(cmd *cobra.Command, args []string) error {
fmt.Printf("Ran qemu-user-static --reset. OK.\n")
}

task := v2execute.ExecTask{
Command: "docker",
Args: []string{"buildx",
"create",
"--use",
"--name=multiarch",
"--node=multiarch"},
StreamStdio: false,
Env: []string{"DOCKER_CLI_EXPERIMENTAL=enabled"},
}
if len(remoteBuilder) == 0 {
task := v2execute.ExecTask{
Command: "docker",
Args: []string{"buildx",
"create",
"--use",
"--name=multiarch",
"--node=multiarch"},
StreamStdio: false,
Env: []string{"DOCKER_CLI_EXPERIMENTAL=enabled"},
}

res, err := task.Execute(cmd.Context())
if err != nil {
return err
}
res, err := task.Execute(cmd.Context())
if err != nil {
return err
}

if res.ExitCode != 0 {
return fmt.Errorf("non-zero exit code: %d, stderr: %s", res.ExitCode, res.Stderr)
}
if res.ExitCode != 0 {
return fmt.Errorf("non-zero exit code: %d, stderr: %s", res.ExitCode, res.Stderr)
}

fmt.Printf("Created buildx node: \"multiarch\"\n")
fmt.Printf("Created buildx node: \"multiarch\"\n")
}

if len(services.StackConfiguration.TemplateConfigs) != 0 && !disableStackPull {
newTemplateInfos, err := filterExistingTemplates(services.StackConfiguration.TemplateConfigs, "./template")
Expand Down

0 comments on commit 8b55f87

Please sign in to comment.