mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Add config flags support to lambda create and import (#245)
* Add config flags support to lambda create and import * fix publishing and path formatting * expand env variables properly for lambda functionality * add proper env variables handling for aws-import
This commit is contained in:
committed by
C Cirello
parent
71e1f3c245
commit
ce8a449079
@@ -35,13 +35,13 @@ var (
|
||||
)
|
||||
|
||||
type funcfile struct {
|
||||
App *string
|
||||
Image string
|
||||
Route *string
|
||||
Type string
|
||||
Memory int64
|
||||
Config map[string]string
|
||||
Build []string
|
||||
App *string `yaml:"app,omitempty",json:"app,omitempty"`
|
||||
Image string `yaml:"image,omitempty",json:"image,omitempty"`
|
||||
Route *string `yaml:"route,omitempty",json:"route,omitempty"`
|
||||
Type *string `yaml:"type,omitempty",json:"type,omitempty"`
|
||||
Memory *int64 `yaml:"memory,omitempty",json:"memory,omitempty"`
|
||||
Config map[string]string `yaml:"config,omitempty",json:"config,omitempty"`
|
||||
Build []string `yaml:"build,omitempty",json:"build,omitempty"`
|
||||
}
|
||||
|
||||
func parsefuncfile(path string) (*funcfile, error) {
|
||||
|
||||
@@ -12,6 +12,8 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
@@ -79,9 +81,27 @@ func getFlags() []cli.Flag {
|
||||
Name: "download-only",
|
||||
Usage: "Only download the function into a directory. Will not create a Docker image.",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "config",
|
||||
Usage: "function configuration",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func transcribeEnvConfig(configs []string) map[string]string {
|
||||
c := make(map[string]string)
|
||||
for _, v := range configs {
|
||||
kv := strings.SplitN(v, "=", 2)
|
||||
if len(kv) == 1 {
|
||||
// TODO: Make sure it is compatible cross platform
|
||||
c[kv[0]] = fmt.Sprintf("$%s", kv[0])
|
||||
} else {
|
||||
c[kv[0]] = kv[1]
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func create(c *cli.Context) error {
|
||||
args := c.Args()
|
||||
if len(args) < 4 {
|
||||
@@ -100,6 +120,7 @@ func create(c *cli.Context) error {
|
||||
Handler: handler,
|
||||
OutputStream: newdockerJSONWriter(os.Stdout),
|
||||
RawJSONStream: true,
|
||||
Config: transcribeEnvConfig(c.StringSlice("config")),
|
||||
}
|
||||
|
||||
if handler == "" {
|
||||
@@ -125,7 +146,6 @@ func create(c *cli.Context) error {
|
||||
}
|
||||
|
||||
return createDockerfile(opts, files...)
|
||||
|
||||
}
|
||||
|
||||
var runtimeCreateHandlers = map[string]func(filenames []string, opts *createImageOptions) error{
|
||||
@@ -208,6 +228,7 @@ func awsImport(c *cli.Context) error {
|
||||
Handler: *function.Configuration.Handler,
|
||||
OutputStream: newdockerJSONWriter(os.Stdout),
|
||||
RawJSONStream: true,
|
||||
Config: transcribeEnvConfig(c.StringSlice("config")),
|
||||
}
|
||||
|
||||
runtime := *function.Configuration.Runtime
|
||||
@@ -254,16 +275,21 @@ func basicImportHandler(functionName, tmpFileName string, opts *createImageOptio
|
||||
return unzipAndGetTopLevelFiles(functionName, tmpFileName)
|
||||
}
|
||||
|
||||
const fnYAMLTemplate = `
|
||||
app: %s
|
||||
image: %s
|
||||
route: "/%s"
|
||||
`
|
||||
func createFunctionYaml(opts createImageOptions) error {
|
||||
strs := strings.Split(opts.Name, "/")
|
||||
route := fmt.Sprintf("/%s", strs[1])
|
||||
funcDesc := &funcfile{
|
||||
App: &strs[0],
|
||||
Image: opts.Name,
|
||||
Route: &route,
|
||||
Config: opts.Config,
|
||||
}
|
||||
|
||||
func createFunctionYaml(image string) error {
|
||||
strs := strings.Split(image, "/")
|
||||
data := []byte(fmt.Sprintf(fnYAMLTemplate, strs[0], image, strs[1]))
|
||||
return ioutil.WriteFile(filepath.Join(image, "function.yaml"), data, 0644)
|
||||
out, err := yaml.Marshal(funcDesc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(filepath.Join(opts.Name, "function.yaml"), out, 0644)
|
||||
}
|
||||
|
||||
type createImageOptions struct {
|
||||
@@ -273,6 +299,7 @@ type createImageOptions struct {
|
||||
Handler string
|
||||
OutputStream io.Writer
|
||||
RawJSONStream bool
|
||||
Config map[string]string
|
||||
}
|
||||
|
||||
type fileLike interface {
|
||||
@@ -359,7 +386,7 @@ func createDockerfile(opts createImageOptions, files ...fileLike) error {
|
||||
}
|
||||
|
||||
fmt.Print("Creating function.yaml ... ")
|
||||
if err := createFunctionYaml(opts.Name); err != nil {
|
||||
if err := createFunctionYaml(opts); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("OK")
|
||||
|
||||
@@ -105,13 +105,19 @@ func (p *publishcmd) route(path string, ff *funcfile) error {
|
||||
if ff.Route == nil {
|
||||
ff.Route = &r
|
||||
}
|
||||
if ff.Memory == nil {
|
||||
ff.Memory = new(int64)
|
||||
}
|
||||
if ff.Type == nil {
|
||||
ff.Type = new(string)
|
||||
}
|
||||
|
||||
body := functions.RouteWrapper{
|
||||
Route: functions.Route{
|
||||
Path: *ff.Route,
|
||||
Image: ff.Image,
|
||||
Memory: ff.Memory,
|
||||
Type_: ff.Type,
|
||||
Memory: *ff.Memory,
|
||||
Type_: *ff.Type,
|
||||
Config: expandEnvConfig(ff.Config),
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user