Skip to content

Commit d6e131f

Browse files
authored
feat: set builder images in templates and .faas.yaml (#136)
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. After extracting the builder image from .builder.yaml in the project directory, this file is deleted. This commit also adds Verbose to the init command.
1 parent 05efee8 commit d6e131f

File tree

13 files changed

+70
-20
lines changed

13 files changed

+70
-20
lines changed

buildpacks/builder.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,25 @@ func NewBuilder() *Builder {
2222
return &Builder{}
2323
}
2424

25-
var Runtimes = map[string]string{
25+
var RuntimeToBuildpack = map[string]string{
2626
"quarkus": "quay.io/boson/faas-quarkus-builder",
2727
"node": "quay.io/boson/faas-nodejs-builder",
2828
"go": "quay.io/boson/faas-go-builder",
2929
}
3030

3131
// Build the Function at path.
3232
func (builder *Builder) Build(f faas.Function) (err error) {
33-
// dervive the builder from the specificed runtime
34-
packBuilder, ok := Runtimes[f.Runtime]
35-
if !ok {
36-
return errors.New(fmt.Sprint("unsupported runtime: ", f.Runtime))
33+
34+
// Use the builder found in the Function configuration file
35+
// If one isn't found, use the defaults
36+
var packBuilder string
37+
if f.Builder != "" {
38+
packBuilder = f.Builder
39+
} else {
40+
packBuilder = RuntimeToBuildpack[f.Runtime]
41+
if packBuilder == "" {
42+
return errors.New(fmt.Sprint("unsupported runtime: ", f.Runtime))
43+
}
3744
}
3845

3946
// Build options for the pack client.

client.go

+37-12
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"io/ioutil"
78
"os"
9+
"path/filepath"
10+
11+
"gopkg.in/yaml.v2"
812
)
913

1014
const (
11-
DefaultRegistry = "docker.io"
12-
DefaultRuntime = "go"
13-
DefaultTrigger = "http"
15+
DefaultRegistry = "docker.io"
16+
DefaultRuntime = "go"
17+
DefaultTrigger = "http"
1418
)
1519

1620
// Client for managing Function instances.
@@ -117,15 +121,15 @@ type DNSProvider interface {
117121
func New(options ...Option) *Client {
118122
// Instantiate client with static defaults.
119123
c := &Client{
120-
builder: &noopBuilder{output: os.Stdout},
121-
pusher: &noopPusher{output: os.Stdout},
122-
deployer: &noopDeployer{output: os.Stdout},
123-
updater: &noopUpdater{output: os.Stdout},
124-
runner: &noopRunner{output: os.Stdout},
125-
remover: &noopRemover{output: os.Stdout},
126-
lister: &noopLister{output: os.Stdout},
127-
dnsProvider: &noopDNSProvider{output: os.Stdout},
128-
progressListener: &noopProgressListener{},
124+
builder: &noopBuilder{output: os.Stdout},
125+
pusher: &noopPusher{output: os.Stdout},
126+
deployer: &noopDeployer{output: os.Stdout},
127+
updater: &noopUpdater{output: os.Stdout},
128+
runner: &noopRunner{output: os.Stdout},
129+
remover: &noopRemover{output: os.Stdout},
130+
lister: &noopLister{output: os.Stdout},
131+
dnsProvider: &noopDNSProvider{output: os.Stdout},
132+
progressListener: &noopProgressListener{},
129133
}
130134

131135
// Apply passed options, which take ultimate precidence.
@@ -345,6 +349,27 @@ func (c *Client) Initialize(cfg Function) (err error) {
345349
return
346350
}
347351

352+
// Check if template specifies a builder image. If so, add to configuration
353+
builderFilePath := filepath.Join(f.Root, ".builders.yaml")
354+
if builderConfig, err := ioutil.ReadFile(builderFilePath); err == nil {
355+
// A .builder file was found. Read the default builder and set in the config file
356+
// TODO: A command line flag could be used to specify non-default builders
357+
builders := make(map[string]string)
358+
if err := yaml.Unmarshal(builderConfig, builders); err == nil {
359+
f.Builder = builders["default"]
360+
if c.verbose {
361+
fmt.Printf("Builder: %s\n", f.Builder)
362+
}
363+
}
364+
// Remove the builders.yaml file so the user is not confused by a
365+
// configuration file that is only used for project creation/initialization
366+
if err := os.Remove(builderFilePath); err != nil {
367+
if c.verbose {
368+
fmt.Printf("Cannot remove %v. %v\n", builderFilePath, err)
369+
}
370+
}
371+
}
372+
348373
// Write out the config.
349374
if err = writeConfig(f); err != nil {
350375
return

cmd/completion_util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func CompleteFunctionList(cmd *cobra.Command, args []string, toComplete string)
2828
}
2929
func CompleteRuntimeList(cmd *cobra.Command, args []string, toComplete string) (strings []string, directive cobra.ShellCompDirective) {
3030
strings = []string{}
31-
for lang := range buildpacks.Runtimes {
31+
for lang := range buildpacks.RuntimeToBuildpack {
3232
strings = append(strings, lang)
3333
}
3434
directive = cobra.ShellCompDirectiveDefault

cmd/init.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ func runInit(cmd *cobra.Command, args []string) error {
5555
Trigger: config.Trigger,
5656
}
5757

58-
client := faas.New(faas.WithTemplates(config.Templates))
58+
client := faas.New(
59+
faas.WithTemplates(config.Templates),
60+
faas.WithVerbose(config.Verbose))
5961

6062
return client.Initialize(function)
6163
}
@@ -82,6 +84,9 @@ type initConfig struct {
8284
// Function which will be invoked with CloudEvents.
8385
Trigger string
8486

87+
// Verbose output
88+
Verbose bool
89+
8590
// Confirm: confirm values arrived upon from environment plus flags plus defaults,
8691
// with interactive prompting (only applicable when attached to a TTY).
8792
Confirm bool
@@ -103,6 +108,7 @@ func newInitConfig(args []string) initConfig {
103108
Templates: viper.GetString("templates"),
104109
Trigger: viper.GetString("trigger"),
105110
Confirm: viper.GetBool("confirm"),
111+
Verbose: viper.GetBool("verbose"),
106112
}
107113
}
108114

config.go

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type config struct {
1919
Runtime string `yaml:"runtime"`
2020
Image string `yaml:"image"`
2121
Trigger string `yaml:"trigger"`
22+
Builder string `yaml:"builder"`
2223
// Add new values to the toConfig/fromConfig functions.
2324
}
2425

@@ -53,6 +54,7 @@ func fromConfig(c config) (f Function) {
5354
Runtime: c.Runtime,
5455
Image: c.Image,
5556
Trigger: c.Trigger,
57+
Builder: c.Builder,
5658
}
5759
}
5860

@@ -64,6 +66,7 @@ func toConfig(f Function) config {
6466
Runtime: f.Runtime,
6567
Image: f.Image,
6668
Trigger: f.Trigger,
69+
Builder: f.Builder,
6770
}
6871
}
6972

function.go

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ type Function struct {
4040
// If Image is provided, it overrides the default of concatenating
4141
// "Repo+Name:latest" to derive the Image.
4242
Image string
43+
44+
// Builder represents the CNCF Buildpack builder image for a function
45+
Builder string
4346
}
4447

4548
// NewFunction loads a Function from a path on disk. use .Initialized() to determine if

pkged.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/go/events/.builders.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default: quay.io/boson/faas-go-builder

templates/go/http/.builders.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default: quay.io/boson/faas-go-builder

templates/node/events/.builders.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default: quay.io/boson/faas-nodejs-builder

templates/node/http/.builders.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default: quay.io/boson/faas-nodejs-builder
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default: quay.io/boson/faas-quarkus-builder

templates/quarkus/http/.builders.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default: quay.io/boson/faas-quarkus-builder

0 commit comments

Comments
 (0)