Skip to content

Commit c402b91

Browse files
committed
Silence errors for namespace listing
Fixes: openfaas#724 Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 2c470fc commit c402b91

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

chart/openfaas/templates/gateway-dep.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ spec:
237237
value: "{{ .Values.faasnetes.livenessProbe.timeoutSeconds }}"
238238
- name: liveness_probe_period_seconds
239239
value: "{{ .Values.faasnetes.livenessProbe.periodSeconds }}"
240+
- name: cluster_role
241+
value: "{{ .Values.clusterRole }}"
240242
volumeMounts:
241243
- mountPath: /tmp
242244
name: faas-netes-temp-volume

main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func main() {
135135
runController(setup)
136136
} else {
137137
log.Println("Starting operator")
138-
runOperator(setup)
138+
runOperator(setup, config)
139139
}
140140
}
141141

@@ -178,14 +178,14 @@ func runController(setup serverSetup) {
178178
InfoHandler: handlers.MakeInfoHandler(version.BuildVersion(), version.GitCommit),
179179
SecretHandler: handlers.MakeSecretHandler(config.DefaultFunctionNamespace, kubeClient),
180180
LogHandler: logs.NewLogHandlerFunc(k8s.NewLogRequestor(kubeClient, config.DefaultFunctionNamespace), config.FaaSConfig.WriteTimeout),
181-
ListNamespaceHandler: handlers.MakeNamespacesLister(config.DefaultFunctionNamespace, kubeClient),
181+
ListNamespaceHandler: handlers.MakeNamespacesLister(config.DefaultFunctionNamespace, config.ClusterRole, kubeClient),
182182
}
183183

184184
faasProvider.Serve(&bootstrapHandlers, &config.FaaSConfig)
185185
}
186186

187187
// runOperator runs the CRD Operator
188-
func runOperator(setup serverSetup) {
188+
func runOperator(setup serverSetup, cfg config.BootstrapConfig) {
189189
// pull out the required config and clients fromthe setup, this is largely a
190190
// leftover from refactoring the setup to a shared step and keeping the function
191191
// signature readable
@@ -218,7 +218,7 @@ func runOperator(setup serverSetup) {
218218
factory,
219219
)
220220

221-
srv := server.New(faasClient, kubeClient, endpointsInformer, deploymentInformer)
221+
srv := server.New(faasClient, kubeClient, endpointsInformer, deploymentInformer, cfg.ClusterRole)
222222

223223
go faasInformerFactory.Start(stopCh)
224224
go kubeInformerFactory.Start(stopCh)

pkg/handlers/namespaces.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,29 @@ import (
1919
)
2020

2121
// MakeNamespacesLister builds a list of namespaces with an "openfaas" tag, or the default name
22-
func MakeNamespacesLister(defaultNamespace string, clientset kubernetes.Interface) http.HandlerFunc {
22+
func MakeNamespacesLister(defaultNamespace string, clusterRole bool, clientset kubernetes.Interface) http.HandlerFunc {
2323
return func(w http.ResponseWriter, r *http.Request) {
24-
log.Println("Query namespaces")
24+
// log.Println("List namespaces")
2525

2626
if r.Body != nil {
2727
defer r.Body.Close()
2828
}
2929

30-
res := ListNamespaces(defaultNamespace, clientset)
30+
namespaces := []string{}
31+
if clusterRole {
32+
namespaces = ListNamespaces(defaultNamespace, clientset)
33+
} else {
34+
namespaces = append(namespaces, defaultNamespace)
35+
}
3136

32-
out, err := json.Marshal(res)
37+
out, err := json.Marshal(namespaces)
3338
if err != nil {
3439
glog.Errorf("Failed to list namespaces: %s", err.Error())
35-
w.WriteHeader(http.StatusInternalServerError)
36-
w.Write([]byte("Failed to list namespaces"))
40+
http.Error(w, "Failed to list namespaces", http.StatusInternalServerError)
3741
return
3842
}
3943

4044
w.Header().Set("Content-Type", "application/json")
41-
4245
w.WriteHeader(http.StatusOK)
4346
w.Write(out)
4447
}

pkg/handlers/replicas.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io/ioutil"
1111
"log"
1212
"net/http"
13+
"time"
1314

1415
"github.com/gorilla/mux"
1516
"github.com/openfaas/faas-provider/types"
@@ -102,6 +103,7 @@ func MakeReplicaReader(defaultNamespace string, clientset *kubernetes.Clientset)
102103
lookupNamespace = namespace
103104
}
104105

106+
s := time.Now()
105107
function, err := getService(lookupNamespace, functionName, clientset)
106108
if err != nil {
107109
log.Printf("Unable to fetch service: %s %s\n", functionName, namespace)
@@ -113,8 +115,8 @@ func MakeReplicaReader(defaultNamespace string, clientset *kubernetes.Clientset)
113115
w.WriteHeader(http.StatusNotFound)
114116
return
115117
}
116-
117-
log.Printf("Read replicas - %s %s, %d/%d\n", functionName, lookupNamespace, function.AvailableReplicas, function.Replicas)
118+
d := time.Since(s)
119+
log.Printf("Replicas: %s.%s, (%d/%d)\t%dms\n", functionName, lookupNamespace, function.AvailableReplicas, function.Replicas, d.Milliseconds())
118120

119121
functionBytes, err := json.Marshal(function)
120122
if err != nil {

pkg/server/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ const defaultWriteTimeout = 8
3434
func New(client clientset.Interface,
3535
kube kubernetes.Interface,
3636
endpointsInformer coreinformer.EndpointsInformer,
37-
deploymentsInformer appsinformer.DeploymentInformer) *Server {
37+
deploymentsInformer appsinformer.DeploymentInformer,
38+
clusterRole bool) *Server {
3839

3940
functionNamespace := "openfaas-fn"
4041
if namespace, exists := os.LookupEnv("function_namespace"); exists {
@@ -92,8 +93,8 @@ func New(client clientset.Interface,
9293
HealthHandler: makeHealthHandler(),
9394
InfoHandler: makeInfoHandler(),
9495
SecretHandler: handlers.MakeSecretHandler(functionNamespace, kube),
95-
ListNamespaceHandler: handlers.MakeNamespacesLister(functionNamespace, kube),
9696
LogHandler: logs.NewLogHandlerFunc(faasnetesk8s.NewLogRequestor(kube, functionNamespace), bootstrapConfig.WriteTimeout),
97+
ListNamespaceHandler: handlers.MakeNamespacesLister(functionNamespace, clusterRole, kube),
9798
}
9899

99100
if pprof == "true" {

0 commit comments

Comments
 (0)