diff --git a/pkg/provider/handlers/functions.go b/pkg/provider/handlers/functions.go index d755c15..457cba8 100644 --- a/pkg/provider/handlers/functions.go +++ b/pkg/provider/handlers/functions.go @@ -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 { diff --git a/pkg/provider/handlers/functions_test.go b/pkg/provider/handlers/functions_test.go index 3ef214c..8209b3e 100644 --- a/pkg/provider/handlers/functions_test.go +++ b/pkg/provider/handlers/functions_test.go @@ -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) + } }) } diff --git a/pkg/provider/handlers/read.go b/pkg/provider/handlers/read.go index 6bba877..ec37d22 100644 --- a/pkg/provider/handlers/read.go +++ b/pkg/provider/handlers/read.go @@ -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, }) } diff --git a/pkg/provider/handlers/replicas.go b/pkg/provider/handlers/replicas.go index 5b8eb7b..88a52d2 100644 --- a/pkg/provider/handlers/replicas.go +++ b/pkg/provider/handlers/replicas.go @@ -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)