-
Notifications
You must be signed in to change notification settings - Fork 474
/
istio_config.go
271 lines (227 loc) · 7.66 KB
/
istio_config.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package handlers
import (
"io/ioutil"
"net/http"
"strings"
"sync"
"github.com/gorilla/mux"
"github.com/kiali/kiali/business"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/models"
)
func IstioConfigList(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
namespace := params["namespace"]
query := r.URL.Query()
objects := ""
parsedTypes := make([]string, 0)
if _, ok := query["objects"]; ok {
objects = strings.ToLower(query.Get("objects"))
if len(objects) > 0 {
parsedTypes = strings.Split(objects, ",")
}
}
includeValidations := false
if _, found := query["validate"]; found {
includeValidations = true
}
labelSelector := ""
if _, found := query["labelSelector"]; found {
labelSelector = query.Get("labelSelector")
}
workloadSelector := ""
if _, found := query["workloadSelector"]; found {
workloadSelector = query.Get("workloadSelector")
}
criteria := business.ParseIstioConfigCriteria(namespace, objects, labelSelector, workloadSelector)
// Get business layer
business, err := getBusiness(r)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Services initialization error: "+err.Error())
return
}
var istioConfigValidations models.IstioValidations
wg := sync.WaitGroup{}
if includeValidations {
wg.Add(1)
go func(namespace string, istioConfigValidations *models.IstioValidations, err *error) {
defer wg.Done()
// We don't filter by objects when calling validations, because certain validations require fetching all types to get the correct errors
istioConfigValidationResults, errValidations := business.Validations.GetValidations(namespace, "")
if errValidations != nil && *err == nil {
*err = errValidations
} else {
if len(parsedTypes) > 0 {
istioConfigValidationResults = istioConfigValidationResults.FilterByTypes(parsedTypes)
}
*istioConfigValidations = istioConfigValidationResults
}
}(namespace, &istioConfigValidations, &err)
}
istioConfig, err := business.IstioConfig.GetIstioConfigList(criteria)
if includeValidations {
// Add validation results to the IstioConfigList once they're available (previously done in the UI layer)
wg.Wait()
istioConfig.IstioValidations = istioConfigValidations
}
if err != nil {
handleErrorResponse(w, err)
return
}
RespondWithJSON(w, http.StatusOK, istioConfig)
}
func IstioConfigDetails(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
namespace := params["namespace"]
objectType := params["object_type"]
object := params["object"]
includeValidations := false
query := r.URL.Query()
if _, found := query["validate"]; found {
includeValidations = true
}
if !checkObjectType(objectType) {
RespondWithError(w, http.StatusBadRequest, "Object type not managed: "+objectType)
return
}
// Get business layer
business, err := getBusiness(r)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Services initialization error: "+err.Error())
return
}
var istioConfigValidations models.IstioValidations
wg := sync.WaitGroup{}
if includeValidations {
wg.Add(1)
go func(istioConfigValidations *models.IstioValidations, err *error) {
defer wg.Done()
istioConfigValidationResults, errValidations := business.Validations.GetIstioObjectValidations(namespace, objectType, object)
if errValidations != nil && *err == nil {
*err = errValidations
} else {
*istioConfigValidations = istioConfigValidationResults
}
}(&istioConfigValidations, &err)
}
istioConfigDetails, err := business.IstioConfig.GetIstioConfigDetails(namespace, objectType, object)
if includeValidations && err == nil {
wg.Wait()
if validation, found := istioConfigValidations[models.IstioValidationKey{ObjectType: models.ObjectTypeSingular[objectType], Namespace: namespace, Name: object}]; found {
istioConfigDetails.IstioValidation = validation
}
}
if err != nil {
handleErrorResponse(w, err)
return
}
RespondWithJSON(w, http.StatusOK, istioConfigDetails)
}
func IstioConfigDelete(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
namespace := params["namespace"]
objectType := params["object_type"]
object := params["object"]
api := business.GetIstioAPI(objectType)
if api == "" {
RespondWithError(w, http.StatusBadRequest, "Object type not managed: "+objectType)
return
}
// Get business layer
business, err := getBusiness(r)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Services initialization error: "+err.Error())
return
}
err = business.IstioConfig.DeleteIstioConfigDetail(api, namespace, objectType, object)
if err != nil {
handleErrorResponse(w, err)
return
} else {
audit(r, "DELETE on Namespace: "+namespace+" Type: "+objectType+" Name: "+object)
RespondWithCode(w, http.StatusOK)
}
}
func IstioConfigUpdate(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
namespace := params["namespace"]
objectType := params["object_type"]
object := params["object"]
api := business.GetIstioAPI(objectType)
if api == "" {
RespondWithError(w, http.StatusBadRequest, "Object type not managed: "+objectType)
return
}
// Get business layer
business, err := getBusiness(r)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Services initialization error: "+err.Error())
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
RespondWithError(w, http.StatusBadRequest, "Update request with bad update patch: "+err.Error())
}
jsonPatch := string(body)
updatedConfigDetails, err := business.IstioConfig.UpdateIstioConfigDetail(api, namespace, objectType, object, jsonPatch)
if err != nil {
handleErrorResponse(w, err)
return
}
audit(r, "UPDATE on Namespace: "+namespace+" Type: "+objectType+" Name: "+object+" Patch: "+jsonPatch)
RespondWithJSON(w, http.StatusOK, updatedConfigDetails)
}
func IstioConfigCreate(w http.ResponseWriter, r *http.Request) {
// Feels kinda replicated for multiple functions..
params := mux.Vars(r)
namespace := params["namespace"]
objectType := params["object_type"]
api := business.GetIstioAPI(objectType)
if api == "" {
RespondWithError(w, http.StatusBadRequest, "Object type not managed: "+objectType)
return
}
// Get business layer
business, err := getBusiness(r)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Services initialization error: "+err.Error())
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
RespondWithError(w, http.StatusBadRequest, "Create request could not be read: "+err.Error())
}
createdConfigDetails, err := business.IstioConfig.CreateIstioConfigDetail(api, namespace, objectType, body)
if err != nil {
handleErrorResponse(w, err)
return
}
audit(r, "CREATE on Namespace: "+namespace+" Type: "+objectType+" Object: "+string(body))
RespondWithJSON(w, http.StatusOK, createdConfigDetails)
}
func checkObjectType(objectType string) bool {
return business.GetIstioAPI(objectType) != ""
}
func audit(r *http.Request, message string) {
if config.Get().Server.AuditLog {
user := r.Header.Get("Kiali-User")
log.Infof("AUDIT User [%s] Msg [%s]", user, message)
}
}
func IstioConfigPermissions(w http.ResponseWriter, r *http.Request) {
// query params
params := r.URL.Query()
namespaces := params.Get("namespaces") // csl of namespaces
business, err := getBusiness(r)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Services initialization error: "+err.Error())
return
}
istioConfigPermissions := models.IstioConfigPermissions{}
if len(namespaces) > 0 {
ns := strings.Split(namespaces, ",")
istioConfigPermissions = business.IstioConfig.GetIstioConfigPermissions(ns)
}
RespondWithJSON(w, http.StatusOK, istioConfigPermissions)
}