forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.go
125 lines (109 loc) · 3.9 KB
/
layer.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
package business
import (
"sync"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/jaeger"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/kubernetes/cache"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/prometheus"
)
// Layer is a container for fast access to inner services
type Layer struct {
Svc SvcService
Health HealthService
Validations IstioValidationsService
IstioConfig IstioConfigService
Workload WorkloadService
App AppService
Namespace NamespaceService
Jaeger JaegerService
k8s kubernetes.IstioClientInterface
OpenshiftOAuth OpenshiftOAuthService
TLS TLSService
ThreeScale ThreeScaleService
Iter8 Iter8Service
}
// Global clientfactory and prometheus clients.
var clientFactory kubernetes.ClientFactory
var prometheusClient prometheus.ClientInterface
var once sync.Once
var kialiCache cache.KialiCache
func initKialiCache() {
if config.Get().KubernetesConfig.CacheEnabled {
if cache, err := cache.NewKialiCache(); err != nil {
log.Errorf("Error initializing Kiali Cache. Details: %s", err)
} else {
kialiCache = cache
}
}
if excludedWorkloads == nil {
excludedWorkloads = make(map[string]bool)
for _, w := range config.Get().KubernetesConfig.ExcludeWorkloads {
excludedWorkloads[w] = true
}
}
}
func GetUnauthenticated() (*Layer, error) {
return Get("")
}
// Get the business.Layer
func Get(token string) (*Layer, error) {
// Kiali Cache will be initialized once at first use of Business layer
once.Do(initKialiCache)
// Use an existing client factory if it exists, otherwise create and use in the future
if clientFactory == nil {
userClient, err := kubernetes.GetClientFactory()
if err != nil {
return nil, err
}
clientFactory = userClient
}
// Creates a new k8s client based on the current users token
k8s, err := clientFactory.GetClient(token)
if err != nil {
return nil, err
}
// Use an existing Prometheus client if it exists, otherwise create and use in the future
if prometheusClient == nil {
prom, err := prometheus.NewClient()
if err != nil {
return nil, err
}
prometheusClient = prom
}
// Create Jaeger client
jaegerLoader := func() (jaeger.ClientInterface, error) {
return jaeger.NewClient(token)
}
return NewWithBackends(k8s, prometheusClient, jaegerLoader), nil
}
// SetWithBackends allows for specifying the ClientFactory and Prometheus clients to be used.
// Mock friendly. Used only with tests.
func SetWithBackends(cf kubernetes.ClientFactory, prom prometheus.ClientInterface) {
clientFactory = cf
prometheusClient = prom
}
// NewWithBackends creates the business layer using the passed k8s and prom clients
func NewWithBackends(k8s kubernetes.IstioClientInterface, prom prometheus.ClientInterface, jaegerClient JaegerLoader) *Layer {
temporaryLayer := &Layer{}
temporaryLayer.Health = HealthService{prom: prom, k8s: k8s, businessLayer: temporaryLayer}
temporaryLayer.Svc = SvcService{prom: prom, k8s: k8s, businessLayer: temporaryLayer}
temporaryLayer.IstioConfig = IstioConfigService{k8s: k8s, businessLayer: temporaryLayer}
temporaryLayer.Workload = WorkloadService{k8s: k8s, prom: prom, businessLayer: temporaryLayer}
temporaryLayer.Validations = IstioValidationsService{k8s: k8s, businessLayer: temporaryLayer}
temporaryLayer.App = AppService{prom: prom, k8s: k8s, businessLayer: temporaryLayer}
temporaryLayer.Namespace = NewNamespaceService(k8s)
temporaryLayer.Jaeger = JaegerService{loader: jaegerClient, businessLayer: temporaryLayer}
temporaryLayer.k8s = k8s
temporaryLayer.OpenshiftOAuth = OpenshiftOAuthService{k8s: k8s}
temporaryLayer.TLS = TLSService{k8s: k8s, businessLayer: temporaryLayer}
temporaryLayer.ThreeScale = ThreeScaleService{k8s: k8s}
temporaryLayer.Iter8 = Iter8Service{k8s: k8s, businessLayer: temporaryLayer}
return temporaryLayer
}
func Stop() {
if kialiCache != nil {
kialiCache.Stop()
}
}