-
Notifications
You must be signed in to change notification settings - Fork 4
/
env-config-map-ref.yaml
54 lines (54 loc) · 1.66 KB
/
env-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
apiVersion: kubevious.io/v1alpha1
kind: ClusterRule
metadata:
name: container-env-config-map-ref
spec:
summary: |
Validate ContainerSpec environment variable ConfigMap reference.
target: |
Shortcut("ContainerSpec")
categories:
- k8s
- container
- environment-variable
- config-map
- reference
rule: |
for(const env of config.spec?.env ?? [])
{
if (env.valueFrom)
{
if (env.valueFrom.configMapKeyRef)
{
if (env.valueFrom.configMapKeyRef.name)
{
const configMapItem = ApiVersion('v1')
.Kind("ConfigMap")
.name(env.valueFrom.configMapKeyRef.name)
.single()
if (!configMapItem)
{
if (!env.valueFrom.configMapKeyRef.optional)
{
error(`Could not find ConfigMap ${env.valueFrom.configMapKeyRef.name} referenced in env variable ${env.name}`);
}
}
else
{
const dataObj = configMapItem.config.data ?? {};
if (dataObj[env.valueFrom.configMapKeyRef.key] === undefined)
{
if (!env.valueFrom.configMapKeyRef.optional)
{
error(`Could not find data key ${env.valueFrom.configMapKeyRef.key} in ConfigMap ${env.valueFrom.configMapKeyRef.name} referenced in env variable ${env.name}`);
}
}
}
}
else
{
error(`ConfigMap name not set in env.valueFrom.configMapKeyRef`);
}
}
}
}