Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit 1b1f780

Browse files
committed
Implement HistoryLimit for ScheduledScans
1 parent 219022d commit 1b1f780

11 files changed

+226
-59
lines changed

operator/apis/execution/v1/scan_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ type ScanStatus struct {
4141
// RawResultFile Filename of the result file of the scanner. e.g. `nmap-result.xml`
4242
RawResultFile string `json:"rawResultFile,omitempty"`
4343

44+
StartTime *metav1.Time `json:"startTime,omitempty"`
45+
4446
// FindingCount indicates how many findings were identified in total
4547
FindingCount uint64 `json:"findingCount,omitempty"`
4648
// FindingSeverities indicates the count of finding with the respective severity

operator/apis/execution/v1/zz_generated.deepcopy.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

operator/config/crd/bases/execution.experimental.securecodebox.io_parsedefinitions.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
apiVersion: apiextensions.k8s.io/v1beta1
44
kind: CustomResourceDefinition
55
metadata:
6+
annotations:
7+
controller-gen.kubebuilder.io/version: v0.2.5
68
creationTimestamp: null
79
name: parsedefinitions.execution.experimental.securecodebox.io
810
spec:
911
group: execution.experimental.securecodebox.io
1012
names:
1113
kind: ParseDefinition
14+
listKind: ParseDefinitionList
1215
plural: parsedefinitions
13-
scope: ""
16+
singular: parsedefinition
17+
scope: Namespaced
1418
validation:
1519
openAPIV3Schema:
1620
description: ParseDefinition is the Schema for the parsedefinitions API

operator/config/crd/bases/execution.experimental.securecodebox.io_persistenceproviders.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
apiVersion: apiextensions.k8s.io/v1beta1
44
kind: CustomResourceDefinition
55
metadata:
6+
annotations:
7+
controller-gen.kubebuilder.io/version: v0.2.5
68
creationTimestamp: null
79
name: persistenceproviders.execution.experimental.securecodebox.io
810
spec:
@@ -14,8 +16,10 @@ spec:
1416
group: execution.experimental.securecodebox.io
1517
names:
1618
kind: PersistenceProvider
19+
listKind: PersistenceProviderList
1720
plural: persistenceproviders
18-
scope: ""
21+
singular: persistenceprovider
22+
scope: Namespaced
1923
subresources: {}
2024
validation:
2125
openAPIV3Schema:
@@ -103,9 +107,13 @@ spec:
103107
for env vars'
104108
type: string
105109
divisor:
110+
anyOf:
111+
- type: integer
112+
- type: string
106113
description: Specifies the output format of the exposed
107114
resources, defaults to "1"
108-
type: string
115+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
116+
x-kubernetes-int-or-string: true
109117
resource:
110118
description: 'Required: resource to select'
111119
type: string

operator/config/crd/bases/execution.experimental.securecodebox.io_scans.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
apiVersion: apiextensions.k8s.io/v1beta1
44
kind: CustomResourceDefinition
55
metadata:
6+
annotations:
7+
controller-gen.kubebuilder.io/version: v0.2.5
68
creationTimestamp: null
79
name: scans.execution.experimental.securecodebox.io
810
spec:
@@ -32,8 +34,10 @@ spec:
3234
group: execution.experimental.securecodebox.io
3335
names:
3436
kind: Scan
37+
listKind: ScanList
3538
plural: scans
36-
scope: ""
39+
singular: scan
40+
scope: Namespaced
3741
subresources:
3842
status: {}
3943
validation:
@@ -102,6 +106,9 @@ spec:
102106
description: RawResultType determines which kind of ParseDefinition
103107
will be used to turn the raw results of the scanner into findings
104108
type: string
109+
startTime:
110+
format: date-time
111+
type: string
105112
state:
106113
type: string
107114
type: object

operator/config/crd/bases/execution.experimental.securecodebox.io_scantypes.yaml

Lines changed: 142 additions & 44 deletions
Large diffs are not rendered by default.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
apiVersion: apiextensions.k8s.io/v1beta1
44
kind: CustomResourceDefinition
55
metadata:
6+
annotations:
7+
controller-gen.kubebuilder.io/version: v0.2.5
68
creationTimestamp: null
79
name: scheduledscans.execution.experimental.securecodebox.io
810
spec:
911
group: execution.experimental.securecodebox.io
1012
names:
1113
kind: ScheduledScan
14+
listKind: ScheduledScanList
1215
plural: scheduledscans
13-
scope: ""
16+
singular: scheduledscan
17+
scope: Namespaced
1418
subresources:
1519
status: {}
1620
validation:

operator/config/samples/execution_v1_scheduledscan.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
name: scheduled-nmap-localhost
55
spec:
66
interval: 1m
7-
# historyLimit: 1
7+
historyLimit: 2
88
scanSpec:
99
scanType: "nmap"
1010
parameters:

operator/controllers/execution/scheduledscan_controller.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22+
"sort"
2223
"time"
2324

2425
"github.com/go-logr/logr"
@@ -61,7 +62,34 @@ func (r *ScheduledScanReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
6162
return ctrl.Result{}, err
6263
}
6364

64-
log.Info("ScanInverval parsed", "interval", scheduledScan.Spec.Interval)
65+
log.Info("Got Scans", "count", len(childScans.Items))
66+
67+
var completedScans []executionv1.Scan
68+
for _, scan := range childScans.Items {
69+
if scan.Status.State == "Done" {
70+
completedScans = append(completedScans, scan)
71+
}
72+
}
73+
74+
sort.Slice(completedScans, func(i, j int) bool {
75+
if completedScans[i].Status.StartTime == nil {
76+
return completedScans[j].Status.StartTime != nil
77+
}
78+
return completedScans[i].Status.StartTime.Before(completedScans[j].Status.StartTime)
79+
})
80+
81+
for i, scan := range completedScans {
82+
if int64(i) >= int64(len(completedScans))-scheduledScan.Spec.HistoryLimit {
83+
break
84+
}
85+
if err := r.Delete(ctx, &scan, client.PropagationPolicy(metav1.DeletePropagationBackground)); (err) != nil {
86+
log.Error(err, "unable to delete old scan", "scan", scan)
87+
} else {
88+
log.V(0).Info("deleted old successful job", "scan", scan)
89+
}
90+
}
91+
92+
log.Info("ScanInterval parsed", "interval", scheduledScan.Spec.Interval)
6593

6694
var nextSchedule time.Time
6795
if scheduledScan.Status.LastScheduleTime != nil {
@@ -83,6 +111,8 @@ func (r *ScheduledScanReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
83111
Spec: *scheduledScan.Spec.ScanSpec.DeepCopy(),
84112
}
85113
scan.Name = fmt.Sprintf("%s-%d", scheduledScan.Name, nextSchedule.Unix())
114+
metaNow := metav1.Now()
115+
scan.Status.StartTime = &metaNow
86116
if err := ctrl.SetControllerReference(&scheduledScan, scan, r.Scheme); err != nil {
87117
log.Error(err, "unable to set owner reference on scan")
88118
return ctrl.Result{}, err
@@ -117,7 +147,7 @@ func (r *ScheduledScanReconciler) SetupWithManager(mgr ctrl.Manager) error {
117147
return nil
118148
}
119149
// ...make sure it's a Scan belonging to a Target...
120-
if owner.APIVersion != apiGVStr || owner.Kind != "Target" {
150+
if owner.APIVersion != apiGVStr || owner.Kind != "ScheduledScan" {
121151
return nil
122152
}
123153

operator/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ require (
1010
k8s.io/api v0.17.2
1111
k8s.io/apimachinery v0.17.2
1212
k8s.io/client-go v0.17.2
13-
sigs.k8s.io/controller-runtime v0.5.0
13+
sigs.k8s.io/controller-runtime v0.5.2
1414
)

0 commit comments

Comments
 (0)