Skip to content
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

send connected clients message upon startup #96

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions adapters/backend/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"slices"
"time"

"github.com/kubescape/go-logger"
"github.com/kubescape/go-logger/helpers"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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

Expand Down
Loading