-
Notifications
You must be signed in to change notification settings - Fork 630
Generate unique, stable pubsub topic and subscription IDs #458
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ package gcppubsub | |
|
|
||
| import ( | ||
| "context" | ||
| "crypto/sha1" | ||
| "fmt" | ||
|
|
||
| "cloud.google.com/go/pubsub" | ||
|
|
@@ -29,7 +30,7 @@ import ( | |
| const BusType = "gcppubsub" | ||
|
|
||
| type CloudPubSubBus struct { | ||
| ref buses.BusReference | ||
| id string | ||
| reconciler *buses.Reconciler | ||
| dispatcher buses.BusDispatcher | ||
| provisioner buses.BusProvisioner | ||
|
|
@@ -40,15 +41,15 @@ type CloudPubSubBus struct { | |
| logger *zap.SugaredLogger | ||
| } | ||
|
|
||
| func NewCloudPubSubBusDispatcher(ref buses.BusReference, projectID string, opts *buses.BusOpts) (*CloudPubSubBus, error) { | ||
| func NewCloudPubSubBusDispatcher(ref buses.BusReference, busUID string, projectID string, opts *buses.BusOpts) (*CloudPubSubBus, error) { | ||
| ctx := context.Background() | ||
| pubsubClient, err := pubsub.NewClient(ctx, projectID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| bus := &CloudPubSubBus{ | ||
| ref: ref, | ||
| id: busId(busUID), | ||
| pubsubClient: pubsubClient, | ||
| } | ||
| eventHandlers := buses.EventHandlerFuncs{ | ||
|
|
@@ -70,15 +71,15 @@ func NewCloudPubSubBusDispatcher(ref buses.BusReference, projectID string, opts | |
| return bus, nil | ||
| } | ||
|
|
||
| func NewCloudPubSubBusProvisioner(ref buses.BusReference, projectID string, opts *buses.BusOpts) (*CloudPubSubBus, error) { | ||
| func NewCloudPubSubBusProvisioner(ref buses.BusReference, busUID string, projectID string, opts *buses.BusOpts) (*CloudPubSubBus, error) { | ||
| ctx := context.Background() | ||
| pubsubClient, err := pubsub.NewClient(ctx, projectID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| bus := &CloudPubSubBus{ | ||
| ref: ref, | ||
| id: busId(busUID), | ||
|
Contributor
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. rather than storing the id like this I wonder if it might be cleaner to set a topic prefix field that is generated from the id and our special strings (like And then we are free to change how we generate that prefix, and the code does not have to change each time.
Member
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. It seems like we should store the generated UID in the status so we can recover it in the future. Additionally, storing the topic ID means that we don't get thrown off later if we change the Since this is the old Bus code, it's probably not worth updating the |
||
| pubsubClient: pubsubClient, | ||
| } | ||
| eventHandlers := buses.EventHandlerFuncs{ | ||
|
|
@@ -284,9 +285,18 @@ func (b *CloudPubSubBus) deleteSubscription(ref buses.SubscriptionReference) err | |
| } | ||
|
|
||
| func (b *CloudPubSubBus) topicID(channel buses.ChannelReference) string { | ||
| return fmt.Sprintf("channel-%s-%s-%s", b.ref.Name, channel.Namespace, channel.Name) | ||
| return fmt.Sprintf("channel-%s-%s-%s", channel.Namespace, channel.Name, b.id) | ||
|
Contributor
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. is it intended to flip the order of this? it use to be general bus to specific channel. |
||
| } | ||
|
|
||
| func (b *CloudPubSubBus) subscriptionID(subscription buses.SubscriptionReference) string { | ||
| return fmt.Sprintf("subscription-%s-%s-%s", b.ref.Name, subscription.Namespace, subscription.Name) | ||
| return fmt.Sprintf("subscription-%s-%s-%s", subscription.Namespace, subscription.Name, b.id) | ||
| } | ||
|
|
||
| // busId generates a short unique identifier for a bus from the Bus's UID. | ||
| // This value is used as a component of the topic and subscription IDs | ||
| func busId(uid string) string { | ||
|
Contributor
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. There is no test added for this method. Please add a test to make sure this does not make the same busID for similar UIDs
Contributor
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. In go style, |
||
| h := sha1.New() | ||
| h.Write([]byte(uid)) | ||
| bs := h.Sum(nil) | ||
| return fmt.Sprintf("%x", bs)[0:5] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ func main() { | |
| os.Getenv("BUS_NAME"), | ||
| os.Getenv("BUS_NAMESPACE"), | ||
| ) | ||
| busUID := os.Getenv("BUS_UID") | ||
|
Contributor
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. what happens if there is no BUS_UID? |
||
|
|
||
| config := buses.NewLoggingConfig() | ||
| logger := buses.NewBusLoggerFromConfig(config) | ||
|
|
@@ -58,7 +59,7 @@ func main() { | |
| flag.StringVar(&opts.MasterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") | ||
| flag.Parse() | ||
|
|
||
| bus, err := gcppubsub.NewCloudPubSubBusDispatcher(ref, projectID, opts) | ||
| bus, err := gcppubsub.NewCloudPubSubBusDispatcher(ref, busUID, projectID, opts) | ||
| if err != nil { | ||
| logger.Fatalf("Error starting pub/sub bus dispatcher: %v", err) | ||
| } | ||
|
|
||
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.
what is ref in this context? It looks like it is being passed in but I would assume a ref to a bus would point to the object that is being created with
&CloudPubSubBus{...}