From d1c22d833d8aa05094662bcc47c08ee3d270bfbd Mon Sep 17 00:00:00 2001 From: Dibyo Mukherjee Date: Mon, 7 Dec 2020 22:11:23 -0500 Subject: [PATCH] interceptors: small changes to types 1. Change body from []byte to json.RawMessage 2. Remove omitempty tag for boolean type so that it marshals when false 3. Change context.url to context.event_url to match struct type Part of #271 Signed-off-by: Dibyo Mukherjee --- pkg/apis/triggers/v1alpha1/interceptor_types.go | 14 +++++++++----- pkg/interceptors/cel/cel.go | 2 +- pkg/interceptors/cel/cel_test.go | 5 ++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/pkg/apis/triggers/v1alpha1/interceptor_types.go b/pkg/apis/triggers/v1alpha1/interceptor_types.go index a8f5a3fb8..c197fd570 100644 --- a/pkg/apis/triggers/v1alpha1/interceptor_types.go +++ b/pkg/apis/triggers/v1alpha1/interceptor_types.go @@ -2,6 +2,7 @@ package v1alpha1 import ( "context" + "encoding/json" "fmt" "strings" @@ -9,6 +10,10 @@ import ( ) type InterceptorInterface interface { + // Process executes the given InterceptorRequest. Simply getting a non-nil InterceptorResponse back is not sufficient + // to determine if the interceptor processing was successful. Instead use the InterceptorResponse.Status.Continue to + // see if processing should continue and InterceptorResponse.Status.Code to distinguish between the kinds of errors + // (i.e user errors vs system errors) Process(ctx context.Context, r *InterceptorRequest) *InterceptorResponse } @@ -16,7 +21,7 @@ type InterceptorInterface interface { // +k8s:deepcopy-gen=false type InterceptorRequest struct { // Body is the incoming HTTP event body - Body []byte `json:"body,omitempty"` + Body json.RawMessage `json:"body,omitempty"` // Header are the headers for the incoming HTTP event Header map[string][]string `json:"header,omitempty"` // Extensions are extra values that are added by previous interceptors in a chain @@ -26,12 +31,12 @@ type InterceptorRequest struct { InterceptorParams map[string]interface{} `json:"interceptor_params,omitempty"` // Context contains additional metadata about the event being processed - Context *TriggerContext + Context *TriggerContext `json:"context"` } type TriggerContext struct { // EventURL is the URL of the incoming event - EventURL string `json:"url,omitempty"` + EventURL string `json:"event_url,omitempty"` // EventID is a unique ID assigned by Triggers to each event EventID string `json:"event_id,omitempty"` // TriggerID is of the form namespace/$ns/triggers/$name @@ -42,10 +47,9 @@ type TriggerContext struct { // +k8s:deepcopy-gen=false type InterceptorResponse struct { // Extensions are additional fields that is added to the interceptor event. - // See TEP-0022. Naming TBD. Extensions map[string]interface{} `json:"extensions,omitempty"` // Continue indicates if the EventListener should continue processing the Trigger or not - Continue bool `json:"continue,omitempty"` + Continue bool `json:"continue"` //Don't add omitempty -- it will remove the continue field when the value is false. // Status is an Error status containing details on any interceptor processing errors Status Status `json:"status"` } diff --git a/pkg/interceptors/cel/cel.go b/pkg/interceptors/cel/cel.go index fc7b69674..e48a64256 100644 --- a/pkg/interceptors/cel/cel.go +++ b/pkg/interceptors/cel/cel.go @@ -61,7 +61,7 @@ var ( ) // NewInterceptor creates a prepopulated Interceptor. -func NewInterceptor(k kubernetes.Interface, l *zap.SugaredLogger) *Interceptor { +func NewInterceptor(k kubernetes.Interface, l *zap.SugaredLogger) interceptors.Interceptor { return &Interceptor{ Logger: l, KubeClientSet: k, diff --git a/pkg/interceptors/cel/cel_test.go b/pkg/interceptors/cel/cel_test.go index 68396319d..65ad99923 100644 --- a/pkg/interceptors/cel/cel_test.go +++ b/pkg/interceptors/cel/cel_test.go @@ -254,7 +254,10 @@ func TestInterceptor_Process(t *testing.T) { if _, err := kubeClient.CoreV1().Secrets(testNS).Create(ctx, makeSecret(), metav1.CreateOptions{}); err != nil { rt.Error(err) } - w := NewInterceptor(kubeClient, logger.Sugar()) + w := &Interceptor{ + KubeClientSet: kubeClient, + Logger: logger.Sugar(), + } res := w.Process(ctx, &triggersv1.InterceptorRequest{ Body: tt.body, Header: http.Header{