Skip to content

Commit d092b94

Browse files
committed
adding docs for disk driver
Signed-off-by: Jaydip Gabani <gabanijaydip@gmail.com>
1 parent af37b42 commit d092b94

File tree

6 files changed

+125
-8
lines changed

6 files changed

+125
-8
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ MANAGER_SIDECAR_IMAGE_PATCH := "\n - --enable-violation-export=true\
140140
\n - mountPath: /tmp/violations\
141141
\n name: tmp-violations\
142142
\n - name: go-sub\
143-
\n image: fake-reader:latest\
143+
\n image: ${FAKE_READER_IMAGE}\
144144
\n imagePullPolicy: Never\
145145
\n securityContext:\
146146
\n allowPrivilegeEscalation: false\

pkg/export/disk/diskwriter.go renamed to pkg/export/disk/disk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const (
3636
)
3737

3838
const (
39-
Name = "diskwriter"
39+
Name = "disk"
4040
)
4141

4242
var Connections = &Writer{
File renamed without changes.

test/export/fake-reader/export_config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
name: audit
55
namespace: gatekeeper-system
66
data:
7-
driver: "diskwriter"
7+
driver: "disk"
88
config: |
99
{
1010
"path": "/tmp/violations",

test/export/fake-reader/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func main() {
7676

7777
// Process the read content
7878
for _, line := range lines {
79-
log.Printf("Processed line: %s\n", line)
79+
log.Printf("%s\n", line)
8080
}
8181

8282
// Release the lock

website/docs/export.md

Lines changed: 121 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ data:
4141
- `config` field is a json object that configures how the connection is made. E.g. which queue messages should be sent to.
4242

4343
#### Available drivers
44-
Dapr: https://dapr.io/
44+
45+
- Dapr: Export violations using pubsub model provided with [Dapr](https://dapr.io/)
46+
- Disk: Export violations to file system.
4547

4648
### Quick start with exporting violations using Dapr and Redis
4749

48-
#### Prerequisites
50+
#### Prerequisites for Dapr
4951

5052
1. Install Dapr
5153

@@ -130,10 +132,10 @@ Dapr: https://dapr.io/
130132
```
131133

132134
:::important
133-
Please make sure `fake-subscriber` image is built and available in your cluster. Dockerfile to build image for `fake-subscriber` is under [gatekeeper/test/fake-subscriber](https://github.com/open-policy-agent/gatekeeper/tree/master/test/export/fake-subscriber).
135+
Please make sure `fake-subscriber` image is built and available in your cluster. Dockerfile to build image for `fake-subscriber` is under [gatekeeper/test/export/fake-subscriber](https://github.com/open-policy-agent/gatekeeper/tree/master/test/export/fake-subscriber).
134136
:::
135137

136-
#### Configure Gatekeeper with Export enabled
138+
#### Configure Gatekeeper with Export enabled with Dapr
137139

138140
1. Create Gatekeeper namespace, and create Dapr pubsub component and Redis secret in Gatekeeper's namespace (`gatekeeper-system` by default). Please make sure to update `gatekeeper-system` namespace for the next steps if your cluster's Gatekeeper namespace is different.
139141

@@ -209,6 +211,121 @@ Dapr: https://dapr.io/
209211
2023/07/18 20:37:20 main.ExportMsg{ID:"2023-07-18T20:37:19Z", Details:map[string]interface {}{"missing_labels":[]interface {}{"test"}}, EventType:"violation_audited", Group:"constraints.gatekeeper.sh", Version:"v1beta1", Kind:"K8sRequiredLabels", Name:"pod-must-have-test", Namespace:"", Message:"you must provide labels: {\"test\"}", EnforcementAction:"deny", ConstraintAnnotations:map[string]string(nil), ResourceGroup:"", ResourceAPIVersion:"v1", ResourceKind:"Pod", ResourceNamespace:"nginx", ResourceName:"nginx-deployment-58899467f5-j85bs", ResourceLabels:map[string]string{"app":"nginx", "owner":"admin", "pod-template-hash":"58899467f5"}}
210212
```
211213

214+
### Quick start with exporting violations on node storage using Disk driver via emptyDir
215+
216+
#### Prerequisites for Disk driver
217+
218+
1. Build `fake-reader` image from [gatekeeper/test/export/fake-reader](https://github.com/open-policy-agent/gatekeeper/tree/master/test/export/fake-reader)
219+
220+
```bash
221+
docker buildx build -t <your_img_name:tag> --load -f test/export/fake-reader/Dockerfile test/export/fake-reader
222+
```
223+
224+
2. Update `gatekeeper-audit` deployment to add `emptyDir` volume.
225+
226+
```yaml
227+
volumes:
228+
- emptyDir: {}
229+
name: tmp-violations
230+
```
231+
232+
:::tip
233+
You can replace emptyDir to use PVC or any other types of volumes.
234+
:::
235+
236+
3. Update `gatekeeper-audit` deployment to add `volumeMount` to `manager` container.
237+
238+
```yaml
239+
volumeMounts:
240+
- mountPath: /tmp/violations
241+
name: tmp-violations
242+
```
243+
244+
4. Update `gatekeeper-audit` deployment to add a `sidecar` reader container.
245+
246+
```yaml
247+
volumeMounts:
248+
- mountPath: /tmp/violations
249+
name: tmp-violations
250+
- name: go-sub
251+
image: <your_img_name:tag>
252+
imagePullPolicy: Never
253+
securityContext:
254+
allowPrivilegeEscalation: false
255+
capabilities:
256+
drop:
257+
- ALL
258+
readOnlyRootFilesystem: true
259+
runAsGroup: 999
260+
runAsNonRoot: true
261+
runAsUser: 1000
262+
seccompProfile:
263+
type: RuntimeDefault
264+
volumeMounts:
265+
- mountPath: /tmp/violations
266+
name: tmp-violations
267+
```
268+
269+
#### Configure Gatekeeper with Export enabled to Disk
270+
271+
1. Update `gatekeeper-audit` deployment to add following flags
272+
273+
```yaml
274+
...
275+
- --enable-violation-export=true
276+
- --audit-connection=audit
277+
- --audit-channel=audit
278+
...
279+
```
280+
281+
2. Deploy Gatekeeper charts with aforementioned changes.
282+
283+
:::tip
284+
You can use below command that uses a rule defined in [Makefile](https://github.com/open-policy-agent/gatekeeper/blob/master/Makefile) to deploy gatekeeper that mounts emptyDir with sidecar reader container.
285+
286+
287+
make deploy IMG=gatekeeper-e2e:latest IMG=<gatekeeper_image> EXPORT_BACKEND=disk FAKE_READER_IMAGE=<your_reader_image>
288+
:::
289+
290+
**Note:** Verify that after the audit pod is running there is a Dapr sidecar injected and running along side `manager` container.
291+
292+
3. Create connection config to establish a connection.
293+
294+
```shell
295+
kubectl apply -f - <<EOF
296+
apiVersion: v1
297+
kind: ConfigMap
298+
metadata:
299+
name: audit
300+
namespace: gatekeeper-system
301+
data:
302+
driver: "disk"
303+
config: |
304+
{
305+
"path": "/tmp/violations",
306+
"maxAuditResults": 3
307+
}
308+
309+
EOF
310+
```
311+
312+
**Note:** Name of the connection configMap must match the value of `--audit-connection` for it to be used by audit to export violation. At the moment, only one connection config can exists for audit.
313+
314+
4. Create the constraint templates and constraints, and make sure audit ran by checking constraints. If constraint status is updated with information such as `auditTimeStamp` or `totalViolations`, then audit has ran at least once. Additionally, populated `TOTAL-VIOLATIONS` field for all constraints while listing constraints also indicates that audit has ran at least once.
315+
316+
```log
317+
kubectl get constraint
318+
NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS
319+
pod-must-have-test 0
320+
```
321+
322+
5. Finally, check the sidecar reader logs to see the violations written.
323+
324+
```log
325+
kubectl logs -l gatekeeper.sh/operation=audit -c go-sub -n gatekeeper-system
326+
2025/03/05 00:37:16 {"id":"2025-03-05T00:37:13Z","details":{"missing_labels":["test"]},"eventType":"violation_audited","group":"constraints.gatekeeper.sh","version":"v1beta1","kind":"K8sRequiredLabels","name":"pod-must-have-test","message":"you must provide labels: {\"test\"}","enforcementAction":"deny","resourceAPIVersion":"v1","resourceKind":"Pod","resourceNamespace":"nginx","resourceName":"nginx-deployment-2-79479fc6db-7qbnm","resourceLabels":{"app":"nginx-ingress","app.kubernetes.io/component":"controller","pod-template-hash":"79479fc6db"}}
327+
```
328+
212329
### Violations
213330

214331
The audit pod exports violations in following format:

0 commit comments

Comments
 (0)