This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
handler.go
92 lines (78 loc) · 3.03 KB
/
handler.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
package http
import (
"encoding/json"
"net/http"
"net/url"
"strings"
"k8s.io/apimachinery/pkg/api/errors"
"github.com/kiali/k-charted/business"
"github.com/kiali/k-charted/config"
"github.com/kiali/k-charted/log"
"github.com/kiali/k-charted/model"
)
// DashboardHandler is the API handler to fetch runtime metrics to be displayed.
// It expects "namespace" and "dashboard" to be provided as path params. Label filters can be provided as query params
// (see also: ExtractDashboardQueryParams)
func DashboardHandler(queryParams url.Values, pathParams map[string]string, w http.ResponseWriter, conf config.Config, logger log.LogAdapter) {
namespace := pathParams["namespace"]
dashboardName := pathParams["dashboard"]
svc := business.NewDashboardsService(conf, logger)
params := model.DashboardQuery{Namespace: namespace}
err := ExtractDashboardQueryParams(queryParams, ¶ms)
if err != nil {
respondWithError(svc.Logger, w, http.StatusBadRequest, err.Error())
return
}
dashboard, err := svc.GetDashboard(params, dashboardName)
if err != nil {
if errors.IsNotFound(err) {
respondWithError(svc.Logger, w, http.StatusNotFound, err.Error())
} else {
respondWithError(svc.Logger, w, http.StatusInternalServerError, err.Error())
}
return
}
respondWithJSON(svc.Logger, w, http.StatusOK, dashboard)
}
// SearchDashboardsHandler is the API handler to search for all available dashboards on pods
// It expects "namespace" to be provided as path param. Label filters can be provided as query params
// (see also: ExtractDashboardQueryParams)
func SearchDashboardsHandler(queryParams url.Values, pathParams map[string]string, w http.ResponseWriter, conf config.Config, logger log.LogAdapter) {
namespace := pathParams["namespace"]
labels := queryParams.Get("labelsFilters")
var runtimes []model.Runtime
svc := business.NewDashboardsService(conf, logger)
if conf.PodsLoader != nil {
pods, err := conf.PodsLoader(namespace, strings.Replace(labels, ":", "=", -1))
if err != nil {
if errors.IsNotFound(err) {
respondWithError(svc.Logger, w, http.StatusNotFound, err.Error())
} else {
respondWithError(svc.Logger, w, http.StatusInternalServerError, err.Error())
}
return
}
runtimes = svc.SearchExplicitDashboards(namespace, pods)
}
if len(runtimes) == 0 {
labelsMap := extractLabelsFilters(labels)
runtimes = svc.DiscoverDashboards(namespace, labelsMap)
}
respondWithJSON(svc.Logger, w, http.StatusOK, runtimes)
}
func respondWithJSON(logger log.SafeAdapter, w http.ResponseWriter, code int, payload interface{}) {
response, err := json.Marshal(payload)
if err != nil {
response, _ = json.Marshal(map[string]string{"error": err.Error()})
code = http.StatusInternalServerError
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
_, err = w.Write(response)
if err != nil {
logger.Errorf("could not write response: %v", err)
}
}
func respondWithError(logger log.SafeAdapter, w http.ResponseWriter, code int, message string) {
respondWithJSON(logger, w, code, map[string]string{"error": message})
}