Skip to content

Commit

Permalink
fix(metrics): double counting injection failures
Browse files Browse the repository at this point in the history
Fix injection counting logic in `Handle()` to address a potential
double counting of an injection failure. Prior to this change, if
`Mutate()` returned an error, an injection failure would be counted
immediately. Then, if the response could not be serialized or written
back, a second failure would be counted.
  • Loading branch information
gsantos-hc committed Nov 18, 2024
1 parent 3560708 commit a2ec614
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions agent-inject/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,6 @@ func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) {
admResp.Response = h.Mutate(admReq.Request)
}

if !admResp.Response.Allowed {
incrementInjectionFailures(admReq.Request.Namespace)
}

// Default to a v1 AdmissionReview, otherwise the API server may not recognize the request
// if multiple AdmissionReview versions are permitted by the webhook config.
if actualAdmRevGVK == nil || *actualAdmRevGVK == (schema.GroupVersionKind{}) {
Expand All @@ -162,8 +158,13 @@ func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write(resp); err != nil {
h.Log.Error("error writing response", "Error", err)
incrementInjectionFailures(admReq.Request.Namespace)
} else {
return
}

if admResp.Response.Allowed {
incrementInjections(admReq.Request.Namespace)
} else {
incrementInjectionFailures(admReq.Request.Namespace)
}
}

Expand Down

0 comments on commit a2ec614

Please sign in to comment.