-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de41e97
commit b788c70
Showing
6 changed files
with
118 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
cache: | ||
cleanUpIntervalSecond: 10 | ||
entryTtlSecond: 5 | ||
ingressClasses: | ||
- "private" | ||
- "inter-venture" | ||
- "inter-dc" | ||
- "public" | ||
webhook: | ||
port: 8443 | ||
tlsCertFile: "testbin/tls.crt" | ||
tlsKeyFile: "testbin/tls.key" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package webhook | ||
|
||
import ( | ||
"github.com/snapp-incubator/contour-global-ratelimit-operator/pkg/rlsparser" | ||
admissionv1 "k8s.io/api/admission/v1" | ||
) | ||
|
||
type rlsValidator struct { | ||
next checker | ||
} | ||
|
||
func (e *rlsValidator) check(checkrequest *checkRequest) (*admissionv1.AdmissionResponse, *httpErr) { | ||
// check if there is any error in parsing rls configs in HTTPProxy Object | ||
_, _, err := rlsparser.ParseGlobalRateLimit(checkrequest.newObj) | ||
if err != nil { | ||
return acceptWithWarning(err.Error()) | ||
} | ||
|
||
if e.next != nil { | ||
return e.next.check(checkrequest) | ||
} | ||
|
||
return &admissionv1.AdmissionResponse{Allowed: true}, nil | ||
} | ||
|
||
func (e *rlsValidator) setNext(c checker) { | ||
e.next = c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package webhook | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
admissionv1 "k8s.io/api/admission/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func validateWariningRules(response *admissionv1.AdmissionResponse, err *httpErr, request *checkRequest, checkers ...checker) (*admissionv1.AdmissionResponse, *httpErr) { | ||
//If the request is rejected during rule checking, then do not process the warning rules. | ||
if !response.Allowed { | ||
return response, err | ||
} | ||
|
||
msg := make([]string, 0) | ||
// Combine all warining msg together | ||
for _, c := range checkers { | ||
resp, _ := c.check(request) | ||
if len(resp.Warnings) > 0 { | ||
msg = append(msg, resp.Warnings...) | ||
} | ||
} | ||
|
||
if wariningResponseCount := len(msg); wariningResponseCount == 0 { | ||
//There isn't any warning | ||
return &admissionv1.AdmissionResponse{Allowed: true}, nil | ||
} | ||
//return all warnings | ||
return &admissionv1.AdmissionResponse{Allowed: true, Warnings: msg, Result: &metav1.Status{ | ||
Code: http.StatusAccepted, | ||
Message: fmt.Sprint(msg), | ||
}}, nil | ||
} | ||
|
||
func acceptWithWarning(message string) (*admissionv1.AdmissionResponse, *httpErr) { | ||
message = fmt.Sprint("Rate Limit Config Error: ", message) | ||
|
||
messageMaxLenth := 120 | ||
if lenMsg := len(message); lenMsg < 120 { | ||
messageMaxLenth = lenMsg | ||
} | ||
|
||
return &admissionv1.AdmissionResponse{Allowed: true, Warnings: []string{message[:messageMaxLenth]}, Result: &metav1.Status{ | ||
Code: http.StatusAccepted, | ||
Message: message, | ||
}}, nil | ||
} |