Skip to content

Commit

Permalink
feat: set builder images in templates and .faas.yaml
Browse files Browse the repository at this point in the history
This commit adds a .builder.yaml file to each template directory. In the file
there is at the moment a single key/value pair, "default: <image>", where the
actual builder image name is <image>. Using a mapping allows the future
possibility that a user may specify a builder image by name via a flag on the
command line. For example,

```console
faas build --builder native
```

When a project is initialized, the .builder.yaml file is read, and the default
builder is saved in the project's .faas.yaml file. The .faas.yaml file is then
consulted when building an image with `faas build`. If the builder image is
specified, then the builder will use it. Otherwise, it will fallback to the
defaults. This allows developers to create custom builders, and specify them
in the configuration file.

An open question is whether the .builder.yaml file should be deleted after it
is read during project initialization. The `init` command is the only time the
file is consulted, and it may be confusing for the user if they discover a
second configuration-y file in the project directory.
  • Loading branch information
lance committed Sep 23, 2020
1 parent 7e298fd commit 734bd33
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 5 deletions.
15 changes: 11 additions & 4 deletions buildpacks/builder.go
Expand Up @@ -30,10 +30,17 @@ var Runtimes = map[string]string{

// Build the Function at path.
func (builder *Builder) Build(f faas.Function) (err error) {
// dervive the builder from the specificed runtime
packBuilder, ok := Runtimes[f.Runtime]
if !ok {
return errors.New(fmt.Sprint("unsupported runtime: ", f.Runtime))

// Use the builder found in the Function configuration file
// If one isn't found, use the defaults
var packBuilder string
if f.Builder != "" {
packBuilder = f.Builder
} else {
packBuilder = Runtimes[f.Runtime]
if packBuilder == "" {
return errors.New(fmt.Sprint("unsupported runtime: ", f.Runtime))
}
}

// Build options for the pack client.
Expand Down
14 changes: 14 additions & 0 deletions client.go
Expand Up @@ -4,7 +4,11 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

"gopkg.in/yaml.v2"
)

const (
Expand Down Expand Up @@ -348,6 +352,16 @@ func (c *Client) Initialize(cfg Function) (err error) {
return
}

// Check if template specifies a builder image. If so, add to configuration
if builderConfig, err := ioutil.ReadFile(filepath.Join(f.Root, ".builder.yaml")); err == nil {
// A .builder file was found. Read the default builder and set in the config file
// TODO: A command line flag could be used to specify non-default builders
builders := make(map[string]string)
if err := yaml.Unmarshal(builderConfig, builders); err == nil {
f.Builder = builders["default"]
}
}

// Write out the config.
if err = writeConfig(f); err != nil {
return
Expand Down
3 changes: 3 additions & 0 deletions config.go
Expand Up @@ -19,6 +19,7 @@ type config struct {
Runtime string `yaml:"runtime"`
Image string `yaml:"image"`
Trigger string `yaml:"trigger"`
Builder string `yaml:"builder"`
// Add new values to the toConfig/fromConfig functions.
}

Expand Down Expand Up @@ -53,6 +54,7 @@ func fromConfig(c config) (f Function) {
Runtime: c.Runtime,
Image: c.Image,
Trigger: c.Trigger,
Builder: c.Builder,
}
}

Expand All @@ -64,6 +66,7 @@ func toConfig(f Function) config {
Runtime: f.Runtime,
Image: f.Image,
Trigger: f.Trigger,
Builder: f.Builder,
}
}

Expand Down
3 changes: 3 additions & 0 deletions function.go
Expand Up @@ -42,6 +42,9 @@ type Function struct {
// If Image is provided, it overrides the default of concatenating
// "Repo+Name:latest" to derive the Image.
Image string

// Builder represents the CNCF Buildpack builder image for a function
Builder string
}

// NewFunction loads a Function from a path on disk. use .Initialized() to determine if
Expand Down
2 changes: 1 addition & 1 deletion pkged.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions templates/go/events/.builder.yaml
@@ -0,0 +1 @@
default: nodejs quay.io/boson/faas-go-builder
1 change: 1 addition & 0 deletions templates/go/http/.builder.yaml
@@ -0,0 +1 @@
default: quay.io/boson/faas-go-builder
1 change: 1 addition & 0 deletions templates/node/events/.builder.yaml
@@ -0,0 +1 @@
default: quay.io/boson/faas-nodejs-builder
1 change: 1 addition & 0 deletions templates/node/http/.builder.yaml
@@ -0,0 +1 @@
default: quay.io/boson/faas-nodejs-builder
1 change: 1 addition & 0 deletions templates/quarkus/events/.builder.yaml
@@ -0,0 +1 @@
default: quay.io/boson/faas-quarkus-builder
1 change: 1 addition & 0 deletions templates/quarkus/http/.builder.yaml
@@ -0,0 +1 @@
default: quay.io/boson/faas-quarkus-builder

0 comments on commit 734bd33

Please sign in to comment.