/
compliancesuite_controller.go
651 lines (575 loc) · 24.9 KB
/
compliancesuite_controller.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
642
643
644
645
646
647
648
649
650
651
package compliancesuite
import (
"context"
"fmt"
"time"
"github.com/go-logr/logr"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
compv1alpha1 "github.com/openshift/compliance-operator/pkg/apis/compliance/v1alpha1"
"github.com/openshift/compliance-operator/pkg/controller/common"
"github.com/openshift/compliance-operator/pkg/controller/metrics"
"github.com/openshift/compliance-operator/pkg/utils"
)
var log = logf.Log.WithName("suitectrl")
const (
// The default time we should wait before requeuing
requeueAfterDefault = 10 * time.Second
)
// Add creates a new ComplianceSuite Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager, met *metrics.Metrics, si utils.CtlplaneSchedulingInfo) error {
return add(mgr, newReconciler(mgr, met, si))
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager, met *metrics.Metrics, si utils.CtlplaneSchedulingInfo) reconcile.Reconciler {
return &ReconcileComplianceSuite{
reader: mgr.GetAPIReader(),
client: mgr.GetClient(),
scheme: mgr.GetScheme(),
recorder: mgr.GetEventRecorderFor("suitectrl"),
metrics: met,
schedulingInfo: si,
}
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller
c, err := controller.New("compliancesuite-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to primary resource ComplianceSuite
err = c.Watch(&source.Kind{Type: &compv1alpha1.ComplianceSuite{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}
// Watch for changes to secondary resource ComplianceScans and requeue the owner ComplianceSuite
err = c.Watch(&source.Kind{Type: &compv1alpha1.ComplianceScan{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &compv1alpha1.ComplianceSuite{},
})
if err != nil {
return err
}
return nil
}
// blank assignment to verify that ReconcileComplianceSuite implements reconcile.Reconciler
var _ reconcile.Reconciler = &ReconcileComplianceSuite{}
// ReconcileComplianceSuite reconciles a ComplianceSuite object
type ReconcileComplianceSuite struct {
// Accesses the API server directly
reader client.Reader
// This client, initialized using mgr.Client() above, is a split client
// that reads objects from the cache and writes to the apiserver
client client.Client
scheme *runtime.Scheme
recorder record.EventRecorder
metrics *metrics.Metrics
// helps us schedule platform scans on the nodes labeled for the
// compliance operator's control plane
schedulingInfo utils.CtlplaneSchedulingInfo
}
// Reconcile reads that state of the cluster for a ComplianceSuite object and makes changes based on the state read
// and what is in the ComplianceSuite.Spec
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *ReconcileComplianceSuite) Reconcile(request reconcile.Request) (reconcile.Result, error) {
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling ComplianceSuite")
// Fetch the ComplianceSuite suite
suite := &compv1alpha1.ComplianceSuite{}
err := r.client.Get(context.TODO(), request.NamespacedName, suite)
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
reqLogger.Error(err, "Cannot get the suite")
return reconcile.Result{}, err
}
// examine DeletionTimestamp to determine if object is under deletion
if suite.ObjectMeta.DeletionTimestamp.IsZero() {
// The object is not being deleted, so if it does not have our finalizer,
// then lets add the finalizer and update the object. This is equivalent
// registering our finalizer.
if !common.ContainsFinalizer(suite.ObjectMeta.Finalizers, compv1alpha1.SuiteFinalizer) {
suite.ObjectMeta.Finalizers = append(suite.ObjectMeta.Finalizers, compv1alpha1.SuiteFinalizer)
if err := r.client.Update(context.TODO(), suite); err != nil {
return reconcile.Result{}, err
}
}
} else {
// The object is being deleted
return reconcile.Result{}, r.suiteDeleteHandler(suite, reqLogger)
}
// We only update the status to pending if there isn't a result already.
if suite.Status.Phase == "" || (suite.Status.Conditions.GetCondition("Ready") == nil && !suite.IsResultAvailable()) {
sCopy := suite.DeepCopy()
sCopy.Status.Phase = compv1alpha1.PhasePending
sCopy.Status.SetConditionPending()
// ScanStatuses was initially marked as required. We create it here
// for cases whent he CRD is cached.
if sCopy.Status.ScanStatuses == nil {
sCopy.Status.ScanStatuses = make([]compv1alpha1.ComplianceScanStatusWrapper, 0)
}
updateErr := r.client.Status().Update(context.TODO(), sCopy)
if updateErr != nil {
return reconcile.Result{}, fmt.Errorf("Error setting initial status for suite: %w", updateErr)
}
return reconcile.Result{}, nil
}
if isValid, errorMsg := r.validateSuite(suite); !isValid {
// return immediately and don't schedule nor reconcile scans
return reconcile.Result{}, r.issueValidationError(suite, errorMsg, reqLogger)
}
if suite.Status.Conditions.GetCondition("Processing") == nil {
sCopy := suite.DeepCopy()
sCopy.Status.SetConditionsProcessing()
updateErr := r.client.Status().Update(context.TODO(), sCopy)
if updateErr != nil {
return reconcile.Result{}, fmt.Errorf("Error setting processing status for suite: %w", updateErr)
}
return reconcile.Result{}, nil
}
suiteCopy := suite.DeepCopy()
rescheduleWithDelay, err := r.reconcileScans(suiteCopy, reqLogger)
if err != nil {
return common.ReturnWithRetriableError(reqLogger, err)
} else if rescheduleWithDelay {
return reconcile.Result{Requeue: true, RequeueAfter: requeueAfterDefault}, err
}
var res reconcile.Result
if res, err = r.reconcileRemediations(suiteCopy, reqLogger); err != nil {
return common.ReturnWithRetriableError(reqLogger, err)
}
if suiteCopy.IsResultAvailable() {
sCopy := suite.DeepCopy()
sCopy.Status.SetConditionReady()
updateErr := r.client.Status().Update(context.TODO(), sCopy)
if updateErr != nil {
return reconcile.Result{}, fmt.Errorf("Error setting ready status for suite: %w", updateErr)
}
return res, r.reconcileScanRerunnerCronJob(suiteCopy, reqLogger)
}
return res, nil
}
func (r *ReconcileComplianceSuite) suiteDeleteHandler(suite *compv1alpha1.ComplianceSuite, logger logr.Logger) error {
rerunner := r.getRerunner(suite)
if err := r.handleRerunnerDelete(rerunner, suite.Name, logger); err != nil {
return err
}
suiteCopy := suite.DeepCopy()
// remove our finalizer from the list and update it.
suiteCopy.ObjectMeta.Finalizers = common.RemoveFinalizer(suiteCopy.ObjectMeta.Finalizers, compv1alpha1.SuiteFinalizer)
if err := r.client.Update(context.TODO(), suiteCopy); err != nil {
return err
}
return nil
}
func (r *ReconcileComplianceSuite) validateSuite(suite *compv1alpha1.ComplianceSuite) (bool, string) {
if isValid, errorMsg := r.validateSchedule(suite); !isValid {
return isValid, errorMsg
}
return true, ""
}
func (r *ReconcileComplianceSuite) issueValidationError(suite *compv1alpha1.ComplianceSuite, errorMsg string, logger logr.Logger) error {
enhancedMessage := fmt.Sprintf("Suite was invalid: %s", errorMsg)
logger.Info(enhancedMessage)
// Issue event
r.recorder.Event(
suite,
corev1.EventTypeWarning,
"SuiteValidationError",
errorMsg,
)
// Update status
suiteCopy := suite.DeepCopy()
suiteCopy.Status.Phase = compv1alpha1.PhaseDone
suiteCopy.Status.Result = compv1alpha1.ResultError
suiteCopy.Status.ErrorMessage = enhancedMessage
suiteCopy.Status.SetConditionInvalid()
if suiteCopy.Status.ScanStatuses == nil {
suiteCopy.Status.ScanStatuses = make([]compv1alpha1.ComplianceScanStatusWrapper, 0)
}
return r.client.Status().Update(context.TODO(), suiteCopy)
}
func (r *ReconcileComplianceSuite) reconcileScans(suite *compv1alpha1.ComplianceSuite, logger logr.Logger) (bool, error) {
for idx := range suite.Spec.Scans {
scanWrap := &suite.Spec.Scans[idx]
scan := &compv1alpha1.ComplianceScan{}
err := r.client.Get(context.TODO(), types.NamespacedName{Name: scanWrap.Name, Namespace: suite.Namespace}, scan)
if err != nil && errors.IsNotFound(err) {
// If the scan was not found, launch it
logger.Info("Scan not found, launching..", "ComplianceScan.Name", scanWrap.Name)
if err = launchScanForSuite(r, suite, scanWrap, logger); err != nil {
return false, err
}
logger.Info("Scan created", "ComplianceScan.Name", scanWrap.Name)
// No point in reconciling status yet
continue
} else if err != nil {
logger.Error(err, "Cannot get the scan for a suite", "ComplianceScan.Name", scanWrap.Name)
return false, err
}
// The scan already exists and is up to date, let's just make sure its status is reflected
if err := r.reconcileScanStatus(suite, scan, logger); err != nil {
return false, err
}
// Update the scan spec (last becuase it's a corner case)
rescheduleWithDelay, err := r.reconcileScanSpec(scanWrap, scan, logger)
if rescheduleWithDelay || err != nil {
return rescheduleWithDelay, err
}
// FIXME: delete scans that went away
}
return false, nil
}
func (r *ReconcileComplianceSuite) reconcileScanStatus(suite *compv1alpha1.ComplianceSuite, scan *compv1alpha1.ComplianceScan, logger logr.Logger) error {
// See if we already have a ScanStatusWrapper for this name
for idx := range suite.Status.ScanStatuses {
scanStatusWrap := suite.Status.ScanStatuses[idx]
if scan.Name == scanStatusWrap.Name {
err := r.updateScanStatus(suite, idx, &scanStatusWrap, scan, logger)
if err != nil {
logger.Error(err, "Could not update scan status")
return err
}
if r.recorder != nil && suite.IsResultAvailable() {
r.generateEventsForSuite(suite, logger)
}
return nil
}
}
err := r.addScanStatus(suite, scan, logger)
if err != nil {
logger.Error(err, "Could not add scan status")
return err
}
return nil
}
func (r *ReconcileComplianceSuite) reconcileScanSpec(scanWrap *compv1alpha1.ComplianceScanSpecWrapper, origScan *compv1alpha1.ComplianceScan, logger logr.Logger) (bool, error) {
// Do we need to update the scan?
if scanWrap.ScanSpecDiffers(origScan) == false {
logger.Info("Scan is up to date", "ComplianceScan.Name", origScan.Name)
return false, nil
}
// Fetch the scan again in case the status is updated. Updating the scan spec
// is so rare that the extra API server round-trip shouldn't matter
foundScan := &compv1alpha1.ComplianceScan{}
err := r.client.Get(context.TODO(), types.NamespacedName{Name: origScan.Name, Namespace: origScan.Namespace}, foundScan)
if err != nil {
return false, err
}
// We only update scans that are in DONE, because otherwise we might end up
// with inconsistencies (scan pods running where they shouldn't run, checks
// or remediations might be already created for the old content etc...)
if foundScan.Status.Phase != compv1alpha1.PhaseDone {
logger.Info("Refusing to update a scan that is not done, retrying later")
return true, nil
}
scanWrap.ComplianceScanSpec.DeepCopyInto(&foundScan.Spec)
err = r.client.Update(context.TODO(), foundScan)
if err != nil {
logger.Error(err, "Cannot update scan spec", "ComplianceScan.Name", origScan.Name)
return false, err
}
logger.Info("Scan updated", "ComplianceScan.Name", origScan.Name)
return false, nil
}
// updates the status of a scan in the compliance suite. Note that the suite that this takes is already a copy, so it's safe to modify
func (r *ReconcileComplianceSuite) updateScanStatus(suite *compv1alpha1.ComplianceSuite, idx int, scanStatusWrap *compv1alpha1.ComplianceScanStatusWrapper, scan *compv1alpha1.ComplianceScan, logger logr.Logger) error {
// if yes, update it, if the status differs
if scanStatusWrap.Phase == scan.Status.Phase {
logger.Info("Not updating scan, the phase is the same", "ComplianceScan.Name", scanStatusWrap.Name, "ComplianceScan.Phase", scanStatusWrap.Phase)
return nil
}
modScanStatus := compv1alpha1.ScanStatusWrapperFromScan(scan)
// Replace the copy so we use fresh metadata
suite = suite.DeepCopy()
suite.Status.ScanStatuses[idx] = modScanStatus
suite.Status.Phase = suite.LowestCommonState()
suite.Status.Result = suite.LowestCommonResult()
if suite.Status.Result == compv1alpha1.ResultNotApplicable {
suite.Status.ErrorMessage = "The suite result is not applicable, please check if you're using the correct platform"
} else if suite.Status.Result == compv1alpha1.ResultInconsistent {
suite.Status.ErrorMessage = fmt.Sprintf("results were not consistent, search for compliancecheckresults labeled with %s",
compv1alpha1.ComplianceCheckInconsistentLabel)
} else {
suite.Status.SetConditionsProcessing()
}
logger.Info("Updating scan status", "ComplianceScan.Name", modScanStatus.Name, "ComplianceScan.Phase", modScanStatus.Phase)
if err := r.client.Status().Update(context.TODO(), suite); err != nil {
return err
}
return r.setSuiteMetric(suite)
}
func (r *ReconcileComplianceSuite) generateEventsForSuite(suite *compv1alpha1.ComplianceSuite, logger logr.Logger) {
logger.Info("Generating events for suite")
if suite.Status.Result == compv1alpha1.ResultNotApplicable {
r.recorder.Eventf(
suite, corev1.EventTypeNormal, "SuiteNotApplicable",
"The suite result is not applicable, please check if you're using the correct platform")
} else if suite.Status.Result == compv1alpha1.ResultInconsistent {
r.recorder.Eventf(
suite, corev1.EventTypeNormal, "SuiteNotConsistent",
"The suite result is not consistent, please check for scan results labeled with %s",
compv1alpha1.ComplianceCheckInconsistentLabel)
}
err, haveOutdatedRems := utils.HaveOutdatedRemediations(r.client)
if err != nil {
logger.Info("Could not check if there exist any obsolete remediations", "Suite.Name", suite.Name)
}
if haveOutdatedRems {
r.recorder.Eventf(
suite, corev1.EventTypeNormal, "HaveOutdatedRemediations",
"One of suite's scans produced outdated remediations, please check for complianceremediation objects labeled with %s",
compv1alpha1.OutdatedRemediationLabel)
}
common.GenerateEventForResult(r.recorder, suite, suite, suite.Status.Result)
}
func (r *ReconcileComplianceSuite) addScanStatus(suite *compv1alpha1.ComplianceSuite, scan *compv1alpha1.ComplianceScan, logger logr.Logger) error {
// if not, create the scan status with the name and the current state
newScanStatus := compv1alpha1.ScanStatusWrapperFromScan(scan)
// Replace the copy so we use fresh metadata
suite = suite.DeepCopy()
suite.Status.ScanStatuses = append(suite.Status.ScanStatuses, newScanStatus)
logger.Info("Adding scan status", "ComplianceScan.Name", newScanStatus.Name, "ComplianceScan.Phase", newScanStatus.Phase)
suite.Status.Phase = suite.LowestCommonState()
suite.Status.Result = suite.LowestCommonResult()
if err := r.client.Status().Update(context.TODO(), suite); err != nil {
return err
}
return r.setSuiteMetric(suite)
}
func launchScanForSuite(r *ReconcileComplianceSuite, suite *compv1alpha1.ComplianceSuite, scanWrap *compv1alpha1.ComplianceScanSpecWrapper, logger logr.Logger) error {
scan := newScanForSuite(suite, scanWrap)
if scan == nil {
return fmt.Errorf("cannot create ComplianceScan for %s:%s", suite.Name, scanWrap.Name)
}
if err := controllerutil.SetControllerReference(suite, scan, r.scheme); err != nil {
log.Error(err, "Failed to set scan ownership", "ComplianceScan.Name", scan.Name)
return err
}
err := r.client.Create(context.TODO(), scan)
if errors.IsAlreadyExists(err) {
// Was there a race that created the scan in the meantime?
return nil
} else if err != nil {
log.Error(err, "Failed to launch scan", "ComplianceScan.Name", scan.Name)
return err
}
return nil
}
func newScanForSuite(suite *compv1alpha1.ComplianceSuite, scanWrap *compv1alpha1.ComplianceScanSpecWrapper) *compv1alpha1.ComplianceScan {
scan := compv1alpha1.ComplianceScanFromWrapper(scanWrap)
scan.SetLabels(map[string]string{
compv1alpha1.SuiteLabel: suite.Name,
})
scan.SetNamespace(suite.Namespace)
return scan
}
// Reconcile the remediation application in the suite. Note that the suite that this takes is already
// a copy, so it's safe to modify.
func (r *ReconcileComplianceSuite) reconcileRemediations(suite *compv1alpha1.ComplianceSuite, logger logr.Logger) (reconcile.Result, error) {
// We don't need to do anything else unless auto-applied is enabled
if !suite.ShouldApplyRemediations() {
return reconcile.Result{}, nil
}
// Get all the remediations
remList := &compv1alpha1.ComplianceRemediationList{}
mcfgpools := &mcfgv1.MachineConfigPoolList{}
affectedMcfgPools := map[string]*mcfgv1.MachineConfigPool{}
listOpts := client.ListOptions{
LabelSelector: labels.SelectorFromSet(labels.Set{compv1alpha1.SuiteLabel: suite.Name}),
}
if err := r.client.List(context.TODO(), remList, &listOpts); err != nil {
log.Error(err, "Failed to list remediations")
return reconcile.Result{}, err
}
if err := r.client.List(context.TODO(), mcfgpools); err != nil {
log.Error(err, "Failed to list pools")
return reconcile.Result{}, err
}
// Construct the list of the statuses
for _, rem := range remList.Items {
// get relevant scan
scan := &compv1alpha1.ComplianceScan{}
scanKey := types.NamespacedName{Name: rem.Labels[compv1alpha1.ComplianceScanLabel], Namespace: rem.Namespace}
if err := r.client.Get(context.TODO(), scanKey, scan); err != nil {
return reconcile.Result{}, err
}
// Only apply remediations once the scan is done. This hopefully ensures
// that we already have all the relevant remediations in place.
// We only care for remediations that haven't been applied
if scan.Status.Phase != compv1alpha1.PhaseDone {
continue
}
if err := r.applyRemediation(rem, suite, scan, mcfgpools, affectedMcfgPools, logger); err != nil {
return reconcile.Result{}, err
}
}
// We only post-process when everything is done. This avoids unnecessary
// "unpauses" for MachineConfigPools
if suite.Status.Phase != compv1alpha1.PhaseDone {
logger.Info("Waiting until all scans are in Done phase before post-procesing remediations")
return reconcile.Result{}, nil
}
logger.Info("All scans are in Done phase. Post-processing remediations")
// refresh remediationList
postProcessRemList := &compv1alpha1.ComplianceRemediationList{}
if err := r.client.List(context.TODO(), postProcessRemList, &listOpts); err != nil {
return reconcile.Result{}, err
}
// Check that all remediations have been applied yet. If not, requeue.
for _, rem := range postProcessRemList.Items {
if !rem.IsApplied() {
if rem.Status.ApplicationState == compv1alpha1.RemediationNeedsReview {
r.recorder.Event(suite, corev1.EventTypeWarning, "CannotRemediate", "Remediation needs-review. Values not set"+" Remediation:"+rem.Name)
continue
}
logger.Info("Remediation not applied yet. Skipping post-processing", "ComplianceRemediation.Name", rem.Name)
return reconcile.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil
}
}
// Only un-pause MachineConfigPools once the remediations have been applied
for idx := range affectedMcfgPools {
pool := affectedMcfgPools[idx]
poolKey := types.NamespacedName{Name: pool.GetName()}
// refresh pool reference directly from the API Server
if getErr := r.reader.Get(context.TODO(), poolKey, pool); getErr != nil {
logger.Error(getErr, "Could get newer machine config pool reference", "MachineConfigPool.Name", poolKey.Name)
return reconcile.Result{}, getErr
}
if pool.Spec.Paused {
logger.Info("Unpausing pool", "MachineConfigPool.Name", pool.Name)
poolCopy := pool.DeepCopy()
poolCopy.Spec.Paused = false
if err := r.client.Update(context.TODO(), poolCopy); err != nil {
logger.Error(err, "Could not unpause pool", "MachineConfigPool.Name", pool.Name)
return reconcile.Result{}, err
}
}
}
if suite.ApplyRemediationsAnnotationSet() || suite.RemoveOutdatedAnnotationSet() {
suiteCopy := suite.DeepCopy()
if suite.ApplyRemediationsAnnotationSet() {
delete(suiteCopy.Annotations, compv1alpha1.ApplyRemediationsAnnotation)
}
if suite.RemoveOutdatedAnnotationSet() {
delete(suiteCopy.Annotations, compv1alpha1.RemoveOutdatedAnnotation)
}
updateErr := r.client.Update(context.TODO(), suiteCopy)
return reconcile.Result{}, updateErr
}
return reconcile.Result{}, nil
}
func (r *ReconcileComplianceSuite) applyRemediation(rem compv1alpha1.ComplianceRemediation,
suite *compv1alpha1.ComplianceSuite,
scan *compv1alpha1.ComplianceScan,
mcfgpools *mcfgv1.MachineConfigPoolList,
affectedMcfgPools map[string]*mcfgv1.MachineConfigPool,
logger logr.Logger) error {
if utils.IsMachineConfig(rem.Spec.Current.Object) || utils.IsKubeletConfig(rem.Spec.Current.Object) {
// get affected pool
pool := r.getAffectedMcfgPool(scan, mcfgpools)
// we only need to operate on pools that are affected
if pool != nil {
foundPool, poolIsTracked := affectedMcfgPools[pool.Name]
if !poolIsTracked {
foundPool = pool.DeepCopy()
affectedMcfgPools[pool.Name] = foundPool
}
// we will use the same logic here for Kubelet Config remediation
if err := r.applyMcfgRemediationAndPausePool(rem, suite, foundPool, logger); err != nil {
return err
}
}
return nil
}
if err := r.applyGenericRemediation(rem, suite, logger); err != nil {
return err
}
return nil
}
func (r *ReconcileComplianceSuite) applyGenericRemediation(rem compv1alpha1.ComplianceRemediation,
suite *compv1alpha1.ComplianceSuite,
logger logr.Logger) error {
remCopy := rem.DeepCopy()
remCopy.Spec.Apply = true
if remediationNeedsOutdatedRemoval(remCopy, suite) {
logger.Info("Updating Outdated Remediation", "Remediation.Name", remCopy.Name)
remCopy.Spec.Outdated.Object = nil
}
logger.Info("Setting Remediation to applied", "ComplianceRemediation.Name", rem.Name)
if err := r.client.Update(context.TODO(), remCopy); err != nil {
return err
}
return nil
}
// This gets the remediation to be applied. Note that before being able to do that, the machineConfigPool is
// paused in order to reduce restarts of nodes.
func (r *ReconcileComplianceSuite) applyMcfgRemediationAndPausePool(rem compv1alpha1.ComplianceRemediation,
suite *compv1alpha1.ComplianceSuite,
pool *mcfgv1.MachineConfigPool, logger logr.Logger) error {
remCopy := rem.DeepCopy()
// Only pause pools where the pool wasn't paused before and
// the remediation hasn't been applied
if !pool.Spec.Paused {
logger.Info("Pausing pool", "MachineConfigPool.Name", pool.Name)
pool.Spec.Paused = true
if err := r.client.Update(context.TODO(), pool); err != nil {
logger.Error(err, "Could not pause pool", "MachineConfigPool.Name", pool.Name)
return err
}
}
remCopy.Spec.Apply = true
if remediationNeedsOutdatedRemoval(remCopy, suite) {
logger.Info("Updating Outdated Remediation", "Remediation.Name", remCopy.Name)
remCopy.Spec.Outdated.Object = nil
}
if err := r.client.Update(context.TODO(), remCopy); err != nil {
return err
}
return nil
}
func (r *ReconcileComplianceSuite) getAffectedMcfgPool(scan *compv1alpha1.ComplianceScan, mcfgpools *mcfgv1.MachineConfigPoolList) *mcfgv1.MachineConfigPool {
for i := range mcfgpools.Items {
pool := &mcfgpools.Items[i]
if utils.McfgPoolLabelMatches(scan.Spec.NodeSelector, pool) {
return pool
}
}
return nil
}
func remediationNeedsOutdatedRemoval(rem *compv1alpha1.ComplianceRemediation, suite *compv1alpha1.ComplianceSuite) bool {
return suite.ShouldRemoveOutdated() && rem.Status.ApplicationState == compv1alpha1.RemediationOutdated
}
func (r *ReconcileComplianceSuite) setSuiteMetric(suite *compv1alpha1.ComplianceSuite) error {
if suite.Status.Result == compv1alpha1.ResultCompliant {
r.metrics.SetComplianceStateInCompliance(suite.Name)
} else if suite.Status.Result == compv1alpha1.ResultNonCompliant {
r.metrics.SetComplianceStateOutOfCompliance(suite.Name)
} else if suite.Status.Result == compv1alpha1.ResultInconsistent {
r.metrics.SetComplianceStateInconsistent(suite.Name)
} else if suite.Status.Result == compv1alpha1.ResultError {
r.metrics.SetComplianceStateError(suite.Name)
}
return nil
}