Skip to content

Commit f94dd6f

Browse files
Merge pull request #611 from snyk/RUN-1250/draft
feat:support storage in PVC
2 parents 34401e6 + 42998a2 commit f94dd6f

File tree

9 files changed

+114
-4
lines changed

9 files changed

+114
-4
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ To lower `snyk-monitor`'s logging verbosity `log_level` value could be set to on
148148

149149
By default, `log_level` is `'INFO'`.
150150

151+
## Using a PVC ##
152+
153+
By default, `snyk-monitor` uses an emptyDir for temporary storage. If you prefer to have a PVC that uses a statically or
154+
dynamically provisioned PV that you have created, then set the following value
155+
* `pvc.enabled` `true`
156+
157+
The PVC's name defaults to `snyk-monitor-pvc`. If you prefer to override this, then use the following value:
158+
* `pvc.name`
159+
151160
## Terms and conditions ##
152161

153162
*The Snyk Container Kubernetes integration uses Red Hat UBI (Universal Base Image).*

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"aws-sdk": "^2.633.0",
4141
"bunyan": "^1.8.13",
4242
"child-process-promise": "^2.2.1",
43+
"fs-extra": "^9.0.1",
4344
"lru-cache": "^5.1.1",
4445
"needle": "^2.5.0",
4546
"sleep-promise": "^8.0.1",

snyk-monitor/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ helm upgrade --install snyk-monitor snyk-charts/snyk-monitor \
136136
--set log_level="WARN"
137137
```
138138

139+
## Using a PVC ##
140+
141+
By default, `snyk-monitor` uses an emptyDir for temporary storage. If you prefer to have a PVC that uses a statically or
142+
dynamically provisioned PV that you have created, then set the following value
143+
* `pvc.enabled` `true`
144+
145+
The PVC's name defaults to `snyk-monitor-pvc`. If you prefer to override this, then use the following value:
146+
* `pvc.name`
147+
139148
## PodSecurityPolicies
140149
**This should not be used when installing on OpenShift.**
141150

snyk-monitor/templates/deployment.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ spec:
2020
spec:
2121
serviceAccountName: {{ include "snyk-monitor.name" . }}
2222
restartPolicy: Always
23+
initContainers:
24+
- name: volume-permissions
25+
image: "{{ .Values.initContainerImage.repository }}:{{ .Values.initContainerImage.tag }}"
26+
command : ['sh', '-c', 'chmod -R 777 /var/tmp']
27+
volumeMounts:
28+
- name: temporary-storage
29+
mountPath: "/var/tmp"
2330
containers:
2431
- name: {{ include "snyk-monitor.name" . }}
2532
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
@@ -83,8 +90,13 @@ spec:
8390
- key: dockercfg.json
8491
path: config.json
8592
- name: temporary-storage
93+
{{- if .Values.pvc.enabled }}
94+
persistentVolumeClaim:
95+
claimName: {{ .Values.pvc.name }}
96+
{{- else }}
8697
emptyDir:
8798
sizeLimit: {{ .Values.temporaryStorageSize }}
99+
{{- end }}
88100
- name: ssl-certs
89101
configMap:
90102
name: {{ .Values.certsConfigMap }}

snyk-monitor/templates/pvc.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{ if .Values.pvc.enabled }}
2+
kind: PersistentVolumeClaim
3+
apiVersion: v1
4+
metadata:
5+
name: {{ .Values.pvc.name }}
6+
spec:
7+
accessModes:
8+
- ReadWriteOnce
9+
volumeMode: Filesystem
10+
resources:
11+
requests:
12+
storage: {{ .Values.temporaryStorageSize }}
13+
{{ end }}

snyk-monitor/values.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,24 @@ image:
2222
tag: IMAGE_TAG_OVERRIDE_WHEN_PUBLISHING
2323
pullPolicy: Always
2424

25+
# If deploying in an air-gapped environment that can't pull from DockerHub, override the initContainer's image here for one that is accessible to your environment.
26+
initContainerImage:
27+
repository: busybox
28+
tag: latest
29+
2530
# The snyk-monitor requires knowing the cluster name so that it can organise
2631
# scanned workloads. The Kubernetes API does not provide an API to query this.
2732
# Set the name of the cluster, otherwise the snyk-monitor will set this to a default value.
2833
clusterName: ""
2934

3035
# The snyk-monitor requires disk storage to temporarily pull container images and to scan them for vulnerabilities.
31-
# This value controls how much disk storage _at most_ may be allocated for the snyk-monitor. The snyk-monitor mounts an emptyDir for storage.
32-
temporaryStorageSize: 50Gi
36+
# This value controls how much disk storage _at most_ may be allocated for the snyk-monitor. Unless overridden by the `pvc` value, the snyk-monitor mounts an emptyDir for storage.
37+
temporaryStorageSize: 50Gi # Applies to PVC too
38+
39+
# Change to true to use a PVC instead of emptyDir for local storage
40+
pvc:
41+
enabled: false
42+
name: 'snyk-monitor-pvc'
3343

3444
# CPU/Mem requests and limits for snyk-monitor
3545
requests:

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import { emptyDirSync } from 'fs-extra';
2+
13
import * as SourceMapSupport from 'source-map-support';
24

35
import * as state from './state';
6+
import * as config from './common/config';
47
import logger = require('./common/logger');
58
import { currentClusterName } from './supervisor/cluster';
69
import { beginWatchingWorkloads } from './supervisor/watchers';
@@ -33,6 +36,16 @@ process.on('unhandledRejection', (reason) => {
3336
}
3437
});
3538

39+
function cleanUpTempStorage() {
40+
const { IMAGE_STORAGE_ROOT } = config;
41+
try {
42+
emptyDirSync(IMAGE_STORAGE_ROOT);
43+
logger.info({}, 'Cleaned temp storage');
44+
} catch (err) {
45+
logger.error({ err }, 'Error deleting files');
46+
}
47+
};
48+
3649
function monitor(): void {
3750
try {
3851
logger.info({cluster: currentClusterName}, 'starting to monitor');
@@ -44,4 +57,5 @@ function monitor(): void {
4457
}
4558

4659
SourceMapSupport.install();
60+
cleanUpTempStorage();
4761
monitor();

test/system/kind.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as sinon from 'sinon';
2+
import * as fsExtra from 'fs-extra';
13
import * as tap from 'tap';
24
import * as nock from 'nock';
35
import * as sleep from 'sleep-promise';
@@ -21,6 +23,8 @@ tap.tearDown(tearDown);
2123

2224
tap.test('Kubernetes-Monitor with KinD', async (t) => {
2325

26+
const emptyDirSyncStub = sinon.stub(fsExtra, 'emptyDirSync').returns({});
27+
2428
// Start fresh
2529
try {
2630
await tearDown();
@@ -176,6 +180,8 @@ tap.test('Kubernetes-Monitor with KinD', async (t) => {
176180
// Start the monitor
177181
require('../../src');
178182

183+
sinon.assert.called(emptyDirSyncStub);
184+
179185
// TODO: replace with being event driven?
180186
// will still need SOME timeout
181187
while (true) {

0 commit comments

Comments
 (0)