44package builder
55
66import (
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
3825func 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) {
207200func 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