Skip to content

Commit c53096f

Browse files
committed
Use Go SDK MakeTar function to prepare remote build archive
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <[email protected]>
1 parent 8b5c765 commit c53096f

File tree

2 files changed

+18
-70
lines changed

2 files changed

+18
-70
lines changed

builder/build.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,21 @@ func BuildImage(image string, handler string, functionName string, language stri
7373
fmt.Printf("Building: %s with %s template. Please wait..\n", imageName, language)
7474

7575
if remoteBuilder != "" {
76-
tempDir, err := os.MkdirTemp(os.TempDir(), "builder-*")
76+
tempDir, err := os.MkdirTemp(os.TempDir(), "openfaas-build-*")
7777
if err != nil {
78-
return fmt.Errorf("failed to create temporary directory for %s, error: %w", functionName, err)
78+
return fmt.Errorf("failed to create temporary directory: %w", err)
7979
}
8080
defer os.RemoveAll(tempDir)
8181

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

84-
if err := makeTar(buildConfig{Image: imageName, BuildArgs: buildArgMap}, path.Join("build", functionName), tarPath); err != nil {
84+
buildConfig := builder.BuildConfig{
85+
Image: imageName,
86+
BuildArgs: map[string]string{},
87+
}
88+
89+
// Prepare a tar archive that contains the build config and build context.
90+
if err := builder.MakeTar(tarPath, path.Join("build", functionName), &buildConfig); err != nil {
8591
return fmt.Errorf("failed to create tar file for %s, error: %w", functionName, err)
8692
}
8793

builder/publish.go

Lines changed: 9 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
44
package builder
55

66
import (
7-
"archive/tar"
87
"bytes"
98
"context"
10-
"encoding/json"
119
"fmt"
12-
"io"
1310
"log"
1411
"net/http"
1512
"net/url"
1613
"os"
1714
"path"
18-
"path/filepath"
1915
"strings"
2016

2117
v2execute "github.com/alexellis/go-execute/v2"
@@ -24,15 +20,6 @@ import (
2420
"github.com/openfaas/go-sdk/builder"
2521
)
2622

27-
type buildConfig struct {
28-
Image string `json:"image"`
29-
Frontend string `json:"frontend,omitempty"`
30-
BuildArgs map[string]string `json:"buildArgs,omitempty"`
31-
Platforms []string `json:"platforms,omitempty"`
32-
}
33-
34-
const BuilderConfigFilename = "com.openfaas.docker.config"
35-
3623
// PublishImage will publish images as multi-arch
3724
// TODO: refactor signature to a struct to simplify the length of the method header
3825
func PublishImage(image string, handler string, functionName string, language string, nocache bool, squash bool, shrinkwrap bool, buildArgMap map[string]string,
@@ -78,17 +65,23 @@ func PublishImage(image string, handler string, functionName string, language st
7865
return fmt.Errorf("--pull is not supported with --remote-builder")
7966
}
8067

81-
tempDir, err := os.MkdirTemp(os.TempDir(), "builder-*")
68+
tempDir, err := os.MkdirTemp(os.TempDir(), "openfaas-build-*")
8269
if err != nil {
83-
return fmt.Errorf("failed to create temporary directory for %s, error: %w", functionName, err)
70+
return fmt.Errorf("failed to create temporary directory: %w", err)
8471
}
8572
defer os.RemoveAll(tempDir)
8673

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

8976
builderPlatforms := strings.Split(platforms, ",")
77+
buildConfig := builder.BuildConfig{
78+
Image: imageName,
79+
BuildArgs: map[string]string{},
80+
Platforms: builderPlatforms,
81+
}
9082

91-
if err := makeTar(buildConfig{Image: imageName, BuildArgs: buildArgMap, Platforms: builderPlatforms}, path.Join("build", functionName), tarPath); err != nil {
83+
// Prepare a tar archive that contains the build config and build context.
84+
if err := builder.MakeTar(tarPath, path.Join("build", functionName), &buildConfig); err != nil {
9285
return fmt.Errorf("failed to create tar file for %s, error: %w", functionName, err)
9386
}
9487

@@ -207,54 +200,3 @@ func getDockerBuildxCommand(build dockerBuild) (string, []string) {
207200
func applyTag(index int, baseImage, tag string) string {
208201
return fmt.Sprintf("%s:%s", baseImage[:index], tag)
209202
}
210-
211-
func makeTar(buildConfig buildConfig, base, tarPath string) error {
212-
configBytes, _ := json.Marshal(buildConfig)
213-
if err := os.WriteFile(path.Join(base, BuilderConfigFilename), configBytes, 0664); err != nil {
214-
return err
215-
}
216-
217-
tarFile, err := os.Create(tarPath)
218-
if err != nil {
219-
return err
220-
}
221-
222-
tarWriter := tar.NewWriter(tarFile)
223-
defer tarWriter.Close()
224-
225-
err = filepath.Walk(base, func(path string, f os.FileInfo, pathErr error) error {
226-
if pathErr != nil {
227-
return pathErr
228-
}
229-
230-
targetFile, err := os.Open(path)
231-
if err != nil {
232-
return err
233-
}
234-
235-
header, err := tar.FileInfoHeader(f, f.Name())
236-
if err != nil {
237-
return err
238-
}
239-
240-
header.Name = strings.TrimPrefix(path, base)
241-
if header.Name != fmt.Sprintf("/%s", BuilderConfigFilename) {
242-
header.Name = filepath.Join("context", header.Name)
243-
}
244-
245-
header.Name = strings.TrimPrefix(header.Name, "/")
246-
247-
if err := tarWriter.WriteHeader(header); err != nil {
248-
return err
249-
}
250-
251-
if f.Mode().IsDir() {
252-
return nil
253-
}
254-
255-
_, err = io.Copy(tarWriter, targetFile)
256-
return err
257-
})
258-
259-
return err
260-
}

0 commit comments

Comments
 (0)