Skip to content

Commit a9c9fb1

Browse files
committed
Use Go SDK CreateBuildContext function
A version of the creaetBuildContext function has been included in the Go SDK. Srart using the builder.CreateBuildContext function from the Go SDK to reduce code duplication. Signed-off-by: Han Verstraete (OpenFaaS Ltd) <han@openfaas.com>
1 parent 232e1ae commit a9c9fb1

3 files changed

Lines changed: 16 additions & 127 deletions

File tree

builder/build.go

Lines changed: 8 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,18 @@ func BuildImage(image string, handler string, functionName string, language stri
5353
return fmt.Errorf("building %s, %s is an invalid path", functionName, handler)
5454
}
5555

56-
tempPath, err := createBuildContext(functionName, handler, language, isLanguageTemplate(language), langTemplate.HandlerFolder, copyExtraPaths)
56+
opts := []builder.BuildContextOption{}
57+
if len(langTemplate.HandlerFolder) > 0 {
58+
opts = append(opts, builder.WithHandlerOverlay(langTemplate.HandlerFolder))
59+
}
60+
61+
buildContext, err := builder.CreateBuildContext(functionName, handler, language, copyExtraPaths, opts...)
5762
if err != nil {
5863
return err
5964
}
6065

6166
if shrinkwrap {
62-
fmt.Printf("%s shrink-wrapped to %s\n", functionName, tempPath)
67+
fmt.Printf("%s shrink-wrapped to %s\n", functionName, buildContext)
6368
return nil
6469
}
6570

