Skip to content

Commit 2633a07

Browse files
committed
#275 Added a concurrency policy option for scheduledScan CRD
Signed-off-by: Ilyes Ben Dlala <ilyes.bendlala@iteratec.com>
1 parent 42f0719 commit 2633a07

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

‎operator/apis/execution/v1/scheduledscan_types.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,20 @@ type ScheduledScanSpec struct {
2424
// +kubebuilder:validation:Optional
2525
// +kubebuilder:validation:Minimum=0
2626
SuccessfulJobsHistoryLimit *int32 `json:"successfulJobsHistoryLimit,omitempty"`
27+
2728
// FailedJobsHistoryLimit determines how many failed past Scans will be kept until the oldest one will be deleted, defaults to 3. When set to 0, Scans will be deleted directly after failure
2829
// +kubebuilder:validation:Optional
2930
// +kubebuilder:validation:Minimum=0
3031
FailedJobsHistoryLimit *int32 `json:"failedJobsHistoryLimit,omitempty"`
3132

33+
// Specifies how to treat concurrent executions of a Job.
34+
// Valid values are:
35+
// - "Allow" (default): allows CronJobs to run concurrently;
36+
// - "Forbid": forbids concurrent runs, skipping next run if previous run hasn't finished yet;
37+
// - "Replace": cancels currently running job and replaces it with a new one
38+
// +optional
39+
ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`
40+
3241
// ScanSpec describes the scan which should be started regularly
3342
ScanSpec *ScanSpec `json:"scanSpec"`
3443

@@ -38,6 +47,25 @@ type ScheduledScanSpec struct {
3847
RetriggerOnScanTypeChange bool `json:"retriggerOnScanTypeChange,omitempty"`
3948
}
4049

50+
// ConcurrencyPolicy describes how the job will be handled.
51+
// Only one of the following concurrent policies may be specified.
52+
// If none of the following policies is specified, the default one
53+
// is AllowConcurrent.
54+
// +kubebuilder:validation:Enum=Allow;Forbid;Replace
55+
type ConcurrencyPolicy string
56+
57+
const (
58+
// AllowConcurrent allows CronJobs to run concurrently.
59+
AllowConcurrent ConcurrencyPolicy = "Allow"
60+
61+
// ForbidConcurrent forbids concurrent runs, skipping next run if previous
62+
// hasn't finished yet.
63+
ForbidConcurrent ConcurrencyPolicy = "Forbid"
64+
65+
// ReplaceConcurrent cancels currently running job and replaces it with a new one.
66+
ReplaceConcurrent ConcurrencyPolicy = "Replace"
67+
)
68+
4169
// ScheduledScanStatus defines the observed state of ScheduledScan
4270
type ScheduledScanStatus struct {
4371
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster

‎operator/config/crd/bases/execution.securecodebox.io_scheduledscans.yaml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ spec:
5858
spec:
5959
description: ScheduledScanSpec defines the desired state of ScheduledScan
6060
properties:
61+
concurrencyPolicy:
62+
description: 'Specifies how to treat concurrent executions of a Job.
63+
Valid values are: - "Allow" (default): allows CronJobs to run concurrently;
64+
- "Forbid": forbids concurrent runs, skipping next run if previous
65+
run hasn''t finished yet; - "Replace": cancels currently running
66+
job and replaces it with a new one'
67+
enum:
68+
- Allow
69+
- Forbid
70+
- Replace
71+
type: string
6172
failedJobsHistoryLimit:
6273
description: FailedJobsHistoryLimit determines how many failed past
6374
Scans will be kept until the oldest one will be deleted, defaults

‎operator/controllers/execution/scheduledscan_controller.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,27 @@ func (r *ScheduledScanReconciler) Reconcile(ctx context.Context, req ctrl.Reques
107107
nextSchedule = time.Now().Add(-1 * time.Second)
108108
}
109109

110+
InProgressScans := getScansInProgress(childScans.Items)
111+
110112
// check if it is time to start the next Scan
111113
if !time.Now().Before(nextSchedule) {
114+
// check concurrency policy
115+
if scheduledScan.Spec.ConcurrencyPolicy == executionv1.ForbidConcurrent && len(InProgressScans) > 0 {
116+
log.V(8).Info("concurrency policy blocks concurrent runs, skipping", "num active", len(InProgressScans))
117+
return ctrl.Result{RequeueAfter: 5 * time.Minute}, nil
118+
}
119+
120+
// ...or instruct us to replace existing ones...
121+
if scheduledScan.Spec.ConcurrencyPolicy == executionv1.ReplaceConcurrent {
122+
for _, scan := range InProgressScans {
123+
// we don't care if the job was already deleted
124+
if err := r.Delete(context.Background(), &scan, client.PropagationPolicy(metav1.DeletePropagationBackground)); (err) != nil {
125+
log.Error(err, "unable to delete active job", "job", scan)
126+
return ctrl.Result{}, err
127+
}
128+
}
129+
}
130+
112131
if scheduledScan.Spec.RetriggerOnScanTypeChange == true {
113132
// generate hash for current state of the configured ScanType
114133
var scanType executionv1.ScanType
@@ -196,6 +215,22 @@ func getScansWithState(scans []executionv1.Scan, state string) []executionv1.Sca
196215
return newScans
197216
}
198217

218+
// Returns a sorted list of scans in progress
219+
func getScansInProgress(scans []executionv1.Scan) []executionv1.Scan {
220+
// Get a sorted list of scans.
221+
var newScans []executionv1.Scan
222+
for _, scan := range scans {
223+
if scan.Status.State != "Done" && scan.Status.State != "Errored" {
224+
newScans = append(newScans, scan)
225+
}
226+
}
227+
sort.Slice(newScans, func(i, j int) bool {
228+
return newScans[i].ObjectMeta.CreationTimestamp.Before(&newScans[j].ObjectMeta.CreationTimestamp)
229+
})
230+
231+
return newScans
232+
}
233+
199234
// DeleteOldScans when exceeding the history limit
200235
func (r *ScheduledScanReconciler) deleteOldScans(scans []executionv1.Scan, maxCount int32) error {
201236
for i, scan := range scans {

0 commit comments

Comments
 (0)