-
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
chore: updating pubsub system #3646
base: master
Are you sure you want to change the base?
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
package pubsub | ||
package export | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/open-policy-agent/gatekeeper/v3/pkg/export" | ||
"github.com/open-policy-agent/gatekeeper/v3/pkg/logging" | ||
"github.com/open-policy-agent/gatekeeper/v3/pkg/pubsub" | ||
"github.com/open-policy-agent/gatekeeper/v3/pkg/readiness" | ||
"github.com/open-policy-agent/gatekeeper/v3/pkg/util" | ||
corev1 "k8s.io/api/core/v1" | ||
|
@@ -25,36 +25,36 @@ import ( | |
) | ||
|
||
var ( | ||
PubsubEnabled = flag.Bool("enable-pub-sub", false, "(alpha) Enabled pubsub to publish messages") | ||
ExportEnabled = flag.Bool("enable-pub-sub", false, "(alpha) Enabled pubsub to publish messages") | ||
log = logf.Log.WithName("controller").WithValues(logging.Process, "pubsub_controller") | ||
) | ||
|
||
type Adder struct { | ||
PubsubSystem *pubsub.System | ||
ExportSystem *export.System | ||
} | ||
|
||
func (a *Adder) Add(mgr manager.Manager) error { | ||
if !*PubsubEnabled { | ||
if !*ExportEnabled { | ||
return nil | ||
} | ||
log.Info("Warning: Alpha flag enable-pub-sub is set to true. This flag may change in the future.") | ||
r := newReconciler(mgr, a.PubsubSystem) | ||
r := newReconciler(mgr, a.ExportSystem) | ||
return add(mgr, r) | ||
} | ||
|
||
func (a *Adder) InjectTracker(_ *readiness.Tracker) {} | ||
|
||
func (a *Adder) InjectPubsubSystem(pubsubSystem *pubsub.System) { | ||
a.PubsubSystem = pubsubSystem | ||
func (a *Adder) InjectExportSystem(exportSystem *export.System) { | ||
a.ExportSystem = exportSystem | ||
} | ||
|
||
type Reconciler struct { | ||
client.Client | ||
scheme *runtime.Scheme | ||
system *pubsub.System | ||
system *export.System | ||
} | ||
|
||
func newReconciler(mgr manager.Manager, system *pubsub.System) *Reconciler { | ||
func newReconciler(mgr manager.Manager, system *export.System) *Reconciler { | ||
return &Reconciler{ | ||
Client: mgr.GetClient(), | ||
scheme: mgr.GetScheme(), | ||
|
@@ -63,7 +63,7 @@ func newReconciler(mgr manager.Manager, system *pubsub.System) *Reconciler { | |
} | ||
|
||
func add(mgr manager.Manager, r reconcile.Reconciler) error { | ||
c, err := controller.New("pubsub-config-controller", mgr, controller.Options{Reconciler: r}) | ||
c, err := controller.New("export-config-controller", mgr, controller.Options{Reconciler: r}) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -111,22 +111,22 @@ func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) ( | |
} | ||
|
||
if len(cfg.Data) == 0 { | ||
return reconcile.Result{}, fmt.Errorf(fmt.Sprintf("data missing in configmap %s, unable to configure respective pubsub", request.NamespacedName)) | ||
return reconcile.Result{}, fmt.Errorf(fmt.Sprintf("data missing in configmap %s, unable to establish connection", request.NamespacedName)) | ||
} | ||
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 a "connection" necessarily? 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. "link" may be? I just couldn't come up with better alternative at the time. |
||
if _, ok := cfg.Data["provider"]; !ok { | ||
return reconcile.Result{}, fmt.Errorf(fmt.Sprintf("missing provider field in configmap %s, unable to configure respective pubsub", request.NamespacedName)) | ||
if _, ok := cfg.Data["driver"]; !ok { | ||
return reconcile.Result{}, fmt.Errorf(fmt.Sprintf("missing driver field in configmap %s, unable to establish connection", request.NamespacedName)) | ||
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 changing from config map to CRD coming later? 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. I can raise a follow up PR, we have the design finalized as per last discussion. |
||
} | ||
var config interface{} | ||
err = json.Unmarshal([]byte(cfg.Data["config"]), &config) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
|
||
err = r.system.UpsertConnection(ctx, config, request.Name, cfg.Data["provider"]) | ||
err = r.system.UpsertConnection(ctx, config, request.Name, cfg.Data["driver"]) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
|
||
log.Info("Connection upsert successful", "name", request.Name, "provider", cfg.Data["provider"]) | ||
log.Info("Connection upsert successful", "name", request.Name, "driver", cfg.Data["driver"]) | ||
return reconcile.Result{}, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package dapr | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
daprClient "github.com/dapr/go-sdk/client" | ||
) | ||
|
||
type Connection struct { | ||
// Name of the component object to use in Dapr | ||
component string | ||
|
||
client daprClient.Client | ||
} | ||
|
||
// Dapr represents driver to use Dapr. | ||
type Dapr struct { | ||
openConnections map[string]Connection | ||
} | ||
|
||
const ( | ||
Name = "dapr" | ||
) | ||
|
||
var Connections = &Dapr{ | ||
openConnections: make(map[string]Connection), | ||
} | ||
|
||
func (r *Dapr) Publish(_ context.Context, connectionName string, data interface{}, topic string) error { | ||
jsonData, err := json.Marshal(data) | ||
if err != nil { | ||
return fmt.Errorf("error marshaling data: %w", err) | ||
} | ||
|
||
conn, ok := r.openConnections[connectionName] | ||
if !ok { | ||
return fmt.Errorf("connection not found: %s for Dapr driver", connectionName) | ||
} | ||
err = conn.client.PublishEvent(context.Background(), conn.component, topic, jsonData) | ||
if err != nil { | ||
return fmt.Errorf("error publishing message to dapr: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *Dapr) CloseConnection(connectionName string) error { | ||
delete(r.openConnections, connectionName) | ||
return nil | ||
} | ||
|
||
func (r *Dapr) UpdateConnection(_ context.Context, connectionName string, config interface{}) error { | ||
cfg, ok := config.(map[string]interface{}) | ||
if !ok { | ||
return fmt.Errorf("invalid type assertion, config is not in expected format") | ||
} | ||
component, ok := cfg["component"].(string) | ||
if !ok { | ||
return fmt.Errorf("failed to get value of component") | ||
} | ||
conn := r.openConnections[connectionName] | ||
conn.component = component | ||
r.openConnections[connectionName] = conn | ||
return nil | ||
} | ||
|
||
func (r *Dapr) CreateConnection(_ context.Context, connectionName string, config interface{}) error { | ||
var conn Connection | ||
cfg, ok := config.(map[string]interface{}) | ||
if !ok { | ||
return fmt.Errorf("invalid type assertion, config is not in expected format") | ||
} | ||
conn.component, ok = cfg["component"].(string) | ||
if !ok { | ||
return fmt.Errorf("failed to get value of component") | ||
} | ||
|
||
tmp, err := daprClient.NewClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
conn.client = tmp | ||
r.openConnections[connectionName] = conn | ||
return nil | ||
} |
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.
Do we want to rename the flag to remove
pub-sub
word?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.
May as well while it's still alpha
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.
should we provide a warning for the user if we change the name? we can remove it after a release?
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 do you mean? How would we provide warning for changing a name before release?