Skip to content

Commit 13399f9

Browse files
committed
Fix issues with namespace usage
The update handler was trying to read the namespace from a query-string, but clients pass this in the request body. The handler was changed to match the approach of the create handler. Tested e2e with Kubernetes 1.14.1 Additionally the delete handler was writing HTTP headers more than once in some conditions. This has also been updated. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent d85b428 commit 13399f9

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

handlers/delete.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package handlers
55

66
import (
77
"encoding/json"
8+
"fmt"
89
"io/ioutil"
910
"net/http"
1011

@@ -31,6 +32,7 @@ func MakeDeleteHandler(defaultNamespace string, clientset *kubernetes.Clientset)
3132

3233
if lookupNamespace == "kube-system" {
3334
http.Error(w, "unable to list within the kube-system namespace", http.StatusUnauthorized)
35+
return
3436
}
3537

3638
body, _ := ioutil.ReadAll(r.Body)
@@ -66,7 +68,10 @@ func MakeDeleteHandler(defaultNamespace string, clientset *kubernetes.Clientset)
6668
}
6769

6870
if isFunction(deployment) {
69-
deleteFunction(lookupNamespace, clientset, request, w)
71+
err := deleteFunction(lookupNamespace, clientset, request, w)
72+
if err != nil {
73+
return
74+
}
7075
} else {
7176
w.WriteHeader(http.StatusBadRequest)
7277

@@ -88,7 +93,7 @@ func isFunction(deployment *appsv1.Deployment) bool {
8893
return false
8994
}
9095

91-
func deleteFunction(functionNamespace string, clientset *kubernetes.Clientset, request requests.DeleteFunctionRequest, w http.ResponseWriter) {
96+
func deleteFunction(functionNamespace string, clientset *kubernetes.Clientset, request requests.DeleteFunctionRequest, w http.ResponseWriter) error {
9297
foregroundPolicy := metav1.DeletePropagationForeground
9398
opts := &metav1.DeleteOptions{PropagationPolicy: &foregroundPolicy}
9499

@@ -101,7 +106,7 @@ func deleteFunction(functionNamespace string, clientset *kubernetes.Clientset, r
101106
w.WriteHeader(http.StatusInternalServerError)
102107
}
103108
w.Write([]byte(deployErr.Error()))
104-
return
109+
return fmt.Errorf("error deleting function's deployment")
105110
}
106111

107112
if svcErr := clientset.CoreV1().
@@ -115,6 +120,7 @@ func deleteFunction(functionNamespace string, clientset *kubernetes.Clientset, r
115120
}
116121

117122
w.Write([]byte(svcErr.Error()))
118-
return
123+
return fmt.Errorf("error deleting function's service")
119124
}
125+
return nil
120126
}

handlers/deploy.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ const initialReplicasCount = 1
3030
// MakeDeployHandler creates a handler to create new functions in the cluster
3131
func MakeDeployHandler(functionNamespace string, factory k8s.FunctionFactory) http.HandlerFunc {
3232
return func(w http.ResponseWriter, r *http.Request) {
33-
defer r.Body.Close()
33+
34+
if r.Body != nil {
35+
defer r.Body.Close()
36+
}
3437

3538
body, _ := ioutil.ReadAll(r.Body)
3639

handlers/reader.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
// MakeFunctionReader handler for reading functions deployed in the cluster as deployments.
2020
func MakeFunctionReader(defaultNamespace string, clientset *kubernetes.Clientset) http.HandlerFunc {
2121
return func(w http.ResponseWriter, r *http.Request) {
22+
2223
q := r.URL.Query()
2324
namespace := q.Get("namespace")
2425

@@ -30,6 +31,7 @@ func MakeFunctionReader(defaultNamespace string, clientset *kubernetes.Clientset
3031

3132
if lookupNamespace == "kube-system" {
3233
http.Error(w, "unable to list within the kube-system namespace", http.StatusUnauthorized)
34+
return
3335
}
3436

3537
functions, err := getServiceList(lookupNamespace, clientset)

handlers/update.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,8 @@ import (
2020
// MakeUpdateHandler update specified function
2121
func MakeUpdateHandler(defaultNamespace string, factory k8s.FunctionFactory) http.HandlerFunc {
2222
return func(w http.ResponseWriter, r *http.Request) {
23-
defer r.Body.Close()
24-
25-
q := r.URL.Query()
26-
namespace := q.Get("namespace")
27-
28-
lookupNamespace := defaultNamespace
29-
30-
if len(namespace) > 0 {
31-
lookupNamespace = namespace
32-
}
33-
34-
if lookupNamespace == "kube-system" {
35-
http.Error(w, "unable to list within the kube-system namespace", http.StatusUnauthorized)
36-
return
23+
if r.Body != nil {
24+
defer r.Body.Close()
3725
}
3826

3927
body, _ := ioutil.ReadAll(r.Body)
@@ -46,25 +34,35 @@ func MakeUpdateHandler(defaultNamespace string, factory k8s.FunctionFactory) htt
4634
return
4735
}
4836

37+
lookupNamespace := defaultNamespace
38+
if len(request.Namespace) > 0 {
39+
lookupNamespace = request.Namespace
40+
}
41+
42+
if lookupNamespace == "kube-system" {
43+
http.Error(w, "unable to list within the kube-system namespace", http.StatusUnauthorized)
44+
return
45+
}
46+
4947
annotations := buildAnnotations(request)
5048
if err, status := updateDeploymentSpec(lookupNamespace, factory, request, annotations); err != nil {
5149
if !isNotFound(err) {
52-
log.Printf("error updating deployment: %s\n", err)
50+
log.Printf("error updating deployment: %s.%s, error: %s\n", request.Service, lookupNamespace, err)
5351

5452
return
5553
}
5654

57-
wrappedErr := fmt.Errorf("unable update Deployment: %s", err.Error())
55+
wrappedErr := fmt.Errorf("unable update Deployment: %s.%s, error: %s", request.Service, lookupNamespace, err.Error())
5856
http.Error(w, wrappedErr.Error(), status)
5957
return
6058
}
6159

6260
if err, status := updateService(lookupNamespace, factory, request, annotations); err != nil {
6361
if !isNotFound(err) {
64-
log.Printf("error updating service: %s\n", err)
62+
log.Printf("error updating service: %s.%s, error: %s\n", request.Service, lookupNamespace, err)
6563
}
6664

67-
wrappedErr := fmt.Errorf("unable update Service: %s", err.Error())
65+
wrappedErr := fmt.Errorf("unable update Service: %s.%s, error: %s", request.Service, request.Namespace, err.Error())
6866
http.Error(w, wrappedErr.Error(), status)
6967
return
7068
}

0 commit comments

Comments
 (0)