Skip to content

Commit cbf797a

Browse files
authored
node-exporter: only render clusterrole if PSP is enabled (#203)
1 parent 9abc264 commit cbf797a

File tree

7 files changed

+94
-18
lines changed

7 files changed

+94
-18
lines changed

charts/sourcegraph/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ In addition to the documented values, all services also support the following va
192192
| nodeExporter.podSecurityContext | object | `{"fsGroup":65534,"runAsGroup":65534,"runAsNonRoot":true,"runAsUser":65534}` | Security context for the `node-exporter` pod, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod) |
193193
| nodeExporter.podSecurityPolicy.enabled | bool | `false` | Enable [PodSecurityPolicy](https://kubernetes.io/docs/concepts/policy/pod-security-policy/) for `node-exporter` pods |
194194
| nodeExporter.resources | object | `{"limits":{"cpu":"1","memory":"1Gi"},"requests":{"cpu":".2","memory":"100Mi"}}` | Resource requests & limits for the `node-exporter` container, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/) |
195-
| nodeExporter.serviceAccount.create | bool | `true` | Enable creation of ServiceAccount for `node-exporter` |
195+
| nodeExporter.serviceAccount.create | bool | `false` | Enable creation of ServiceAccount for `node-exporter` |
196196
| nodeExporter.serviceAccount.name | string | `"node-exporter"` | Name of the ServiceAccount to be created or an existing ServiceAccount |
197197
| openTelemetry.agent.name | string | `"otel-agent"` | Name used by resources. Does not affect service names or PVCs. |
198198
| openTelemetry.agent.resources | object | `{"limits":{"cpu":"500m","memory":"500Mi"},"requests":{"cpu":"100m","memory":"100Mi"}}` | Resource requests & limits for the `otel-agent` container, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/) |

charts/sourcegraph/templates/node-exporter/node-exporter.ClusterRole.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if .Values.nodeExporter.enabled -}}
1+
{{- if and .Values.nodeExporter.enabled .Values.nodeExporter.podSecurityPolicy.enabled -}}
22
kind: ClusterRole
33
apiVersion: rbac.authorization.k8s.io/v1
44
metadata:

charts/sourcegraph/templates/node-exporter/node-exporter.ClusterRoleBinding.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if .Values.nodeExporter.enabled -}}
1+
{{- if and .Values.nodeExporter.enabled .Values.nodeExporter.podSecurityPolicy.enabled -}}
22
kind: ClusterRoleBinding
33
apiVersion: rbac.authorization.k8s.io/v1
44
metadata:

charts/sourcegraph/templates/node-exporter/node-exporter.DaemonSet.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ spec:
4141
deploy: sourcegraph
4242
app: node-exporter
4343
spec:
44+
{{- if .Values.nodeExporter.serviceAccount.create }}
4445
{{- include "sourcegraph.renderServiceAccountName" (list . "nodeExporter") | trim | nindent 6 }}
46+
{{- end }}
4547
containers:
4648
- name: node-exporter
4749
image: {{ include "sourcegraph.image" (list . "nodeExporter" ) }}

charts/sourcegraph/templates/node-exporter/node-exporter.PodSecurityPolicy.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{{- if and .Values.nodeExporter.enabled .Values.nodeExporter.podSecurityPolicy.enabled -}}
2+
3+
{{- if not .Values.nodeExporter.serviceAccount.create -}}
4+
{{ fail "Node Exporter's service account must be enabled in order to use its pod security policy (set 'nodeExporter.ServiceAccount.create' to true)" }}
5+
{{- end -}}
6+
27
apiVersion: policy/v1beta1
38
kind: PodSecurityPolicy
49
metadata:

charts/sourcegraph/tests/nodeExporter_test.yaml

Lines changed: 83 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ templates:
88
- node-exporter/node-exporter.Service.yaml
99
- node-exporter/node-exporter.ServiceAccount.yaml
1010
tests:
11-
- it: should render the DaemonSet, Service, and ClusterRoles if node-exporter is enabled
11+
- it: should render the DaemonSet and Service if node-exporter is enabled
1212
set:
1313
nodeExporter:
1414
enabled: true
@@ -23,16 +23,6 @@ tests:
2323
apiVersion: v1
2424
name: node-exporter
2525
template: node-exporter/node-exporter.Service.yaml
26-
- containsDocument:
27-
kind: ClusterRole
28-
apiVersion: rbac.authorization.k8s.io/v1
29-
name: node-exporter
30-
template: node-exporter/node-exporter.ClusterRole.yaml
31-
- containsDocument:
32-
kind: ClusterRoleBinding
33-
apiVersion: rbac.authorization.k8s.io/v1
34-
name: node-exporter
35-
template: node-exporter/node-exporter.ClusterRoleBinding.yaml
3626

3727
- it: should not render any resources if node-exporter is disabled
3828
set:
@@ -49,9 +39,26 @@ tests:
4939
- node-exporter/node-exporter.Service.yaml
5040
- node-exporter/node-exporter.ServiceAccount.yaml
5141

