-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
30 lines (26 loc) · 1.08 KB
/
helper.go
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
package util
import (
"strings"
apierrs "k8s.io/kubernetes/pkg/api/errors"
)
// errMessageString is a part of error message copied from quotaAdmission.Admit() method in
// k8s.io/kubernetes/plugin/pkg/admission/resourcequota/admission.go module
const errQuotaMessageString = `exceeded quota:`
const errQuotaUnknownMessageString = `status unknown for quota:`
const errLimitsMessageString = `exceeds the maximum limit`
// IsErrorQuotaExceeded returns true if the given error stands for a denied request caused by detected quota
// abuse.
func IsErrorQuotaExceeded(err error) bool {
if isForbidden := apierrs.IsForbidden(err); isForbidden || apierrs.IsInvalid(err) {
lowered := strings.ToLower(err.Error())
// the limit error message can be accompanied only by Invalid reason
if strings.Contains(lowered, errLimitsMessageString) {
return true
}
// the quota error message can be accompanied only by Forbidden reason
if isForbidden && (strings.Contains(lowered, errQuotaMessageString) || strings.Contains(lowered, errQuotaUnknownMessageString)) {
return true
}
}
return false
}