@@ -98,15 +98,28 @@ func (r *PrometheusRecorder) RecordEvaluation(decision Decision, policy api.Leve
98
98
}
99
99
}
100
100
101
- r .evaluationsCounter .CachedInc (evaluationsLabels {
102
- decision : string (decision ),
103
- level : string (policy .Level ),
104
- version : version ,
105
- mode : string (evalMode ),
106
- operation : operationLabel (attrs .GetOperation ()),
107
- resource : resourceLabel (attrs .GetResource ()),
108
- subresource : attrs .GetSubresource (),
109
- })
101
+ // prevent cardinality explosion by only recording the platform namespaces
102
+ namespace := attrs .GetNamespace ()
103
+ if ! (namespace == "openshift" ||
104
+ strings .HasPrefix (namespace , "openshift-" ) ||
105
+ strings .HasPrefix (namespace , "kube-" ) ||
106
+ namespace == "default" ) {
107
+ // remove non-OpenShift platform namespace names to prevent cardinality explosion
108
+ namespace = ""
109
+ }
110
+
111
+ el := evaluationsLabels {
112
+ decision : string (decision ),
113
+ level : string (policy .Level ),
114
+ version : version ,
115
+ mode : string (evalMode ),
116
+ operation : operationLabel (attrs .GetOperation ()),
117
+ resource : resourceLabel (attrs .GetResource ()),
118
+ subresource : attrs .GetSubresource (),
119
+ ocpNamespace : namespace ,
120
+ }
121
+
122
+ r .evaluationsCounter .CachedInc (el )
110
123
}
111
124
112
125
func (r * PrometheusRecorder ) RecordExemption (attrs api.Attributes ) {
@@ -156,17 +169,18 @@ func operationLabel(op admissionv1.Operation) string {
156
169
}
157
170
158
171
type evaluationsLabels struct {
159
- decision string
160
- level string
161
- version string
162
- mode string
163
- operation string
164
- resource string
165
- subresource string
172
+ decision string
173
+ level string
174
+ version string
175
+ mode string
176
+ operation string
177
+ resource string
178
+ subresource string
179
+ ocpNamespace string
166
180
}
167
181
168
182
func (l * evaluationsLabels ) labels () []string {
169
- return []string {l .decision , l .level , l .version , l .mode , l .operation , l .resource , l .subresource }
183
+ return []string {l .decision , l .level , l .version , l .mode , l .operation , l .resource , l .subresource , l . ocpNamespace }
170
184
}
171
185
172
186
type exemptionsLabels struct {
@@ -194,7 +208,7 @@ func newEvaluationsCounter() *evaluationsCounter {
194
208
Help : "Number of policy evaluations that occurred, not counting ignored or exempt requests." ,
195
209
StabilityLevel : metrics .ALPHA ,
196
210
},
197
- []string {"decision" , "policy_level" , "policy_version" , "mode" , "request_operation" , "resource" , "subresource" },
211
+ []string {"decision" , "policy_level" , "policy_version" , "mode" , "request_operation" , "resource" , "subresource" , "ocp_namespace" },
198
212
),
199
213
cache : make (map [evaluationsLabels ]metrics.CounterMetric ),
200
214
}
@@ -231,8 +245,8 @@ func (c *evaluationsCounter) Reset() {
231
245
232
246
func (c * evaluationsCounter ) populateCache () {
233
247
labelsToCache := []evaluationsLabels {
234
- {decision : "allow" , level : "privileged" , version : "latest" , mode : "enforce" , operation : "create" , resource : "pod" , subresource : "" },
235
- {decision : "allow" , level : "privileged" , version : "latest" , mode : "enforce" , operation : "update" , resource : "pod" , subresource : "" },
248
+ {decision : "allow" , level : "privileged" , version : "latest" , mode : "enforce" , operation : "create" , resource : "pod" , subresource : "" , ocpNamespace : "" },
249
+ {decision : "allow" , level : "privileged" , version : "latest" , mode : "enforce" , operation : "update" , resource : "pod" , subresource : "" , ocpNamespace : "" },
236
250
}
237
251
for _ , l := range labelsToCache {
238
252
c .cache [l ] = c .CounterVec .WithLabelValues (l .labels ()... )
0 commit comments