Skip to content

Commit a680222

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) <[email protected]>
1 parent c53096f commit a680222

File tree

3 files changed

+16
-127
lines changed

3 files changed

+16
-127
lines changed

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

@@ -152,7 +157,7 @@ func BuildImage(image string, handler string, functionName string, language stri
152157
log.Printf("Build flags: %+v\n", args)
153158

154159
task := v2execute.ExecTask{
155-
Cwd: tempPath,
160+
Cwd: buildContext,
156161
Command: command,
157162
Args: args,
158163
StreamStdio: !quietBuild,
@@ -306,101 +311,6 @@ type dockerBuild struct {
306311
ForcePull bool
307312
}
308313

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

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

@@ -142,7 +147,7 @@ func PublishImage(image string, handler string, functionName string, language st
142147
fmt.Printf("Publishing with command: %v %v\n", command, args)
143148

144149
task := v2execute.ExecTask{
145-
Cwd: tempPath,
150+
Cwd: buildContext,
146151
Command: command,
147152
Args: args,
148153
StreamStdio: !quietBuild,

0 commit comments

Comments
 (0)