Skip to content

Commit

Permalink
Revert "Export CreateBuildContext function"
Browse files Browse the repository at this point in the history
Revert to fix #1001
The CreateBuildContext will be moved to the go-sdk in later PR.

This reverts commit 35dc699.

Signed-off-by: Han Verstraete (OpenFaaS Ltd) <han@openfaas.com>
  • Loading branch information
welteki authored and alexellis committed Aug 7, 2024
1 parent 35dc699 commit 7264f71
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 59 deletions.
62 changes: 21 additions & 41 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,7 @@ func BuildImage(image string, handler string, functionName string, language stri
return fmt.Errorf("building %s, %s is an invalid path", functionName, handler)
}

// To avoid breaking the CLI for custom templates that do not set the language attribute
// we ensure it is always set.
//
// While templates are expected to have the language in `template.yaml` set to the same name as the template folder
// this was never enforced.
langTemplate.Language = language

if isDockerfileTemplate(langTemplate.Language) {
langTemplate = nil
}

tempPath, err := CreateBuildContext(functionName, handler, langTemplate, copyExtraPaths)
tempPath, err := createBuildContext(functionName, handler, language, isLanguageTemplate(language), langTemplate.HandlerFolder, copyExtraPaths)
if err != nil {
return err
}
Expand Down Expand Up @@ -314,17 +303,8 @@ func isRunningInCI() bool {
return false
}

// CreateBuildContext creates a Docker build context using the provided function handler and language template.
//
// Parameters:
// - functionName: the name of the function.
// - handler: path to the function handler folder.
// - template: function language template to use. Set to nil if no template is required (e.g. the handler folder is the build context with Dockerfile).
// - copyExtraPaths: additional path to copy into the function handler folder. Paths should be relative to the current directory.
// Any path outside of this current directory will be skipped.
//
// Returns the path to the new build context. An error is returned if creating the build context fails.
func CreateBuildContext(functionName string, handler string, template *stack.LanguageTemplate, copyExtraPaths []string) (string, error) {
// createBuildContext creates temporary build folder to perform a Docker build with language template
func createBuildContext(functionName string, handler string, language string, useFunction bool, handlerFolder string, copyExtraPaths []string) (string, error) {
tempPath := fmt.Sprintf("./build/%s/", functionName)

if err := os.RemoveAll(tempPath); err != nil {
Expand All @@ -333,19 +313,16 @@ func CreateBuildContext(functionName string, handler string, template *stack.Lan

functionPath := tempPath

useTemplate := false
if template != nil {
useTemplate = true
}

if useTemplate {
if len(template.HandlerFolder) > 0 {
functionPath = path.Join(functionPath, template.HandlerFolder)
} else {
if useFunction {
if handlerFolder == "" {
functionPath = path.Join(functionPath, defaultHandlerFolder)
} else {
functionPath = path.Join(functionPath, handlerFolder)
}
}

// fmt.Printf("Preparing: %s %s\n", handler+"/", functionPath)

if isRunningInCI() {
defaultDirPermissions = 0777
}
Expand All @@ -356,8 +333,8 @@ func CreateBuildContext(functionName string, handler string, template *stack.Lan
return tempPath, mkdirErr
}

if useTemplate {
if err := CopyFiles(path.Join("./template/", template.Language), tempPath); err != nil {
if useFunction {
if err := CopyFiles(path.Join("./template/", language), tempPath); err != nil {
fmt.Printf("Error copying template directory: %s.\n", err.Error())
return tempPath, err
}
Expand All @@ -373,7 +350,6 @@ func CreateBuildContext(functionName string, handler string, template *stack.Lan

for _, info := range infos {
switch info.Name() {
// Ignore the build and template folder.
case "build", "template":
fmt.Printf("Skipping \"%s\" folder\n", info.Name())
continue
Expand All @@ -392,12 +368,16 @@ func CreateBuildContext(functionName string, handler string, template *stack.Lan
if err != nil {
return tempPath, err
}

// Note that if template is nil, i.e. is a `dockerfile` template, then
// Note that if useFunction is false, ie is a `dockerfile` template, then
// functionPath == tempPath, the docker build context, not the `function` handler folder
// inside the docker build context
if err := CopyFiles(extraPathAbs, filepath.Clean(path.Join(functionPath, extraPath))); err != nil {
return tempPath, err
copyErr := CopyFiles(
extraPathAbs,
filepath.Clean(path.Join(functionPath, extraPath)),
)

if copyErr != nil {
return tempPath, copyErr
}
}

Expand Down Expand Up @@ -536,6 +516,6 @@ func deDuplicate(buildOptPackages []string) []string {
return retPackages
}

func isDockerfileTemplate(language string) bool {
return strings.ToLower(language) == "dockerfile"
func isLanguageTemplate(language string) bool {
return strings.ToLower(language) != "dockerfile"
}
13 changes: 7 additions & 6 deletions builder/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ import (
"github.com/openfaas/faas-cli/stack"
)

func Test_isDockerfileTemplate_Dockerfile(t *testing.T) {
func Test_isLanguageTemplate_Dockerfile(t *testing.T) {

language := "Dockerfile"

want := true
got := isDockerfileTemplate(language)
want := false
got := isLanguageTemplate(language)
if got != want {
t.Errorf("language: %s got %v, want %v", language, got, want)
}
}

func Test_isDockerfileTemplate_Node(t *testing.T) {
func Test_isLanguageTemplate_Node(t *testing.T) {

language := "node"

want := false
got := isDockerfileTemplate(language)
want := true
got := isLanguageTemplate(language)
if got != want {
t.Errorf("language: %s got %v, want %v", language, got, want)
}
Expand Down
13 changes: 1 addition & 12 deletions builder/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,7 @@ func PublishImage(image string, handler string, functionName string, language st
return fmt.Errorf("building %s, %s is an invalid path", functionName, handler)
}

// To avoid breaking the CLI for custom templates that do not set the language attribute
// we ensure it is always set.
//
// While templates are expected to have the language in `template.yaml` set to the same name as the template folder
// this was never enforced.
langTemplate.Language = language

if isDockerfileTemplate(langTemplate.Language) {
langTemplate = nil
}

tempPath, err := CreateBuildContext(functionName, handler, langTemplate, copyExtraPaths)
tempPath, err := createBuildContext(functionName, handler, language, isLanguageTemplate(language), langTemplate.HandlerFolder, copyExtraPaths)
if err != nil {
return err
}
Expand Down

0 comments on commit 7264f71

Please sign in to comment.