diff --git a/pkg/provider/handlers/functions.go b/pkg/provider/handlers/functions.go index d236dbb..d755c15 100644 --- a/pkg/provider/handlers/functions.go +++ b/pkg/provider/handlers/functions.go @@ -24,6 +24,7 @@ type Function struct { labels map[string]string annotations map[string]string secrets []string + envVars map[string]string } // ListFunctions returns a map of all functions with running tasks on namespace @@ -78,6 +79,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) secrets := readSecretsFromMounts(spec.Mounts) fn.name = containerName @@ -86,6 +88,8 @@ func GetFunction(client *containerd.Client, name string) (Function, error) { fn.labels = labels fn.annotations = annotations fn.secrets = secrets + fn.envVars = envVars + replicas := 0 task, err := c.Task(ctx, nil) if err == nil { @@ -114,6 +118,24 @@ func GetFunction(client *containerd.Client, name string) (Function, error) { return fn, nil } +func readEnvVarsFromProcessEnv(env []string) map[string]string { + foundEnv := make(map[string]string) + for _, e := range env { + kv := strings.Split(e, "=") + if len(kv) == 1 { + continue + } + + if kv[0] == "fprocess" || kv[0] == "PATH" { + continue + } + + foundEnv[kv[0]] = kv[1] + } + + return foundEnv +} + func readSecretsFromMounts(mounts []specs.Mount) []string { secrets := []string{} for _, mnt := range mounts { diff --git a/pkg/provider/handlers/functions_test.go b/pkg/provider/handlers/functions_test.go index d6458a8..3ef214c 100644 --- a/pkg/provider/handlers/functions_test.go +++ b/pkg/provider/handlers/functions_test.go @@ -38,6 +38,7 @@ func Test_SplitMountToSecrets(t *testing.T) { } tests := []test{ {Name: "No matching openfaas secrets", Input: []specs.Mount{{Destination: "/foo/"}}, Expected: []string{}}, + {Name: "Nil mounts", Input: nil, Expected: []string{}}, {Name: "No Mounts", Input: []specs.Mount{{Destination: "/foo/"}}, Expected: []string{}}, {Name: "One Mounts IS secret", Input: []specs.Mount{{Destination: "/var/openfaas/secrets/secret1"}}, Expected: []string{"secret1"}}, {Name: "Multiple Mounts 1 secret", Input: []specs.Mount{{Destination: "/var/openfaas/secrets/secret1"}, {Destination: "/some/other/path"}}, Expected: []string{"secret1"}}, @@ -53,3 +54,27 @@ func Test_SplitMountToSecrets(t *testing.T) { }) } } + +func Test_ProcessEnvToEnvVars(t *testing.T) { + type test struct { + Name string + Input []string + Expected map[string]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: "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) + if !reflect.DeepEqual(got, tc.Expected) { + t.Fatalf("expected %s, got %s", tc.Expected, got) + } + }) + } +} diff --git a/pkg/provider/handlers/read.go b/pkg/provider/handlers/read.go index 943dbed..6bba877 100644 --- a/pkg/provider/handlers/read.go +++ b/pkg/provider/handlers/read.go @@ -32,6 +32,7 @@ func MakeReadHandler(client *containerd.Client) func(w http.ResponseWriter, r *h Labels: labels, Annotations: annotations, Secrets: fn.secrets, + EnvVars: fn.envVars, }) } diff --git a/pkg/provider/handlers/replicas.go b/pkg/provider/handlers/replicas.go index 52e2137..5b8eb7b 100644 --- a/pkg/provider/handlers/replicas.go +++ b/pkg/provider/handlers/replicas.go @@ -24,6 +24,7 @@ func MakeReplicaReaderHandler(client *containerd.Client) func(w http.ResponseWri Labels: &f.labels, Annotations: &f.annotations, Secrets: f.secrets, + EnvVars: f.envVars, } functionBytes, _ := json.Marshal(found)