Add fprocess to faasd provider

This commit adds fprocess to the return values from faasd provider
/system/functions and /system/function/{name}

This has unit tests and has been tested on a faasd installation

Signed-off-by: Alistair Hey <alistair@heyal.co.uk>
This commit is contained in:
Alistair Hey 2021-01-21 21:03:58 +00:00 committed by Alex Ellis
parent 87f105d581
commit e199827883
4 changed files with 26 additions and 10 deletions

View File

@ -25,6 +25,7 @@ type Function struct {
annotations map[string]string
secrets []string
envVars map[string]string
envProcess string
}
// ListFunctions returns a map of all functions with running tasks on namespace
@ -79,7 +80,7 @@ func GetFunction(client *containerd.Client, name string) (Function, error) {
return Function{}, fmt.Errorf("unable to load function spec for reading secrets: %s, error %s", name, err)
}
envVars := readEnvVarsFromProcessEnv(spec.Process.Env)
envVars, envProcess := readEnvFromProcessEnv(spec.Process.Env)
secrets := readSecretsFromMounts(spec.Mounts)
fn.name = containerName
@ -89,6 +90,7 @@ func GetFunction(client *containerd.Client, name string) (Function, error) {
fn.annotations = annotations
fn.secrets = secrets
fn.envVars = envVars
fn.envProcess = envProcess
replicas := 0
task, err := c.Task(ctx, nil)
@ -118,22 +120,28 @@ func GetFunction(client *containerd.Client, name string) (Function, error) {
return fn, nil
}
func readEnvVarsFromProcessEnv(env []string) map[string]string {
func readEnvFromProcessEnv(env []string) (map[string]string, string) {
foundEnv := make(map[string]string)
fprocess := ""
for _, e := range env {
kv := strings.Split(e, "=")
if len(kv) == 1 {
continue
}
if kv[0] == "fprocess" || kv[0] == "PATH" {
if kv[0] == "PATH" {
continue
}
if kv[0] == "fprocess" {
fprocess = kv[1]
continue
}
foundEnv[kv[0]] = kv[1]
}
return foundEnv
return foundEnv, fprocess
}
func readSecretsFromMounts(mounts []specs.Mount) []string {

View File

@ -60,20 +60,26 @@ func Test_ProcessEnvToEnvVars(t *testing.T) {
Name string
Input []string
Expected map[string]string
fprocess string
}
tests := []test{
{Name: "No matching EnvVars", Input: []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "fprocess=python", "index.py"}, Expected: make(map[string]string)},
{Name: "No EnvVars", Input: []string{}, Expected: make(map[string]string)},
{Name: "One EnvVar", Input: []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "fprocess=python", "env=this", "index.py"}, Expected: map[string]string{"env": "this"}},
{Name: "Multiple EnvVars", Input: []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "this=that", "env=var", "fprocess=python", "index.py"}, Expected: map[string]string{"this": "that", "env": "var"}},
{Name: "No matching EnvVars", Input: []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "fprocess=python index.py"}, Expected: make(map[string]string), fprocess: "python index.py"},
{Name: "No EnvVars", Input: []string{}, Expected: make(map[string]string), fprocess: ""},
{Name: "One EnvVar", Input: []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "fprocess=python index.py", "env=this"}, Expected: map[string]string{"env": "this"}, fprocess: "python index.py"},
{Name: "Multiple EnvVars", Input: []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "this=that", "env=var", "fprocess=python index.py"}, Expected: map[string]string{"this": "that", "env": "var"}, fprocess: "python index.py"},
{Name: "Nil EnvVars", Input: nil, Expected: make(map[string]string)},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
got := readEnvVarsFromProcessEnv(tc.Input)
got, fprocess := readEnvFromProcessEnv(tc.Input)
if !reflect.DeepEqual(got, tc.Expected) {
t.Fatalf("expected %s, got %s", tc.Expected, got)
t.Fatalf("expected: %s, got: %s", tc.Expected, got)
}
if fprocess != tc.fprocess {
t.Fatalf("expected fprocess: %s, got: %s", tc.fprocess, got)
}
})
}

View File

@ -33,6 +33,7 @@ func MakeReadHandler(client *containerd.Client) func(w http.ResponseWriter, r *h
Annotations: annotations,
Secrets: fn.secrets,
EnvVars: fn.envVars,
EnvProcess: fn.envProcess,
})
}

View File

@ -25,6 +25,7 @@ func MakeReplicaReaderHandler(client *containerd.Client) func(w http.ResponseWri
Annotations: &f.annotations,
Secrets: f.secrets,
EnvVars: f.envVars,
EnvProcess: f.envProcess,
}
functionBytes, _ := json.Marshal(found)