-
Notifications
You must be signed in to change notification settings - Fork 477
/
versions.go
388 lines (340 loc) · 14.3 KB
/
versions.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
package status
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
"time"
"github.com/hashicorp/go-version"
kversion "k8s.io/apimachinery/pkg/version"
kube "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/util/httputil"
)
type externalService func() (*ExternalServiceInfo, error)
var (
// Example Maistra product version is:
// redhat@redhat-docker.io/maistra-0.1.0-1-3a136c90ec5e308f236e0d7ebb5c4c5e405217f4-unknown
// Example Maistra upstream project version is:
// redhat@redhat-pulp.abc.xyz.redhat.com:8888/openshift-istio-tech-preview-0.1.0-1-3a136c90ec5e308f236e0d7ebb5c4c5e405217f4-Custom
// Maistra_1.1.0-291c5419cf19d2b015e7e5dee970c458fb8f1982-Clean
// Example OpenShift Service Mesh 1.1 product version is:
// OSSM_1.1.0-291c5419cf19d2b015e7e5dee970c458fb8f1982-Clean
// Example Istio snapshot version is:
// root@f72e3d3ef3c2-docker.io/istio-release-1.0-20180927-21-10-cbe9c05c470ec1924f7bcf02334b183e7e6175cb-Clean
// Example Istio dev version is:
// 1.5-alpha.dbd2aca8887fb42c2bb358417621a78de372f906-dbd2aca8887fb42c2bb358417621a78de372f906-Clean
maistraProductVersionExpr = regexp.MustCompile(`maistra-([0-9]+\.[0-9]+\.[0-9]+)`)
ossmVersionExpr = regexp.MustCompile(`(?:OSSM_|openshift-service-mesh-)([0-9]+\.[0-9]+\.[0-9]+)`)
maistraProjectVersionExpr = regexp.MustCompile(`(?:Maistra_|openshift-istio.*-)([0-9]+\.[0-9]+\.[0-9]+)`)
istioDevVersionExpr = regexp.MustCompile(`(\d+\.\d+)-alpha\.([[:alnum:]]+)-.*`)
istioRCVersionExpr = regexp.MustCompile(`(\d+\.\d+.\d+)-((?:alpha|beta|rc|RC)\.\d+)`)
istioSnapshotVersionExpr = regexp.MustCompile(`istio-release-([0-9]+\.[0-9]+)(-[0-9]{8})`)
istioVersionExpr = regexp.MustCompile(`([0-9]+\.[0-9]+\.[0-9]+)`)
)
func getVersions() {
components := []externalService{
istioVersion,
prometheusVersion,
kubernetesVersion,
}
if config.Get().ExternalServices.Grafana.Enabled {
components = append(components, grafanaVersion)
} else {
log.Debugf("Grafana is disabled in Kiali by configuration")
}
if config.Get().ExternalServices.Tracing.Enabled {
components = append(components, jaegerVersion)
} else {
log.Debugf("Jaeger is disabled in Kiali by configuration")
}
for _, comp := range components {
getVersionComponent(comp)
}
}
func getVersionComponent(serviceComponent externalService) {
componentInfo, err := serviceComponent()
if err == nil {
info.ExternalServices = append(info.ExternalServices, *componentInfo)
}
}
// validateVersion returns true if requiredVersion "<op> version" (e.g. ">= 0.7.1") is satisfied by installedversion
func validateVersion(requiredVersion string, installedVersion string) bool {
reqWords := strings.Split(requiredVersion, " ")
requirementV, errReqV := version.NewVersion(reqWords[1])
installedV, errInsV := version.NewVersion(installedVersion)
if errReqV != nil || errInsV != nil {
return false
}
switch operator := reqWords[0]; operator {
case "==":
return installedV.Equal(requirementV)
case ">=":
return installedV.GreaterThan(requirementV) || installedV.Equal(requirementV)
case ">":
return installedV.GreaterThan(requirementV)
case "<=":
return installedV.LessThan(requirementV) || installedV.Equal(requirementV)
case "<":
return installedV.LessThan(requirementV)
}
return false
}
// istioVersion returns the current istio version information
func istioVersion() (*ExternalServiceInfo, error) {
var (
body []byte
err error
product *ExternalServiceInfo
resp *http.Response
)
istioConfig := config.Get().ExternalServices.Istio
resp, err = http.Get(istioConfig.UrlServiceVersion)
if err == nil {
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err == nil {
rawVersion := string(body)
product, err = parseIstioRawVersion(rawVersion)
return product, err
}
}
return nil, err
}
func parseIstioRawVersion(rawVersion string) (*ExternalServiceInfo, error) {
product := ExternalServiceInfo{Name: "Unknown", Version: "Unknown"}
// First see if we detect Maistra (either product or upstream project).
// If it is not Maistra, see if it is upstream Istio (either a release or snapshot).
// If it is neither then it is some unknown Istio implementation that we do not support.
maistraVersionStringArr := maistraProductVersionExpr.FindStringSubmatch(rawVersion)
if maistraVersionStringArr != nil {
log.Debugf("Detected Maistra product version [%v]", rawVersion)
if len(maistraVersionStringArr) > 1 {
product.Name = "Maistra"
product.Version = maistraVersionStringArr[1] // get regex group #1 ,which is the "#.#.#" version string
if !validateVersion(config.MaistraVersionSupported, product.Version) {
info.WarningMessages = append(info.WarningMessages, "Maistra version "+product.Version+" is not supported, the version should be "+config.MaistraVersionSupported)
}
// we know this is Maistra - either a supported or unsupported version - return now
return &product, nil
}
}
maistraVersionStringArr = maistraProjectVersionExpr.FindStringSubmatch(rawVersion)
if maistraVersionStringArr != nil {
log.Debugf("Detected Maistra project version [%v]", rawVersion)
if len(maistraVersionStringArr) > 1 {
product.Name = "Maistra Project"
product.Version = maistraVersionStringArr[1] // get regex group #1 ,which is the "#.#.#" version string
if !validateVersion(config.MaistraVersionSupported, product.Version) {
info.WarningMessages = append(info.WarningMessages, "Maistra project version "+product.Version+" is not supported, the version should be "+config.MaistraVersionSupported)
}
// we know this is Maistra - either a supported or unsupported version - return now
return &product, nil
}
}
// OpenShift Service Mesh
ossmStringArr := ossmVersionExpr.FindStringSubmatch(rawVersion)
if ossmStringArr != nil {
log.Debugf("Detected OpenShift Service Mesh version [%v]", rawVersion)
if len(ossmStringArr) > 1 {
product.Name = "OpenShift Service Mesh"
product.Version = ossmStringArr[1] // get regex group #1 ,which is the "#.#.#" version string
if !validateVersion(config.OSSMVersionSupported, product.Version) {
info.WarningMessages = append(info.WarningMessages, "OpenShift Service Mesh version "+product.Version+" is not supported, the version should be "+config.OSSMVersionSupported)
}
// we know this is OpenShift Service Mesh - either a supported or unsupported version - return now
return &product, nil
}
}
// see if it is a snapshot version of Istio
istioVersionStringArr := istioSnapshotVersionExpr.FindStringSubmatch(rawVersion)
if istioVersionStringArr != nil {
log.Debugf("Detected Istio snapshot version [%v]", rawVersion)
if len(istioVersionStringArr) > 2 {
product.Name = "Istio Snapshot"
majorMinor := istioVersionStringArr[1] // regex group #1 is the "#.#" version numbers
snapshotStr := istioVersionStringArr[2] // regex group #2 is the date/time stamp
product.Version = majorMinor + snapshotStr
if !validateVersion(config.IstioVersionSupported, majorMinor) {
info.WarningMessages = append(info.WarningMessages, "Istio snapshot version "+product.Version+" is not supported, the version should be "+config.IstioVersionSupported)
}
// we know this is Istio upstream - either a supported or unsupported version - return now
return &product, nil
}
}
// see if it is a dev version of Istio
istioVersionStringArr = istioDevVersionExpr.FindStringSubmatch(rawVersion)
if istioVersionStringArr != nil {
log.Debugf("Detected Istio dev version [%v]", rawVersion)
if len(istioVersionStringArr) > 2 {
product.Name = "Istio Dev"
majorMinor := istioVersionStringArr[1] // regex group #1 is the "#.#" version numbers
buildHash := istioVersionStringArr[2] // regex group #2 is the build hash
product.Version = fmt.Sprintf("%s (dev %s)", majorMinor, buildHash)
if !validateVersion(config.IstioVersionSupported, majorMinor) {
info.WarningMessages = append(info.WarningMessages, "Istio dev version "+product.Version+" is not supported, the version should be "+config.IstioVersionSupported)
}
// we know this is Istio upstream - either a supported or unsupported version - return now
return &product, nil
}
}
// see if it is an RC version of Istio
istioVersionStringArr = istioRCVersionExpr.FindStringSubmatch(rawVersion)
if istioVersionStringArr != nil {
log.Debugf("Detected Istio RC version [%v]", rawVersion)
if len(istioVersionStringArr) > 2 {
product.Name = "Istio RC"
majorMinor := istioVersionStringArr[1] // regex group #1 is the "#.#.#" version numbers
rc := istioVersionStringArr[2] // regex group #2 is the alpha or beta version
product.Version = fmt.Sprintf("%s (%s)", majorMinor, rc)
if !validateVersion(config.IstioVersionSupported, majorMinor) {
info.WarningMessages = append(info.WarningMessages, "Istio release candidate version "+product.Version+" is not supported, the version should be "+config.IstioVersionSupported)
}
// we know this is Istio upstream - either a supported or unsupported version - return now
return &product, nil
}
}
// see if it is a released version of Istio
istioVersionStringArr = istioVersionExpr.FindStringSubmatch(rawVersion)
if istioVersionStringArr != nil {
log.Debugf("Detected Istio version [%v]", rawVersion)
if len(istioVersionStringArr) > 1 {
product.Name = "Istio"
product.Version = istioVersionStringArr[1] // get regex group #1 ,which is the "#.#.#" version string
if !validateVersion(config.IstioVersionSupported, product.Version) {
info.WarningMessages = append(info.WarningMessages, "Istio version "+product.Version+" is not supported, the version should be "+config.IstioVersionSupported)
}
// we know this is Istio upstream - either a supported or unsupported version - return now
return &product, nil
}
}
log.Debugf("Detected unknown Istio implementation version [%v]", rawVersion)
product.Name = "Unknown Istio Implementation"
product.Version = rawVersion
info.WarningMessages = append(info.WarningMessages, "Unknown Istio implementation version "+product.Version+" is not recognized, thus not supported.")
return &product, nil
}
type p8sResponseVersion struct {
Version string `json:"version"`
Revision string `json:"revision"`
}
func jaegerVersion() (*ExternalServiceInfo, error) {
jaegerConfig := config.Get().ExternalServices.Tracing
if !jaegerConfig.Enabled {
return nil, nil
}
product := ExternalServiceInfo{}
product.Name = "Jaeger"
product.Url = jaegerConfig.URL
return &product, nil
}
func grafanaVersion() (*ExternalServiceInfo, error) {
product := ExternalServiceInfo{}
product.Name = "Grafana"
product.Url = DiscoverGrafana()
return &product, nil
}
func prometheusVersion() (*ExternalServiceInfo, error) {
product := ExternalServiceInfo{}
prometheusV := new(p8sResponseVersion)
cfg := config.Get().ExternalServices.Prometheus
// Be sure to copy config.Auth and not modify the existing
auth := cfg.Auth
if auth.UseKialiToken {
token, err := kubernetes.GetKialiToken()
if err != nil {
log.Errorf("Could not read the Kiali Service Account token: %v", err)
return nil, err
}
auth.Token = token
}
body, _, err := httputil.HttpGet(cfg.URL+"/version", &auth, 10*time.Second)
if err == nil {
err = json.Unmarshal(body, &prometheusV)
if err == nil {
product.Name = "Prometheus"
product.Version = prometheusV.Version
return &product, nil
}
}
return nil, err
}
func kubernetesVersion() (*ExternalServiceInfo, error) {
var (
err error
k8sConfig *rest.Config
k8s *kube.Clientset
serverVersion *kversion.Info
)
product := ExternalServiceInfo{}
k8sConfig, err = kubernetes.ConfigClient()
if err == nil {
k8sConfig.QPS = config.Get().KubernetesConfig.QPS
k8sConfig.Burst = config.Get().KubernetesConfig.Burst
k8s, err = kube.NewForConfig(k8sConfig)
if err == nil {
serverVersion, err = k8s.Discovery().ServerVersion()
if err == nil {
product.Name = "Kubernetes"
product.Version = serverVersion.GitVersion
return &product, nil
}
}
}
return nil, err
}
// set this one time, it is very unlikely that mixer will be enabled/disabled without a kiali pod restart, or if it
// did that that change will matter, and the kiali pod could be bounced as a workaround.
var isMixerDisabled *bool
// IsMixerDisabled returns true if Telemetry V2 is enabled (mixer is not collecting metrics)
// TODO: This test can be removed when Kiali stops supporting Istio versions with Mixer Telemetry
func IsMixerDisabled() bool {
if isMixerDisabled != nil {
return *isMixerDisabled
}
clientFactory, error := kubernetes.GetClientFactory()
if error != nil {
log.Warningf("IsMixerDisabled: Cannot connect to Kubernetes API")
return true
}
token, error := kubernetes.GetKialiToken()
if error != nil {
log.Warningf("IsMixerDisabled: Cannot get Kiali token")
return true
}
client, error := clientFactory.GetClient(token)
if error != nil {
log.Warningf("IsMixerDisabled: Cannot get Kubernetes Client")
return true
}
mixedDisabled := client.IsMixerDisabled()
isMixerDisabled = &mixedDisabled
return *isMixerDisabled
}
// set this one time, it is very unlikely that the istio version will change without a kiali pod restart, or if it
// did that that version change will matter, and the kiali pod could be bounced as a workaround.
var istioSupportsCanonical *bool
// AreCanonicalMetricsAvailable returns true if canonical labels are present in Istio Telemetry.
// TODO: This test can be removed when Kiali stops supporting Istio versions with Mixer Telemetry
func AreCanonicalMetricsAvailable() bool { // AreCanonicalMetricsAvailable() bool {
if istioSupportsCanonical == nil {
// First, check Istio version because canonical labels were first introduced in Istio v1.5. Prior
// Istio versions won't have canonical labels in metrics regardless of active Telemetry version.
istioVersion, err := istioVersion()
if err != nil {
return false
}
valid := validateVersion(">= 1.5", istioVersion.Version)
// Result is cached; we now know whether Istio supports canonical labels in Telemetry.
log.Infof("IstioSupportsCanonical: %t", valid)
istioSupportsCanonical = &valid
}
// Canonical metrics are present only if Istio version is 1.5 or later (aka canonical is supported)
// AND if mixer is disabled.
return *istioSupportsCanonical && IsMixerDisabled()
}