-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterHandler.go
478 lines (390 loc) · 16.7 KB
/
clusterHandler.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
package monitoring
import (
"encoding/json"
"fmt"
"reflect"
"sort"
"strings"
"github.com/pkg/errors"
kcluster "github.com/rancher/kontainer-engine/cluster"
"github.com/rancher/rancher/pkg/controllers/user/nslabels"
"github.com/rancher/rancher/pkg/monitoring"
"github.com/rancher/rancher/pkg/node"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/rancher/pkg/settings"
"github.com/rancher/rke/pki"
corev1 "github.com/rancher/types/apis/core/v1"
mgmtv3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/apis/project.cattle.io/v3"
k8scorev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
const (
exporterEtcdCertName = "exporter-etcd-cert"
etcd = "etcd"
controlplane = "controlplane"
creatorIDAnno = "field.cattle.io/creatorId"
)
type etcdTLSConfig struct {
certPath string
keyPath string
internalAddress string
}
type clusterHandler struct {
clusterName string
cattleClustersClient mgmtv3.ClusterInterface
agentEndpointsLister corev1.EndpointsLister
app *appHandler
}
func (ch *clusterHandler) sync(key string, cluster *mgmtv3.Cluster) (runtime.Object, error) {
if cluster == nil || cluster.DeletionTimestamp != nil || cluster.Name != ch.clusterName {
return cluster, nil
}
if !mgmtv3.ClusterConditionAgentDeployed.IsTrue(cluster) {
return cluster, nil
}
clusterTag := getClusterTag(cluster)
src := cluster
cpy := src.DeepCopy()
err := ch.doSync(cpy)
if !reflect.DeepEqual(cpy, src) {
updated, updateErr := ch.cattleClustersClient.Update(cpy)
if updateErr != nil {
return updated, errors.Wrapf(updateErr, "failed to update Cluster %s", clusterTag)
}
cpy = updated
}
return cpy, err
}
func (ch *clusterHandler) doSync(cluster *mgmtv3.Cluster) error {
appName, appTargetNamespace := monitoring.ClusterMonitoringInfo()
if cluster.Spec.EnableClusterMonitoring {
appProjectName, err := ch.ensureAppProjectName(cluster.Name, appTargetNamespace)
if err != nil {
mgmtv3.ClusterConditionMonitoringEnabled.Unknown(cluster)
mgmtv3.ClusterConditionMonitoringEnabled.Message(cluster, err.Error())
return errors.Wrap(err, "failed to ensure monitoring project name")
}
var etcdTLSConfigs []*etcdTLSConfig
var systemComponentMap map[string][]string
if isRkeCluster(cluster) {
if etcdTLSConfigs, err = ch.deployEtcdCert(cluster.Name, appTargetNamespace); err != nil {
mgmtv3.ClusterConditionMonitoringEnabled.Unknown(cluster)
mgmtv3.ClusterConditionMonitoringEnabled.Message(cluster, err.Error())
return errors.Wrap(err, "failed to deploy etcd cert")
}
if systemComponentMap, err = ch.getExporterEndpoint(); err != nil {
mgmtv3.ClusterConditionMonitoringEnabled.Unknown(cluster)
mgmtv3.ClusterConditionMonitoringEnabled.Message(cluster, err.Error())
return errors.Wrap(err, "failed to get exporter endpoint")
}
}
_, err = ch.deployApp(appName, appTargetNamespace, appProjectName, cluster, etcdTLSConfigs, systemComponentMap)
if err != nil {
mgmtv3.ClusterConditionMonitoringEnabled.Unknown(cluster)
mgmtv3.ClusterConditionMonitoringEnabled.Message(cluster, err.Error())
return errors.Wrap(err, "failed to deploy monitoring")
}
if cluster.Status.MonitoringStatus == nil {
cluster.Status.MonitoringStatus = &mgmtv3.MonitoringStatus{}
}
isReady, err := ch.isPrometheusReady(cluster)
if err != nil {
mgmtv3.ClusterConditionMonitoringEnabled.Unknown(cluster)
mgmtv3.ClusterConditionMonitoringEnabled.Message(cluster, err.Error())
return err
}
if !isReady {
mgmtv3.ClusterConditionMonitoringEnabled.Unknown(cluster)
mgmtv3.ClusterConditionMonitoringEnabled.Message(cluster, "prometheus is not ready")
return nil
}
cluster.Status.MonitoringStatus.GrafanaEndpoint = fmt.Sprintf("/k8s/clusters/%s/api/v1/namespaces/%s/services/http:access-grafana:80/proxy/", cluster.Name, appTargetNamespace)
_, err = ConditionMetricExpressionDeployed.DoUntilTrue(cluster.Status.MonitoringStatus, func() (status *mgmtv3.MonitoringStatus, e error) {
return status, ch.deployMetrics(cluster)
})
if err != nil {
mgmtv3.ClusterConditionMonitoringEnabled.Unknown(cluster)
mgmtv3.ClusterConditionMonitoringEnabled.Message(cluster, err.Error())
return errors.Wrap(err, "failed to deploy monitoring metrics")
}
mgmtv3.ClusterConditionMonitoringEnabled.True(cluster)
mgmtv3.ClusterConditionMonitoringEnabled.Message(cluster, "")
} else if enabledStatus := mgmtv3.ClusterConditionMonitoringEnabled.GetStatus(cluster); enabledStatus != "" && enabledStatus != "False" {
if err := ch.app.withdrawApp(cluster.Name, appName, appTargetNamespace); err != nil {
mgmtv3.ClusterConditionMonitoringEnabled.Unknown(cluster)
mgmtv3.ClusterConditionMonitoringEnabled.Message(cluster, err.Error())
return errors.Wrap(err, "failed to withdraw monitoring")
}
if err := ch.withdrawMetrics(cluster); err != nil {
mgmtv3.ClusterConditionMonitoringEnabled.Unknown(cluster)
mgmtv3.ClusterConditionMonitoringEnabled.Message(cluster, err.Error())
return errors.Wrap(err, "failed to withdraw monitoring metrics")
}
mgmtv3.ClusterConditionMonitoringEnabled.False(cluster)
mgmtv3.ClusterConditionMonitoringEnabled.Message(cluster, "")
cluster.Status.MonitoringStatus = nil
}
return nil
}
func (ch *clusterHandler) ensureAppProjectName(clusterID, appTargetNamespace string) (string, error) {
appDeployProjectID, err := monitoring.GetSystemProjectID(ch.app.cattleProjectClient)
if err != nil {
return "", err
}
appProjectName, err := monitoring.EnsureAppProjectName(ch.app.agentNamespaceClient, appDeployProjectID, clusterID, appTargetNamespace)
if err != nil {
return "", err
}
return appProjectName, nil
}
func (ch *clusterHandler) deployEtcdCert(clusterName, appTargetNamespace string) ([]*etcdTLSConfig, error) {
var etcdTLSConfigs []*etcdTLSConfig
rkeCertSecretName := "c-" + clusterName
systemNamespace := "cattle-system"
sec, err := ch.app.cattleSecretClient.GetNamespaced(systemNamespace, rkeCertSecretName, metav1.GetOptions{})
if err != nil {
return nil, errors.Wrapf(err, "failed to get %s:%s in deploy etcd cert to prometheus", systemNamespace, rkeCertSecretName)
}
var data kcluster.Cluster
if err = json.Unmarshal(sec.Data["cluster"], &data); err != nil {
return nil, errors.Wrapf(err, "failed to decode secret %s:%s to get etcd cert", systemNamespace, rkeCertSecretName)
}
crts := make(map[string]map[string]string)
if err = json.Unmarshal([]byte(data.Metadata["Certs"]), &crts); err != nil {
return nil, errors.Wrapf(err, "failed to decode secret %s:%s cert data to get etcd cert", systemNamespace, rkeCertSecretName)
}
secretData := make(map[string][]byte)
for k, v := range crts {
if strings.HasPrefix(k, pki.EtcdCertName) {
certName := getCertName(k)
keyName := getKeyName(k)
tlsConfig := etcdTLSConfig{
internalAddress: k,
certPath: getSecretPath(exporterEtcdCertName, certName),
keyPath: getSecretPath(exporterEtcdCertName, keyName),
}
etcdTLSConfigs = append(etcdTLSConfigs, &tlsConfig)
secretData[certName] = []byte(v["CertPEM"])
secretData[keyName] = []byte(v["KeyPEM"])
}
}
agentSecretClient := ch.app.agentSecretClient
oldSec, err := agentSecretClient.GetNamespaced(appTargetNamespace, exporterEtcdCertName, metav1.GetOptions{})
if err != nil && k8serrors.IsNotFound(err) {
secret := &k8scorev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: exporterEtcdCertName,
Namespace: appTargetNamespace,
},
Data: secretData,
}
if _, err = agentSecretClient.Create(secret); err != nil && !k8serrors.IsAlreadyExists(err) {
return nil, err
}
return etcdTLSConfigs, nil
}
newSec := oldSec.DeepCopy()
newSec.Data = secretData
_, err = agentSecretClient.Update(newSec)
return etcdTLSConfigs, err
}
func (ch *clusterHandler) getExporterEndpoint() (map[string][]string, error) {
endpointMap := make(map[string][]string)
etcdLablels := labels.Set{
"node-role.kubernetes.io/etcd": "true",
}
controlplaneLabels := labels.Set{
"node-role.kubernetes.io/controlplane": "true",
}
agentNodeLister := ch.app.agentNodeClient.Controller().Lister()
etcdNodes, err := agentNodeLister.List(metav1.NamespaceAll, etcdLablels.AsSelector())
if err != nil {
return nil, errors.Wrap(err, "failed to get etcd nodes")
}
for _, v := range etcdNodes {
endpointMap[etcd] = append(endpointMap[etcd], node.GetNodeInternalAddress(v))
}
controlplaneNodes, err := agentNodeLister.List(metav1.NamespaceAll, controlplaneLabels.AsSelector())
if err != nil {
return nil, errors.Wrap(err, "failed to get controlplane nodes")
}
for _, v := range controlplaneNodes {
endpointMap[controlplane] = append(endpointMap[controlplane], node.GetNodeInternalAddress(v))
}
return endpointMap, nil
}
func (ch *clusterHandler) deployApp(appName, appTargetNamespace string, appProjectName string, cluster *mgmtv3.Cluster, etcdTLSConfig []*etcdTLSConfig, systemComponentMap map[string][]string) (map[string]string, error) {
_, appDeployProjectID := ref.Parse(appProjectName)
clusterAlertManagerSvcName, clusterAlertManagerSvcNamespaces, clusterAlertManagerPort := monitoring.ClusterAlertManagerEndpoint()
optionalAppAnswers := map[string]string{
"exporter-kube-state.enabled": "true",
"exporter-kubelets.enabled": "true",
"exporter-kubernetes.enabled": "true",
"exporter-node.enabled": "true",
"exporter-fluentd.enabled": "true",
"grafana.persistence.enabled": "false",
"prometheus.persistence.enabled": "false",
}
mustAppAnswers := map[string]string{
"enabled": "false",
"exporter-coredns.enabled": "false",
"exporter-coredns.apiGroup": monitoring.APIVersion.Group,
"exporter-kube-controller-manager.enabled": "false",
"exporter-kube-controller-manager.apiGroup": monitoring.APIVersion.Group,
"exporter-kube-dns.enabled": "false",
"exporter-kube-dns.apiGroup": monitoring.APIVersion.Group,
"exporter-kube-etcd.enabled": "false",
"exporter-kube-etcd.apiGroup": monitoring.APIVersion.Group,
"exporter-kube-scheduler.enabled": "false",
"exporter-kube-scheduler.apiGroup": monitoring.APIVersion.Group,
"exporter-kube-state.apiGroup": monitoring.APIVersion.Group,
"exporter-kubelets.apiGroup": monitoring.APIVersion.Group,
"exporter-kubernetes.apiGroup": monitoring.APIVersion.Group,
"exporter-node.apiGroup": monitoring.APIVersion.Group,
"exporter-fluentd.apiGroup": monitoring.APIVersion.Group,
"grafana.enabled": "true",
"grafana.apiGroup": monitoring.APIVersion.Group,
"grafana.serviceAccountName": appName,
"prometheus.enabled": "true",
"prometheus.apiGroup": monitoring.APIVersion.Group,
"prometheus.externalLabels.prometheus_from": cluster.Spec.DisplayName,
"prometheus.serviceAccountNameOverride": appName,
"prometheus.additionalAlertManagerConfigs[0].static_configs[0].targets[0]": fmt.Sprintf("%s.%s:%s", clusterAlertManagerSvcName, clusterAlertManagerSvcNamespaces, clusterAlertManagerPort),
"prometheus.additionalAlertManagerConfigs[0].static_configs[0].labels.level": "cluster",
"prometheus.additionalAlertManagerConfigs[0].static_configs[0].labels.cluster_id": cluster.Name,
"prometheus.additionalAlertManagerConfigs[0].static_configs[0].labels.cluster_name": cluster.Spec.DisplayName,
"prometheus.serviceMonitorNamespaceSelector.matchExpressions[0].key": nslabels.ProjectIDFieldLabel,
"prometheus.serviceMonitorNamespaceSelector.matchExpressions[0].operator": "In",
"prometheus.serviceMonitorNamespaceSelector.matchExpressions[0].values[0]": appDeployProjectID,
"prometheus.ruleNamespaceSelector.matchExpressions[0].key": nslabels.ProjectIDFieldLabel,
"prometheus.ruleNamespaceSelector.matchExpressions[0].operator": "In",
"prometheus.ruleNamespaceSelector.matchExpressions[0].values[0]": appDeployProjectID,
"prometheus.ruleSelector.matchExpressions[0].key": monitoring.CattlePrometheusRuleLabelKey,
"prometheus.ruleSelector.matchExpressions[0].operator": "In",
"prometheus.ruleSelector.matchExpressions[0].values[0]": monitoring.CattleAlertingPrometheusRuleLabelValue,
"prometheus.ruleSelector.matchExpressions[0].values[1]": monitoring.CattleMonitoringPrometheusRuleLabelValue,
}
appAnswers := monitoring.OverwriteAppAnswers(optionalAppAnswers, cluster.Annotations)
// cannot overwrite mustAppAnswers
for mustKey, mustVal := range mustAppAnswers {
appAnswers[mustKey] = mustVal
}
if systemComponentMap != nil {
if etcdEndpoints, ok := systemComponentMap[etcd]; ok {
appAnswers["prometheus.secrets[0]"] = exporterEtcdCertName
appAnswers["exporter-kube-etcd.enabled"] = "true"
appAnswers["exporter-kube-etcd.ports.metrics.port"] = "2379"
sort.Strings(etcdEndpoints)
for k, v := range etcdEndpoints {
key := fmt.Sprintf("exporter-kube-etcd.endpoints[%d]", k)
appAnswers[key] = v
}
if etcdTLSConfig != nil {
appAnswers["exporter-kube-etcd.certFile"] = etcdTLSConfig[0].certPath
appAnswers["exporter-kube-etcd.keyFile"] = etcdTLSConfig[0].keyPath
}
}
if controlplaneEndpoints, ok := systemComponentMap[controlplane]; ok {
appAnswers["exporter-kube-scheduler.enabled"] = "true"
appAnswers["exporter-kube-controller-manager.enabled"] = "true"
sort.Strings(controlplaneEndpoints)
for k, v := range controlplaneEndpoints {
key1 := fmt.Sprintf("exporter-kube-scheduler.endpoints[%d]", k)
key2 := fmt.Sprintf("exporter-kube-controller-manager.endpoints[%d]", k)
appAnswers[key1] = v
appAnswers[key2] = v
}
}
}
creator, err := ch.app.systemAccountManager.GetSystemUser(ch.clusterName)
if err != nil {
return nil, err
}
appCatalogID := settings.SystemMonitoringCatalogID.Get()
app := &v3.App{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{creatorIDAnno: creator.Name},
Labels: monitoring.OwnedLabels(appName, appTargetNamespace, appProjectName, monitoring.ClusterLevel),
Name: appName,
Namespace: appDeployProjectID,
},
Spec: v3.AppSpec{
Answers: appAnswers,
Description: "Rancher Cluster Monitoring",
ExternalID: appCatalogID,
ProjectName: appProjectName,
TargetNamespace: appTargetNamespace,
},
}
_, err = monitoring.DeployApp(ch.app.cattleAppClient, appDeployProjectID, app, false)
if err != nil {
return nil, err
}
return appAnswers, nil
}
func getClusterTag(cluster *mgmtv3.Cluster) string {
return fmt.Sprintf("%s(%s)", cluster.Name, cluster.Spec.DisplayName)
}
func isRkeCluster(cluster *mgmtv3.Cluster) bool {
return cluster.Status.Driver == mgmtv3.ClusterDriverRKE
}
func getSecretPath(secretName, name string) string {
return fmt.Sprintf("/etc/prometheus/secrets/%s/%s", secretName, name)
}
func getCertName(name string) string {
return fmt.Sprintf("%s.pem", name)
}
func getKeyName(name string) string {
return fmt.Sprintf("%s-key.pem", name)
}
func (ch *clusterHandler) deployMetrics(cluster *mgmtv3.Cluster) error {
clusterName := cluster.Name
for _, metric := range preDefinedClusterMetrics {
newObj := metric.DeepCopy()
newObj.Namespace = clusterName
_, err := ch.app.cattleMonitorMetricClient.Create(newObj)
if err != nil && !k8serrors.IsAlreadyExists(err) {
return err
}
}
for _, graph := range preDefinedClusterGraph {
newObj := graph.DeepCopy()
newObj.Namespace = clusterName
_, err := ch.app.cattleClusterGraphClient.Create(newObj)
if err != nil && !k8serrors.IsAlreadyExists(err) {
return err
}
}
return nil
}
func (ch *clusterHandler) withdrawMetrics(cluster *mgmtv3.Cluster) error {
clusterName := cluster.Name
for _, metric := range preDefinedClusterMetrics {
err := ch.app.cattleMonitorMetricClient.DeleteNamespaced(clusterName, metric.Name, &metav1.DeleteOptions{})
if err != nil && !k8serrors.IsNotFound(err) {
return err
}
}
for _, graph := range preDefinedClusterGraph {
err := ch.app.cattleClusterGraphClient.DeleteNamespaced(clusterName, graph.Name, &metav1.DeleteOptions{})
if err != nil && !k8serrors.IsNotFound(err) {
return err
}
}
return nil
}
func (ch *clusterHandler) isPrometheusReady(cluster *mgmtv3.Cluster) (bool, error) {
svcName, namespace, _ := monitoring.ClusterPrometheusEndpoint()
endpoints, err := ch.agentEndpointsLister.Get(namespace, svcName)
if err != nil {
return false, errors.Wrapf(err, "failed to get %s/%s endpoints", namespace, svcName)
}
if len(endpoints.Subsets) == 0 || len(endpoints.Subsets[0].Addresses) == 0 {
return false, nil
}
return true, nil
}