From 560c295eb0ee760541de9203759936065d71404e Mon Sep 17 00:00:00 2001 From: Martin Dekov Date: Sat, 15 Feb 2020 20:42:38 +0200 Subject: [PATCH] Enable labeling containers Enabling the faasd-provider to label containers Signed-off-by: Martin Dekov --- pkg/provider/handlers/deploy.go | 1 + pkg/provider/handlers/functions.go | 14 ++++++++++++-- pkg/provider/handlers/read.go | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/provider/handlers/deploy.go b/pkg/provider/handlers/deploy.go index 0987424..f382be8 100644 --- a/pkg/provider/handlers/deploy.go +++ b/pkg/provider/handlers/deploy.go @@ -107,6 +107,7 @@ func deploy(ctx context.Context, req types.FunctionDeployment, client *container oci.WithCapabilities([]string{"CAP_NET_RAW"}), oci.WithMounts(mounts), oci.WithEnv(envs)), + containerd.WithContainerLabels(*req.Labels), ) if err != nil { diff --git a/pkg/provider/handlers/functions.go b/pkg/provider/handlers/functions.go index 124eafb..fdc2d8b 100644 --- a/pkg/provider/handlers/functions.go +++ b/pkg/provider/handlers/functions.go @@ -3,10 +3,11 @@ package handlers import ( "context" "fmt" + "log" - "github.com/openfaas/faasd/pkg/cninetwork" "github.com/containerd/containerd" "github.com/containerd/containerd/namespaces" + "github.com/openfaas/faasd/pkg/cninetwork" ) type Function struct { @@ -16,6 +17,7 @@ type Function struct { pid uint32 replicas int IP string + labels map[string]string } const ( @@ -44,10 +46,18 @@ func GetFunction(client *containerd.Client, name string) (Function, error) { if err == nil { image, _ := c.Image(ctx) + + containerName := c.ID() + labels, labelErr := c.Labels(ctx) + if labelErr != nil { + log.Printf("cannot list container %s labels: %s", containerName, labelErr.Error()) + } + f := Function{ - name: c.ID(), + name: containerName, namespace: FunctionNamespace, image: image.Name(), + labels: labels, } replicas := 0 diff --git a/pkg/provider/handlers/read.go b/pkg/provider/handlers/read.go index 8bbade8..e337165 100644 --- a/pkg/provider/handlers/read.go +++ b/pkg/provider/handlers/read.go @@ -26,6 +26,7 @@ func MakeReadHandler(client *containerd.Client) func(w http.ResponseWriter, r *h Image: function.image, Replicas: uint64(function.replicas), Namespace: function.namespace, + Labels: &function.labels, }) }