-
Notifications
You must be signed in to change notification settings - Fork 474
/
apps.go
485 lines (432 loc) · 16.3 KB
/
apps.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package business
import (
"context"
"fmt"
"sort"
"strings"
"sync"
"time"
"k8s.io/apimachinery/pkg/labels"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/models"
"github.com/kiali/kiali/observability"
"github.com/kiali/kiali/prometheus"
)
// AppService deals with fetching Workloads group by "app" label, which will be identified as an "application"
type AppService struct {
prom prometheus.ClientInterface
userClients map[string]kubernetes.ClientInterface
businessLayer *Layer
}
type AppCriteria struct {
Namespace string
Cluster string
AppName string
IncludeIstioResources bool
IncludeHealth bool
RateInterval string
QueryTime time.Time
}
func joinMap(m1 map[string][]string, m2 map[string]string) {
for k, v2 := range m2 {
dup := false
for _, v1 := range m1[k] {
if v1 == v2 {
dup = true
break
}
}
if !dup {
m1[k] = append(m1[k], v2)
}
}
}
func buildFinalLabels(m map[string][]string) map[string]string {
consolidated := make(map[string]string, len(m))
for k, list := range m {
sort.Strings(list)
consolidated[k] = strings.Join(list, ",")
}
return consolidated
}
// GetClusterAppList is the API handler to fetch the list of applications in a given namespace and cluster
func (in *AppService) GetClusterAppList(ctx context.Context, criteria AppCriteria) (models.ClusterApps, error) {
var end observability.EndFunc
ctx, end = observability.StartSpan(ctx, "GetClusterAppList",
observability.Attribute("package", "business"),
observability.Attribute("namespace", criteria.Namespace),
observability.Attribute("cluster", criteria.Cluster),
observability.Attribute("includeHealth", criteria.IncludeHealth),
observability.Attribute("includeIstioResources", criteria.IncludeIstioResources),
observability.Attribute("rateInterval", criteria.RateInterval),
observability.Attribute("queryTime", criteria.QueryTime),
)
defer end()
appList := &models.ClusterApps{
Apps: []models.AppListItem{},
}
namespace := criteria.Namespace
cluster := criteria.Cluster
if _, ok := in.userClients[cluster]; !ok {
return *appList, fmt.Errorf("Cluster [%s] is not found or is not accessible for Kiali", cluster)
}
if _, err := in.businessLayer.Namespace.GetClusterNamespace(ctx, namespace, cluster); err != nil {
return *appList, err
}
allApps, err := in.businessLayer.App.fetchNamespaceApps(ctx, namespace, cluster, "")
if err != nil {
log.Errorf("Error fetching Applications for cluster %s per namespace %s: %s", cluster, namespace, err)
return *appList, err
}
icCriteria := IstioConfigCriteria{
IncludeAuthorizationPolicies: true,
IncludeDestinationRules: true,
IncludeEnvoyFilters: true,
IncludeGateways: true,
IncludePeerAuthentications: true,
IncludeRequestAuthentications: true,
IncludeSidecars: true,
IncludeVirtualServices: true,
}
istioConfigList := &models.IstioConfigList{}
if criteria.IncludeIstioResources {
istioConfigList, err = in.businessLayer.IstioConfig.GetIstioConfigListForNamespace(ctx, cluster, namespace, icCriteria)
if err != nil {
log.Errorf("Error fetching Istio Config for Cluster %s per namespace %s: %s", cluster, namespace, err)
return *appList, err
}
}
for keyApp, valueApp := range allApps {
appItem := &models.AppListItem{
Name: keyApp,
IstioSidecar: true,
Health: models.EmptyAppHealth(),
}
applabels := make(map[string][]string)
svcReferences := make([]*models.IstioValidationKey, 0)
for _, srv := range valueApp.Services {
joinMap(applabels, srv.Labels)
if criteria.IncludeIstioResources {
vsFiltered := kubernetes.FilterVirtualServicesByService(istioConfigList.VirtualServices, srv.Namespace, srv.Name)
for _, v := range vsFiltered {
ref := models.BuildKey(v.Kind, v.Name, v.Namespace)
svcReferences = append(svcReferences, &ref)
}
drFiltered := kubernetes.FilterDestinationRulesByService(istioConfigList.DestinationRules, srv.Namespace, srv.Name)
for _, d := range drFiltered {
ref := models.BuildKey(d.Kind, d.Name, d.Namespace)
svcReferences = append(svcReferences, &ref)
}
gwFiltered := kubernetes.FilterGatewaysByVirtualServices(istioConfigList.Gateways, istioConfigList.VirtualServices)
for _, g := range gwFiltered {
ref := models.BuildKey(g.Kind, g.Name, g.Namespace)
svcReferences = append(svcReferences, &ref)
}
}
}
wkdReferences := make([]*models.IstioValidationKey, 0)
for _, wrk := range valueApp.Workloads {
joinMap(applabels, wrk.Labels)
if criteria.IncludeIstioResources {
wSelector := labels.Set(wrk.Labels).AsSelector().String()
wkdReferences = append(wkdReferences, FilterWorkloadReferences(wSelector, *istioConfigList)...)
}
}
appItem.Labels = buildFinalLabels(applabels)
appItem.IstioReferences = FilterUniqueIstioReferences(append(svcReferences, wkdReferences...))
for _, w := range valueApp.Workloads {
if appItem.IstioSidecar = w.IstioSidecar; !appItem.IstioSidecar {
break
}
}
for _, w := range valueApp.Workloads {
if appItem.IstioAmbient = w.HasIstioAmbient(); !appItem.IstioAmbient {
break
}
}
if criteria.IncludeHealth {
appItem.Health, err = in.businessLayer.Health.GetAppHealth(ctx, criteria.Namespace, valueApp.cluster, appItem.Name, criteria.RateInterval, criteria.QueryTime, valueApp)
if err != nil {
log.Errorf("Error fetching Health in namespace %s for app %s: %s", criteria.Namespace, appItem.Name, err)
}
}
appItem.Cluster = cluster
appItem.Namespace = namespace
appList.Apps = append(appList.Apps, *appItem)
}
return *appList, nil
}
// GetAppList is the API handler to fetch the list of applications in a given namespace
func (in *AppService) GetAppList(ctx context.Context, criteria AppCriteria) (models.AppList, error) {
var end observability.EndFunc
ctx, end = observability.StartSpan(ctx, "GetAppList",
observability.Attribute("package", "business"),
observability.Attribute("namespace", criteria.Namespace),
observability.Attribute("cluster", criteria.Cluster),
observability.Attribute("includeHealth", criteria.IncludeHealth),
observability.Attribute("includeIstioResources", criteria.IncludeIstioResources),
observability.Attribute("rateInterval", criteria.RateInterval),
observability.Attribute("queryTime", criteria.QueryTime),
)
defer end()
appList := &models.AppList{
Namespace: models.Namespace{Name: criteria.Namespace},
Cluster: criteria.Cluster,
Apps: []models.AppListItem{},
}
var err error
var allApps []namespaceApps
wg := &sync.WaitGroup{}
type result struct {
cluster string
nsApps namespaceApps
err error
}
resultsCh := make(chan result)
// TODO: Use a context to define a timeout. The context should be passed to the k8s client
go func() {
for cluster := range in.userClients {
wg.Add(1)
go func(c string) {
defer wg.Done()
nsApps, error2 := in.fetchNamespaceApps(ctx, criteria.Namespace, c, "")
if error2 != nil {
resultsCh <- result{cluster: c, nsApps: nil, err: error2}
} else {
resultsCh <- result{cluster: c, nsApps: nsApps, err: nil}
}
}(cluster)
}
wg.Wait()
close(resultsCh)
}()
// Combine namespace data
conf := config.Get()
for resultCh := range resultsCh {
if resultCh.err != nil {
// Return failure if we are in single cluster
if resultCh.cluster == conf.KubernetesConfig.ClusterName && len(in.userClients) == 1 {
log.Errorf("Error fetching Applications for local cluster %s: %s", resultCh.cluster, resultCh.err)
return models.AppList{}, resultCh.err
} else {
log.Infof("Error fetching Applications for cluster %s: %s", resultCh.cluster, resultCh.err)
}
}
allApps = append(allApps, resultCh.nsApps)
}
icCriteria := IstioConfigCriteria{
IncludeAuthorizationPolicies: true,
IncludeDestinationRules: true,
IncludeEnvoyFilters: true,
IncludeGateways: true,
IncludePeerAuthentications: true,
IncludeRequestAuthentications: true,
IncludeSidecars: true,
IncludeVirtualServices: true,
}
var istioConfigMap models.IstioConfigMap
// TODO: MC
if criteria.IncludeIstioResources {
istioConfigMap, err = in.businessLayer.IstioConfig.GetIstioConfigMap(ctx, criteria.Namespace, icCriteria)
if err != nil {
log.Errorf("Error fetching Istio Config per namespace %s: %s", criteria.Namespace, err)
return models.AppList{}, err
}
}
for _, clusterApps := range allApps {
for keyApp, valueApp := range clusterApps {
appItem := &models.AppListItem{
Name: keyApp,
IstioSidecar: true,
Health: models.EmptyAppHealth(),
}
istioConfigList := models.IstioConfigList{}
if _, ok := istioConfigMap[valueApp.cluster]; ok {
istioConfigList = istioConfigMap[valueApp.cluster]
}
applabels := make(map[string][]string)
svcReferences := make([]*models.IstioValidationKey, 0)
for _, srv := range valueApp.Services {
joinMap(applabels, srv.Labels)
if criteria.IncludeIstioResources {
vsFiltered := kubernetes.FilterVirtualServicesByService(istioConfigList.VirtualServices, srv.Namespace, srv.Name)
for _, v := range vsFiltered {
ref := models.BuildKey(v.Kind, v.Name, v.Namespace)
svcReferences = append(svcReferences, &ref)
}
drFiltered := kubernetes.FilterDestinationRulesByService(istioConfigList.DestinationRules, srv.Namespace, srv.Name)
for _, d := range drFiltered {
ref := models.BuildKey(d.Kind, d.Name, d.Namespace)
svcReferences = append(svcReferences, &ref)
}
gwFiltered := kubernetes.FilterGatewaysByVirtualServices(istioConfigList.Gateways, istioConfigList.VirtualServices)
for _, g := range gwFiltered {
ref := models.BuildKey(g.Kind, g.Name, g.Namespace)
svcReferences = append(svcReferences, &ref)
}
}
}
wkdReferences := make([]*models.IstioValidationKey, 0)
for _, wrk := range valueApp.Workloads {
joinMap(applabels, wrk.Labels)
if criteria.IncludeIstioResources {
wSelector := labels.Set(wrk.Labels).AsSelector().String()
wkdReferences = append(wkdReferences, FilterWorkloadReferences(wSelector, istioConfigList)...)
}
}
appItem.Labels = buildFinalLabels(applabels)
appItem.IstioReferences = FilterUniqueIstioReferences(append(svcReferences, wkdReferences...))
for _, w := range valueApp.Workloads {
if appItem.IstioSidecar = w.IstioSidecar; !appItem.IstioSidecar {
break
}
}
for _, w := range valueApp.Workloads {
if appItem.IstioAmbient = w.HasIstioAmbient(); !appItem.IstioAmbient {
break
}
}
if criteria.IncludeHealth {
appItem.Health, err = in.businessLayer.Health.GetAppHealth(ctx, criteria.Namespace, valueApp.cluster, appItem.Name, criteria.RateInterval, criteria.QueryTime, valueApp)
if err != nil {
log.Errorf("Error fetching Health in namespace %s for app %s: %s", criteria.Namespace, appItem.Name, err)
}
}
appItem.Cluster = valueApp.cluster
appItem.Namespace = criteria.Namespace
appList.Apps = append(appList.Apps, *appItem)
}
}
return *appList, nil
}
// GetApp is the API handler to fetch the details for a given namespace and app name
func (in *AppService) GetAppDetails(ctx context.Context, criteria AppCriteria) (models.App, error) {
var end observability.EndFunc
ctx, end = observability.StartSpan(ctx, "GetApp",
observability.Attribute("package", "business"),
observability.Attribute("namespace", criteria.Namespace),
observability.Attribute("cluster", criteria.Cluster),
observability.Attribute("appName", criteria.AppName),
observability.Attribute("rateInterval", criteria.RateInterval),
observability.Attribute("queryTime", criteria.QueryTime),
)
defer end()
appInstance := &models.App{Namespace: models.Namespace{Name: criteria.Namespace}, Name: criteria.AppName, Health: models.EmptyAppHealth(), Cluster: criteria.Cluster}
ns, err := in.businessLayer.Namespace.GetClusterNamespace(ctx, criteria.Namespace, criteria.Cluster)
if err != nil {
return *appInstance, err
}
appInstance.Namespace = *ns
namespaceApps, err := in.fetchNamespaceApps(ctx, criteria.Namespace, criteria.Cluster, criteria.AppName)
if err != nil {
return *appInstance, err
}
var appDetails *appDetails
var ok bool
// Send a NewNotFound if the app is not found in the deployment list, instead to send an empty result
if appDetails, ok = namespaceApps[criteria.AppName]; !ok {
return *appInstance, kubernetes.NewNotFound(criteria.AppName, "Kiali", "App")
}
(*appInstance).Workloads = make([]models.WorkloadItem, len(appDetails.Workloads))
for i, wkd := range appDetails.Workloads {
(*appInstance).Workloads[i] = models.WorkloadItem{WorkloadName: wkd.Name, IstioSidecar: wkd.IstioSidecar, Labels: wkd.Labels, IstioAmbient: wkd.IstioAmbient, ServiceAccountNames: wkd.Pods.ServiceAccounts()}
}
(*appInstance).ServiceNames = make([]string, len(appDetails.Services))
for i, svc := range appDetails.Services {
(*appInstance).ServiceNames[i] = svc.Name
}
pods := models.Pods{}
for _, workload := range appDetails.Workloads {
pods = append(pods, workload.Pods...)
}
(*appInstance).Runtimes = NewDashboardsService(ns, nil).GetCustomDashboardRefs(criteria.Namespace, criteria.AppName, "", pods)
if criteria.IncludeHealth {
(*appInstance).Health, err = in.businessLayer.Health.GetAppHealth(ctx, criteria.Namespace, criteria.Cluster, criteria.AppName, criteria.RateInterval, criteria.QueryTime, appDetails)
if err != nil {
log.Errorf("Error fetching Health in namespace %s for app %s: %s", criteria.Namespace, criteria.AppName, err)
}
}
(*appInstance).Cluster = appDetails.cluster
return *appInstance, nil
}
// AppDetails holds Services and Workloads having the same "app" label
type appDetails struct {
app string
cluster string
Services []models.ServiceOverview
Workloads models.Workloads
}
// NamespaceApps is a map of app_name and cluster x AppDetails
type namespaceApps = map[string]*appDetails
func castAppDetails(allEntities namespaceApps, ss *models.ServiceList, w *models.Workload, cluster string) {
appLabel := config.Get().IstioLabels.AppLabelName
if app, ok := w.Labels[appLabel]; ok {
if appEntities, ok := allEntities[app]; ok {
appEntities.Workloads = append(appEntities.Workloads, w)
} else {
allEntities[app] = &appDetails{
app: app,
cluster: cluster,
Workloads: models.Workloads{w},
}
}
if ss != nil {
for _, service := range ss.Services {
if appEntities, ok := allEntities[app]; ok {
found := false
for _, s := range appEntities.Services {
if s.Name == service.Name && s.Namespace == service.Namespace {
found = true
}
}
if !found {
appEntities.Services = append(appEntities.Services, service)
}
}
}
}
}
}
// Helper method to fetch all applications for a given namespace.
// Optionally if appName parameter is provided, it filters apps for that name.
// Return an error on any problem.
func (in *AppService) fetchNamespaceApps(ctx context.Context, namespace string, cluster string, appName string) (namespaceApps, error) {
var ss *models.ServiceList
var ws models.Workloads
cfg := config.Get()
appNameSelector := ""
if appName != "" {
selector := labels.Set(map[string]string{cfg.IstioLabels.AppLabelName: appName})
appNameSelector = selector.String()
}
// Check if user has access to the namespace (RBAC) in cache scenarios and/or
// if namespace is accessible from Kiali (Deployment.AccessibleNamespaces)
if _, err := in.businessLayer.Namespace.GetClusterNamespace(ctx, namespace, cluster); err != nil {
return nil, err
}
var err error
ws, err = in.businessLayer.Workload.fetchWorkloadsFromCluster(ctx, cluster, namespace, appNameSelector)
if err != nil {
return nil, err
}
allEntities := make(namespaceApps)
for _, w := range ws {
// Check if namespace is cached
serviceCriteria := ServiceCriteria{
Cluster: cluster,
Namespace: namespace,
IncludeHealth: false,
IncludeIstioResources: false,
IncludeOnlyDefinitions: true,
ServiceSelector: labels.Set(w.Labels).String(),
}
ss, err = in.businessLayer.Svc.GetServiceList(ctx, serviceCriteria)
if err != nil {
return nil, err
}
castAppDetails(allEntities, ss, w, cluster)
}
return allEntities, nil
}