summaryrefslogtreecommitdiffstats
path: root/src/k8splugin
diff options
context:
space:
mode:
authorRitu Sood <Ritu.Sood@intel.com>2019-11-12 17:48:45 +0000
committerGerrit Code Review <gerrit@onap.org>2019-11-12 17:48:45 +0000
commitc58a35989f731a0d1c8ec75e85480873da4b1c35 (patch)
treee38e5ce514fbf8d1ba0af40ae75d38fa7c79c4e9 /src/k8splugin
parent43eb8399839b26cafb2682731f5665a7c8e593a6 (diff)
parentd345d6ed10245a042c2cda50b1cf8c90bf366b4b (diff)
Merge "Update logutils to support multiple fields"
Diffstat (limited to 'src/k8splugin')
-rw-r--r--src/k8splugin/api/instancehandler.go80
-rw-r--r--src/k8splugin/internal/app/client.go39
-rw-r--r--src/k8splugin/internal/logutils/logger.go17
3 files changed, 104 insertions, 32 deletions
diff --git a/src/k8splugin/api/instancehandler.go b/src/k8splugin/api/instancehandler.go
index 1dcbcda9..b0437426 100644
--- a/src/k8splugin/api/instancehandler.go
+++ b/src/k8splugin/api/instancehandler.go
@@ -20,10 +20,10 @@ import (
"net/http"
"github.com/onap/multicloud-k8s/src/k8splugin/internal/app"
+ log "github.com/onap/multicloud-k8s/src/k8splugin/internal/logutils"
"github.com/gorilla/mux"
pkgerrors "github.com/pkg/errors"
- log "github.com/onap/multicloud-k8s/src/k8splugin/internal/logutils"
)
// Used to store the backend implementation objects
@@ -37,18 +37,25 @@ func (i instanceHandler) validateBody(body interface{}) error {
switch b := body.(type) {
case app.InstanceRequest:
if b.CloudRegion == "" {
- log.WithFields("CreateVnfRequest bad request", "CloudRegion", "Invalid/Missing CloudRegion in POST request")
+ log.Error("CreateVnfRequest Bad Request", log.Fields{
+ "cloudRegion": "Missing CloudRegion in POST request",
+ })
werr := pkgerrors.Wrap(errors.New("Invalid/Missing CloudRegion in POST request"), "CreateVnfRequest bad request")
return werr
}
if b.RBName == "" || b.RBVersion == "" {
- log.WithFields("CreateVnfRequest bad request", "RBName", "Invalid/Missing resource bundle parameters in POST request")
- log.WithFields("CreateVnfRequest bad request", "RBVersion", "Invalid/Missing resource bundle parameters in POST request")
+ log.Error("CreateVnfRequest Bad Request", log.Fields{
+ "message": "One of RBName, RBVersion is missing",
+ "RBName": b.RBName,
+ "RBVersion": b.RBVersion,
+ })
werr := pkgerrors.Wrap(errors.New("Invalid/Missing resource bundle parameters in POST request"), "CreateVnfRequest bad request")
return werr
}
if b.ProfileName == "" {
- log.WithFields("CreateVnfRequest bad request", "ProfileName", "Invalid/Missing profile name in POST request")
+ log.Error("CreateVnfRequest bad request", log.Fields{
+ "ProfileName": "Missing profile name in POST request",
+ })
werr := pkgerrors.Wrap(errors.New("Invalid/Missing profile name in POST request"), "CreateVnfRequest bad request")
return werr
}
@@ -62,11 +69,15 @@ func (i instanceHandler) createHandler(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&resource)
switch {
case err == io.EOF:
- log.WithFields("http.StatusBadRequest", "Error", "Body empty")
+ log.Error("Body Empty", log.Fields{
+ "error": io.EOF,
+ })
http.Error(w, "Body empty", http.StatusBadRequest)
return
case err != nil:
- log.WithFields("http.StatusUnprocessableEntity", "Error", "http.StatusUnprocessableEntity")
+ log.Error("Error unmarshaling Body", log.Fields{
+ "error": err,
+ })
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}
@@ -74,14 +85,19 @@ func (i instanceHandler) createHandler(w http.ResponseWriter, r *http.Request) {
// Check body for expected parameters
err = i.validateBody(resource)
if err != nil {
- log.WithFields("StatusUnprocessableEntity", "Error", "http.StatusUnprocessableEntity")
+ log.Error("Invalid Parameters in Body", log.Fields{
+ "error": err,
+ })
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}
resp, err := i.client.Create(resource)
if err != nil {
- log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError")
+ log.Error("Error Creating Resource", log.Fields{
+ "error": err,
+ "resource": resource,
+ })
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -90,7 +106,10 @@ func (i instanceHandler) createHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(resp)
if err != nil {
- log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError")
+ log.Error("Error Marshaling Response", log.Fields{
+ "error": err,
+ "response": resp,
+ })
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -103,7 +122,10 @@ func (i instanceHandler) getHandler(w http.ResponseWriter, r *http.Request) {
resp, err := i.client.Get(id)
if err != nil {
- log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError")
+ log.Error("Error getting Instance", log.Fields{
+ "error": err,
+ "id": id,
+ })
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -112,7 +134,10 @@ func (i instanceHandler) getHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(resp)
if err != nil {
- log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError")
+ log.Error("Error Marshaling Response", log.Fields{
+ "error": err,
+ "response": resp,
+ })
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -125,7 +150,10 @@ func (i instanceHandler) statusHandler(w http.ResponseWriter, r *http.Request) {
resp, err := i.client.Status(id)
if err != nil {
- log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError")
+ log.Error("Error getting Status", log.Fields{
+ "error": err,
+ "id": id,
+ })
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -134,7 +162,10 @@ func (i instanceHandler) statusHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(resp)
if err != nil {
- log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError")
+ log.Error("Error Marshaling Response", log.Fields{
+ "error": err,
+ "response": resp,
+ })
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -147,11 +178,16 @@ func (i instanceHandler) listHandler(w http.ResponseWriter, r *http.Request) {
//Which will list all instances
rbName := r.FormValue("rb-name")
rbVersion := r.FormValue("rb-version")
- ProfileName := r.FormValue("profile-name")
+ profileName := r.FormValue("profile-name")
- resp, err := i.client.List(rbName, rbVersion, ProfileName)
+ resp, err := i.client.List(rbName, rbVersion, profileName)
if err != nil {
- log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError")
+ log.Error("Error listing instances", log.Fields{
+ "error": err,
+ "rb-name": rbName,
+ "rb-version": rbVersion,
+ "profile-name": profileName,
+ })
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -160,7 +196,10 @@ func (i instanceHandler) listHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(resp)
if err != nil {
- log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError")
+ log.Error("Error Marshaling Response", log.Fields{
+ "error": err,
+ "response": resp,
+ })
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -173,7 +212,9 @@ func (i instanceHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
err := i.client.Delete(id)
if err != nil {
- log.WithFields("StatusInternalServerError", "Error", "http.StatusInternalServerError")
+ log.Error("Error Deleting Instance", log.Fields{
+ "error": err,
+ })
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -181,4 +222,3 @@ func (i instanceHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
}
-
diff --git a/src/k8splugin/internal/app/client.go b/src/k8splugin/internal/app/client.go
index b75c7dfd..78477a82 100644
--- a/src/k8splugin/internal/app/client.go
+++ b/src/k8splugin/internal/app/client.go
@@ -14,13 +14,12 @@ limitations under the License.
package app
import (
- "log"
"os"
"time"
"github.com/onap/multicloud-k8s/src/k8splugin/internal/connection"
"github.com/onap/multicloud-k8s/src/k8splugin/internal/helm"
- logutils "github.com/onap/multicloud-k8s/src/k8splugin/internal/logutils"
+ log "github.com/onap/multicloud-k8s/src/k8splugin/internal/logutils"
"github.com/onap/multicloud-k8s/src/k8splugin/internal/plugin"
pkgerrors "github.com/pkg/errors"
@@ -118,16 +117,24 @@ func (k *KubernetesClient) ensureNamespace(namespace string) error {
}, namespace, k)
if err != nil {
- logutils.WithFields(err.Error(), "namespace", namespace)
+ log.Error("Error checking for namespace", log.Fields{
+ "error": err,
+ "namespace": namespace,
+ })
return pkgerrors.Wrap(err, "Error checking for namespace: "+namespace)
}
if ns == "" {
- logutils.WithFields("Creating namespace", "namespace", namespace)
+ log.Info("Creating Namespace", log.Fields{
+ "namespace": namespace,
+ })
_, err = pluginImpl.Create("", namespace, k)
if err != nil {
- logutils.WithFields(err.Error(), "namespace", namespace)
+ log.Error("Error Creating Namespace", log.Fields{
+ "error": err,
+ "namespace": namespace,
+ })
return pkgerrors.Wrap(err, "Error creating "+namespace+" namespace")
}
}
@@ -141,7 +148,9 @@ func (k *KubernetesClient) createKind(resTempl helm.KubernetesResourceTemplate,
return helm.KubernetesResource{}, pkgerrors.New("File " + resTempl.FilePath + "does not exists")
}
- log.Println("Processing file: " + resTempl.FilePath)
+ log.Info("Processing Kubernetes Resource", log.Fields{
+ "filepath": resTempl.FilePath,
+ })
pluginImpl, err := plugin.GetPluginByKind(resTempl.GVK.Kind)
if err != nil {
@@ -150,11 +159,19 @@ func (k *KubernetesClient) createKind(resTempl helm.KubernetesResourceTemplate,
createdResourceName, err := pluginImpl.Create(resTempl.FilePath, namespace, k)
if err != nil {
- log.Printf("Error: %s while creating: %s", err.Error(), resTempl.GVK.Kind)
+ log.Error("Error Creating Resource", log.Fields{
+ "error": err,
+ "gvk": resTempl.GVK,
+ "filepath": resTempl.FilePath,
+ })
return helm.KubernetesResource{}, pkgerrors.Wrap(err, "Error in plugin "+resTempl.GVK.Kind+" plugin")
}
- log.Print(createdResourceName + " created")
+ log.Info("Created Kubernetes Resource", log.Fields{
+ "resource": createdResourceName,
+ "gvk": resTempl.GVK,
+ })
+
return helm.KubernetesResource{
GVK: resTempl.GVK,
Name: createdResourceName,
@@ -182,14 +199,16 @@ func (k *KubernetesClient) createResources(sortedTemplates []helm.KubernetesReso
}
func (k *KubernetesClient) deleteKind(resource helm.KubernetesResource, namespace string) error {
- log.Println("Deleting Kind: " + resource.GVK.Kind)
+ log.Warn("Deleting Resource", log.Fields{
+ "gvk": resource.GVK,
+ "resource": resource.Name,
+ })
pluginImpl, err := plugin.GetPluginByKind(resource.GVK.Kind)
if err != nil {
return pkgerrors.Wrap(err, "Error loading plugin")
}
- log.Println("Deleting resource: " + resource.Name)
err = pluginImpl.Delete(resource, namespace, k)
if err != nil {
return pkgerrors.Wrap(err, "Error deleting "+resource.Name)
diff --git a/src/k8splugin/internal/logutils/logger.go b/src/k8splugin/internal/logutils/logger.go
index 7df23474..2e8f9969 100644
--- a/src/k8splugin/internal/logutils/logger.go
+++ b/src/k8splugin/internal/logutils/logger.go
@@ -4,12 +4,25 @@ import (
log "github.com/sirupsen/logrus"
)
+//Fields is type that will be used by the calling function
+type Fields map[string]interface{}
+
func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{})
}
-func WithFields(msg string, fkey string, fvalue string) {
- log.WithFields(log.Fields{fkey: fvalue}).Error(msg)
+// Error uses the fields provided and logs
+func Error(msg string, fields Fields) {
+ log.WithFields(log.Fields(fields)).Error(msg)
+}
+
+// Warn uses the fields provided and logs
+func Warn(msg string, fields Fields) {
+ log.WithFields(log.Fields(fields)).Warn(msg)
}
+// Info uses the fields provided and logs
+func Info(msg string, fields Fields) {
+ log.WithFields(log.Fields(fields)).Info(msg)
+}