diff --git a/pkg/handlers/namespaces.go b/pkg/handlers/namespaces.go index a1bde0955..885dc7a03 100644 --- a/pkg/handlers/namespaces.go +++ b/pkg/handlers/namespaces.go @@ -15,6 +15,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" + glog "k8s.io/klog" ) // MakeNamespacesLister builds a list of namespaces with an "openfaas" tag, or the default name @@ -24,7 +25,14 @@ func MakeNamespacesLister(defaultNamespace string, clientset kubernetes.Interfac res := ListNamespaces(defaultNamespace, clientset) - out, _ := json.Marshal(res) + out, err := json.Marshal(res) + if err != nil { + glog.Errorf("Failed to list namespaces: %s", err.Error()) + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("Failed to list namespaces")) + return + } + w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) diff --git a/pkg/handlers/reader.go b/pkg/handlers/reader.go index d521a526f..8723b7f7d 100644 --- a/pkg/handlers/reader.go +++ b/pkg/handlers/reader.go @@ -15,6 +15,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" + glog "k8s.io/klog" "github.com/openfaas/faas-netes/pkg/k8s" ) @@ -45,7 +46,14 @@ func MakeFunctionReader(defaultNamespace string, clientset *kubernetes.Clientset return } - functionBytes, _ := json.Marshal(functions) + functionBytes, err := json.Marshal(functions) + if err != nil { + glog.Errorf("Failed to marshal functions: %s", err.Error()) + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("Failed to marshal functions")) + return + } + w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) w.Write(functionBytes) diff --git a/pkg/handlers/replicas.go b/pkg/handlers/replicas.go index eb37e1555..6d0ef59ae 100644 --- a/pkg/handlers/replicas.go +++ b/pkg/handlers/replicas.go @@ -15,6 +15,7 @@ import ( "github.com/openfaas/faas-provider/types" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" + glog "k8s.io/klog" ) // MakeReplicaUpdater updates desired count of replicas @@ -115,7 +116,14 @@ func MakeReplicaReader(defaultNamespace string, clientset *kubernetes.Clientset) log.Printf("Read replicas - %s %s, %d/%d\n", functionName, lookupNamespace, function.AvailableReplicas, function.Replicas) - functionBytes, _ := json.Marshal(function) + functionBytes, err := json.Marshal(function) + if err != nil { + glog.Errorf("Failed to marshal function: %s", err.Error()) + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("Failed to marshal function")) + return + } + w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) w.Write(functionBytes) diff --git a/pkg/server/info.go b/pkg/server/info.go index 5553037e7..fd9fd3e74 100644 --- a/pkg/server/info.go +++ b/pkg/server/info.go @@ -6,6 +6,7 @@ import ( "github.com/openfaas/faas-netes/version" "github.com/openfaas/faas-provider/types" + glog "k8s.io/klog" ) // makeInfoHandler provides the system/info endpoint @@ -27,8 +28,9 @@ func makeInfoHandler() http.HandlerFunc { infoBytes, err := json.Marshal(info) if err != nil { + glog.Errorf("Failed to marshal info: %s", err.Error()) w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) + w.Write([]byte("Failed to marshal info")) return } diff --git a/pkg/server/list.go b/pkg/server/list.go index ef4febb04..b317ed05b 100644 --- a/pkg/server/list.go +++ b/pkg/server/list.go @@ -67,10 +67,16 @@ func makeListHandler(defaultNamespace string, functions = append(functions, function) } - functionBytes, _ := json.Marshal(functions) + functionBytes, err := json.Marshal(functions) + if err != nil { + glog.Errorf("Failed to marshal functions: %s", err.Error()) + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("Failed to marshal functions")) + return + } + w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) w.Write(functionBytes) - } } diff --git a/pkg/server/namespace.go b/pkg/server/namespace.go index 15dba36e7..7b31d9a0a 100644 --- a/pkg/server/namespace.go +++ b/pkg/server/namespace.go @@ -6,13 +6,21 @@ import ( "github.com/openfaas/faas-netes/pkg/handlers" "k8s.io/client-go/kubernetes" + glog "k8s.io/klog" ) func makeListNamespaceHandler(defaultNamespace string, clientset kubernetes.Interface) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { res := handlers.ListNamespaces(defaultNamespace, clientset) - out, _ := json.Marshal(res) + out, err := json.Marshal(res) + if err != nil { + glog.Errorf("Failed to marshal namespaces: %s", err.Error()) + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("Failed to marshal namespaces")) + return + } + w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) diff --git a/pkg/server/replicas.go b/pkg/server/replicas.go index d513e1c91..a69799c57 100644 --- a/pkg/server/replicas.go +++ b/pkg/server/replicas.go @@ -53,7 +53,14 @@ func makeReplicaReader(defaultNamespace string, client clientset.Interface, list Namespace: lookupNamespace, } - res, _ := json.Marshal(result) + res, err := json.Marshal(result) + if err != nil { + glog.Errorf("Failed to marshal function status: %s", err.Error()) + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("Failed to marshal function status")) + return + } + w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) w.Write(res)