Skip to content

Commit 4d58433

Browse files
authored
feat: Add a parameter to consume additional runtime variables for docker run (#113)
* Add a flag for function_concurrency and pass it down to docker run * whitespaces * Use a custom type for runtime env variables * remove logging * Fixing some errors * addressing changes * fixing an error * fixing tabs * address comments * go fmt changes
1 parent 4f37e55 commit 4d58433

File tree

4 files changed

+43
-20
lines changed

4 files changed

+43
-20
lines changed

client/buildpacks.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type buildpacksFunctionServer struct {
4747
logStderr *os.File
4848
stdoutFile string
4949
stderrFile string
50+
envs []string
5051
}
5152

5253
func (b *buildpacksFunctionServer) Start(stdoutFile, stderrFile, functionOutputFile string) (func(), error) {
@@ -125,16 +126,8 @@ func (b *buildpacksFunctionServer) run() (func(), error) {
125126
if err != nil {
126127
return nil, err
127128
}
128-
129-
args := []string{"docker", "run",
130-
"--network=host",
131-
// TODO: figure out why these aren't getting set in the buildpack.
132-
"--env=FUNCTION_TARGET=" + b.target,
133-
"--env=FUNCTION_SIGNATURE_TYPE=" + b.funcType,
134-
image,
135-
}
129+
var args = b.getDockerRunCommand()
136130
cmd := exec.Command(args[0], args[1:]...)
137-
138131
err = cmd.Start()
139132

140133
// TODO: figure out why this isn't picking up errors.
@@ -161,6 +154,23 @@ func (b *buildpacksFunctionServer) run() (func(), error) {
161154
}, nil
162155
}
163156

157+
func (b *buildpacksFunctionServer) getDockerRunCommand() []string {
158+
runtimeVars := []string{"docker",
159+
"run",
160+
"--network=host",
161+
// TODO: figure out why these aren't getting set in the buildpack.
162+
"--env=FUNCTION_TARGET=" + b.target,
163+
"--env=FUNCTION_SIGNATURE_TYPE=" + b.funcType}
164+
165+
for _, s := range b.envs {
166+
if s != "" {
167+
runtimeVars = append(runtimeVars, fmt.Sprintf("--env=%s", s))
168+
}
169+
}
170+
171+
return append(runtimeVars, image)
172+
}
173+
164174
func (b *buildpacksFunctionServer) containerID() string {
165175
if b.ctID != "" {
166176
return b.ctID

client/local.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type localFunctionServer struct {
2727
cmd string
2828
stdoutFile string
2929
stderrFile string
30+
envs []string
3031
}
3132

3233
func (l *localFunctionServer) Start(stdoutFile, stderrFile, functionOutputFile string) (func(), error) {
@@ -47,6 +48,12 @@ func (l *localFunctionServer) Start(stdoutFile, stderrFile, functionOutputFile s
4748
return nil, err
4849
}
4950
cmd.Stderr = stderr
51+
cmd.Env = os.Environ()
52+
for _, s := range l.envs {
53+
if s != "" {
54+
cmd.Env = append(cmd.Env, s)
55+
}
56+
}
5057
err = cmd.Start()
5158
if err != nil {
5259
return nil, err

client/main.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@ package main
1818
import (
1919
"flag"
2020
"log"
21+
"strings"
2122
)
2223

2324
var (
24-
runCmd = flag.String("cmd", "", "string with command to run a Functions Framework server at localhost:8080. Ignored if -buildpacks=true.")
25-
functionType = flag.String("type", "http", "type of function to validate (must be 'http', 'cloudevent', or 'legacyevent'")
26-
validateMapping = flag.Bool("validate-mapping", true, "whether to validate mapping from legacy->cloud events and vice versa (as applicable)")
27-
outputFile = flag.String("output-file", "function_output.json", "name of file output by function")
28-
useBuildpacks = flag.Bool("buildpacks", true, "whether to use the current release of buildpacks to run the validation. If true, -cmd is ignored and --builder-* flags must be set.")
29-
source = flag.String("builder-source", "", "function source directory to use in building. Required if -buildpacks=true")
30-
target = flag.String("builder-target", "", "function target to use in building. Required if -buildpacks=true")
31-
runtime = flag.String("builder-runtime", "", "runtime to use in building. Required if -buildpacks=true")
32-
tag = flag.String("builder-tag", "latest", "builder image tag to use in building")
33-
startDelay = flag.Uint("start-delay", 1, "Seconds to wait before sending HTTP request to command process")
25+
runCmd = flag.String("cmd", "", "string with command to run a Functions Framework server at localhost:8080. Ignored if -buildpacks=true.")
26+
functionType = flag.String("type", "http", "type of function to validate (must be 'http', 'cloudevent', or 'legacyevent'")
27+
validateMapping = flag.Bool("validate-mapping", true, "whether to validate mapping from legacy->cloud events and vice versa (as applicable)")
28+
outputFile = flag.String("output-file", "function_output.json", "name of file output by function")
29+
useBuildpacks = flag.Bool("buildpacks", true, "whether to use the current release of buildpacks to run the validation. If true, -cmd is ignored and --builder-* flags must be set.")
30+
source = flag.String("builder-source", "", "function source directory to use in building. Required if -buildpacks=true")
31+
target = flag.String("builder-target", "", "function target to use in building. Required if -buildpacks=true")
32+
runtime = flag.String("builder-runtime", "", "runtime to use in building. Required if -buildpacks=true")
33+
tag = flag.String("builder-tag", "latest", "builder image tag to use in building")
34+
startDelay = flag.Uint("start-delay", 1, "Seconds to wait before sending HTTP request to command process")
3435
validateConcurrencyFlag = flag.Bool("validate-concurrency", false, "whether to validate concurrent requests can be handled, requires a function that sleeps for 1 second ")
36+
envs = flag.String("envs", "", "a comma separated string of additional runtime environment variables")
3537
)
3638

3739
func main() {
@@ -54,6 +56,7 @@ func main() {
5456
functionType: *functionType,
5557
tag: *tag,
5658
validateConcurrency: *validateConcurrencyFlag,
59+
envs: strings.Split(*envs, ","),
5760
})
5861

5962
if err := v.runValidation(); err != nil {

client/validate.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type validatorParams struct {
3535
tag string
3636
functionType string
3737
validateConcurrency bool
38+
envs []string
3839
}
3940

4041
type validator struct {
@@ -59,7 +60,8 @@ func newValidator(params validatorParams) *validator {
5960

6061
if !params.useBuildpacks {
6162
v.funcServer = &localFunctionServer{
62-
cmd: params.runCmd,
63+
cmd: params.runCmd,
64+
envs: params.envs,
6365
}
6466
return &v
6567
}
@@ -74,6 +76,7 @@ func newValidator(params validatorParams) *validator {
7476
runtime: params.runtime,
7577
tag: params.tag,
7678
funcType: params.functionType,
79+
envs: params.envs,
7780
}
7881
return &v
7982
}

0 commit comments

Comments
 (0)