-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvolume-config-map-ref.yaml
65 lines (64 loc) · 1.61 KB
/
volume-config-map-ref.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
apiVersion: kubevious.io/v1alpha1
kind: ClusterRule
metadata:
name: pod-spec-volume-config-map-ref
spec:
summary: |
Validate PodSpec volume mount ConfigMap reference.
target: |
Shortcut("PodSpec")
categories:
- k8s
- pod-spec
- config-map
- volume
- reference
rule: |
for(const volume of config.spec?.volumes ?? [])
{
if (volume.configMap)
{
checkConfigMapRef(volume.name, volume.configMap);
}
else if (volume.projected)
{
for(const source of volume.projected.sources)
{
if (source.configMap)
{
checkConfigMapRef(volume.name, source.configMap);
}
}
}
}
function checkConfigMapRef(volumeName, configMapRef)
{
const configMapItem = ApiVersion('v1')
.Kind("ConfigMap")
.name(configMapRef.name)
.single()
if (!configMapItem)
{
if (!configMapRef.optional)
{
error(`Could not find ConfigMap ${configMapRef.name} referenced in volume ${volumeName}.`);
}
}
else
{
if (configMapRef.items)
{
const dataObj = configMapItem.config.data ?? {};
for(const item of configMapRef.items)
{
if (!dataObj[item.key])
{
if (!configMapRef.optional)
{
error(`Could not find data key ${item.key} in ConfigMap ${configMapRef.name} referenced in volume ${volumeName}.`);
}
}
}
}
}
}