-
Notifications
You must be signed in to change notification settings - Fork 630
Bus monitor polish #382
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
Merged
Merged
Bus monitor polish #382
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3be8c55
Bus Refactor
scothis d08fcaf
Update path to stub bus dispatcher for e2e tests
scothis cb8c668
Properly update existing channels in stub bus
scothis fce72f2
Review feedback
scothis 26605a1
Fix kafka bus
scothis 928f10d
Merge remote-tracking branch 'upstream/master' into monitor-polish
scothis b0ece00
Review feedback
scothis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,169 @@ | ||
| /* | ||
| * Copyright 2018 The Knative Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package buses | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| channelsv1alpha1 "github.com/knative/eventing/pkg/apis/channels/v1alpha1" | ||
| ) | ||
|
|
||
| type bus struct { | ||
| busRef BusReference | ||
| handlerFuncs EventHandlerFuncs | ||
|
|
||
| reconciler *Reconciler | ||
| cache *Cache | ||
| dispatcher *MessageDispatcher | ||
| receiver *MessageReceiver | ||
| } | ||
|
|
||
| // BusOpts holds configuration options for new buses. These options are not | ||
| // required for proper operation of the bus, but are useful to override the | ||
| // default behavior and for testing. | ||
| type BusOpts struct { | ||
| // MasterURL is the address of the Kubernetes API server. Overrides any | ||
| // value in kubeconfig. Only required if out-of-cluster. | ||
| MasterURL string | ||
| // KubeConfig is the path to a kubeconfig. Only required if out-of-cluster. | ||
| KubeConfig string | ||
|
|
||
| // Cache to use for this bus. A cache will be created for the bus if not | ||
| // specified. | ||
| Cache *Cache | ||
| // Reconciler to use for this bus. A reconciler wil be created for the bus | ||
| // if not specified. | ||
| Reconciler *Reconciler | ||
| // MessageDispatcher to use for this bus. The message dispatcher is used to | ||
| // send messages from the bus to a subscriber. A message dispatcher will be | ||
| // created for the bus if needed and not specified. | ||
| MessageDispatcher *MessageDispatcher | ||
| // MessageReceiver to use for this bus. The message receiver is used to | ||
| // receive message sent to a channel. A message receiver will be created | ||
| // for the bus if needed and not specified. | ||
| MessageReceiver *MessageReceiver | ||
| } | ||
|
|
||
| // BusProvisioner provisions channels and subscriptions for a bus on backing | ||
| // infrastructure. | ||
| type BusProvisioner interface { | ||
| Run(threadiness int, stopCh <-chan struct{}) | ||
| } | ||
|
|
||
| // NewBusProvisioner creates a new provisioner for a specific bus. | ||
| // EventHandlerFuncs are used to be notified when a channel or subscription is | ||
| // created, updated or removed. | ||
| func NewBusProvisioner(busRef BusReference, handlerFuncs EventHandlerFuncs, opts *BusOpts) BusProvisioner { | ||
| if opts == nil { | ||
| opts = &BusOpts{} | ||
| } | ||
| if opts.Cache == nil { | ||
| opts.Cache = NewCache() | ||
| } | ||
| if opts.Reconciler == nil { | ||
| opts.Reconciler = NewReconciler(Provisioner, opts.MasterURL, opts.KubeConfig, opts.Cache, handlerFuncs) | ||
| } | ||
|
|
||
| return &bus{ | ||
| busRef: busRef, | ||
| handlerFuncs: handlerFuncs, | ||
| cache: opts.Cache, | ||
| reconciler: opts.Reconciler, | ||
| } | ||
| } | ||
|
|
||
| // BusDispatcher dispatches messages from channels to subscribers via backing | ||
| // infrastructure. | ||
| type BusDispatcher interface { | ||
| Run(threadiness int, stopCh <-chan struct{}) | ||
| DispatchMessage(subscriptionRef SubscriptionReference, message *Message) error | ||
| } | ||
|
|
||
| // NewBusDispatcher creates a new dispatcher for a specific bus. | ||
| // EventHandlerFuncs are used to be notified when a subscription is created, | ||
| // updated or removed, or a message is received. | ||
| func NewBusDispatcher(busRef BusReference, handlerFuncs EventHandlerFuncs, opts *BusOpts) BusDispatcher { | ||
| var b *bus | ||
|
|
||
| if opts == nil { | ||
| opts = &BusOpts{} | ||
| } | ||
| if opts.Cache == nil { | ||
| opts.Cache = NewCache() | ||
| } | ||
| if opts.Reconciler == nil { | ||
| opts.Reconciler = NewReconciler(Dispatcher, opts.MasterURL, opts.KubeConfig, opts.Cache, handlerFuncs) | ||
| } | ||
| if opts.MessageDispatcher == nil { | ||
| opts.MessageDispatcher = NewMessageDispatcher() | ||
| } | ||
| if opts.MessageReceiver == nil { | ||
| opts.MessageReceiver = NewMessageReceiver(func(channelRef ChannelReference, message *Message) error { | ||
| return b.receiveMessage(channelRef, message) | ||
| }) | ||
| } | ||
|
|
||
| b = &bus{ | ||
| busRef: busRef, | ||
| handlerFuncs: handlerFuncs, | ||
|
|
||
| cache: opts.Cache, | ||
| reconciler: opts.Reconciler, | ||
| dispatcher: opts.MessageDispatcher, | ||
| receiver: opts.MessageReceiver, | ||
| } | ||
|
|
||
| return b | ||
| } | ||
|
|
||
| // Run starts the bus's processing. | ||
| func (b bus) Run(threadiness int, stopCh <-chan struct{}) { | ||
| go b.reconciler.Run(b.busRef, threadiness, stopCh) | ||
| b.reconciler.WaitForCacheSync(stopCh) | ||
| if b.receiver != nil { | ||
| go b.receiver.Run(stopCh) | ||
| } | ||
|
|
||
| <-stopCh | ||
| } | ||
|
|
||
| func (b *bus) receiveMessage(channelRef ChannelReference, message *Message) error { | ||
| _, err := b.cache.Channel(channelRef) | ||
| if err != nil { | ||
| return ErrUnknownChannel | ||
| } | ||
| return b.handlerFuncs.onReceiveMessage(channelRef, message) | ||
| } | ||
|
|
||
| // DispatchMessage sends a message to a subscriber. This function is only | ||
| // avilable for bus dispatchers. | ||
| func (b *bus) DispatchMessage(subscriptionRef SubscriptionReference, message *Message) error { | ||
| subscription, err := b.cache.Subscription(subscriptionRef) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to dispatch to unknown subscription %q", subscriptionRef.String()) | ||
| } | ||
| return b.dispatchMessage(subscription, message) | ||
| } | ||
|
|
||
| func (b *bus) dispatchMessage(subscription *channelsv1alpha1.Subscription, message *Message) error { | ||
| subscriber := subscription.Spec.Subscriber | ||
| defaults := DispatchDefaults{ | ||
| Namespace: subscription.Namespace, | ||
| ReplyTo: subscription.Spec.ReplyTo, | ||
| } | ||
| return b.dispatcher.DispatchMessage(message, subscriber, defaults) | ||
| } | ||
This file contains hidden or 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,98 @@ | ||
| /* | ||
| * Copyright 2018 The Knative Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package buses | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| channelsv1alpha1 "github.com/knative/eventing/pkg/apis/channels/v1alpha1" | ||
| ) | ||
|
|
||
| // NewCache create a cache that is able to save and retrive Channels and | ||
| // Subscriptions by their reference. | ||
| func NewCache() *Cache { | ||
| return &Cache{ | ||
| channels: make(map[ChannelReference]*channelsv1alpha1.Channel), | ||
| subscriptions: make(map[SubscriptionReference]*channelsv1alpha1.Subscription), | ||
| } | ||
| } | ||
|
|
||
| // Cache able to save and retrive Channels and Subscriptions by their | ||
| // reference. It is used by the reconciler to track which resources have been | ||
| // provisioned and comparing updated resources to the provisioned version. | ||
| type Cache struct { | ||
| channels map[ChannelReference]*channelsv1alpha1.Channel | ||
| subscriptions map[SubscriptionReference]*channelsv1alpha1.Subscription | ||
| } | ||
|
|
||
| // Channel returns a cached channel for provided reference or an error if the | ||
| // channel is not in the cache. | ||
| func (c *Cache) Channel(channelRef ChannelReference) (*channelsv1alpha1.Channel, error) { | ||
| channel, ok := c.channels[channelRef] | ||
| if !ok { | ||
| return nil, fmt.Errorf("unknown channel %q", channelRef.String()) | ||
| } | ||
| return channel, nil | ||
| } | ||
|
|
||
| // Subscription returns a cached subscription for provided reference or an | ||
| // error if the subscription is not in the cache. | ||
| func (c *Cache) Subscription(subscriptionRef SubscriptionReference) (*channelsv1alpha1.Subscription, error) { | ||
| subscription, ok := c.subscriptions[subscriptionRef] | ||
| if !ok { | ||
| return nil, fmt.Errorf("unknown subscription %q", subscriptionRef.String()) | ||
| } | ||
| return subscription, nil | ||
| } | ||
|
|
||
| // AddChannel adds, or updates, the provided channel to the cache for later | ||
| // retrieal by its reference. | ||
| func (c *Cache) AddChannel(channel *channelsv1alpha1.Channel) { | ||
| if channel == nil { | ||
| return | ||
| } | ||
| channelRef := NewChannelReference(channel) | ||
| c.channels[channelRef] = channel | ||
| } | ||
|
|
||
| // RemoveChannel removes the provided channel from the cache. | ||
| func (c *Cache) RemoveChannel(channel *channelsv1alpha1.Channel) { | ||
| if channel == nil { | ||
| return | ||
| } | ||
| channelRef := NewChannelReference(channel) | ||
| delete(c.channels, channelRef) | ||
| } | ||
|
|
||
| // AddSubscription adds, or updates, the provided subscription to the cache for | ||
| // later retrieal by its reference. | ||
| func (c *Cache) AddSubscription(subscription *channelsv1alpha1.Subscription) { | ||
| if subscription == nil { | ||
| return | ||
| } | ||
| subscriptionRef := NewSubscriptionReference(subscription) | ||
| c.subscriptions[subscriptionRef] = subscription | ||
| } | ||
|
|
||
| // RemoveSubscription removes the provided subscription from the cache. | ||
| func (c *Cache) RemoveSubscription(subscription *channelsv1alpha1.Subscription) { | ||
| if subscription == nil { | ||
| return | ||
| } | ||
| subscriptionRef := NewSubscriptionReference(subscription) | ||
| delete(c.subscriptions, subscriptionRef) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can subscription be nil here? should we check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't think of a reason to why it should be allowed to be empty. I'll add a check to the validator.