#55183 follow up: Reinstate admission chain composition and ns test
This commit is contained in:

committed by
Dr. Stefan Schimanski

parent
142579c16d
commit
d82ae45a4c
@@ -129,15 +129,19 @@ func TestAdmissionNamespaceExists(t *testing.T) {
|
|||||||
|
|
||||||
// TestIgnoreAdmission validates that a request is ignored if its not a create
|
// TestIgnoreAdmission validates that a request is ignored if its not a create
|
||||||
func TestIgnoreAdmission(t *testing.T) {
|
func TestIgnoreAdmission(t *testing.T) {
|
||||||
|
namespace := "test"
|
||||||
mockClient := newMockClientForTest([]string{})
|
mockClient := newMockClientForTest([]string{})
|
||||||
handler, informerFactory, err := newHandlerForTest(mockClient)
|
handler, informerFactory, err := newHandlerForTest(mockClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error initializing handler: %v", err)
|
t.Errorf("unexpected error initializing handler: %v", err)
|
||||||
}
|
}
|
||||||
informerFactory.Start(wait.NeverStop)
|
informerFactory.Start(wait.NeverStop)
|
||||||
|
chainHandler := admission.NewChainHandler(admission.NewNamedHandler("ns", handler))
|
||||||
|
|
||||||
if handler.Handles(admission.Update) {
|
pod := newPod(namespace)
|
||||||
t.Errorf("expected not to handle Update")
|
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) {
|
if hasCreateNamespaceAction(mockClient) {
|
||||||
t.Errorf("unexpected create namespace action")
|
t.Errorf("unexpected create namespace action")
|
||||||
|
@@ -29,8 +29,8 @@ func NewChainHandler(handlers ...NamedHandler) chainAdmissionHandler {
|
|||||||
|
|
||||||
func NewNamedHandler(name string, i Interface) NamedHandler {
|
func NewNamedHandler(name string, i Interface) NamedHandler {
|
||||||
return &pluginHandler{
|
return &pluginHandler{
|
||||||
i: i,
|
Interface: i,
|
||||||
name: name,
|
name: name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,10 +49,10 @@ func (admissionHandler chainAdmissionHandler) Admit(a Attributes) error {
|
|||||||
|
|
||||||
func (admissionHandler chainAdmissionHandler) admit(a Attributes) error {
|
func (admissionHandler chainAdmissionHandler) admit(a Attributes) error {
|
||||||
for _, handler := range admissionHandler {
|
for _, handler := range admissionHandler {
|
||||||
if !handler.Interface().Handles(a.GetOperation()) {
|
if !handler.Handles(a.GetOperation()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if mutator, ok := handler.Interface().(MutationInterface); ok {
|
if mutator, ok := handler.(MutationInterface); ok {
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
err := mutator.Admit(a)
|
err := mutator.Admit(a)
|
||||||
Metrics.ObserveAdmissionController(time.Since(t), err != nil, handler, a, stepAdmit)
|
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) {
|
func (admissionHandler chainAdmissionHandler) validate(a Attributes) (err error) {
|
||||||
for _, handler := range admissionHandler {
|
for _, handler := range admissionHandler {
|
||||||
if !handler.Interface().Handles(a.GetOperation()) {
|
if !handler.Handles(a.GetOperation()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if validator, ok := handler.Interface().(ValidationInterface); ok {
|
if validator, ok := handler.(ValidationInterface); ok {
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
err := validator.Validate(a)
|
err := validator.Validate(a)
|
||||||
Metrics.ObserveAdmissionController(time.Since(t), err != nil, handler, a, stepValidate)
|
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
|
// Handles will return true if any of the handlers handles the given operation
|
||||||
func (admissionHandler chainAdmissionHandler) Handles(operation Operation) bool {
|
func (admissionHandler chainAdmissionHandler) Handles(operation Operation) bool {
|
||||||
for _, handler := range admissionHandler {
|
for _, handler := range admissionHandler {
|
||||||
if handler.Interface().Handles(operation) {
|
if handler.Handles(operation) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -93,7 +93,7 @@ func TestAdmitAndValidate(t *testing.T) {
|
|||||||
t.Errorf("unexpected result of admit call: %v", accepted)
|
t.Errorf("unexpected result of admit call: %v", accepted)
|
||||||
}
|
}
|
||||||
for _, h := range test.chain {
|
for _, h := range test.chain {
|
||||||
fake := h.Interface().(*FakeHandler)
|
fake := h.(*FakeHandler)
|
||||||
_, shouldBeCalled := test.calls[h.Name()]
|
_, shouldBeCalled := test.calls[h.Name()]
|
||||||
if shouldBeCalled != fake.admitCalled {
|
if shouldBeCalled != fake.admitCalled {
|
||||||
t.Errorf("admit handler %s not called as expected: %v", h.Name(), 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)
|
t.Errorf("unexpected result of validate call: %v\n", accepted)
|
||||||
}
|
}
|
||||||
for _, h := range test.chain {
|
for _, h := range test.chain {
|
||||||
fake := h.Interface().(*FakeHandler)
|
fake := h.(*FakeHandler)
|
||||||
|
|
||||||
_, shouldBeCalled := test.calls[h.Name()]
|
_, shouldBeCalled := test.calls[h.Name()]
|
||||||
if shouldBeCalled != fake.validateCalled {
|
if shouldBeCalled != fake.validateCalled {
|
||||||
|
@@ -41,7 +41,7 @@ var (
|
|||||||
|
|
||||||
// NamedHandler requires each admission.Interface be named, primarly for metrics tracking purposes.
|
// NamedHandler requires each admission.Interface be named, primarly for metrics tracking purposes.
|
||||||
type NamedHandler interface {
|
type NamedHandler interface {
|
||||||
Interface() Interface
|
Interface
|
||||||
Name() string
|
Name() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -41,14 +41,10 @@ type Plugins struct {
|
|||||||
|
|
||||||
// pluginHandler associates name with a admission.Interface handler.
|
// pluginHandler associates name with a admission.Interface handler.
|
||||||
type pluginHandler struct {
|
type pluginHandler struct {
|
||||||
i Interface
|
Interface
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *pluginHandler) Interface() Interface {
|
|
||||||
return h.i
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *pluginHandler) Name() string {
|
func (h *pluginHandler) Name() string {
|
||||||
return h.name
|
return h.name
|
||||||
}
|
}
|
||||||
@@ -147,7 +143,7 @@ func (ps *Plugins) NewFromPlugins(pluginNames []string, configProvider ConfigPro
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if plugin != nil {
|
if plugin != nil {
|
||||||
handler := &pluginHandler{i: plugin, name: pluginName}
|
handler := &pluginHandler{Interface: plugin, name: pluginName}
|
||||||
handlers = append(handlers, handler)
|
handlers = append(handlers, handler)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -28,11 +28,16 @@ import (
|
|||||||
// methods have been called and always returns an error if admit is false.
|
// methods have been called and always returns an error if admit is false.
|
||||||
type FakeHandler struct {
|
type FakeHandler struct {
|
||||||
*Handler
|
*Handler
|
||||||
|
name string
|
||||||
admit bool
|
admit bool
|
||||||
admitCalled bool
|
admitCalled bool
|
||||||
validateCalled bool
|
validateCalled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *FakeHandler) Name() string {
|
||||||
|
return h.name
|
||||||
|
}
|
||||||
|
|
||||||
func (h *FakeHandler) Admit(a Attributes) (err error) {
|
func (h *FakeHandler) Admit(a Attributes) (err error) {
|
||||||
h.admitCalled = true
|
h.admitCalled = true
|
||||||
if h.admit {
|
if h.admit {
|
||||||
@@ -57,12 +62,10 @@ func makeHandler(admit bool, ops ...Operation) *FakeHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func makeNamedHandler(name string, admit bool, ops ...Operation) NamedHandler {
|
func makeNamedHandler(name string, admit bool, ops ...Operation) NamedHandler {
|
||||||
return &pluginHandler{
|
return &FakeHandler{
|
||||||
i: &FakeHandler{
|
name: name,
|
||||||
admit: admit,
|
admit: admit,
|
||||||
Handler: NewHandler(ops...),
|
Handler: NewHandler(ops...),
|
||||||
},
|
|
||||||
name: name,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +93,7 @@ func makeValidatingHandler(validate bool, ops ...Operation) *FakeValidatingHandl
|
|||||||
|
|
||||||
func makeValidatingNamedHandler(name string, validate bool, ops ...Operation) NamedHandler {
|
func makeValidatingNamedHandler(name string, validate bool, ops ...Operation) NamedHandler {
|
||||||
return &pluginHandler{
|
return &pluginHandler{
|
||||||
i: &FakeValidatingHandler{
|
Interface: &FakeValidatingHandler{
|
||||||
validate: validate,
|
validate: validate,
|
||||||
Handler: NewHandler(ops...),
|
Handler: NewHandler(ops...),
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user