Skip to content

Commit 8b55f87

Browse files
committed
Support multi-arch builds via --remote-builder in publish
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) <alexellis2@gmail.com>
1 parent cf22c9e commit 8b55f87

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

builder/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ func BuildImage(image string, handler string, functionName string, language stri
8080

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

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

87-
res, err := callBuilder(tarPath, tempPath, remoteBuilder, functionName, payloadSecretPath)
87+
res, err := callBuilder(tarPath, remoteBuilder, functionName, payloadSecretPath)
8888
if err != nil {
8989
return err
9090
}

builder/publish.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ import (
2626
hmac "github.com/alexellis/hmac/v2"
2727
)
2828

29-
type builderConfig struct {
29+
type buildConfig struct {
3030
Image string `json:"image"`
31+
Frontend string `json:"frontend,omitempty"`
3132
BuildArgs map[string]string `json:"buildArgs,omitempty"`
33+
Platforms []string `json:"platforms,omitempty"`
3234
}
3335

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

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

90-
if err := makeTar(builderConfig{Image: imageName, BuildArgs: buildArgMap}, path.Join("build", functionName), tarPath); err != nil {
92+
builderPlatforms := strings.Split(platforms, ",")
93+
94+
if err := makeTar(buildConfig{Image: imageName, BuildArgs: buildArgMap, Platforms: builderPlatforms}, path.Join("build", functionName), tarPath); err != nil {
9195
return fmt.Errorf("failed to create tar file for %s, error: %w", functionName, err)
9296
}
9397

94-
res, err := callBuilder(tarPath, tempPath, remoteBuilder, functionName, payloadSecretPath)
98+
res, err := callBuilder(tarPath, remoteBuilder, functionName, payloadSecretPath)
9599
if err != nil {
96100
return err
97101
}
@@ -200,7 +204,7 @@ func applyTag(index int, baseImage, tag string) string {
200204
return fmt.Sprintf("%s:%s", baseImage[:index], tag)
201205
}
202206

203-
func makeTar(buildConfig builderConfig, base, tarPath string) error {
207+
func makeTar(buildConfig buildConfig, base, tarPath string) error {
204208
configBytes, _ := json.Marshal(buildConfig)
205209
if err := os.WriteFile(path.Join(base, BuilderConfigFilename), configBytes, 0664); err != nil {
206210
return err
@@ -251,7 +255,7 @@ func makeTar(buildConfig builderConfig, base, tarPath string) error {
251255
return err
252256
}
253257

254-
func callBuilder(tarPath, tempPath, builderAddress, functionName, payloadSecretPath string) (*http.Response, error) {
258+
func callBuilder(tarPath, builderAddress, functionName, payloadSecretPath string) (*http.Response, error) {
255259

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

283-
log.Printf("%s invoking the API for build at %s ", functionName, builderAddress)
287+
log.Printf("%s invoking the API for build at %s", functionName, builderAddress)
284288
res, err := http.DefaultClient.Do(r)
285289
if err != nil {
286290
return nil, err

commands/publish.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -171,27 +171,29 @@ func runPublish(cmd *cobra.Command, args []string) error {
171171
fmt.Printf("Ran qemu-user-static --reset. OK.\n")
172172
}
173173

174-
task := v2execute.ExecTask{
175-
Command: "docker",
176-
Args: []string{"buildx",
177-
"create",
178-
"--use",
179-
"--name=multiarch",
180-
"--node=multiarch"},
181-
StreamStdio: false,
182-
Env: []string{"DOCKER_CLI_EXPERIMENTAL=enabled"},
183-
}
174+
if len(remoteBuilder) == 0 {
175+
task := v2execute.ExecTask{
176+
Command: "docker",
177+
Args: []string{"buildx",
178+
"create",
179+
"--use",
180+
"--name=multiarch",
181+
"--node=multiarch"},
182+
StreamStdio: false,
183+
Env: []string{"DOCKER_CLI_EXPERIMENTAL=enabled"},
184+
}
184185

185-
res, err := task.Execute(cmd.Context())
186-
if err != nil {
187-
return err
188-
}
186+
res, err := task.Execute(cmd.Context())
187+
if err != nil {
188+
return err
189+
}
189190

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

194-
fmt.Printf("Created buildx node: \"multiarch\"\n")
195+
fmt.Printf("Created buildx node: \"multiarch\"\n")
196+
}
195197

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

0 commit comments

Comments
 (0)