Skip to content

Commit ed73801

Browse files
erikgbtenstad
andauthored
feat: make scan job lifetime configurable (#1287)
Co-authored-by: Amund Tenstad <[email protected]>
1 parent bd9475d commit ed73801

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

internal/config/config.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ import (
88
)
99

1010
type Config struct {
11-
MetricsLabels []string `mapstructure:"cis-metrics-labels"`
12-
ScanInterval time.Duration `mapstructure:"scan-interval"`
13-
ScanJobNamespace string `mapstructure:"scan-job-namespace"`
14-
ScanJobServiceAccount string `mapstructure:"scan-job-service-account"`
15-
ScanNamespaces []string `mapstructure:"namespaces"`
16-
ScanNamespaceExcludeRegexp *regexp.Regexp `mapstructure:"scan-namespace-exclude-regexp"`
17-
ScanNamespaceIncludeRegexp *regexp.Regexp `mapstructure:"scan-namespace-include-regexp"`
18-
ScanWorkloadResources []string `mapstructure:"scan-workload-resources"`
19-
TrivyImage string `mapstructure:"trivy-image"`
20-
TrivyCommand TrivyCommand `mapstructure:"trivy-command"`
21-
ActiveScanJobLimit int `mapstructure:"active-scan-job-limit"`
11+
MetricsLabels []string `mapstructure:"cis-metrics-labels"`
12+
ScanInterval time.Duration `mapstructure:"scan-interval"`
13+
ScanJobNamespace string `mapstructure:"scan-job-namespace"`
14+
ScanJobServiceAccount string `mapstructure:"scan-job-service-account"`
15+
ScanJobTTLSecondsAfterFinished int32 `mapstructure:"scan-job-ttl-seconds-after-finished"`
16+
ScanNamespaces []string `mapstructure:"namespaces"`
17+
ScanNamespaceExcludeRegexp *regexp.Regexp `mapstructure:"scan-namespace-exclude-regexp"`
18+
ScanNamespaceIncludeRegexp *regexp.Regexp `mapstructure:"scan-namespace-include-regexp"`
19+
ScanWorkloadResources []string `mapstructure:"scan-workload-resources"`
20+
TrivyImage string `mapstructure:"trivy-image"`
21+
TrivyCommand TrivyCommand `mapstructure:"trivy-command"`
22+
ActiveScanJobLimit int `mapstructure:"active-scan-job-limit"`
2223
}
2324

2425
type TrivyCommand string

internal/controller/stas/suite_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@ var _ = BeforeSuite(func() {
112112
k8sScheme = k8sManager.GetScheme()
113113

114114
config := config.Config{
115-
ScanJobNamespace: scanJobNamespace,
116-
ScanJobServiceAccount: "image-scanner-job",
117-
ScanInterval: time.Hour,
118-
TrivyCommand: config.RootfsTrivyCommand,
119-
TrivyImage: "aquasecurity/trivy",
115+
ScanJobNamespace: scanJobNamespace,
116+
ScanJobServiceAccount: "image-scanner-job",
117+
ScanJobTTLSecondsAfterFinished: 60,
118+
ScanInterval: time.Hour,
119+
TrivyCommand: config.RootfsTrivyCommand,
120+
TrivyImage: "aquasecurity/trivy",
120121
}
121122

122123
podReconciler := &PodReconciler{

internal/controller/stas/testdata/scan-job/expected-scan-job.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,4 @@ spec:
130130
name: image-scanner
131131
- emptyDir: {}
132132
name: tmp
133-
ttlSecondsAfterFinished: 7200 # Two hours
133+
ttlSecondsAfterFinished: 60

internal/operator/operator.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func (o Operator) BindFlags(cfg *config.Config, fs *flag.FlagSet) error {
5858
fs.Duration("scan-interval", 12*time.Hour, "The minimum time between fetch scan reports from image scanner")
5959
fs.String("scan-job-namespace", "", "The namespace to schedule scan jobs.")
6060
fs.String("scan-job-service-account", "default", "The service account used to run scan jobs.")
61+
fs.Int("scan-job-ttl-seconds-after-finished", 7200, "The lifetime (in seconds) of a scan job that has finished. Value must be positive to allow scan reports to be harvested by the operator.")
6162
fs.String("scan-workload-resources", "", "A comma-separated list of workload resources to scan. Format used for resource is \"resource.group\", i.e. \"deployments.apps\".")
6263
fs.String("scan-namespace-exclude-regexp", "^(kube-|openshift-).*", "regexp for namespace to exclude from scanning")
6364
fs.String("scan-namespace-include-regexp", "", "regexp for namespace to include for scanning")
@@ -104,6 +105,10 @@ func (o Operator) ValidateConfig(cfg config.Config) error {
104105
return fmt.Errorf("required flag (%q) or env (%q) not set", "scan-job-namespace", "SCAN_JOB_NAMESPACE")
105106
}
106107

108+
if cfg.ScanJobTTLSecondsAfterFinished <= 0 {
109+
return fmt.Errorf("flag (%q) or env (%q) must be greater than zero", "scan-job-ttl-seconds-after-finished", "SCAN_JOB_TTL_SECONDS_AFTER_FINISHED")
110+
}
111+
107112
return nil
108113
}
109114

internal/trivy/scan_job.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (f *filesystemScanJobBuilder) newImageScanJob(spec stasv1alpha1.ContainerIm
145145
job.Spec.Completions = ptr.To(int32(1))
146146
job.Spec.ActiveDeadlineSeconds = ptr.To(int64(3600))
147147
job.Spec.BackoffLimit = ptr.To(int32(3))
148-
job.Spec.TTLSecondsAfterFinished = ptr.To(int32(7200))
148+
job.Spec.TTLSecondsAfterFinished = ptr.To(f.ScanJobTTLSecondsAfterFinished)
149149
job.Spec.Template.Spec.ServiceAccountName = f.ScanJobServiceAccount
150150

151151
if len(f.preferredNodeNames) > 0 {

0 commit comments

Comments
 (0)