Skip to content

Commit 3180c77

Browse files
Merge pull request #9 from GitGuardian/ctourriere/-/split_jobs
chore(job): Refactor cronjob definition
2 parents 65fca50 + 771e73d commit 3180c77

13 files changed

+180
-38
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ An example values file that fetches from HashiCorp Vault and GitLab CI:
1818

1919
```yaml
2020
inventory:
21-
# Run every 15 minutes
22-
schedule: '*/15 * * * *'
23-
# Set to `true` to enable syncing secrets from GitGuardian into your vaults
24-
sync: false
2521
config:
2622
sources:
2723
vault-secrets:
@@ -40,6 +36,21 @@ inventory:
4036
gitguardian:
4137
endpoint: "https://your-gg-instance/v1"
4238
api_token: "${GG_API_TOKEN}"
39+
jobs:
40+
# Job to fetch defined sources
41+
fetch:
42+
# Set to `true` to enable the job
43+
enabled: false
44+
# Run every 15 minutes
45+
schedule: '*/15 * * * *'
46+
send: true
47+
# Job to be able to sync/write secrets from GitGuardian into you vault
48+
sync:
49+
# Set to `false` to disable the job
50+
enabled: true
51+
# Run every minute
52+
schedule: '* * * * *'
53+
# Set to `true` to enable sending fetched data to the GitGuardian instance
4354

4455
# This needs to be created separately, and contain the following keys:
4556
# - `HASHICORP_VAULT_TOKEN` - the hashicorp vault token to use

charts/nhi-scout/templates/_cronjob.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ metadata:
66
labels:
77
{{- include "nhi-scout.labels" . | nindent 4 }}
88
spec:
9-
schedule: {{ toJson .Values.inventory.schedule }}
9+
schedule: {{ toJson .schedule }}
1010
jobTemplate:
1111
spec:
1212
template:
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
{{- if and (ne .Values.inventory.config.gitguardian nil) (ne .Values.inventory.jobs nil) (ne .Values.inventory.jobs.fetch nil) (.Values.inventory.jobs.fetch.enabled) }}
2+
13
{{ $command := "fetch" -}}
2-
{{- if ne .Values.inventory.config.gitguardian nil }}
4+
5+
{{- if .Values.inventory.jobs.fetch.send }}
36
{{ $command = "fetch-and-send" -}}
47
{{- end }}
58

6-
{{ include "nhi-scout.cronjob" (merge (dict "cronjob_name" "inventory" "command" $command) .) -}}
9+
{{ include "nhi-scout.cronjob" (merge (dict "cronjob_name" "inventory" "command" $command "schedule" .Values.inventory.jobs.fetch.schedule) .) -}}
10+
{{- end }}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{- if and (ne .Values.inventory.config.gitguardian nil) (ne .Values.inventory.jobs nil) (ne .Values.inventory.jobs.ping nil)}}
2+
{{ include "nhi-scout.cronjob" (merge (dict "cronjob_name" "ping" "command" "ping" "schedule" .Values.inventory.jobs.ping.schedule) .) -}}
3+
{{- end }}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
{{- if .Values.inventory.sync }}
2-
{{ include "nhi-scout.cronjob" (merge (dict "cronjob_name" "sync" "command" "sync-secrets") .) -}}
1+
{{- if and (ne .Values.inventory.config.gitguardian nil) (.Values.inventory.jobs.sync.enabled) }}
2+
{{ include "nhi-scout.cronjob" (merge (dict "cronjob_name" "sync" "command" "sync-secrets" "schedule" .Values.inventory.jobs.sync.schedule) .) -}}
33
{{- end }}

charts/nhi-scout/test_values.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# yaml-language-server: $schema=values.schema.json
33

44
inventory:
5-
sync: false
65
config:
76
sources: {}
87
gitguardian: null

charts/nhi-scout/tests/base_cronjob_test.yaml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ suite: test base cronjob
44
values:
55
- ../test_values.yaml
66
templates:
7-
- cronjob_inventory.yaml
7+
- cronjob_ping.yaml
88
set:
99
inventory.config.gitguardian.api_token: "foobar"
1010
inventory.config.gitguardian.endpoint: "https://some-url.com"
@@ -19,14 +19,6 @@ tests:
1919
path: spec.jobTemplate.spec.template.spec.containers[0].image
2020
value: ghcr.io/gitguardian/gitguardian-nhi-scout/chainguard:latest
2121

22-
- it: should set schedule
23-
set:
24-
inventory.schedule: 1 * * * *
25-
asserts:
26-
- equal:
27-
path: spec.schedule
28-
value: 1 * * * *
29-
3022
- it: should set image pull secrets
3123
set:
3224
imagePullSecrets:

charts/nhi-scout/tests/inventory_cronjob_test.yaml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ values:
55
- ../test_values.yaml
66
templates:
77
- cronjob_inventory.yaml
8+
set:
9+
inventory.jobs.fetch.enabled: true
10+
inventory.config.gitguardian.api_token: "foobar"
11+
inventory.config.gitguardian.endpoint: "https://some-url.com"
812
tests:
913
- it: should work
1014
set:
@@ -18,17 +22,24 @@ tests:
1822

1923
- it: should use the gitguardian command if set
2024
set:
21-
inventory.config.gitguardian.api_token: "foobar"
22-
inventory.config.gitguardian.endpoint: "https://some-url.com"
25+
inventory.jobs.fetch.send: true
2326
asserts:
2427
- equal:
2528
path: spec.jobTemplate.spec.template.spec.containers[0].args[0]
2629
value: "fetch-and-send"
2730

2831
- it: should use the fetch command if no upload is set
2932
set:
30-
inventory.config.gitguardian: null
33+
inventory.jobs.fetch.send: false
3134
asserts:
3235
- equal:
3336
path: spec.jobTemplate.spec.template.spec.containers[0].args[0]
3437
value: "fetch"
38+
39+
- it: should be possible to modify schedule
40+
set:
41+
inventory.jobs.fetch.schedule: 2 * * * *
42+
asserts:
43+
- equal:
44+
path: spec.schedule
45+
value: 2 * * * *

charts/nhi-scout/tests/sync_cronjob_test.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ values:
66
templates:
77
- cronjob_sync.yaml
88
set:
9-
inventory.sync: true
9+
inventory.jobs.sync.enabled: true
1010
inventory.config.gitguardian.api_token: "foobar"
1111
inventory.config.gitguardian.endpoint: "https://some-url.com"
1212
tests:
@@ -33,3 +33,11 @@ tests:
3333
- containsDocument:
3434
kind: CronJob
3535
not: true
36+
37+
- it: should be possible to modify schedule
38+
set:
39+
inventory.jobs.sync.schedule: 2 * * * *
40+
asserts:
41+
- equal:
42+
path: spec.schedule
43+
value: 2 * * * *

charts/nhi-scout/values-base-schema.schema.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
"type": "object",
88
"properties": {
99
"version": {"type": "string"},
10-
"schedule": {"type": "string"},
11-
"sync": {"type": "boolean"},
1210
"log_level": {
1311
"$ref": "inventory-log-level.schema.json"
1412
},
1513
"config": {
1614
"$ref": "inventory-config.schema.json"
17-
}
15+
},
16+
"jobs": {
17+
"$ref": "jobs.schema.json"
18+
}
1819
},
1920
"required": ["config"]
2021
}

charts/nhi-scout/values.schema.json

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,12 @@
1616
"config": {
1717
"$ref": "#/definitions/inventory-config.schema.json"
1818
},
19+
"jobs": {
20+
"$ref": "#/definitions/jobs.schema.json"
21+
},
1922
"log_level": {
2023
"$ref": "#/definitions/inventory-log-level.schema.json"
2124
},
22-
"schedule": {
23-
"type": "string",
24-
"minLength": 0
25-
},
26-
"sync": {
27-
"enum": [
28-
false,
29-
true
30-
]
31-
},
3225
"version": {
3326
"type": "string",
3427
"minLength": 0
@@ -466,6 +459,68 @@
466459
"warn",
467460
"error"
468461
]
462+
},
463+
"jobs.schema.json": {
464+
"$schema": "http://json-schema.org/draft-07/schema#",
465+
"type": "object",
466+
"minProperties": 0,
467+
"properties": {
468+
"fetch": {
469+
"description": "Job that fetches data from defined sources. If send is set to `true`, send collected data to the configured GitGuardian instance.",
470+
"type": "object",
471+
"minProperties": 0,
472+
"properties": {
473+
"enabled": {
474+
"default": true,
475+
"enum": [
476+
false,
477+
true
478+
]
479+
},
480+
"schedule": {
481+
"default": "*/15 * * * *",
482+
"type": "string",
483+
"minLength": 0
484+
},
485+
"send": {
486+
"default": true,
487+
"enum": [
488+
false,
489+
true
490+
]
491+
}
492+
}
493+
},
494+
"ping": {
495+
"type": "object",
496+
"minProperties": 0,
497+
"properties": {
498+
"schedule": {
499+
"default": "* * * * *",
500+
"type": "string",
501+
"minLength": 0
502+
}
503+
}
504+
},
505+
"sync": {
506+
"type": "object",
507+
"minProperties": 0,
508+
"properties": {
509+
"enabled": {
510+
"default": false,
511+
"enum": [
512+
false,
513+
true
514+
]
515+
},
516+
"schedule": {
517+
"default": "* * * * *",
518+
"type": "string",
519+
"minLength": 0
520+
}
521+
}
522+
}
523+
}
469524
}
470525
}
471526
}

charts/nhi-scout/values.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@ inventory:
77
# Specific version of the image to use
88
version: 0.11.0
99
# Schedule to run the collection on
10-
schedule: "*/15 * * * *"
1110
log_level: info
1211
# Enable syncing secrets to vaults
13-
sync: false
12+
# sync: false
1413
# Need to add this explicitly
1514
config: null
15+
jobs:
16+
ping:
17+
schedule: "* * * * *"
18+
fetch:
19+
schedule: "*/15 * * * *"
20+
enabled: false
21+
send: false
22+
sync:
23+
schedule: "* * * * *"
24+
enabled: false
25+
1626

1727
# This sets the container image more information can be found here: https://kubernetes.io/docs/concepts/containers/images/
1828
image:

schemas/jobs.schema.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"$id": "jobs.schema.json",
5+
"minProperties": 0,
6+
"properties": {
7+
"ping": {
8+
"type": "object",
9+
"properties": {
10+
"schedule": {
11+
"type": "string",
12+
"default": "* * * * *"
13+
}
14+
}
15+
},
16+
"fetch": {
17+
"description": "Job that fetches data from defined sources. If send is set to `true`, send collected data to the configured GitGuardian instance.",
18+
"type": "object",
19+
"properties": {
20+
"schedule": {
21+
"type": "string",
22+
"default": "*/15 * * * *"
23+
},
24+
"enabled": {
25+
"type": "boolean",
26+
"default": true
27+
},
28+
"send": {
29+
"type": "boolean",
30+
"default": true
31+
}
32+
}
33+
},
34+
"sync": {
35+
"type": "object",
36+
"properties": {
37+
"schedule": {
38+
"type": "string",
39+
"default": "* * * * *"
40+
},
41+
"enabled": {
42+
"type": "boolean",
43+
"default": false
44+
}
45+
}
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)