/
services.go
232 lines (200 loc) · 6.58 KB
/
services.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package handlers
import (
"fmt"
"io"
"net/http"
"strconv"
"sync"
"github.com/gorilla/mux"
"github.com/kiali/kiali/business"
"github.com/kiali/kiali/models"
"github.com/kiali/kiali/util"
)
// serviceListParams holds the path and query parameters for ServiceList
//
// swagger:parameters serviceList
type serviceListParams struct {
baseHealthParams
// The target workload
//
// in: path
Namespace string `json:"namespace"`
// Optional
IncludeHealth bool `json:"health"`
IncludeIstioResources bool `json:"istioResources"`
IncludeOnlyDefinitions bool `json:"onlyDefinitions"`
}
func (p *serviceListParams) extract(r *http.Request) {
vars := mux.Vars(r)
query := r.URL.Query()
p.baseExtract(r, vars)
p.Namespace = vars["namespace"]
var err error
p.IncludeHealth, err = strconv.ParseBool(query.Get("health"))
if err != nil {
p.IncludeHealth = true
}
p.IncludeIstioResources, err = strconv.ParseBool(query.Get("istioResources"))
if err != nil {
p.IncludeIstioResources = true
}
p.IncludeOnlyDefinitions, err = strconv.ParseBool(query.Get("onlyDefinitions"))
if err != nil {
p.IncludeOnlyDefinitions = true
}
}
// ServiceList is the API handler to fetch the list of services in a given namespace
func ServiceList(w http.ResponseWriter, r *http.Request) {
p := serviceListParams{}
p.extract(r)
criteria := business.ServiceCriteria{
// Purposefully leaving cluster out of Criteria because the frontend doesn't
// yet send the cluster param and the business service will iterate over all
// clusters if a cluster criteria is not provided which is what we want.
Namespace: p.Namespace,
IncludeHealth: p.IncludeHealth,
IncludeIstioResources: p.IncludeIstioResources,
IncludeOnlyDefinitions: p.IncludeOnlyDefinitions,
RateInterval: "",
QueryTime: p.QueryTime,
}
// Get business layer
business, err := getBusiness(r)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Services initialization error: "+err.Error())
return
}
if criteria.IncludeHealth {
// When the cluster is not specified, we need to get it. If there are more than one,
// get the one for which the namespace creation time is oldest
namespaces, _ := business.Namespace.GetNamespaceClusters(r.Context(), p.Namespace)
if len(namespaces) == 0 {
err = fmt.Errorf("No clusters found for namespace [%s]", p.Namespace)
handleErrorResponse(w, err, "Error looking for cluster: "+err.Error())
return
}
ns := GetOldestNamespace(namespaces)
rateInterval, err := adjustRateInterval(r.Context(), business, p.Namespace, p.RateInterval, p.QueryTime, ns.Cluster)
if err != nil {
handleErrorResponse(w, err, "Adjust rate interval error: "+err.Error())
return
}
criteria.RateInterval = rateInterval
}
// Fetch and build services
serviceList, err := business.Svc.GetServiceList(r.Context(), criteria)
if err != nil {
handleErrorResponse(w, err)
return
}
RespondWithJSON(w, http.StatusOK, serviceList)
}
// ServiceDetails is the API handler to fetch full details of an specific service
func ServiceDetails(w http.ResponseWriter, r *http.Request) {
// Get business layer
business, err := getBusiness(r)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Services initialization error: "+err.Error())
return
}
// Rate interval is needed to fetch request rates based health
queryParams := r.URL.Query()
rateInterval := queryParams.Get("rateInterval")
if rateInterval == "" {
rateInterval = defaultHealthRateInterval
}
includeValidations := false
if _, found := queryParams["validate"]; found {
includeValidations = true
}
params := mux.Vars(r)
cluster := clusterNameFromQuery(queryParams)
namespace := params["namespace"]
service := params["service"]
queryTime := util.Clock.Now()
rateInterval, err = adjustRateInterval(r.Context(), business, namespace, rateInterval, queryTime, cluster)
if err != nil {
handleErrorResponse(w, err)
return
}
istioConfigValidations := models.IstioValidations{}
var errValidations error
wg := sync.WaitGroup{}
if includeValidations {
wg.Add(1)
go func() {
defer wg.Done()
istioConfigValidations, errValidations = business.Validations.GetValidations(r.Context(), cluster, namespace, service, "")
}()
}
serviceDetails, err := business.Svc.GetServiceDetails(r.Context(), cluster, namespace, service, rateInterval, queryTime)
if includeValidations && err == nil {
wg.Wait()
serviceDetails.Validations = istioConfigValidations
err = errValidations
}
if err != nil {
handleErrorResponse(w, err)
return
}
RespondWithJSON(w, http.StatusOK, serviceDetails)
}
func ServiceUpdate(w http.ResponseWriter, r *http.Request) {
// Get business layer
business, err := getBusiness(r)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Services initialization error: "+err.Error())
return
}
// Rate interval is needed to fetch request rates based health
queryParams := r.URL.Query()
rateInterval := queryParams.Get("rateInterval")
if rateInterval == "" {
rateInterval = defaultHealthRateInterval
}
patchType := queryParams.Get("patchType")
if patchType == "" {
patchType = defaultPatchType
}
includeValidations := false
if _, found := queryParams["validate"]; found {
includeValidations = true
}
params := mux.Vars(r)
cluster := clusterNameFromQuery(queryParams)
namespace := params["namespace"]
service := params["service"]
queryTime := util.Clock.Now()
rateInterval, err = adjustRateInterval(r.Context(), business, namespace, rateInterval, queryTime, cluster)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Adjust rate interval error: "+err.Error())
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
RespondWithError(w, http.StatusBadRequest, "Update request with bad update patch: "+err.Error())
}
jsonPatch := string(body)
istioConfigValidations := models.IstioValidations{}
var errValidations error
wg := sync.WaitGroup{}
if includeValidations {
wg.Add(1)
go func() {
defer wg.Done()
istioConfigValidations, errValidations = business.Validations.GetValidations(r.Context(), cluster, namespace, service, "")
}()
}
serviceDetails, err := business.Svc.UpdateService(r.Context(), cluster, namespace, service, rateInterval, queryTime, jsonPatch, patchType)
if includeValidations && err == nil {
wg.Wait()
serviceDetails.Validations = istioConfigValidations
err = errValidations
}
if err != nil {
handleErrorResponse(w, err)
return
}
audit(r, "UPDATE on Namespace: "+namespace+" Service name: "+service+" Patch: "+jsonPatch)
RespondWithJSON(w, http.StatusOK, serviceDetails)
}