Skip to content

Commit

Permalink
Add EventListener Selector for TriggerCRD
Browse files Browse the repository at this point in the history
EventListener have NamespaceSelector field which gives us namespaces
from where EventListener fetches Trigger object to process events.
  • Loading branch information
khrm committed Sep 28, 2020
1 parent c8123cf commit 260a928
Show file tree
Hide file tree
Showing 17 changed files with 595 additions and 231 deletions.
14 changes: 4 additions & 10 deletions cmd/triggerrun/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func trigger(triggerFile, httpPath, action string, writer io.Writer) error {
}
case "create":
{
err := r.CreateResources("", resources, tri.Name, eventID, eventLog)
err := r.CreateResources(tri.Namespace, "", resources, tri.Name, eventID, eventLog)
if err != nil {
return fmt.Errorf("fail to create resources: %w", err)
}
Expand Down Expand Up @@ -191,15 +191,9 @@ func processTriggerSpec(kubeClient kubernetes.Interface, client triggersclientse
return nil, errors.New("trigger is not defined")
}

//convert trigger to eventListener
el, err := triggersv1.ToEventListenerTrigger(tri.Spec)
if err != nil {
return nil, fmt.Errorf("fail to convert Trigger to EvenetListener: %w", err)
}

log := eventLog.With(zap.String(triggersv1.TriggerLabelKey, el.Name))
log := eventLog.With(zap.String(triggersv1.TriggerLabelKey, r.EventListenerName))

finalPayload, header, err := r.ExecuteInterceptors(&el, request, body, log)
finalPayload, header, err := r.ExecuteInterceptors(*tri, request, body, log)
if err != nil {
log.Error(err)
return nil, err
Expand All @@ -209,7 +203,7 @@ func processTriggerSpec(kubeClient kubernetes.Interface, client triggersclientse
tri.Namespace = "default"
}

rt, err := template.ResolveTrigger(el,
rt, err := template.ResolveTrigger(*tri,
client.TriggersV1alpha1().TriggerBindings(tri.Namespace).Get,
client.TriggersV1alpha1().ClusterTriggerBindings().Get,
client.TriggersV1alpha1().TriggerTemplates(tri.Namespace).Get)
Expand Down
12 changes: 12 additions & 0 deletions pkg/apis/triggers/v1alpha1/event_listener_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type EventListenerSpec struct {
ServiceType corev1.ServiceType `json:"serviceType,omitempty"`
Replicas *int32 `json:"replicas,omitempty"`
PodTemplate PodTemplate `json:"podTemplate,omitempty"`
NamespaceSelector NamespaceSelector `json:"namespaceSelector,omitempty"`
Resources Resources `json:"resources,omitempty"`
}

Expand Down Expand Up @@ -152,6 +153,17 @@ type EventListenerConfig struct {
GeneratedResourceName string `json:"generatedName"`
}

// NamespaceSelector is a selector for selecting either all namespaces or a
// list of namespaces.
// +k8s:openapi-gen=true
type NamespaceSelector struct {
// Boolean describing whether all namespaces are selected in contrast to a
// list restricting them.
Any bool `json:"any,omitempty"`
// List of namespace names.
MatchNames []string `json:"matchNames,omitempty"`
}

// The conditions that are internally resolved by the EventListener reconciler
const (
// ServiceExists is the ConditionType set on the EventListener, which
Expand Down
22 changes: 22 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.

18 changes: 9 additions & 9 deletions pkg/interceptors/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ import (
)

type Interceptor struct {
KubeClientSet kubernetes.Interface
Logger *zap.SugaredLogger
Bitbucket *triggersv1.BitbucketInterceptor
EventListenerNamespace string
KubeClientSet kubernetes.Interface
Logger *zap.SugaredLogger
Bitbucket *triggersv1.BitbucketInterceptor
TriggerNamespace string
}

func NewInterceptor(bh *triggersv1.BitbucketInterceptor, k kubernetes.Interface, ns string, l *zap.SugaredLogger) interceptors.Interceptor {
return &Interceptor{
Logger: l,
Bitbucket: bh,
KubeClientSet: k,
EventListenerNamespace: ns,
Logger: l,
Bitbucket: bh,
KubeClientSet: k,
TriggerNamespace: ns,
}
}

Expand All @@ -64,7 +64,7 @@ func (w *Interceptor) ExecuteTrigger(request *http.Request) (*http.Response, err
if header == "" {
return nil, errors.New("no X-Hub-Signature header set")
}
secretToken, err := interceptors.GetSecretToken(request, w.KubeClientSet, w.Bitbucket.SecretRef, w.EventListenerNamespace)
secretToken, err := interceptors.GetSecretToken(request, w.KubeClientSet, w.Bitbucket.SecretRef, w.TriggerNamespace)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/interceptors/bitbucket/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ func TestInterceptor_ExecuteTrigger_Signature(t *testing.T) {
}
}
w := &Interceptor{
KubeClientSet: kubeClient,
Bitbucket: tt.Bitbucket,
Logger: logger,
EventListenerNamespace: metav1.NamespaceDefault,
KubeClientSet: kubeClient,
Bitbucket: tt.Bitbucket,
Logger: logger,
TriggerNamespace: metav1.NamespaceDefault,
}
resp, err := w.ExecuteTrigger(request)
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions pkg/interceptors/cel/cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ import (
// against the incoming body and headers to match, if the expression returns
// a true value, then the interception is "successful".
type Interceptor struct {
KubeClientSet kubernetes.Interface
Logger *zap.SugaredLogger
CEL *triggersv1.CELInterceptor
EventListenerNamespace string
KubeClientSet kubernetes.Interface
Logger *zap.SugaredLogger
CEL *triggersv1.CELInterceptor
TriggerNamespace string
}

var (
Expand All @@ -60,16 +60,16 @@ var (
// NewInterceptor creates a prepopulated Interceptor.
func NewInterceptor(cel *triggersv1.CELInterceptor, k kubernetes.Interface, ns string, l *zap.SugaredLogger) interceptors.Interceptor {
return &Interceptor{
Logger: l,
CEL: cel,
KubeClientSet: k,
EventListenerNamespace: ns,
Logger: l,
CEL: cel,
KubeClientSet: k,
TriggerNamespace: ns,
}
}

// ExecuteTrigger is an implementation of the Interceptor interface.
func (w *Interceptor) ExecuteTrigger(request *http.Request) (*http.Response, error) {
env, err := makeCelEnv(request, w.EventListenerNamespace, w.KubeClientSet)
env, err := makeCelEnv(request, w.TriggerNamespace, w.KubeClientSet)
if err != nil {
return nil, fmt.Errorf("error creating cel environment: %w", err)
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/interceptors/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ import (
)

type Interceptor struct {
KubeClientSet kubernetes.Interface
Logger *zap.SugaredLogger
GitHub *triggersv1.GitHubInterceptor
EventListenerNamespace string
KubeClientSet kubernetes.Interface
Logger *zap.SugaredLogger
GitHub *triggersv1.GitHubInterceptor
TriggerNamespace string
}

func NewInterceptor(gh *triggersv1.GitHubInterceptor, k kubernetes.Interface, ns string, l *zap.SugaredLogger) interceptors.Interceptor {
return &Interceptor{
Logger: l,
GitHub: gh,
KubeClientSet: k,
EventListenerNamespace: ns,
Logger: l,
GitHub: gh,
KubeClientSet: k,
TriggerNamespace: ns,
}
}

Expand All @@ -64,7 +64,7 @@ func (w *Interceptor) ExecuteTrigger(request *http.Request) (*http.Response, err
if header == "" {
return nil, errors.New("no X-Hub-Signature header set")
}
secretToken, err := interceptors.GetSecretToken(request, w.KubeClientSet, w.GitHub.SecretRef, w.EventListenerNamespace)
secretToken, err := interceptors.GetSecretToken(request, w.KubeClientSet, w.GitHub.SecretRef, w.TriggerNamespace)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/interceptors/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ func TestInterceptor_ExecuteTrigger_Signature(t *testing.T) {
}
}
w := &Interceptor{
KubeClientSet: kubeClient,
GitHub: tt.GitHub,
Logger: logger,
EventListenerNamespace: metav1.NamespaceDefault,
KubeClientSet: kubeClient,
GitHub: tt.GitHub,
Logger: logger,
TriggerNamespace: metav1.NamespaceDefault,
}
resp, err := w.ExecuteTrigger(request)
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions pkg/interceptors/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ import (
)

type Interceptor struct {
KubeClientSet kubernetes.Interface
Logger *zap.SugaredLogger
GitLab *triggersv1.GitLabInterceptor
EventListenerNamespace string
KubeClientSet kubernetes.Interface
Logger *zap.SugaredLogger
GitLab *triggersv1.GitLabInterceptor
TriggerNamespace string
}

func NewInterceptor(gl *triggersv1.GitLabInterceptor, k kubernetes.Interface, ns string, l *zap.SugaredLogger) interceptors.Interceptor {
return &Interceptor{
Logger: l,
GitLab: gl,
KubeClientSet: k,
EventListenerNamespace: ns,
Logger: l,
GitLab: gl,
KubeClientSet: k,
TriggerNamespace: ns,
}
}

Expand All @@ -54,7 +54,7 @@ func (w *Interceptor) ExecuteTrigger(request *http.Request) (*http.Response, err
return nil, errors.New("no X-GitLab-Token header set")
}

secretToken, err := interceptors.GetSecretToken(request, w.KubeClientSet, w.GitLab.SecretRef, w.EventListenerNamespace)
secretToken, err := interceptors.GetSecretToken(request, w.KubeClientSet, w.GitLab.SecretRef, w.TriggerNamespace)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/interceptors/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ func TestInterceptor_ExecuteTrigger(t *testing.T) {
}
}
w := &Interceptor{
KubeClientSet: kubeClient,
GitLab: tt.GitLab,
Logger: logger,
EventListenerNamespace: metav1.NamespaceDefault,
KubeClientSet: kubeClient,
GitLab: tt.GitLab,
Logger: logger,
TriggerNamespace: metav1.NamespaceDefault,
}
resp, err := w.ExecuteTrigger(request)
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions pkg/interceptors/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ const (
)

type Interceptor struct {
HTTPClient *http.Client
EventListenerNamespace string
Logger *zap.SugaredLogger
Webhook *triggersv1.WebhookInterceptor
HTTPClient *http.Client
TriggerNamespace string
Logger *zap.SugaredLogger
Webhook *triggersv1.WebhookInterceptor
}

func NewInterceptor(wh *triggersv1.WebhookInterceptor, c *http.Client, ns string, l *zap.SugaredLogger) interceptors.Interceptor {
Expand All @@ -53,15 +53,15 @@ func NewInterceptor(wh *triggersv1.WebhookInterceptor, c *http.Client, ns string
Timeout: interceptorTimeout,
}
return &Interceptor{
HTTPClient: timeoutClient,
EventListenerNamespace: ns,
Logger: l,
Webhook: wh,
HTTPClient: timeoutClient,
TriggerNamespace: ns,
Logger: l,
Webhook: wh,
}
}

func (w *Interceptor) ExecuteTrigger(request *http.Request) (*http.Response, error) {
u, err := getURI(w.Webhook.ObjectRef, w.EventListenerNamespace) // TODO: Cache this result or do this on initialization
u, err := getURI(w.Webhook.ObjectRef, w.TriggerNamespace) // TODO: Cache this result or do this on initialization
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 260a928

Please sign in to comment.