New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add admission metrics for webhooks #55183
Changes from all commits
3940e4f
9d13d1b
2643c6a
375e2d0
369fd81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,22 +16,46 @@ limitations under the License. | |||||
|
||||||
package admission | ||||||
|
||||||
// chainAdmissionHandler is an instance of admission.Interface that performs admission control using a chain of admission handlers | ||||||
type chainAdmissionHandler []Interface | ||||||
import "time" | ||||||
|
||||||
// chainAdmissionHandler is an instance of admission.NamedHandler that performs admission control using | ||||||
// a chain of admission handlers | ||||||
type chainAdmissionHandler []NamedHandler | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jpbetz This type is a union type of You can do this by creating an |
||||||
|
||||||
// NewChainHandler creates a new chain handler from an array of handlers. Used for testing. | ||||||
func NewChainHandler(handlers ...Interface) chainAdmissionHandler { | ||||||
func NewChainHandler(handlers ...NamedHandler) chainAdmissionHandler { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you'll need to update a couple unit tests:
(See https://github.com/kubernetes/kubernetes/pull/55086/files#diff-97778d709e3ef8a841d3749d2c16072a) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @tallclair! I've fixed the tests, making heavy use of the work you did for #55086. |
||||||
return chainAdmissionHandler(handlers) | ||||||
} | ||||||
|
||||||
func NewNamedHandler(name string, i Interface) NamedHandler { | ||||||
return &pluginHandler{ | ||||||
i: i, | ||||||
name: name, | ||||||
} | ||||||
} | ||||||
|
||||||
const ( | ||||||
stepValidate = "validate" | ||||||
stepAdmit = "admit" | ||||||
) | ||||||
|
||||||
// Admit performs an admission control check using a chain of handlers, and returns immediately on first error | ||||||
func (admissionHandler chainAdmissionHandler) Admit(a Attributes) error { | ||||||
start := time.Now() | ||||||
err := admissionHandler.admit(a) | ||||||
Metrics.ObserveAdmissionStep(time.Since(start), err != nil, a, stepAdmit) | ||||||
return err | ||||||
} | ||||||
|
||||||
func (admissionHandler chainAdmissionHandler) admit(a Attributes) error { | ||||||
for _, handler := range admissionHandler { | ||||||
if !handler.Handles(a.GetOperation()) { | ||||||
if !handler.Interface().Handles(a.GetOperation()) { | ||||||
continue | ||||||
} | ||||||
if mutator, ok := handler.(MutationInterface); ok { | ||||||
if mutator, ok := handler.Interface().(MutationInterface); ok { | ||||||
t := time.Now() | ||||||
err := mutator.Admit(a) | ||||||
Metrics.ObserveAdmissionController(time.Since(t), err != nil, handler, a, stepAdmit) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
@@ -42,12 +66,21 @@ func (admissionHandler chainAdmissionHandler) Admit(a Attributes) error { | |||||
|
||||||
// Validate performs an admission control check using a chain of handlers, and returns immediately on first error | ||||||
func (admissionHandler chainAdmissionHandler) Validate(a Attributes) error { | ||||||
start := time.Now() | ||||||
err := admissionHandler.validate(a) | ||||||
Metrics.ObserveAdmissionStep(time.Since(start), err != nil, a, stepValidate) | ||||||
return err | ||||||
} | ||||||
|
||||||
func (admissionHandler chainAdmissionHandler) validate(a Attributes) (err error) { | ||||||
for _, handler := range admissionHandler { | ||||||
if !handler.Handles(a.GetOperation()) { | ||||||
if !handler.Interface().Handles(a.GetOperation()) { | ||||||
continue | ||||||
} | ||||||
if validator, ok := handler.(ValidationInterface); ok { | ||||||
if validator, ok := handler.Interface().(ValidationInterface); ok { | ||||||
t := time.Now() | ||||||
err := validator.Validate(a) | ||||||
Metrics.ObserveAdmissionController(time.Since(t), err != nil, handler, a, stepValidate) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
@@ -59,7 +92,7 @@ func (admissionHandler chainAdmissionHandler) Validate(a Attributes) error { | |||||
// Handles will return true if any of the handlers handles the given operation | ||||||
func (admissionHandler chainAdmissionHandler) Handles(operation Operation) bool { | ||||||
for _, handler := range admissionHandler { | ||||||
if handler.Handles(operation) { | ||||||
if handler.Interface().Handles(operation) { | ||||||
return true | ||||||
} | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jpbetz This check isn't the same as the
Admit
call that was previously done. You need a followup to restore the test.