This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
Adding send mode to Receive Adapter for PullSubscription. #66
Merged
Merged
Changes from all commits
Commits
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
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -34,20 +34,27 @@ import ( | |
// Adapter implements the Pub/Sub adapter to deliver Pub/Sub messages from a | ||
// pre-existing topic/subscription to a Sink. | ||
type Adapter struct { | ||
// ProjectID is the pre-existing eventing project id to use. | ||
ProjectID string | ||
// TopicID is the pre-existing eventing pub/sub topic id to use. | ||
TopicID string | ||
// SubscriptionID is the pre-existing eventing pub/sub subscription id to use. | ||
SubscriptionID string | ||
// SinkURI is the URI messages will be forwarded on to. | ||
SinkURI string | ||
// TransformerURI is the URI messages will be forwarded on to for any transformation | ||
// before they are sent to SinkURI. | ||
TransformerURI string | ||
|
||
// SendMode describes how the adapter sends events. Default: Binary | ||
SendMode ModeType | ||
// Environment variable containing project id. | ||
Project string `envconfig:"PROJECT_ID"` | ||
|
||
// Environment variable containing the sink URI. | ||
Sink string `envconfig:"SINK_URI" required:"true"` | ||
|
||
// Environment variable containing the transformer URI. | ||
Transformer string `envconfig:"TRANSFORMER_URI"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need Transformer here? What is its purpose if we have SendMode? |
||
|
||
// Topic is the environment variable containing the PubSub Topic being | ||
// subscribed to's name. In the form that is unique within the project. | ||
// E.g. 'laconia', not 'projects/my-gcp-project/topics/laconia'. | ||
Topic string `envconfig:"PUBSUB_TOPIC_ID" required:"true"` | ||
|
||
// Subscription is the environment variable containing the name of the | ||
// subscription to use. | ||
Subscription string `envconfig:"PUBSUB_SUBSCRIPTION_ID" required:"true"` | ||
|
||
// SendMode describes how the adapter sends events. | ||
// One of [binary, structured, push]. Default: binary | ||
SendMode ModeType `envconfig:"SEND_MODE" default:"binary" required:"true"` | ||
|
||
// inbound is the cloudevents client to use to receive events. | ||
inbound cloudevents.Client | ||
|
@@ -67,6 +74,8 @@ const ( | |
Binary ModeType = "binary" | ||
// Structured mode is structured encoding. | ||
Structured ModeType = "structured" | ||
// Push mode emulates Pub/Sub push encoding. | ||
Push ModeType = "push" | ||
// DefaultSendMode is the default choice. | ||
DefaultSendMode = Binary | ||
) | ||
|
@@ -88,15 +97,15 @@ func (a *Adapter) Start(ctx context.Context) error { | |
|
||
// Send events on HTTP. | ||
if a.outbound == nil { | ||
if a.outbound, err = a.newHTTPClient(a.SinkURI); err != nil { | ||
if a.outbound, err = a.newHTTPClient(a.Sink); err != nil { | ||
return fmt.Errorf("failed to create outbound cloudevent client: %s", err.Error()) | ||
} | ||
} | ||
|
||
// Make the transformer client in case the TransformerURI has been set. | ||
if a.TransformerURI != "" { | ||
if a.Transformer != "" { | ||
if a.transformer == nil { | ||
if a.transformer, err = kncloudevents.NewDefaultClient(a.TransformerURI); err != nil { | ||
if a.transformer, err = kncloudevents.NewDefaultClient(a.Transformer); err != nil { | ||
return fmt.Errorf("failed to create transformer cloudevent client: %s", err.Error()) | ||
} | ||
} | ||
|
@@ -106,7 +115,7 @@ func (a *Adapter) Start(ctx context.Context) error { | |
} | ||
|
||
func (a *Adapter) receive(ctx context.Context, event cloudevents.Event, resp *cloudevents.EventResponse) error { | ||
logger := logging.FromContext(ctx).With(zap.Any("event.id", event.ID()), zap.Any("sink", a.SinkURI)) | ||
logger := logging.FromContext(ctx).With(zap.Any("event.id", event.ID()), zap.Any("sink", a.Sink)) | ||
|
||
// If a transformer has been configured, then transform the message. | ||
if a.transformer != nil { | ||
|
@@ -125,6 +134,11 @@ func (a *Adapter) receive(ctx context.Context, event cloudevents.Event, resp *cl | |
event = *transformedEvent | ||
} | ||
|
||
// If send mode is Push, convert to Pub/Sub Push payload style. | ||
if a.SendMode == Push { | ||
event = ConvertToPush(ctx, event) | ||
} | ||
|
||
if r, err := a.outbound.Send(ctx, event); err != nil { | ||
return err | ||
} else if r != nil { | ||
|
@@ -148,16 +162,12 @@ func (a *Adapter) convert(ctx context.Context, m transport.Message, err error) ( | |
event.SetType(v1alpha1.PubSubEventType) | ||
event.Data = msg.Data | ||
event.DataEncoded = true | ||
|
||
// The following is experimental additions to support converting pubsub | ||
// messages received by the PullSubscription into a form that matches | ||
// how Pub/Sub produces http push requests. These might change as | ||
// more testing is done with this concept. | ||
// Attributes are extensions. | ||
if msg.Attributes != nil && len(msg.Attributes) > 0 { | ||
event.SetExtension("attributes", msg.Attributes) | ||
for k, v := range msg.Attributes { | ||
event.SetExtension(k, v) | ||
} | ||
} | ||
event.SetExtension("topic", tx.Topic) | ||
event.SetExtension("subscription", tx.Subscription) | ||
|
||
return &event, nil | ||
} | ||
|
@@ -166,9 +176,9 @@ func (a *Adapter) convert(ctx context.Context, m transport.Message, err error) ( | |
|
||
func (a *Adapter) newPubSubClient(ctx context.Context) (cloudevents.Client, error) { | ||
tOpts := []cepubsub.Option{ | ||
cepubsub.WithProjectID(a.ProjectID), | ||
cepubsub.WithTopicID(a.TopicID), | ||
cepubsub.WithSubscriptionID(a.SubscriptionID), | ||
cepubsub.WithProjectID(a.Project), | ||
cepubsub.WithTopicID(a.Topic), | ||
cepubsub.WithSubscriptionID(a.Subscription), | ||
} | ||
|
||
// Make a pubsub transport for the CloudEvents client. | ||
|
@@ -189,7 +199,7 @@ func (a *Adapter) newHTTPClient(target string) (cloudevents.Client, error) { | |
} | ||
|
||
switch a.SendMode { | ||
case Binary: | ||
case Binary, Push: | ||
tOpts = append(tOpts, cloudevents.WithBinaryEncoding()) | ||
case Structured: | ||
tOpts = append(tOpts, cloudevents.WithStructuredEncoding()) | ||
|
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.
Maybe this would be clearer as
format
thanmode
?mode
is a little vague.