-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
mgr.go
641 lines (572 loc) · 23.2 KB
/
mgr.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
Copyright 2016 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package mgr for the Ceph manager.
package mgr
import (
"bytes"
_ "embed"
"fmt"
"github.com/banzaicloud/k8s-objectmatcher/patch"
"github.com/coreos/pkg/capnslog"
"github.com/imdario/mergo"
"github.com/pkg/errors"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/clusterd"
cephclient "github.com/rook/rook/pkg/daemon/ceph/client"
"github.com/rook/rook/pkg/operator/ceph/cluster/mon"
"github.com/rook/rook/pkg/operator/ceph/config"
"github.com/rook/rook/pkg/operator/ceph/controller"
cephver "github.com/rook/rook/pkg/operator/ceph/version"
"github.com/rook/rook/pkg/operator/k8sutil"
"github.com/rook/rook/pkg/util/exec"
v1 "k8s.io/api/apps/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/yaml"
"os"
"path"
"path/filepath"
"strings"
"text/template"
)
var (
logger = capnslog.NewPackageLogger("github.com/rook/rook", "op-mgr")
prometheusRuleName = "prometheus-ceph-rules"
// PrometheusExternalRuleName is the name of the prometheus external rule
PrometheusExternalRuleName = "prometheus-ceph-rules-external"
monitoringPath = "/etc/ceph-monitoring/"
// PrometheusRuleTemplatePath Local package template path for prometheusrule
//go:embed template/prometheusrule.yaml
PrometheusRuleTemplatePath string
// PrometheusRuleExternalTemplatePath Local package template path for prometheusrule-external
//go:embed template/prometheusrule-external.yaml
PrometheusRuleExternalTemplatePath string
)
const (
AppName = "rook-ceph-mgr"
serviceAccountName = "rook-ceph-mgr"
maxMgrCount = 2
PrometheusModuleName = "prometheus"
crashModuleName = "crash"
PgautoscalerModuleName = "pg_autoscaler"
balancerModuleName = "balancer"
balancerModuleMode = "upmap"
serviceMonitorFile = "service-monitor.yaml"
defaultPrometheusRuleFile = "prometheusrule-default-values.yaml"
// minimum amount of memory in MB to run the pod
cephMgrPodMinimumMemory uint64 = 512
// DefaultMetricsPort prometheus exporter port
DefaultMetricsPort uint16 = 9283
)
// Cluster represents the Rook and environment configuration settings needed to set up Ceph mgrs.
type Cluster struct {
context *clusterd.Context
clusterInfo *cephclient.ClusterInfo
rookVersion string
exitCode func(err error) (int, bool)
spec cephv1.ClusterSpec
}
// New creates an instance of the mgr
func New(context *clusterd.Context, clusterInfo *cephclient.ClusterInfo, spec cephv1.ClusterSpec, rookVersion string) *Cluster {
return &Cluster{
context: context,
clusterInfo: clusterInfo,
spec: spec,
rookVersion: rookVersion,
exitCode: exec.ExitStatus,
}
}
var waitForDeploymentToStart = k8sutil.WaitForDeploymentToStart
var updateDeploymentAndWait = mon.UpdateCephDeploymentAndWait
// for backward compatibility, default to 1 mgr
func (c *Cluster) getReplicas() int {
replicas := c.spec.Mgr.Count
if replicas == 0 {
replicas = 1
}
return replicas
}
func (c *Cluster) getDaemonIDs() []string {
var daemonIDs []string
replicas := c.getReplicas()
if replicas > maxMgrCount {
replicas = maxMgrCount
}
for i := 0; i < replicas; i++ {
daemonIDs = append(daemonIDs, k8sutil.IndexToName(i))
}
return daemonIDs
}
// Start begins the process of running a cluster of Ceph mgrs.
func (c *Cluster) Start() error {
// Validate pod's memory if specified
err := controller.CheckPodMemory(cephv1.ResourcesKeyMgr, cephv1.GetMgrResources(c.spec.Resources), cephMgrPodMinimumMemory)
if err != nil {
return errors.Wrap(err, "error checking pod memory")
}
logger.Infof("start running mgr")
daemonIDs := c.getDaemonIDs()
var deploymentsToWaitFor []*v1.Deployment
for _, daemonID := range daemonIDs {
if c.clusterInfo.Context.Err() != nil {
return c.clusterInfo.Context.Err()
}
resourceName := fmt.Sprintf("%s-%s", AppName, daemonID)
mgrConfig := &mgrConfig{
DaemonID: daemonID,
ResourceName: resourceName,
DataPathMap: config.NewStatelessDaemonDataPathMap(config.MgrType, daemonID, c.clusterInfo.Namespace, c.spec.DataDirHostPath),
}
// We set the owner reference of the Secret to the Object controller instead of the replicaset
// because we watch for that resource and reconcile if anything happens to it
_, err := c.generateKeyring(mgrConfig)
if err != nil {
return errors.Wrapf(err, "failed to generate keyring for %q", resourceName)
}
// start the deployment
d, err := c.makeDeployment(mgrConfig)
if err != nil {
return errors.Wrapf(err, "failed to create deployment")
}
// Set the deployment hash as an annotation
err = patch.DefaultAnnotator.SetLastAppliedAnnotation(d)
if err != nil {
return errors.Wrapf(err, "failed to set annotation for deployment %q", d.Name)
}
newDeployment, err := c.context.Clientset.AppsV1().Deployments(c.clusterInfo.Namespace).Create(c.clusterInfo.Context, d, metav1.CreateOptions{})
if err != nil {
if !kerrors.IsAlreadyExists(err) {
return errors.Wrapf(err, "failed to create mgr deployment %s", resourceName)
}
logger.Infof("deployment for mgr %s already exists. updating if needed", resourceName)
if err := updateDeploymentAndWait(c.context, c.clusterInfo, d, config.MgrType, mgrConfig.DaemonID, c.spec.SkipUpgradeChecks, false); err != nil {
logger.Errorf("failed to update mgr deployment %q. %v", resourceName, err)
}
} else {
// wait for the new deployment
deploymentsToWaitFor = append(deploymentsToWaitFor, newDeployment)
}
}
// If the mgr is newly created, wait for it to start before continuing with the service and
// module configuration
for _, d := range deploymentsToWaitFor {
if err := waitForDeploymentToStart(c.clusterInfo.Context, c.context, d); err != nil {
return errors.Wrapf(err, "failed to wait for mgr %q to start", d.Name)
}
}
// check if any extra mgrs need to be removed
c.removeExtraMgrs(daemonIDs)
activeMgr := daemonIDs[0]
if len(daemonIDs) > 1 {
// When multiple mgrs are running, the mgr sidecar for the active mgr
// will create the services. However, the sidecar will only reconcile all
// the services when the active mgr changes. Here as part of the regular reconcile
// we trigger reconciling all the services to ensure they are current.
activeMgr, err = c.getActiveMgr()
if err != nil || activeMgr == "" {
activeMgr = ""
logger.Infof("cannot reconcile mgr services, no active mgr found. err=%v", err)
}
// reconcile mgr PDB
if err := c.reconcileMgrPDB(); err != nil {
return errors.Wrap(err, "failed to reconcile mgr PDB")
}
} else {
// delete MGR PDB as the count is less than 2
c.deleteMgrPDB()
}
if activeMgr != "" {
if err := c.reconcileServices(activeMgr); err != nil {
return errors.Wrap(err, "failed to enable mgr services")
}
}
// configure the mgr modules
c.configureModules(daemonIDs)
// enable monitoring if `monitoring: enabled: true`
if c.spec.Monitoring.Enabled {
// namespace in which the prometheusRule should be deployed
// if left empty, it will be deployed in current namespace
namespace := c.spec.Monitoring.RulesNamespace
if namespace == "" {
namespace = c.clusterInfo.Namespace
}
if err := c.DeployPrometheusRule(prometheusRuleName, namespace); err != nil {
logger.Errorf("failed to deploy prometheus rule. %v", err)
} else {
logger.Infof("prometheusRule deployed")
}
logger.Debugf("ended monitoring deployment")
}
return nil
}
func (c *Cluster) removeExtraMgrs(daemonIDs []string) {
// In case the mgr count was reduced, delete the extra mgrs
for i := maxMgrCount - 1; i >= len(daemonIDs); i-- {
mgrName := fmt.Sprintf("%s-%s", AppName, k8sutil.IndexToName(i))
err := c.context.Clientset.AppsV1().Deployments(c.clusterInfo.Namespace).Delete(c.clusterInfo.Context, mgrName, metav1.DeleteOptions{})
if err == nil {
logger.Infof("removed extra mgr %q", mgrName)
} else if !kerrors.IsNotFound(err) {
logger.Warningf("failed to remove extra mgr %q. %v", mgrName, err)
}
}
}
// ReconcileActiveMgrServices reconciles the services if the active mgr is the one running
// in the sidecar
func (c *Cluster) ReconcileActiveMgrServices(daemonNameToUpdate string) error {
// If the services are already set to this daemon, no need to attempt to update
svc, err := c.context.Clientset.CoreV1().Services(c.clusterInfo.Namespace).Get(c.clusterInfo.Context, AppName, metav1.GetOptions{})
if err != nil {
logger.Errorf("failed to check current mgr service, proceeding to update. %v", err)
} else {
currentDaemon := svc.Spec.Selector[controller.DaemonIDLabel]
if currentDaemon == daemonNameToUpdate {
logger.Infof("mgr services already set to daemon %q, no need to update", daemonNameToUpdate)
return nil
}
logger.Infof("mgr service currently set to %q, checking if need to update to %q", currentDaemon, daemonNameToUpdate)
}
activeName, err := c.getActiveMgr()
if err != nil {
return err
}
if activeName == "" {
return errors.New("active mgr not found")
}
if daemonNameToUpdate != activeName {
logger.Infof("no need for the mgr update since the active mgr is %q, rather than the local mgr %q", activeName, daemonNameToUpdate)
return nil
}
return c.reconcileServices(activeName)
}
func (c *Cluster) getActiveMgr() (string, error) {
// The preferred way to query the active mgr is "ceph mgr stat", which is only available in pacific or newer
if c.clusterInfo.CephVersion.IsAtLeastPacific() {
mgrStat, err := cephclient.CephMgrStat(c.context, c.clusterInfo)
if err != nil {
return "", errors.Wrap(err, "failed to get mgr stat for the active mgr")
}
return mgrStat.ActiveName, nil
}
// The legacy way to query the active mgr is with the verbose "ceph mgr dump"
mgrMap, err := cephclient.CephMgrMap(c.context, c.clusterInfo)
if err != nil {
return "", errors.Wrap(err, "failed to get mgr map for the active mgr")
}
return mgrMap.ActiveName, nil
}
// reconcile the services, if the active mgr is not detected, use the default mgr
func (c *Cluster) reconcileServices(activeDaemon string) error {
logger.Infof("setting services to point to mgr %q", activeDaemon)
if err := c.configureDashboardService(activeDaemon); err != nil {
return errors.Wrap(err, "failed to configure dashboard svc")
}
// create the metrics service
service, err := c.MakeMetricsService(AppName, activeDaemon, serviceMetricName)
if err != nil {
return err
}
if _, err := k8sutil.CreateOrUpdateService(c.clusterInfo.Context, c.context.Clientset, c.clusterInfo.Namespace, service); err != nil {
return errors.Wrap(err, "failed to create mgr metrics service")
}
// enable monitoring if `monitoring: enabled: true`
if c.spec.Monitoring.Enabled {
if err := c.EnableServiceMonitor(activeDaemon); err != nil {
return errors.Wrap(err, "failed to enable service monitor")
}
}
return c.updateServiceSelectors(activeDaemon)
}
// Make a best effort to update the services that have been labeled for being updated
// when the mgr has changed. They might be services for node ports, ingress, etc
func (c *Cluster) updateServiceSelectors(activeDaemon string) error {
selector := metav1.ListOptions{LabelSelector: "app=rook-ceph-mgr"}
services, err := c.context.Clientset.CoreV1().Services(c.clusterInfo.Namespace).List(c.clusterInfo.Context, selector)
if err != nil {
return errors.Wrap(err, "failed to query mgr services to update")
}
for i, service := range services.Items {
// Update the selector on the service to point to the active mgr
if service.Spec.Selector[controller.DaemonIDLabel] == activeDaemon {
logger.Infof("no need to update service %q", service.Name)
continue
}
// Update the service to point to the new active mgr
service.Spec.Selector[controller.DaemonIDLabel] = activeDaemon
logger.Infof("updating selector on mgr service %q to active mgr %q", service.Name, activeDaemon)
if _, err := c.context.Clientset.CoreV1().Services(c.clusterInfo.Namespace).Update(c.clusterInfo.Context, &services.Items[i], metav1.UpdateOptions{}); err != nil {
logger.Errorf("failed to update service %q. %v", service.Name, err)
} else {
logger.Infof("service %q successfully updated to active mgr %q", service.Name, activeDaemon)
}
}
return nil
}
func (c *Cluster) configureModules(daemonIDs []string) {
// Configure the modules asynchronously so we can complete all the configuration much sooner.
startModuleConfiguration("prometheus", c.enablePrometheusModule)
startModuleConfiguration("dashboard", c.configureDashboardModules)
// "crash" is part of the "always_on_modules" list as of Octopus
if !c.clusterInfo.CephVersion.IsAtLeastOctopus() {
startModuleConfiguration("crash", c.enableCrashModule)
} else {
// The balancer module must be configured on Octopus
// It is a bit confusing but as of Octopus modules that are in the "always_on_modules" list
// are "just" enabled, but still they must be configured to work properly
startModuleConfiguration("balancer", c.enableBalancerModule)
}
startModuleConfiguration("mgr module(s) from the spec", c.configureMgrModules)
}
func startModuleConfiguration(description string, configureModules func() error) {
go func() {
err := configureModules()
if err != nil {
logger.Errorf("failed modules: %q. %v", description, err)
} else {
logger.Infof("successful modules: %s", description)
}
}()
}
// Ceph docs about the prometheus module: http://docs.ceph.com/docs/master/mgr/prometheus/
func (c *Cluster) enablePrometheusModule() error {
if err := cephclient.MgrEnableModule(c.context, c.clusterInfo, PrometheusModuleName, true); err != nil {
return errors.Wrap(err, "failed to enable mgr prometheus module")
}
return nil
}
// Ceph docs about the crash module: https://docs.ceph.com/docs/master/mgr/crash/
func (c *Cluster) enableCrashModule() error {
if err := cephclient.MgrEnableModule(c.context, c.clusterInfo, crashModuleName, true); err != nil {
return errors.Wrap(err, "failed to enable mgr crash module")
}
return nil
}
func (c *Cluster) enableBalancerModule() error {
// The order MATTERS, always configure this module first, then turn it on
// This enables the balancer module mode only in versions older than Pacific
// This let's the user change the default mode if desired
if !c.clusterInfo.CephVersion.IsAtLeastPacific() {
// This sets min compat client to luminous and the balancer module mode
err := cephclient.ConfigureBalancerModule(c.context, c.clusterInfo, balancerModuleMode)
if err != nil {
return errors.Wrapf(err, "failed to configure module %q", balancerModuleName)
}
}
// This turns "on" the balancer
err := cephclient.MgrEnableModule(c.context, c.clusterInfo, balancerModuleName, false)
if err != nil {
return errors.Wrapf(err, "failed to turn on mgr %q module", balancerModuleName)
}
return nil
}
func (c *Cluster) configureMgrModules() error {
// Enable mgr modules from the spec
for _, module := range c.spec.Mgr.Modules {
if module.Name == "" {
return errors.New("name not specified for the mgr module configuration")
}
if wellKnownModule(module.Name) {
return errors.Errorf("cannot configure mgr module %q that is configured with other cluster settings", module.Name)
}
minVersion, versionOK := c.moduleMeetsMinVersion(module.Name)
if !versionOK {
return errors.Errorf("module %q cannot be configured because it requires at least Ceph version %q", module.Name, minVersion.String())
}
if module.Enabled {
if module.Name == balancerModuleName {
// Configure balancer module mode
err := cephclient.ConfigureBalancerModule(c.context, c.clusterInfo, balancerModuleMode)
if err != nil {
return errors.Wrapf(err, "failed to configure module %q", module.Name)
}
}
if err := cephclient.MgrEnableModule(c.context, c.clusterInfo, module.Name, false); err != nil {
return errors.Wrapf(err, "failed to enable mgr module %q", module.Name)
}
// Configure special settings for individual modules that are enabled
switch module.Name {
case PgautoscalerModuleName:
monStore := config.GetMonStore(c.context, c.clusterInfo)
// Ceph Octopus will have that option enabled
err := monStore.Set("global", "osd_pool_default_pg_autoscale_mode", "on")
if err != nil {
return errors.Wrap(err, "failed to enable pg autoscale mode for newly created pools")
}
err = monStore.Set("global", "mon_pg_warn_min_per_osd", "0")
if err != nil {
return errors.Wrap(err, "failed to set minimal number PGs per (in) osd before we warn the admin to")
}
case rookModuleName:
startModuleConfiguration("orchestrator modules", c.configureOrchestratorModules)
}
} else {
if err := cephclient.MgrDisableModule(c.context, c.clusterInfo, module.Name); err != nil {
return errors.Wrapf(err, "failed to disable mgr module %q", module.Name)
}
}
}
return nil
}
func (c *Cluster) moduleMeetsMinVersion(name string) (*cephver.CephVersion, bool) {
minVersions := map[string]cephver.CephVersion{
// Put the modules here, example:
// pgautoscalerModuleName: {Major: 14},
}
if ver, ok := minVersions[name]; ok {
// Check if the required min version is met
return &ver, c.clusterInfo.CephVersion.IsAtLeast(ver)
}
// no min version was required
return nil, true
}
func wellKnownModule(name string) bool {
knownModules := []string{dashboardModuleName, PrometheusModuleName, crashModuleName}
for _, known := range knownModules {
if name == known {
return true
}
}
return false
}
// EnableServiceMonitor add a servicemonitor that allows prometheus to scrape from the monitoring endpoint of the cluster
func (c *Cluster) EnableServiceMonitor(activeDaemon string) error {
serviceMonitor, err := k8sutil.GetServiceMonitor(path.Join(monitoringPath, serviceMonitorFile))
if err != nil {
return errors.Wrap(err, "service monitor could not be enabled")
}
serviceMonitor.SetName(AppName)
serviceMonitor.SetNamespace(c.clusterInfo.Namespace)
cephv1.GetMonitoringLabels(c.spec.Labels).OverwriteApplyToObjectMeta(&serviceMonitor.ObjectMeta)
if c.spec.External.Enable {
serviceMonitor.Spec.Endpoints[0].Port = controller.ServiceExternalMetricName
}
err = c.clusterInfo.OwnerInfo.SetControllerReference(serviceMonitor)
if err != nil {
return errors.Wrapf(err, "failed to set owner reference to service monitor %q", serviceMonitor.Name)
}
serviceMonitor.Spec.NamespaceSelector.MatchNames = []string{c.clusterInfo.Namespace}
serviceMonitor.Spec.Selector.MatchLabels = c.selectorLabels(activeDaemon)
applyMonitoringLabels(c, serviceMonitor)
if _, err = k8sutil.CreateOrUpdateServiceMonitor(c.clusterInfo.Context, serviceMonitor); err != nil {
return errors.Wrap(err, "service monitor could not be enabled")
}
return nil
}
// DeployPrometheusRule deploy prometheusRule that adds alerting and/or recording rules to the cluster
func (c *Cluster) DeployPrometheusRule(name, namespace string) error {
templatePath := PrometheusRuleTemplatePath
if strings.Contains(name, "external") {
templatePath = PrometheusRuleExternalTemplatePath
}
prometheusRule, err := c.templateToPrometheusRule(name, templatePath)
if err != nil {
return errors.Wrap(err, "failed to template prometheus rule")
}
prometheusRule.SetName(name)
prometheusRule.SetNamespace(namespace)
err = c.clusterInfo.OwnerInfo.SetControllerReference(prometheusRule)
if err != nil {
return errors.Wrapf(err, "failed to set owner reference to prometheus rule %q", prometheusRule.Name)
}
cephv1.GetMonitoringLabels(c.spec.Labels).OverwriteApplyToObjectMeta(&prometheusRule.ObjectMeta)
if _, err := k8sutil.CreateOrUpdatePrometheusRule(c.clusterInfo.Context, prometheusRule); err != nil {
logger.Errorf("failed to deploy prometheusRule: %v", prometheusRule)
return errors.Wrap(err, "prometheus rule could not be deployed")
}
return nil
}
func (c *Cluster) templateToPrometheusRule(name, templateData string) (*monitoringv1.PrometheusRule, error) {
var rule monitoringv1.PrometheusRule
customPrometheusRule, err := c.computeCustomizedPrometheusAlerts()
if err != nil {
return nil, errors.Wrap(err, "failed to compute customized prometheus rule")
}
t, err := loadTemplate(name, templateData, customPrometheusRule)
if err != nil {
return nil, errors.Wrap(err, "failed to load prometheus rule template")
}
err = yaml.Unmarshal(t, &rule)
if err != nil {
return nil, err
}
// remove groups that all their alerts where disabled from prometheus rule spec
for i, g := range rule.Spec.Groups {
if g.Rules == nil {
rule.Spec.Groups = append(rule.Spec.Groups[:i], rule.Spec.Groups[i+1:]...)
}
}
return &rule, nil
}
func loadTemplate(name, templateData string, p map[string]*cephv1.CephAlert) ([]byte, error) {
var writer bytes.Buffer
t := template.New(name)
t, err := t.Parse(templateData)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse template %v", name)
}
err = t.Execute(&writer, p)
return writer.Bytes(), err
}
// computeCustomizedPrometheusAlerts compute Alerts by merging the data that was get from cephcluster spec with the default data
func (c *Cluster) computeCustomizedPrometheusAlerts() (map[string]*cephv1.CephAlert, error) {
defaultAlerts := make(map[string]*cephv1.CephAlert)
fi, err := os.ReadFile(filepath.Clean(path.Join(monitoringPath, defaultPrometheusRuleFile)))
if err != nil {
return nil, err
}
//replace dynamic values
operatorNamespace := os.Getenv(k8sutil.PodNamespaceEnvVar)
fi = []byte(strings.NewReplacer("${operatorNamespace}", operatorNamespace).Replace(string(fi)))
err = yaml.Unmarshal(fi, &defaultAlerts)
if err != nil {
return nil, err
}
// merge custom alerts values with default values (if any)
if c.spec.Monitoring.AlertRuleOverrides != nil {
err = mergo.Merge(&defaultAlerts, c.spec.Monitoring.AlertRuleOverrides, mergo.WithOverride)
if err != nil {
return nil, err
}
}
return defaultAlerts, nil
}
// IsModuleInSpec returns whether a module is present in the CephCluster manager spec
func IsModuleInSpec(modules []cephv1.Module, moduleName string) bool {
for _, v := range modules {
if v.Name == moduleName {
return true
}
}
return false
}
// ApplyMonitoringLabels function adds the name of the resource that manages
// cephcluster, as a label on the ceph metrics
func applyMonitoringLabels(c *Cluster, serviceMonitor *monitoringv1.ServiceMonitor) {
if c.spec.Labels != nil {
if monitoringLabels, ok := c.spec.Labels["monitoring"]; ok {
if managedBy, ok := monitoringLabels["rook.io/managedBy"]; ok {
relabelConfig := monitoringv1.RelabelConfig{
TargetLabel: "managedBy",
Replacement: managedBy,
}
serviceMonitor.Spec.Endpoints[0].RelabelConfigs = append(
serviceMonitor.Spec.Endpoints[0].RelabelConfigs, &relabelConfig)
} else {
logger.Info("rook.io/managedBy not specified in monitoring labels")
}
} else {
logger.Info("monitoring labels not specified")
}
}
}