Provider returns secrets for a function

This commit allows the provider to return a list of the names of the
secrets mapped into an openfaas function. This was tested by building
and deploying faasd on multipass and curling the provider directly and
seeing the returned secrets list!

Signed-off-by: Alistair Hey <alistair@heyal.co.uk>
This commit is contained in:
Alistair Hey
2021-01-20 19:23:46 +00:00
committed by Alex Ellis
parent 4e8a1d810a
commit 237a026b79
12 changed files with 157 additions and 92 deletions

View File

@ -3,6 +3,7 @@ package handlers
import (
"context"
"fmt"
"github.com/opencontainers/runtime-spec/specs-go"
"log"
"strings"
@ -22,6 +23,7 @@ type Function struct {
IP string
labels map[string]string
annotations map[string]string
secrets []string
}
// ListFunctions returns a map of all functions with running tasks on namespace
@ -71,12 +73,19 @@ func GetFunction(client *containerd.Client, name string) (Function, error) {
labels, annotations := buildLabelsAndAnnotations(allLabels)
spec, err := c.Spec(ctx)
if err != nil {
return Function{}, fmt.Errorf("unable to load function spec for reading secrets: %s, error %s", name, err)
}
secrets := readSecretsFromMounts(spec.Mounts)
fn.name = containerName
fn.namespace = faasd.FunctionNamespace
fn.image = image.Name()
fn.labels = labels
fn.annotations = annotations
fn.secrets = secrets
replicas := 0
task, err := c.Task(ctx, nil)
if err == nil {
@ -105,6 +114,18 @@ func GetFunction(client *containerd.Client, name string) (Function, error) {
return fn, nil
}
func readSecretsFromMounts(mounts []specs.Mount) []string {
secrets := []string{}
for _, mnt := range mounts {
x := strings.Split(mnt.Destination, "/var/openfaas/secrets/")
if len(x) > 1 {
secrets = append(secrets, x[1])
}
}
return secrets
}
// buildLabelsAndAnnotations returns a separated list with labels first,
// followed by annotations by checking each key of ctrLabels for a prefix.
func buildLabelsAndAnnotations(ctrLabels map[string]string) (map[string]string, map[string]string) {