-
Notifications
You must be signed in to change notification settings - Fork 769
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
fix: refactoring to remove pubsub flags to improve experience #3339
Changes from all commits
63af521
d7733dd
85ccad7
f84bc2b
2d27928
aecd00d
d5cfb9b
acc6280
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 |
---|---|---|
|
@@ -12,6 +12,9 @@ import ( | |
type ClientConfig struct { | ||
// Name of the component to be used for pub sub messaging | ||
Component string `json:"component"` | ||
|
||
// Topic where the messages would be published for the connection | ||
Topic string `json:"topic"` | ||
} | ||
|
||
// Dapr represents driver for interacting with pub sub using dapr. | ||
|
@@ -21,19 +24,22 @@ type Dapr struct { | |
|
||
// Name of the pubsub component | ||
pubSubComponent string | ||
|
||
// Topic where the messages would be published for the connection | ||
topic string | ||
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 part of the connection/channel config should be specify-able by downstream users (e.g. constraint authors) and what should be owned by the author of the constraint objects? We should think about the personas who would be interacting with these knobs. Are the people setting up the infra always the same people writing the policy? |
||
} | ||
|
||
const ( | ||
Name = "dapr" | ||
) | ||
|
||
func (r *Dapr) Publish(_ context.Context, data interface{}, topic string) error { | ||
func (r *Dapr) Publish(_ context.Context, data interface{}) error { | ||
jsonData, err := json.Marshal(data) | ||
if err != nil { | ||
return fmt.Errorf("error marshaling data: %w", err) | ||
} | ||
|
||
err = r.client.PublishEvent(context.Background(), r.pubSubComponent, topic, jsonData) | ||
err = r.client.PublishEvent(context.Background(), r.pubSubComponent, r.topic, jsonData) | ||
if err != nil { | ||
return fmt.Errorf("error publishing message to dapr: %w", err) | ||
} | ||
|
@@ -56,6 +62,11 @@ func (r *Dapr) UpdateConnection(_ context.Context, config interface{}) error { | |
return fmt.Errorf("failed to get value of component") | ||
} | ||
r.pubSubComponent = cfg.Component | ||
cfg.Topic, ok = m["topic"].(string) | ||
if !ok { | ||
return fmt.Errorf("failed to get value of topic") | ||
} | ||
r.topic = cfg.Topic | ||
return nil | ||
} | ||
|
||
|
@@ -70,6 +81,10 @@ func NewConnection(_ context.Context, config interface{}) (connection.Connection | |
if !ok { | ||
return nil, fmt.Errorf("failed to get value of component") | ||
} | ||
cfg.Topic, ok = m["topic"].(string) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to get value of topic") | ||
} | ||
|
||
tmp, err := daprClient.NewClient() | ||
if err != nil { | ||
|
@@ -79,5 +94,6 @@ func NewConnection(_ context.Context, config interface{}) (connection.Connection | |
return &Dapr{ | ||
client: tmp, | ||
pubSubComponent: cfg.Component, | ||
topic: cfg.Topic, | ||
}, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package pubsub | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"sync" | ||
|
||
|
@@ -19,16 +20,19 @@ func NewSystem() *System { | |
return &System{} | ||
} | ||
|
||
func (s *System) Publish(_ context.Context, connection string, topic string, msg interface{}) error { | ||
func (s *System) Publish(ctx context.Context, msg interface{}) error { | ||
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. Are we still looking at supporting multiple queues simultaneously? Possibly specifying which connection/topic gets pushed to as part of a constraint's enforcement action? Example: send violation X to security queue, violation Y to ops queue? If so, |
||
s.mux.RLock() | ||
defer s.mux.RUnlock() | ||
if len(s.connections) > 0 { | ||
if c, ok := s.connections[connection]; ok { | ||
return c.Publish(context.Background(), msg, topic) | ||
} | ||
return fmt.Errorf("connection is not initialized, name: %s ", connection) | ||
var errs error | ||
|
||
if len(s.connections) == 0 { | ||
return fmt.Errorf("no connections are established") | ||
} | ||
|
||
for _, c := range s.connections { | ||
errs = errors.Join(errs, c.Publish(ctx, msg)) | ||
} | ||
return fmt.Errorf("No connections are established") | ||
return errs | ||
} | ||
|
||
func (s *System) UpsertConnection(ctx context.Context, config interface{}, name string, provider string) error { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,5 +24,6 @@ data: | |
provider: "dapr" | ||
config: | | ||
{ | ||
"component": "pubsub" | ||
"component": "pubsub", | ||
"topic": "audit-channel" | ||
} |
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 think you removed the wrong value here?
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.
yip! I will update that.
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.
yea