#55183 follow up: Reinstate admission chain composition and ns test

pull/6/head
Joe Betz 2017-11-16 21:20:14 -08:00 committed by Dr. Stefan Schimanski
parent 142579c16d
commit d82ae45a4c
6 changed files with 28 additions and 25 deletions

View File

@ -129,15 +129,19 @@ func TestAdmissionNamespaceExists(t *testing.T) {
// TestIgnoreAdmission validates that a request is ignored if its not a create
func TestIgnoreAdmission(t *testing.T) {
namespace := "test"
mockClient := newMockClientForTest([]string{})
handler, informerFactory, err := newHandlerForTest(mockClient)
if err != nil {
t.Errorf("unexpected error initializing handler: %v", err)
}
informerFactory.Start(wait.NeverStop)
chainHandler := admission.NewChainHandler(admission.NewNamedHandler("ns", handler))
if handler.Handles(admission.Update) {
t.Errorf("expected not to handle Update")
pod := newPod(namespace)
err = chainHandler.Admit(admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), pod.Namespace, pod.Name, api.Resource("pods").WithVersion("version"), "", admission.Update, nil))
if err != nil {
t.Errorf("unexpected error returned from admission handler")
}
if hasCreateNamespaceAction(mockClient) {
t.Errorf("unexpected create namespace action")

View File

@ -29,8 +29,8 @@ func NewChainHandler(handlers ...NamedHandler) chainAdmissionHandler {
func NewNamedHandler(name string, i Interface) NamedHandler {
return &pluginHandler{
i: i,
name: name,
Interface: i,
name: name,
}
}
@ -49,10 +49,10 @@ func (admissionHandler chainAdmissionHandler) Admit(a Attributes) error {
func (admissionHandler chainAdmissionHandler) admit(a Attributes) error {
for _, handler := range admissionHandler {
if !handler.Interface().Handles(a.GetOperation()) {
if !handler.Handles(a.GetOperation()) {
continue
}
if mutator, ok := handler.Interface().(MutationInterface); ok {
if mutator, ok := handler.(MutationInterface); ok {
t := time.Now()
err := mutator.Admit(a)
Metrics.ObserveAdmissionController(time.Since(t), err != nil, handler, a, stepAdmit)
@ -74,10 +74,10 @@ func (admissionHandler chainAdmissionHandler) Validate(a Attributes) error {
func (admissionHandler chainAdmissionHandler) validate(a Attributes) (err error) {
for _, handler := range admissionHandler {
if !handler.Interface().Handles(a.GetOperation()) {
if !handler.Handles(a.GetOperation()) {
continue
}
if validator, ok := handler.Interface().(ValidationInterface); ok {
if validator, ok := handler.(ValidationInterface); ok {
t := time.Now()
err := validator.Validate(a)
Metrics.ObserveAdmissionController(time.Since(t), err != nil, handler, a, stepValidate)
@ -92,7 +92,7 @@ func (admissionHandler chainAdmissionHandler) validate(a Attributes) (err 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.Interface().Handles(operation) {
if handler.Handles(operation) {
return true
}
}

View File

@ -93,7 +93,7 @@ func TestAdmitAndValidate(t *testing.T) {
t.Errorf("unexpected result of admit call: %v", accepted)
}
for _, h := range test.chain {
fake := h.Interface().(*FakeHandler)
fake := h.(*FakeHandler)
_, shouldBeCalled := test.calls[h.Name()]
if shouldBeCalled != fake.admitCalled {
t.Errorf("admit handler %s not called as expected: %v", h.Name(), fake.admitCalled)
@ -120,7 +120,7 @@ func TestAdmitAndValidate(t *testing.T) {
t.Errorf("unexpected result of validate call: %v\n", accepted)
}
for _, h := range test.chain {
fake := h.Interface().(*FakeHandler)
fake := h.(*FakeHandler)
_, shouldBeCalled := test.calls[h.Name()]
if shouldBeCalled != fake.validateCalled {

View File

@ -41,7 +41,7 @@ var (
// NamedHandler requires each admission.Interface be named, primarly for metrics tracking purposes.
type NamedHandler interface {
Interface() Interface
Interface
Name() string
}

View File

@ -41,14 +41,10 @@ type Plugins struct {
// pluginHandler associates name with a admission.Interface handler.
type pluginHandler struct {
i Interface
Interface
name string
}
func (h *pluginHandler) Interface() Interface {
return h.i
}
func (h *pluginHandler) Name() string {
return h.name
}
@ -147,7 +143,7 @@ func (ps *Plugins) NewFromPlugins(pluginNames []string, configProvider ConfigPro
return nil, err
}
if plugin != nil {
handler := &pluginHandler{i: plugin, name: pluginName}
handler := &pluginHandler{Interface: plugin, name: pluginName}
handlers = append(handlers, handler)
}
}

View File

@ -28,11 +28,16 @@ import (
// methods have been called and always returns an error if admit is false.
type FakeHandler struct {
*Handler
name string
admit bool
admitCalled bool
validateCalled bool
}
func (h *FakeHandler) Name() string {
return h.name
}
func (h *FakeHandler) Admit(a Attributes) (err error) {
h.admitCalled = true
if h.admit {
@ -57,12 +62,10 @@ func makeHandler(admit bool, ops ...Operation) *FakeHandler {
}
func makeNamedHandler(name string, admit bool, ops ...Operation) NamedHandler {
return &pluginHandler{
i: &FakeHandler{
admit: admit,
Handler: NewHandler(ops...),
},
name: name,
return &FakeHandler{
name: name,
admit: admit,
Handler: NewHandler(ops...),
}
}
@ -90,7 +93,7 @@ func makeValidatingHandler(validate bool, ops ...Operation) *FakeValidatingHandl
func makeValidatingNamedHandler(name string, validate bool, ops ...Operation) NamedHandler {
return &pluginHandler{
i: &FakeValidatingHandler{
Interface: &FakeValidatingHandler{
validate: validate,
Handler: NewHandler(ops...),
},