-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
9 changed files
with
235 additions
and
73 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
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,10 @@ | ||
package testing | ||
|
||
type FakeMeshChecker struct { | ||
Mesh bool | ||
Err error | ||
} | ||
|
||
func (m *FakeMeshChecker) OnMesh(ns string) (bool, error) { | ||
return m.Mesh, m.Err | ||
} |
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,65 @@ | ||
package admission | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
aes "github.com/argoproj/argo-events/pkg/apis/eventsource" | ||
as "github.com/argoproj/argo-events/pkg/apis/sensor" | ||
event "github.com/kanopy-platform/argoslower/internal/admission/eventsource" | ||
sensor "github.com/kanopy-platform/argoslower/internal/admission/sensor" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
type RoutingHandler struct { | ||
sensorHandler *sensor.Handler | ||
eventSourceHandler *event.Handler | ||
} | ||
|
||
func NewRoutingHandler(sh *sensor.Handler, es *event.Handler) *RoutingHandler { | ||
return &RoutingHandler{ | ||
sensorHandler: sh, | ||
eventSourceHandler: es, | ||
} | ||
} | ||
|
||
func (h *RoutingHandler) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
kind := req.RequestKind | ||
if kind == nil { | ||
kind = &req.Kind | ||
} | ||
|
||
switch { | ||
case kind.Kind == aes.Kind && h.eventSourceHandler != nil: | ||
return h.eventSourceHandler.Handle(ctx, req) | ||
case kind.Kind == as.Kind && h.sensorHandler != nil: | ||
return h.sensorHandler.Handle(ctx, req) | ||
default: | ||
return admission.Denied(fmt.Sprintf("Kind %s not supported by controller", kind.Kind)) | ||
} | ||
|
||
} | ||
|
||
func (h *RoutingHandler) InjectDecoder(decoder *admission.Decoder) error { | ||
if h.sensorHandler != nil { | ||
err := h.sensorHandler.InjectDecoder(decoder) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if h.eventSourceHandler != nil { | ||
err := h.eventSourceHandler.InjectDecoder(decoder) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
func (h *RoutingHandler) SetupWithManager(m manager.Manager) { | ||
m.GetWebhookServer().Register("/mutate", &webhook.Admission{Handler: h}) | ||
} |
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,120 @@ | ||
package admission | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/kanopy-platform/argoslower/internal/admission/eventsource" | ||
estest "github.com/kanopy-platform/argoslower/internal/admission/eventsource/testing" | ||
sensor "github.com/kanopy-platform/argoslower/internal/admission/sensor" | ||
stest "github.com/kanopy-platform/argoslower/internal/admission/sensor/testing" | ||
"github.com/kanopy-platform/argoslower/pkg/ratelimit" | ||
"github.com/stretchr/testify/assert" | ||
|
||
admissionv1 "k8s.io/api/admission/v1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
aes "github.com/argoproj/argo-events/pkg/apis/eventsource" | ||
v1eventsource "github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1" | ||
as "github.com/argoproj/argo-events/pkg/apis/sensor" | ||
v1sensor "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" | ||
) | ||
|
||
func TestRoutingHandler(t *testing.T) { | ||
|
||
s := v1sensor.Sensor{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Namespace: "test", | ||
Name: "test", | ||
}, | ||
Spec: v1sensor.SensorSpec{}, | ||
} | ||
|
||
sensorBytes, err := json.Marshal(s) | ||
assert.NoError(t, err) | ||
|
||
sar := admissionv1.AdmissionRequest{ | ||
Object: runtime.RawExtension{ | ||
Raw: sensorBytes, | ||
}, | ||
} | ||
|
||
es := v1eventsource.EventSource{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Namespace: "test", | ||
Name: "test", | ||
}, | ||
Spec: v1eventsource.EventSourceSpec{}, | ||
} | ||
|
||
eventSourceBytes, err := json.Marshal(es) | ||
assert.NoError(t, err) | ||
|
||
esar := admissionv1.AdmissionRequest{ | ||
Object: runtime.RawExtension{ | ||
Raw: eventSourceBytes, | ||
}, | ||
} | ||
|
||
frlg := stest.NewFakeRate() | ||
rc := ratelimit.NewRateLimitCalculatorOrDie("Second", int32(80000)) | ||
|
||
fmc := &estest.FakeMeshChecker{ | ||
Mesh: true, | ||
} | ||
|
||
tests := map[string]struct { | ||
sensor *sensor.Handler | ||
es *eventsource.Handler | ||
sdeny bool | ||
esdeny bool | ||
}{ | ||
"empty": {sdeny: true, esdeny: true}, | ||
"sensoronly": {sensor: sensor.NewHandler(&frlg, rc), esdeny: true}, | ||
"esonly": {es: eventsource.NewHandler(fmc), sdeny: true}, | ||
"both": {sensor: sensor.NewHandler(&frlg, rc), es: eventsource.NewHandler(fmc)}, | ||
} | ||
|
||
for name, test := range tests { | ||
|
||
scheme := runtime.NewScheme() | ||
decoder, err := admission.NewDecoder(scheme) | ||
|
||
handler := NewRoutingHandler(test.sensor, test.es) | ||
assert.NoError(t, err) | ||
|
||
err = handler.InjectDecoder(decoder) | ||
assert.NoError(t, err) | ||
|
||
sck := sar.DeepCopy() | ||
skind := v1.GroupVersionKind{ | ||
Kind: as.Kind, | ||
} | ||
sck.Kind = skind | ||
sck.RequestKind = &skind | ||
|
||
resp := handler.Handle(context.TODO(), admission.Request{AdmissionRequest: *sck}) | ||
assert.Equal(t, !test.sdeny, resp.Allowed, name) | ||
|
||
sck.RequestKind = nil | ||
resp = handler.Handle(context.TODO(), admission.Request{AdmissionRequest: *sck}) | ||
assert.Equal(t, !test.sdeny, resp.Allowed, name) | ||
|
||
esck := esar.DeepCopy() | ||
eskind := v1.GroupVersionKind{ | ||
Kind: aes.Kind, | ||
} | ||
esck.Kind = eskind | ||
esck.RequestKind = &eskind | ||
|
||
resp = handler.Handle(context.TODO(), admission.Request{AdmissionRequest: *esck}) | ||
assert.Equal(t, !test.esdeny, resp.Allowed, name) | ||
|
||
esck.RequestKind = nil | ||
resp = handler.Handle(context.TODO(), admission.Request{AdmissionRequest: *esck}) | ||
assert.Equal(t, !test.esdeny, resp.Allowed, name) | ||
} | ||
} |
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,24 @@ | ||
package testing | ||
|
||
import ( | ||
sensor "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" | ||
) | ||
|
||
type FakeRateLimitGetter struct { | ||
Rates map[string]*sensor.RateLimit | ||
Err error | ||
} | ||
|
||
func (frlg *FakeRateLimitGetter) RateLimit(namespace string) (*sensor.RateLimit, error) { | ||
if r, ok := frlg.Rates[namespace]; ok { | ||
return r, nil | ||
} else { | ||
return nil, frlg.Err | ||
} | ||
} | ||
|
||
func NewFakeRate() FakeRateLimitGetter { | ||
return FakeRateLimitGetter{ | ||
Rates: map[string]*sensor.RateLimit{}, | ||
} | ||
} |
Oops, something went wrong.