forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespaces.go
95 lines (81 loc) · 2.91 KB
/
namespaces.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
93
94
95
package handlers
import (
"net/http"
"github.com/gorilla/mux"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/prometheus"
"github.com/kiali/kiali/services/business"
"github.com/kiali/kiali/services/models"
)
func NamespaceList(w http.ResponseWriter, r *http.Request) {
namespaces, err := models.GetNamespaces()
if err != nil {
log.Error(err)
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}
RespondWithJSON(w, http.StatusOK, namespaces)
}
// NamespaceMetrics is the API handler to fetch metrics to be displayed, related to all
// services in the namespace
func NamespaceMetrics(w http.ResponseWriter, r *http.Request) {
getNamespaceMetrics(w, r, prometheus.NewClient)
}
// NamespaceHealth is the API handler to get health of every services in the given namespace
func NamespaceHealth(w http.ResponseWriter, r *http.Request) {
// Get business layer
business, err := business.Get()
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Services initialization error: "+err.Error())
return
}
vars := mux.Vars(r)
namespace := vars["namespace"]
// Rate interval is needed to fetch request rates based health
rateInterval := defaultHealthRateInterval
queryParams := r.URL.Query()
if rateIntervals, ok := queryParams["rateInterval"]; ok && len(rateIntervals) > 0 {
rateInterval = rateIntervals[0]
}
health, err := business.Health.GetNamespaceHealth(namespace, rateInterval)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Error while fetching health: "+err.Error())
return
}
RespondWithJSON(w, http.StatusOK, health)
}
// NamespaceIstioValidations is the API handler to get istio validations of a given namespace
func NamespaceIstioValidations(w http.ResponseWriter, r *http.Request) {
// Get business layer
business, err := business.Get()
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Services initialization error: "+err.Error())
return
}
vars := mux.Vars(r)
namespace := vars["namespace"]
istioValidations, err := business.Validations.GetNamespaceValidations(namespace)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Error checking istio object consistency: "+err.Error())
return
}
RespondWithJSON(w, http.StatusOK, istioValidations)
}
// getServiceMetrics (mock-friendly version)
func getNamespaceMetrics(w http.ResponseWriter, r *http.Request, promClientSupplier func() (*prometheus.Client, error)) {
vars := mux.Vars(r)
namespace := vars["namespace"]
params, err := extractNamespaceMetricsQuery(r, namespace, "")
if err != nil {
RespondWithError(w, http.StatusBadRequest, err.Error())
return
}
prometheusClient, err := promClientSupplier()
if err != nil {
log.Error(err)
RespondWithError(w, http.StatusServiceUnavailable, "Prometheus client error: "+err.Error())
return
}
metrics := prometheusClient.GetNamespaceMetrics(params)
RespondWithJSON(w, http.StatusOK, metrics)
}