52-
- it: should render the podSecurityPolicy if enabled
42+
- it: should not render the PodSecurityPolicy, Service Accounts, and ClusterRoles by default
43+
asserts:
44+
- hasDocuments:
45+
count: 0
46+
template: node-exporter/node-exporter.PodSecurityPolicy.yaml
47+
- hasDocuments:
48+
count: 0
49+
template: node-exporter/node-exporter.ClusterRole.yaml
50+
- hasDocuments:
51+
count: 0
52+
template: node-exporter/node-exporter.ClusterRoleBinding.yaml
53+
- hasDocuments:
54+
count: 0
55+
template: node-exporter/node-exporter.ServiceAccount.yaml
56+
57+
- it: should render the PodSecurityPolicy, ServiceAccounts, and ClusterRoles if PodSecurityPolicy + Service Accounts are enabled
5358
set:
5459
nodeExporter:
60+
serviceAccount:
61+
create: true
5562
podSecurityPolicy:
5663
enabled: true
5764
asserts:
@@ -60,6 +67,56 @@ tests:
6067
apiVersion: policy/v1beta1
6168
name: node-exporter
6269
template: node-exporter/node-exporter.PodSecurityPolicy.yaml
70+
- containsDocument:
71+
kind: ServiceAccount
72+
apiVersion: v1
73+
name: node-exporter
74+
template: node-exporter/node-exporter.ServiceAccount.yaml
75+
- containsDocument:
76+
kind: ClusterRole
77+
apiVersion: rbac.authorization.k8s.io/v1
78+
name: node-exporter
79+
template: node-exporter/node-exporter.ClusterRole.yaml
80+
- containsDocument:
81+
kind: ClusterRoleBinding
82+
apiVersion: rbac.authorization.k8s.io/v1
83+
name: node-exporter
84+
template: node-exporter/node-exporter.ClusterRoleBinding.yaml
85+
86+
- it: should add the ServiceAccount name to the DaemonSet spec if the ServiceAccount is enabled
87+
set:
88+
nodeExporter:
89+
serviceAccount:
90+
create: true
91+
name: "test-service-account-name"
92+
asserts:
93+
- equal:
94+
path: spec.template.spec.serviceAccountName
95+
value: "test-service-account-name"
96+
template: node-exporter/node-exporter.DaemonSet.yaml
97+
98+
- it: should add not the ServiceAccount name to the DaemonSet spec if the ServiceAccount is disabled
99+
set:
100+
nodeExporter:
101+
serviceAccount:
102+
create: false
103+
name: "test-service-account-name"
104+
asserts:
105+
- isEmpty:
106+
path: spec.template.spec.serviceAccountName
107+
template: node-exporter/node-exporter.DaemonSet.yaml
108+
109+
- it: should fail to render if the PodSecurityPolicy is enabled, but the ServiceAccount isn't
110+
set:
111+
nodeExporter:
112+
serviceAccount:
113+
create: false
114+
podSecurityPolicy:
115+
enabled: true
116+
asserts:
117+
- failedTemplate:
118+
errorMessage: "Node Exporter's service account must be enabled in order to use its pod security policy (set 'nodeExporter.ServiceAccount.create' to true)"
119+
template: node-exporter/node-exporter.PodSecurityPolicy.yaml
63120

64121
- it: should not render the podSecurityPolicy if disabled
65122
set:
@@ -74,6 +131,12 @@ tests:
74131
- it: should ensure that the namespace is properly propagated to the cluster role binding
75132
release:
76133
namespace: "my-test-namespace"
134+
set:
135+
nodeExporter:
136+
serviceAccount:
137+
create: true
138+
podSecurityPolicy:
139+
enabled: true
77140
asserts:
78141
- equal:
79142
path: subjects[0].namespace
@@ -83,7 +146,10 @@ tests:
83146
- it: should have host Network and PID enabled by default
84147
set:
85148
nodeExporter:
86-
podSecurityPolicy: # (unrelated to host network/pid defaults, just for ensuring that PodSecurityPolicy gets rendered so that we can check them in same test)
149+
# (these settings are unrelated to host network/pid defaults, they're just for ensuring that PodSecurityPolicy gets rendered so that we can check them in same test)
150+
serviceAccount:
151+
create: true
152+
podSecurityPolicy:
87153
enabled: true
88154
asserts:
89155
- equal:
@@ -108,7 +174,10 @@ tests:
108174
nodeExporter:
109175
hostNetwork: false
110176
hostPID: false
111-
podSecurityPolicy: # (unrelated to host network/pid settings, just for ensuring that PodSecurityPolicy gets rendered so that we can check them in same test)
177+
# (these settings are unrelated to host network/pid defaults, they're just for ensuring that PodSecurityPolicy gets rendered so that we can check them in same test)
178+
serviceAccount:
179+
create: true
180+
podSecurityPolicy:
112181
enabled: true
113182
asserts:
114183
- equal:

charts/sourcegraph/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ nodeExporter:
686686
memory: 100Mi
687687
serviceAccount:
688688
# -- Enable creation of ServiceAccount for `node-exporter`
689-
create: true
689+
create: false
690690
# -- Name of the ServiceAccount to be created or an existing ServiceAccount
691691
name: node-exporter
692692
# Share the host process ID namespace.

0 commit comments

Comments
 (0)