From 61cf091d51a4d945ad925559cf0ef1ab344bc826 Mon Sep 17 00:00:00 2001 From: Amir Malka Date: Tue, 26 Nov 2024 12:18:24 +0200 Subject: [PATCH] send connected clients message upon startup Signed-off-by: Amir Malka --- adapters/backend/v1/client.go | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/adapters/backend/v1/client.go b/adapters/backend/v1/client.go index 8ce774f..a8112ad 100644 --- a/adapters/backend/v1/client.go +++ b/adapters/backend/v1/client.go @@ -4,7 +4,9 @@ import ( "context" "encoding/json" "fmt" + "os" "slices" + "time" "github.com/kubescape/go-logger" "github.com/kubescape/go-logger/helpers" @@ -36,6 +38,10 @@ func (c *Client) Start(ctx context.Context) error { return fmt.Errorf("failed to produce server connected message: %w", err) } + if err := c.sendSingleConnectedClientsMessage(ctx); err != nil { + return fmt.Errorf("failed to produce connected clients message: %w", err) + } + return nil } @@ -69,6 +75,40 @@ func (c *Client) sendServerConnectedMessage(ctx context.Context) error { return c.messageProducer.ProduceMessage(ctx, id, messaging.MsgPropEventValueServerConnectedMessage, data) } +func (c *Client) sendSingleConnectedClientsMessage(ctx context.Context) error { + id := utils.ClientIdentifierFromContext(ctx) + + hostname, _ := os.Hostname() + msg := messaging.ConnectedClientsMessage{ + ServerName: hostname, + Clients: []messaging.ConnectedClient{ + { + Account: id.Account, + Cluster: id.Cluster, + SynchronizerVersion: id.SyncVersion, + HelmVersion: id.HelmVersion, + ConnectionId: id.ConnectionId, + ConnectionTime: id.ConnectionTime, + GitVersion: id.GitVersion, + CloudProvider: id.CloudProvider, + }}, + Timestamp: time.Now(), + MsgId: utils.NewMsgId(), + } + + logger.L().Debug("sending connected client message to producer upon startup", + helpers.String("account", id.Account), + helpers.String("cluster", id.Cluster), + helpers.String("msgid", msg.MsgId)) + + data, err := json.Marshal(msg) + if err != nil { + return fmt.Errorf("marshal connected clients message: %w", err) + } + + return c.messageProducer.ProduceMessage(ctx, id, messaging.MsgPropEventValueConnectedClientsMessage, data) +} + // no need to implement callPutOrPatch because we don't send patches from backend // so you just call c.callbacks.PutObject instead