@@ -98,15 +98,28 @@ func (r *PrometheusRecorder) RecordEvaluation(decision Decision, policy api.Leve
9898 }
9999 }
100100
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 )
110123}
111124
112125func (r * PrometheusRecorder ) RecordExemption (attrs api.Attributes ) {
@@ -156,17 +169,18 @@ func operationLabel(op admissionv1.Operation) string {
156169}
157170
158171type 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
166180}
167181
168182func (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 }
170184}
171185
172186type exemptionsLabels struct {
@@ -194,7 +208,7 @@ func newEvaluationsCounter() *evaluationsCounter {
194208 Help : "Number of policy evaluations that occurred, not counting ignored or exempt requests." ,
195209 StabilityLevel : metrics .ALPHA ,
196210 },
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" },
198212 ),
199213 cache : make (map [evaluationsLabels ]metrics.CounterMetric ),
200214 }
@@ -231,8 +245,8 @@ func (c *evaluationsCounter) Reset() {
231245
232246func (c * evaluationsCounter ) populateCache () {
233247 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 : "" },
236250 }
237251 for _ , l := range labelsToCache {
238252 c .cache [l ] = c .CounterVec .WithLabelValues (l .labels ()... )
0 commit comments