Skip to content
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

Added URL field to WebhookInterceptor #1002

Merged
merged 1 commit into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/apis/triggers/v1alpha1/trigger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)

// TriggerSpec represents a connection between TriggerSpecBinding,
Expand Down Expand Up @@ -95,6 +96,8 @@ type WebhookInterceptor struct {
// name to use as the EventInterceptor. Either objectRef or url can be specified
// +optional
ObjectRef *corev1.ObjectReference `json:"objectRef,omitempty"`
// +optional
URL *apis.URL `json:"url,omitempty"`
// Header is a group of key-value pairs that can be appended to the
// interceptor request headers. This allows the interceptor to make
// decisions specific to an EventListenerTrigger.
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/triggers/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 12 additions & 8 deletions pkg/interceptors/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (

pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/triggers/pkg/interceptors"
corev1 "k8s.io/api/core/v1"

"github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"

"go.uber.org/zap"
Expand Down Expand Up @@ -61,7 +61,7 @@ func NewInterceptor(wh *triggersv1.WebhookInterceptor, c *http.Client, ns string
}

func (w *Interceptor) ExecuteTrigger(request *http.Request) (*http.Response, error) {
u, err := getURI(w.Webhook.ObjectRef, w.TriggerNamespace) // TODO: Cache this result or do this on initialization
u, err := getURI(w.Webhook, w.TriggerNamespace) // TODO: Cache this result or do this on initialization
if err != nil {
return nil, err
}
Expand All @@ -85,17 +85,21 @@ func (w *Interceptor) ExecuteTrigger(request *http.Request) (*http.Response, err
}

// getURI retrieves the ObjectReference to URI.
func getURI(objRef *corev1.ObjectReference, ns string) (*url.URL, error) {
func getURI(webhook *v1alpha1.WebhookInterceptor, ns string) (*url.URL, error) {
// TODO: This should work for any Addressable.
// Use something like https://github.com/knative/eventing-contrib/blob/7c0fc5cfa8bd44da0767d9e7b250264ea6eb7d8d/pkg/controller/sinks/sinks.go#L32
if objRef.Kind == "Service" && objRef.APIVersion == "v1" {
switch {
case webhook.URL != nil:
return webhook.URL.URL(), nil
case webhook.ObjectRef.Kind == "Service" && webhook.ObjectRef.APIVersion == "v1":
// TODO: Also assuming port 80 and http here. Use DNS/or the env vars?
if objRef.Namespace != "" {
ns = objRef.Namespace
if webhook.ObjectRef.Namespace != "" {
ns = webhook.ObjectRef.Namespace
}
return url.Parse(fmt.Sprintf("http://%s.%s.svc/", objRef.Name, ns))
return url.Parse(fmt.Sprintf("http://%s.%s.svc/", webhook.ObjectRef.Name, ns))
default:
return nil, errors.New("invalid objRef")
}
return nil, errors.New("Invalid objRef")
}

func addInterceptorHeaders(header http.Header, headerParams []pipelinev1.Param) {
Expand Down
45 changes: 28 additions & 17 deletions pkg/interceptors/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/apis"
)

func TestWebHookInterceptor(t *testing.T) {
Expand Down Expand Up @@ -148,37 +149,47 @@ func TestGetURI(t *testing.T) {
var eventListenerNs = "default"
tcs := []struct {
name string
ref corev1.ObjectReference
ref v1alpha1.WebhookInterceptor
expected string
wantErr bool
}{{
name: "namespace specified",
ref: corev1.ObjectReference{
Kind: "Service",
Name: "foo",
APIVersion: "v1",
Namespace: "bar",
},
ref: v1alpha1.WebhookInterceptor{
ObjectRef: &corev1.ObjectReference{
Kind: "Service",
Name: "foo",
APIVersion: "v1",
Namespace: "bar",
}},
expected: "http://foo.bar.svc/",
wantErr: false,
}, {
name: "no namespace",
ref: corev1.ObjectReference{
Kind: "Service",
Name: "foo",
APIVersion: "v1",
},
ref: v1alpha1.WebhookInterceptor{
ObjectRef: &corev1.ObjectReference{
Kind: "Service",
Name: "foo",
APIVersion: "v1",
}},
expected: "http://foo.default.svc/",
wantErr: false,
}, {
name: "non services",
ref: corev1.ObjectReference{
Kind: "Blah",
Name: "foo",
APIVersion: "v1",
},
ref: v1alpha1.WebhookInterceptor{
ObjectRef: &corev1.ObjectReference{
Kind: "Blah",
Name: "foo",
APIVersion: "v1",
}},
expected: "",
wantErr: true,
}, {
name: "webhook interceptor with url",
ref: v1alpha1.WebhookInterceptor{
URL: apis.HTTP("foo.default.svc"),
},
expected: "http://foo.default.svc",
wantErr: false,
}}

for _, tc := range tcs {
Expand Down