Skip to content

Commit

Permalink
Add namespaces lister endpoint
Browse files Browse the repository at this point in the history
This is required to list namespaces as part of #511.

The default namespace is matched, and any additional namespaces
must be annotated with the "openfaas" label. This is to prevent
kube-system, etc from being used.

Tested e2e with k3s/k8s 1.15 - the default ns showed, a new ns
was created and annotated, which then showed up in the results
subsequently.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Sep 21, 2019
1 parent 0a2dcba commit 001c1cd
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 27 deletions.
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[[constraint]]
name = "github.com/openfaas/faas-provider"
version = "0.10.1"
version = "0.10.2"

[[constraint]]
name = "github.com/gorilla/mux"
Expand Down
8 changes: 4 additions & 4 deletions handlers/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func MakeDeployHandler(functionNamespace string, factory k8s.FunctionFactory) ht
return
}

log.Println("Created deployment - " + request.Service + "," + namespace)
log.Printf("Deployment created: %s.%s\n", request.Service, namespace)

service := factory.Client.Core().Services(namespace)
serviceSpec := makeServiceSpec(request, factory)
Expand All @@ -89,8 +89,9 @@ func MakeDeployHandler(functionNamespace string, factory k8s.FunctionFactory) ht
return
}

log.Println("Created service - " + request.Service + "," + namespace)
log.Println(string(body))
log.Printf("Service created: %s.%s\n", request.Service, namespace)

// log.Println(string(body))

w.WriteHeader(http.StatusAccepted)

Expand Down Expand Up @@ -307,7 +308,6 @@ func createSelector(constraints []string) map[string]string {
}
}

// log.Println("selector: ", selector)
return selector
}

Expand Down
65 changes: 65 additions & 0 deletions handlers/namespaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package handlers

import (
"encoding/json"
"log"
"net/http"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

// MakeNamespacesLister builds a list of namespaces with an "openfaas" tag, or the default name
func MakeNamespacesLister(defaultNamespace string, clientset *kubernetes.Clientset) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("Query namespaces")

res := list(defaultNamespace, clientset)

out, _ := json.Marshal(res)
w.Header().Set("Content-Type", "application/json")

w.WriteHeader(http.StatusAccepted)
w.Write(out)
}
}

func list(defaultNamespace string, clientset *kubernetes.Clientset) []string {
listOptions := metav1.ListOptions{}
namespaces, err := clientset.CoreV1().Namespaces().List(listOptions)

set := []string{}

// Assume that an error means that a Role, instead of ClusterRole is being used
// the Role will not be able to list namespaces, so all functions are in the
// defaultNamespace
if err != nil {
log.Printf("Error listing namespaces: %s", err.Error())
set = append(set, defaultNamespace)
return set
}

for _, n := range namespaces.Items {
if _, ok := n.Annotations["openfaas"]; ok {
set = append(set, n.Name)
}
}

if !findNamespace(defaultNamespace, set) {
set = append(set, defaultNamespace)
}

return set
}

func findNamespace(target string, items []string) bool {
for _, n := range items {
if n == target {
return true
}
}
return false
}
24 changes: 24 additions & 0 deletions handlers/namespaces_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package handlers

import "testing"

func Test_findNamespace_Found(t *testing.T) {
got := findNamespace("fn", []string{"fn", "openfaas-fn"})
want := true

if got != want {
t.Errorf("findNamespace - want: %v, got %v", want, got)
}
}

func Test_findNamespace_NotFound(t *testing.T) {
got := findNamespace("fn", []string{"openfaas-fn"})
want := false

if got != want {
t.Errorf("findNamespace - want: %v, got %v", want, got)
}
}
23 changes: 12 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,18 @@ func main() {
factory := k8s.NewFunctionFactory(clientset, deployConfig)

bootstrapHandlers := bootTypes.FaaSHandlers{
FunctionProxy: handlers.MakeProxy(functionNamespace, cfg.ReadTimeout),
DeleteHandler: handlers.MakeDeleteHandler(functionNamespace, clientset),
DeployHandler: handlers.MakeDeployHandler(functionNamespace, factory),
FunctionReader: handlers.MakeFunctionReader(functionNamespace, clientset),
ReplicaReader: handlers.MakeReplicaReader(functionNamespace, clientset),
ReplicaUpdater: handlers.MakeReplicaUpdater(functionNamespace, clientset),
UpdateHandler: handlers.MakeUpdateHandler(functionNamespace, factory),
HealthHandler: handlers.MakeHealthHandler(),
InfoHandler: handlers.MakeInfoHandler(version.BuildVersion(), version.GitCommit),
SecretHandler: handlers.MakeSecretHandler(functionNamespace, clientset),
LogHandler: logs.NewLogHandlerFunc(handlers.NewLogRequestor(clientset, functionNamespace), cfg.WriteTimeout),
FunctionProxy: handlers.MakeProxy(functionNamespace, cfg.ReadTimeout),
DeleteHandler: handlers.MakeDeleteHandler(functionNamespace, clientset),
DeployHandler: handlers.MakeDeployHandler(functionNamespace, factory),
FunctionReader: handlers.MakeFunctionReader(functionNamespace, clientset),
ReplicaReader: handlers.MakeReplicaReader(functionNamespace, clientset),
ReplicaUpdater: handlers.MakeReplicaUpdater(functionNamespace, clientset),
UpdateHandler: handlers.MakeUpdateHandler(functionNamespace, factory),
HealthHandler: handlers.MakeHealthHandler(),
InfoHandler: handlers.MakeInfoHandler(version.BuildVersion(), version.GitCommit),
SecretHandler: handlers.MakeSecretHandler(functionNamespace, clientset),
LogHandler: logs.NewLogHandlerFunc(handlers.NewLogRequestor(clientset, functionNamespace), cfg.WriteTimeout),
ListNamespaceHandler: handlers.MakeNamespacesLister(functionNamespace, clientset),
}

var port int
Expand Down
4 changes: 3 additions & 1 deletion vendor/github.com/openfaas/faas-provider/Dockerfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/github.com/openfaas/faas-provider/serve.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions vendor/github.com/openfaas/faas-provider/types/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 001c1cd

Please sign in to comment.