/
namespaces.go
127 lines (102 loc) · 3.54 KB
/
namespaces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Copyright 2020 OpenFaaS Author(s)
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package handlers
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
klog "k8s.io/klog"
)
// MakeNamespacesLister builds a list of namespaces with an "openfaas" tag, or the default name
func MakeNamespacesLister(defaultNamespace string, clientset kubernetes.Interface) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
defer r.Body.Close()
}
namespaces := []string{defaultNamespace}
out, err := json.Marshal(namespaces)
if err != nil {
klog.Errorf("Failed to list namespaces: %s", err.Error())
http.Error(w, "Failed to list namespaces", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(out)
}
}
// NamespaceResolver is a method that determines the requested namespace corresponding to an
// HTTP request. It then validates that OpenFaaS is permitted to operate in that the namespace.
type NamespaceResolver func(r *http.Request) (namespace string, err error)
// NewNamespaceResolver returns a generic namespace resolver that will inspect both the GET query
// parameters and the request body. It looks for the query param or json key "namespace".
func NewNamespaceResolver(defaultNamespace string, kube kubernetes.Interface) NamespaceResolver {
return func(r *http.Request) (string, error) {
req := struct{ Namespace string }{Namespace: defaultNamespace}
switch r.Method {
case http.MethodGet:
q := r.URL.Query()
namespace := q.Get("namespace")
if len(namespace) > 0 {
req.Namespace = namespace
}
if req.Namespace != defaultNamespace {
return "", fmt.Errorf("namespace %s is not allowed", req.Namespace)
}
case http.MethodPost, http.MethodPut, http.MethodDelete:
body, _ := io.ReadAll(r.Body)
err := json.Unmarshal(body, &req)
if err != nil {
log.Printf("error while getting namespace: %s\n", err)
return "", fmt.Errorf("unable to unmarshal json request")
}
if req.Namespace != defaultNamespace {
return "", fmt.Errorf("namespace %s is not allowed", req.Namespace)
}
// Reconstruct Body
r.Body = io.NopCloser(bytes.NewBuffer(body))
}
if req.Namespace != defaultNamespace {
return "", fmt.Errorf("unable to manage secrets within the %s namespace", req.Namespace)
}
return defaultNamespace, nil
}
}
// ListNamespaces lists all namespaces annotated with openfaas true
func ListNamespaces(defaultNamespace string, clientset kubernetes.Interface) []string {
listOptions := metav1.ListOptions{}
namespaces, err := clientset.CoreV1().Namespaces().List(context.TODO(), 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
}