-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
9 changed files
with
124 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.