@@ -5,7 +5,6 @@ package automation
5
5
6
6
import (
7
7
"context"
8
- "fmt"
9
8
"strconv"
10
9
"time"
11
10
@@ -83,7 +82,7 @@ func (r *PolicyAutomationReconciler) setOwnerReferences(
83
82
}
84
83
85
84
if ! policyOwnerRefFound {
86
- log .V (3 ).Info (fmt . Sprintf ( "Setting the owner reference on the PolicyAutomation %s" , policyAutomation .GetName () ))
85
+ log .V (3 ).Info ("Setting the owner reference on the PolicyAutomation " + policyAutomation .GetName ())
87
86
policyAutomation .SetOwnerReferences ([]metav1.OwnerReference {
88
87
* metav1 .NewControllerRef (policy , policy .GroupVersionKind ()),
89
88
})
@@ -232,8 +231,8 @@ func (r *PolicyAutomationReconciler) getViolationContext(
232
231
if contextLimit > 0 && len (violationContext .PolicyViolations ) == contextLimit {
233
232
log .V (2 ).Info (
234
233
"PolicyViolationsLimit is %s so skipping %s remaining replicated policies violations." ,
235
- fmt . Sprint (contextLimit ),
236
- fmt . Sprint (len (replicatedPlcList .Items )- contextLimit ),
234
+ strconv . Itoa (contextLimit ),
235
+ strconv . Itoa (len (replicatedPlcList .Items )- contextLimit ),
237
236
)
238
237
239
238
break
@@ -301,9 +300,10 @@ func (r *PolicyAutomationReconciler) Reconcile(
301
300
return reconcile.Result {}, err
302
301
}
303
302
304
- if policyAutomation .Annotations ["policy.open-cluster-management.io/rerun" ] == "true" {
305
- AjExist , err := MatchPAResouceV (policyAutomation ,
306
- r .DynamicClient , policyAutomation .GetResourceVersion ())
303
+ switch {
304
+ case policyAutomation .Annotations ["policy.open-cluster-management.io/rerun" ] == "true" :
305
+ // Rerun logic
306
+ AjExist , err := MatchPAResouceV (policyAutomation , r .DynamicClient , policyAutomation .GetResourceVersion ())
307
307
if err != nil {
308
308
log .Error (err , "Failed to compare Ansible job's resourceVersion" )
309
309
@@ -317,24 +317,18 @@ func (r *PolicyAutomationReconciler) Reconcile(
317
317
}
318
318
319
319
targetList := common .FindNonCompliantClustersForPolicy (policy )
320
- log .Info (
321
- "Creating an Ansible job" , "mode" , "manual" ,
322
- "clusterCount" , strconv .Itoa (len (targetList )))
320
+ log .Info ("Creating an Ansible job" , "mode" , "manual" , "clusterCount" , strconv .Itoa (len (targetList )))
323
321
324
322
violationContext , _ := r .getViolationContext (ctx , policy , targetList , policyAutomation )
325
323
326
- err = CreateAnsibleJob (
327
- policyAutomation ,
328
- r .DynamicClient ,
329
- "manual" ,
330
- violationContext ,
331
- )
324
+ err = CreateAnsibleJob (policyAutomation , r .DynamicClient , "manual" , violationContext )
332
325
if err != nil {
333
326
log .Error (err , "Failed to create the Ansible job" , "mode" , "manual" )
334
327
335
328
return reconcile.Result {}, err
336
329
}
337
- // manual run succeeded, remove annotation
330
+
331
+ // Manual run succeeded, remove annotation
338
332
delete (policyAutomation .Annotations , "policy.open-cluster-management.io/rerun" )
339
333
340
334
err = r .Update (ctx , policyAutomation , & client.UpdateOptions {})
@@ -345,19 +339,17 @@ func (r *PolicyAutomationReconciler) Reconcile(
345
339
}
346
340
347
341
return reconcile.Result {}, nil
348
- } else if policyAutomation .Spec .Mode == policyv1beta1 .Disabled {
349
- log .Info ("Automation is disabled, doing nothing" )
350
342
351
- return reconcile.Result {}, nil
352
- } else {
353
- if policy .Spec .Disabled {
354
- log .Info ("The policy is disabled. Doing nothing." )
343
+ case policy .Spec .Disabled :
344
+ log .Info ("The policy is disabled. Doing nothing." )
355
345
356
- return reconcile.Result {}, nil
357
- }
346
+ return reconcile.Result {}, nil
358
347
359
- if policyAutomation .Spec .Mode == "scan" {
348
+ default :
349
+ switch policyAutomation .Spec .Mode {
350
+ case "scan" :
360
351
log := log .WithValues ("mode" , "scan" )
352
+
361
353
log .V (2 ).Info ("Triggering scan mode" )
362
354
363
355
requeueAfter , err := time .ParseDuration (policyAutomation .Spec .RescanAfter )
@@ -373,45 +365,42 @@ func (r *PolicyAutomationReconciler) Reconcile(
373
365
if len (targetList ) > 0 {
374
366
log .Info ("Creating An Ansible job" , "targetList" , targetList )
375
367
violationContext , _ := r .getViolationContext (ctx , policy , targetList , policyAutomation )
376
- err = CreateAnsibleJob ( policyAutomation , r . DynamicClient , "scan" ,
377
- violationContext )
368
+
369
+ err = CreateAnsibleJob ( policyAutomation , r . DynamicClient , "scan" , violationContext )
378
370
if err != nil {
379
371
return reconcile.Result {RequeueAfter : requeueAfter }, err
380
372
}
381
373
} else {
382
374
log .Info ("All clusters are compliant. Doing nothing." )
383
375
}
384
-
385
376
// no violations found, doing nothing
386
377
r .counter ++
387
- log .V (2 ).Info (
388
- "RequeueAfter." , "RequeueAfter" , requeueAfter .String (), "Counter" , fmt .Sprintf ("%d" , r .counter ),
389
- )
378
+
379
+ log .V (2 ).Info ("RequeueAfter." , "RequeueAfter" , requeueAfter .String (), "Counter" , strconv .Itoa (r .counter ))
390
380
391
381
return reconcile.Result {RequeueAfter : requeueAfter }, nil
392
- } else if policyAutomation .Spec .Mode == policyv1beta1 .Once {
382
+
383
+ case policyv1beta1 .Once :
393
384
log := log .WithValues ("mode" , string (policyv1beta1 .Once ))
394
385
targetList := common .FindNonCompliantClustersForPolicy (policy )
386
+
395
387
if len (targetList ) > 0 {
396
388
log .Info ("Creating an Ansible job" , "targetList" , targetList )
397
389
398
- AjExist , err := MatchPAGeneration (policyAutomation ,
399
- r .DynamicClient , policyAutomation .GetGeneration ())
390
+ AjExist , err := MatchPAGeneration (policyAutomation , r .DynamicClient , policyAutomation .GetGeneration ())
400
391
if err != nil {
401
392
log .Error (err , "Failed to get Ansible job's generation" )
402
393
403
394
return reconcile.Result {}, err
404
395
}
396
+
405
397
if AjExist {
406
398
return reconcile.Result {}, nil
407
399
}
400
+
408
401
violationContext , _ := r .getViolationContext (ctx , policy , targetList , policyAutomation )
409
- err = CreateAnsibleJob (
410
- policyAutomation ,
411
- r .DynamicClient ,
412
- string (policyv1beta1 .Once ),
413
- violationContext ,
414
- )
402
+
403
+ err = CreateAnsibleJob (policyAutomation , r .DynamicClient , string (policyv1beta1 .Once ), violationContext )
415
404
if err != nil {
416
405
log .Error (err , "Failed to create the Ansible job" )
417
406
@@ -429,7 +418,8 @@ func (r *PolicyAutomationReconciler) Reconcile(
429
418
} else {
430
419
log .Info ("All clusters are compliant. Doing nothing." )
431
420
}
432
- } else if policyAutomation .Spec .Mode == policyv1beta1 .EveryEvent {
421
+
422
+ case policyv1beta1 .EveryEvent :
433
423
log := log .WithValues ("mode" , string (policyv1beta1 .EveryEvent ))
434
424
targetList := common .FindNonCompliantClustersForPolicy (policy )
435
425
targetListMap := getTargetListMap (targetList )
@@ -441,6 +431,7 @@ func (r *PolicyAutomationReconciler) Reconcile(
441
431
requeueFlag := false
442
432
// Automation event time grouped by the cluster name
443
433
eventMap := map [string ]policyv1beta1.ClusterEvent {}
434
+
444
435
if len (policyAutomation .Status .ClustersWithEvent ) > 0 {
445
436
eventMap = policyAutomation .Status .ClustersWithEvent
446
437
}
@@ -460,7 +451,6 @@ func (r *PolicyAutomationReconciler) Reconcile(
460
451
log .Error (err , "Failed to retrieve EventTime in ClustersWithEvent" )
461
452
delete (eventMap , clusterName )
462
453
}
463
-
464
454
// The time that delayAfterRunSeconds setting expires
465
455
delayUntil := originalStartTime .Add (time .Duration (delayAfterRunSeconds ) * time .Second )
466
456
@@ -479,22 +469,23 @@ func (r *PolicyAutomationReconciler) Reconcile(
479
469
} else {
480
470
requeueFlag = true
481
471
// Within the delay period and use the earliest requeueDuration to requeue
482
- if ( requeueDuration == 0 ) || ( requeueDuration > int (delayUntil .Sub (now )+ 1 ) ) {
472
+ if requeueDuration == 0 || requeueDuration > int (delayUntil .Sub (now )+ 1 ) {
483
473
requeueDuration = int (delayUntil .Sub (now ) + 1 )
484
474
}
485
475
// keep the event and update eventTime
486
476
clusterEvent .EventTime = nowStr
487
- // new event from compliant to non-compliant
488
477
eventMap [clusterName ] = clusterEvent
489
478
}
490
- } // Otherwise, the policy keeps non-compliant since originalStartTime, do nothing
491
- } else { // The policy is compliant with the target cluster
479
+ }
480
+ } else {
481
+ // Otherwise, the policy keeps non-compliant since originalStartTime, do nothing
482
+ // The policy is compliant with the target cluster
492
483
if delayAfterRunSeconds > 0 && now .Before (delayUntil ) {
493
484
// Within the delay period, keep the event and update eventTime
494
485
clusterEvent .EventTime = nowStr
495
- // new event from non-compliant to compliant
496
486
eventMap [clusterName ] = clusterEvent
497
- } else { // No delay period or it is expired, remove the event
487
+ } else {
488
+ // No delay period or it is expired, remove the event
498
489
delete (eventMap , clusterName )
499
490
}
500
491
}
@@ -514,14 +505,12 @@ func (r *PolicyAutomationReconciler) Reconcile(
514
505
for clusterName := range trimmedTargetMap {
515
506
trimmedTargetList = append (trimmedTargetList , clusterName )
516
507
}
508
+
517
509
log .Info ("Creating An Ansible job" , "trimmedTargetList" , trimmedTargetList )
518
510
violationContext , _ := r .getViolationContext (ctx , policy , trimmedTargetList , policyAutomation )
519
- err = CreateAnsibleJob (
520
- policyAutomation ,
521
- r .DynamicClient ,
522
- string (policyv1beta1 .EveryEvent ),
523
- violationContext ,
524
- )
511
+
512
+ err = CreateAnsibleJob (policyAutomation , r .DynamicClient ,
513
+ string (policyv1beta1 .EveryEvent ), violationContext )
525
514
if err != nil {
526
515
log .Error (err , "Failed to create the Ansible job" )
527
516
@@ -542,22 +531,28 @@ func (r *PolicyAutomationReconciler) Reconcile(
542
531
543
532
policyAutomation .Status .ClustersWithEvent = eventMap
544
533
// use StatusWriter to update status subresource of a Kubernetes object
545
- err = r .Status ().Update (ctx , policyAutomation )
534
+ err : = r .Status ().Update (ctx , policyAutomation )
546
535
if err != nil {
547
536
log .Error (err , "Failed to update ClustersWithEvent in policyAutomation status" )
548
537
549
538
return reconcile.Result {}, err
550
539
}
551
540
552
541
if requeueFlag {
553
- log .Info (
554
- "Requeue for the new non-compliant event during the delay period" ,
555
- "Delay in seconds" , delayAfterRunSeconds ,
556
- "Requeue After" , requeueDuration ,
557
- )
542
+ log .Info ("Requeue for the new non-compliant event during the delay period" , "Delay in seconds" ,
543
+ delayAfterRunSeconds , "Requeue After" , requeueDuration )
558
544
559
545
return reconcile.Result {RequeueAfter : time .Duration (requeueDuration )}, nil
560
546
}
547
+
548
+ case policyv1beta1 .Disabled :
549
+ log .Info ("Automation is disabled, doing nothing" )
550
+
551
+ return reconcile.Result {}, nil
552
+ default :
553
+ log .Info ("Unknown mode. No action taken." )
554
+
555
+ return reconcile.Result {}, nil
561
556
}
562
557
}
563
558
0 commit comments