@@ -154,7 +159,7 @@ func BuildImage(image string, handler string, functionName string, language stri
154159
log.Printf("Build flags: %+v\n", args)
155160

156161
task := v2execute.ExecTask{
157-
Cwd: tempPath,
162+
Cwd: buildContext,
158163
Command: command,
159164
Args: args,
160165
StreamStdio: !quietBuild,
@@ -308,101 +313,6 @@ type dockerBuild struct {
308313
ForcePull bool
309314
}
310315

311-
var defaultDirPermissions os.FileMode = 0700
312-
313-
const defaultHandlerFolder string = "function"
314-
315-
// isRunningInCI checks the ENV var CI and returns true if it's set to true or 1
316-
func isRunningInCI() bool {
317-
if env, ok := os.LookupEnv("CI"); ok {
318-
if env == "true" || env == "1" {
319-
return true
320-
}
321-
}
322-
return false
323-
}
324-
325-
// createBuildContext creates temporary build folder to perform a Docker build with language template
326-
func createBuildContext(functionName string, handler string, language string, useFunction bool, handlerFolder string, copyExtraPaths []string) (string, error) {
327-
tempPath := fmt.Sprintf("./build/%s/", functionName)
328-
329-
if err := os.RemoveAll(tempPath); err != nil {
330-
return tempPath, fmt.Errorf("unable to clear temporary build folder: %s", tempPath)
331-
}
332-
333-
functionPath := tempPath
334-
335-
if useFunction {
336-
if handlerFolder == "" {
337-
functionPath = path.Join(functionPath, defaultHandlerFolder)
338-
} else {
339-
functionPath = path.Join(functionPath, handlerFolder)
340-
}
341-
}
342-
343-
// fmt.Printf("Preparing: %s %s\n", handler+"/", functionPath)
344-
345-
if isRunningInCI() {
346-
defaultDirPermissions = 0777
347-
}
348-
349-
mkdirErr := os.MkdirAll(functionPath, defaultDirPermissions)
350-
if mkdirErr != nil {
351-
fmt.Printf("Error creating path: %s - %s.\n", functionPath, mkdirErr.Error())
352-
return tempPath, mkdirErr
353-
}
354-
355-
if useFunction {
356-
if err := CopyFiles(path.Join("./template/", language), tempPath); err != nil {
357-
fmt.Printf("Error copying template directory: %s.\n", err.Error())
358-
return tempPath, err
359-
}
360-
}
361-
362-
// Overlay in user-function
363-
// CopyFiles(handler, functionPath)
364-
infos, err := os.ReadDir(handler)
365-
if err != nil {
366-
fmt.Printf("Error reading the handler: %s - %s.\n", handler, err.Error())
367-
return tempPath, err
368-
}
369-
370-
for _, info := range infos {
371-
switch info.Name() {
372-
case "build", "template":
373-
fmt.Printf("Skipping \"%s\" folder\n", info.Name())
374-
continue
375-
default:
376-
if err := CopyFiles(
377-
filepath.Clean(path.Join(handler, info.Name())),
378-
filepath.Clean(path.Join(functionPath, info.Name())),
379-
); err != nil {
380-
return tempPath, err
381-
}
382-
}
383-
}
384-
385-
for _, extraPath := range copyExtraPaths {
386-
extraPathAbs, err := pathInScope(extraPath, ".")
387-
if err != nil {
388-
return tempPath, err
389-
}
390-
// Note that if useFunction is false, ie is a `dockerfile` template, then
391-
// functionPath == tempPath, the docker build context, not the `function` handler folder
392-
// inside the docker build context
393-
copyErr := CopyFiles(
394-
extraPathAbs,
395-
filepath.Clean(path.Join(functionPath, extraPath)),
396-
)
397-
398-
if copyErr != nil {
399-
return tempPath, copyErr
400-
}
401-
}
402-
403-
return tempPath, nil
404-
}
405-
406316
// pathInScope returns the absolute path to `path` and ensures that it is located within the
407317
// provided scope. An error will be returned, if the path is outside of the provided scope.
408318
func pathInScope(path string, scope string) (string, error) {
@@ -538,7 +448,3 @@ func deDuplicate(buildOptPackages []string) []string {
538448
}
539449
return retPackages
540450
}
541-
542-
func isLanguageTemplate(language string) bool {
543-
return strings.ToLower(language) != "dockerfile"
544-
}

builder/build_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,6 @@ import (
99
"github.com/openfaas/faas-cli/stack"
1010
)
1111

12-
func Test_isLanguageTemplate_Dockerfile(t *testing.T) {
13-
14-
language := "Dockerfile"
15-
16-
want := false
17-
got := isLanguageTemplate(language)
18-
if got != want {
19-
t.Errorf("language: %s got %v, want %v", language, got, want)
20-
}
21-
}
22-
23-
func Test_isLanguageTemplate_Node(t *testing.T) {
24-
25-
language := "node"
26-
27-
want := true
28-
got := isLanguageTemplate(language)
29-
if got != want {
30-
t.Errorf("language: %s got %v, want %v", language, got, want)
31-
}
32-
}
33-
3412
func Test_getDockerBuildCommand_NoOpts(t *testing.T) {
3513
dockerBuildVal := dockerBuild{
3614
Image: "imagename:latest",

builder/publish.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ func PublishImage(image string, handler string, functionName string, language st
4040
return fmt.Errorf("building %s, %s is an invalid path", functionName, handler)
4141
}
4242

43-
tempPath, err := createBuildContext(functionName, handler, language, isLanguageTemplate(language), langTemplate.HandlerFolder, copyExtraPaths)
43+
opts := []builder.BuildContextOption{}
44+
if len(langTemplate.HandlerFolder) > 0 {
45+
opts = append(opts, builder.WithHandlerOverlay(langTemplate.HandlerFolder))
46+
}
47+
48+
buildContext, err := builder.CreateBuildContext(functionName, handler, language, copyExtraPaths, opts...)
4449
if err != nil {
4550
return err
4651
}
4752

4853
if shrinkwrap {
49-
fmt.Printf("%s shrink-wrapped to %s\n", functionName, tempPath)
54+
fmt.Printf("%s shrink-wrapped to %s\n", functionName, buildContext)
5055
return nil
5156
}
5257

@@ -145,7 +150,7 @@ func PublishImage(image string, handler string, functionName string, language st
145150
fmt.Printf("Publishing with command: %v %v\n", command, args)
146151

147152
task := v2execute.ExecTask{
148-
Cwd: tempPath,
153+
Cwd: buildContext,
149154
Command: command,
150155
Args: args,
151156
StreamStdio: !quietBuild,

0 commit comments

Comments
 (0)