Skip to content

Commit e19afb4

Browse files
committed
restarting gRPC conn
1 parent c9d792d commit e19afb4

File tree

4 files changed

+115
-3
lines changed

4 files changed

+115
-3
lines changed

internal/command/command_plugin.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ func (cp *CommandPlugin) processResourceUpdate(ctx context.Context, msg *bus.Mes
9393
if resource, ok := msg.Data.(*mpi.Resource); ok {
9494
if !cp.commandService.IsConnected() && cp.config.IsFeatureEnabled(pkgConfig.FeatureConnection) {
9595
cp.createConnection(ctx, resource)
96+
97+
newConn, err := cp.conn.Restart(ctx)
98+
if err != nil {
99+
slog.ErrorContext(ctx, "Failed to restart connection", "error", err)
100+
return
101+
}
102+
cp.conn = newConn
103+
cp.createConnection(ctx, resource)
104+
96105
} else {
97106
statusErr := cp.commandService.UpdateDataPlaneStatus(ctx, resource)
98107
if statusErr != nil {

internal/config/config.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,12 @@ func registerCommandFlags(fs *flag.FlagSet) {
302302
DefCommandAuthTokenKey,
303303
"The token used in the authentication handshake with the command server endpoint for command and control.",
304304
)
305+
fs.String(
306+
CommandAuthTokenPathKey,
307+
DefCommandAuthTokenPathKey,
308+
"The path to the file containing the token used in the authentication handshake with "+
309+
"the command server endpoint for command and control.",
310+
)
305311
fs.String(
306312
CommandTLSCertKey,
307313
DefCommandTLSCertKey,
@@ -816,9 +822,10 @@ func resolveCommand() *Command {
816822
},
817823
}
818824

819-
if viperInstance.IsSet(CommandAuthTokenKey) {
825+
if viperInstance.IsSet(CommandAuthTokenKey) || viperInstance.IsSet(CommandAuthTokenPathKey) {
820826
command.Auth = &AuthConfig{
821-
Token: viperInstance.GetString(CommandAuthTokenKey),
827+
Token: viperInstance.GetString(CommandAuthTokenKey),
828+
TokenPath: viperInstance.GetString(CommandAuthTokenPathKey),
822829
}
823830
}
824831

internal/grpc/grpc.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type (
4141
CommandServiceClient() mpi.CommandServiceClient
4242
FileServiceClient() mpi.FileServiceClient
4343
Close(ctx context.Context) error
44+
Restart(ctx context.Context) (*GrpcConnection, error)
4445
}
4546

4647
GrpcConnection struct {
@@ -130,6 +131,22 @@ func (gc *GrpcConnection) Close(ctx context.Context) error {
130131
return nil
131132
}
132133

134+
func (gc *GrpcConnection) Restart(ctx context.Context) (*GrpcConnection, error) {
135+
slog.InfoContext(ctx, "Restarting grpc connection")
136+
err := gc.Close(ctx)
137+
if err != nil {
138+
return nil, err
139+
}
140+
141+
slog.InfoContext(ctx, "Creating grpc connection")
142+
newConn, err := NewGrpcConnection(ctx, gc.config)
143+
if err != nil {
144+
return nil, err
145+
}
146+
147+
return newConn, nil
148+
}
149+
133150
func (w *wrappedStream) RecvMsg(message any) error {
134151
err := w.ClientStream.RecvMsg(message)
135152
if err == nil {
@@ -249,7 +266,7 @@ func addPerRPCCredentials(agentConfig *config.Config, resourceID string, opts []
249266
}
250267
}
251268

252-
slog.Debug("Adding token to RPC credentials")
269+
slog.Debug("Adding token to RPC credentials", "token", token)
253270
opts = append(opts,
254271
grpc.WithPerRPCCredentials(
255272
&PerRPCCredentials{

internal/grpc/grpcfakes/fake_grpc_connection_interface.go

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)