-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathcommand_plugin.go
More file actions
324 lines (285 loc) · 10.5 KB
/
command_plugin.go
File metadata and controls
324 lines (285 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.
package command
import (
"context"
"log/slog"
"sync"
"google.golang.org/protobuf/types/known/timestamppb"
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/internal/bus"
"github.com/nginx/agent/v3/internal/config"
"github.com/nginx/agent/v3/internal/grpc"
"github.com/nginx/agent/v3/internal/logger"
pkgConfig "github.com/nginx/agent/v3/pkg/config"
"github.com/nginx/agent/v3/pkg/id"
)
var _ bus.Plugin = (*CommandPlugin)(nil)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6@v6.8.1 -generate
//counterfeiter:generate . commandService
type (
commandService interface {
UpdateDataPlaneStatus(ctx context.Context, resource *mpi.Resource) error
UpdateDataPlaneHealth(ctx context.Context, instanceHealths []*mpi.InstanceHealth) error
SendDataPlaneResponse(ctx context.Context, response *mpi.DataPlaneResponse) error
UpdateClient(client mpi.CommandServiceClient)
Subscribe(ctx context.Context)
IsConnected() bool
CreateConnection(ctx context.Context, resource *mpi.Resource) (*mpi.CreateConnectionResponse, error)
}
CommandPlugin struct {
messagePipe bus.MessagePipeInterface
config *config.Config
subscribeCancel context.CancelFunc
conn grpc.GrpcConnectionInterface
commandService commandService
subscribeChannel chan *mpi.ManagementPlaneRequest
subscribeMutex sync.Mutex
}
)
func NewCommandPlugin(agentConfig *config.Config, grpcConnection grpc.GrpcConnectionInterface) *CommandPlugin {
return &CommandPlugin{
config: agentConfig,
conn: grpcConnection,
subscribeChannel: make(chan *mpi.ManagementPlaneRequest),
}
}
func (cp *CommandPlugin) Init(ctx context.Context, messagePipe bus.MessagePipeInterface) error {
slog.DebugContext(ctx, "Starting command plugin")
cp.messagePipe = messagePipe
cp.commandService = NewCommandService(cp.conn.CommandServiceClient(), cp.config, cp.subscribeChannel)
go cp.monitorSubscribeChannel(ctx)
return nil
}
func (cp *CommandPlugin) Close(ctx context.Context) error {
slog.InfoContext(ctx, "Canceling subscribe context")
cp.subscribeMutex.Lock()
if cp.subscribeCancel != nil {
cp.subscribeCancel()
}
cp.subscribeMutex.Unlock()
return cp.conn.Close(ctx)
}
func (cp *CommandPlugin) Info() *bus.Info {
return &bus.Info{
Name: "command",
}
}
func (cp *CommandPlugin) Process(ctx context.Context, msg *bus.Message) {
switch msg.Topic {
case bus.ConnectionResetTopic:
cp.processConnectionReset(ctx, msg)
case bus.ResourceUpdateTopic:
cp.processResourceUpdate(ctx, msg)
case bus.InstanceHealthTopic:
cp.processInstanceHealth(ctx, msg)
case bus.DataPlaneHealthResponseTopic:
cp.processDataPlaneHealth(ctx, msg)
case bus.DataPlaneResponseTopic:
cp.processDataPlaneResponse(ctx, msg)
default:
slog.DebugContext(ctx, "Command plugin unknown topic", "topic", msg.Topic)
}
}
func (cp *CommandPlugin) processResourceUpdate(ctx context.Context, msg *bus.Message) {
if resource, ok := msg.Data.(*mpi.Resource); ok {
if !cp.commandService.IsConnected() && cp.config.IsFeatureEnabled(pkgConfig.FeatureConnection) {
cp.createConnection(ctx, resource)
} else {
statusErr := cp.commandService.UpdateDataPlaneStatus(ctx, resource)
if statusErr != nil {
slog.ErrorContext(ctx, "Unable to update data plane status", "error", statusErr)
}
}
}
}
func (cp *CommandPlugin) createConnection(ctx context.Context, resource *mpi.Resource) {
var subscribeCtx context.Context
createConnectionResponse, err := cp.commandService.CreateConnection(ctx, resource)
if err != nil {
slog.ErrorContext(ctx, "Unable to create connection", "error", err)
}
if createConnectionResponse != nil {
cp.subscribeMutex.Lock()
subscribeCtx, cp.subscribeCancel = context.WithCancel(ctx)
cp.subscribeMutex.Unlock()
go cp.commandService.Subscribe(subscribeCtx)
cp.messagePipe.Process(ctx, &bus.Message{
Topic: bus.ConnectionCreatedTopic,
Data: createConnectionResponse,
})
}
}
func (cp *CommandPlugin) processDataPlaneHealth(ctx context.Context, msg *bus.Message) {
if instances, ok := msg.Data.([]*mpi.InstanceHealth); ok {
err := cp.commandService.UpdateDataPlaneHealth(ctx, instances)
correlationID := logger.GetCorrelationID(ctx)
if err != nil {
slog.ErrorContext(ctx, "Unable to update data plane health", "error", err)
cp.messagePipe.Process(ctx, &bus.Message{
Topic: bus.DataPlaneResponseTopic,
Data: cp.createDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE,
"Failed to send the health status update", err.Error()),
})
}
cp.messagePipe.Process(ctx, &bus.Message{
Topic: bus.DataPlaneResponseTopic,
Data: cp.createDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK,
"Successfully sent the health status update", ""),
})
}
}
func (cp *CommandPlugin) processInstanceHealth(ctx context.Context, msg *bus.Message) {
if instances, ok := msg.Data.([]*mpi.InstanceHealth); ok {
err := cp.commandService.UpdateDataPlaneHealth(ctx, instances)
if err != nil {
slog.ErrorContext(ctx, "Unable to update data plane health", "error", err)
}
}
}
func (cp *CommandPlugin) processDataPlaneResponse(ctx context.Context, msg *bus.Message) {
if response, ok := msg.Data.(*mpi.DataPlaneResponse); ok {
err := cp.commandService.SendDataPlaneResponse(ctx, response)
if err != nil {
slog.ErrorContext(ctx, "Unable to send data plane response", "error", err)
}
}
}
func (cp *CommandPlugin) processConnectionReset(ctx context.Context, msg *bus.Message) {
slog.DebugContext(ctx, "Command plugin received connection reset")
if newConnection, ok := msg.Data.(grpc.GrpcConnectionInterface); ok {
err := cp.conn.Close(ctx)
if err != nil {
slog.ErrorContext(ctx, "Command plugin: unable to close connection", "error", err)
}
cp.conn = newConnection
cp.commandService.UpdateClient(cp.conn.CommandServiceClient())
slog.DebugContext(ctx, "Command plugin: client reset successfully")
}
}
func (cp *CommandPlugin) Subscriptions() []string {
return []string{
bus.ConnectionResetTopic,
bus.ResourceUpdateTopic,
bus.InstanceHealthTopic,
bus.DataPlaneHealthResponseTopic,
bus.DataPlaneResponseTopic,
}
}
func (cp *CommandPlugin) monitorSubscribeChannel(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case message := <-cp.subscribeChannel:
newCtx := context.WithValue(
ctx,
logger.CorrelationIDContextKey,
slog.Any(logger.CorrelationIDKey, message.GetMessageMeta().GetCorrelationId()),
)
slog.DebugContext(newCtx, "Received management plane request", "request", message)
switch message.GetRequest().(type) {
case *mpi.ManagementPlaneRequest_ConfigUploadRequest:
cp.handleConfigUploadRequest(newCtx, message)
case *mpi.ManagementPlaneRequest_ConfigApplyRequest:
cp.handleConfigApplyRequest(newCtx, message)
case *mpi.ManagementPlaneRequest_HealthRequest:
cp.handleHealthRequest(newCtx)
case *mpi.ManagementPlaneRequest_ActionRequest:
cp.handleAPIActionRequest(newCtx, message)
default:
slog.DebugContext(newCtx, "Management plane request not implemented yet")
}
}
}
}
func (cp *CommandPlugin) handleAPIActionRequest(ctx context.Context, message *mpi.ManagementPlaneRequest) {
if cp.config.IsFeatureEnabled(pkgConfig.FeatureAPIAction) {
cp.messagePipe.Process(ctx, &bus.Message{Topic: bus.APIActionRequestTopic, Data: message})
} else {
slog.WarnContext(
ctx,
"API Action Request feature disabled. Unable to process API action request",
"request", message,
)
err := cp.commandService.SendDataPlaneResponse(ctx, &mpi.DataPlaneResponse{
MessageMeta: message.GetMessageMeta(),
CommandResponse: &mpi.CommandResponse{
Status: mpi.CommandResponse_COMMAND_STATUS_FAILURE,
Message: "API action failed",
Error: "API action feature is disabled",
},
InstanceId: message.GetActionRequest().GetInstanceId(),
})
if err != nil {
slog.ErrorContext(ctx, "Unable to send data plane response", "error", err)
}
}
}
func (cp *CommandPlugin) handleConfigApplyRequest(newCtx context.Context, message *mpi.ManagementPlaneRequest) {
if cp.config.IsFeatureEnabled(pkgConfig.FeatureConfiguration) {
cp.messagePipe.Process(newCtx, &bus.Message{Topic: bus.ConfigApplyRequestTopic, Data: message})
} else {
slog.WarnContext(
newCtx,
"Configuration feature disabled. Unable to process config apply request",
"request", message,
)
err := cp.commandService.SendDataPlaneResponse(newCtx, &mpi.DataPlaneResponse{
MessageMeta: message.GetMessageMeta(),
CommandResponse: &mpi.CommandResponse{
Status: mpi.CommandResponse_COMMAND_STATUS_FAILURE,
Message: "Config apply failed",
Error: "Configuration feature is disabled",
},
InstanceId: message.GetConfigApplyRequest().GetOverview().GetConfigVersion().GetInstanceId(),
})
if err != nil {
slog.ErrorContext(newCtx, "Unable to send data plane response", "error", err)
}
}
}
func (cp *CommandPlugin) handleConfigUploadRequest(newCtx context.Context, message *mpi.ManagementPlaneRequest) {
if cp.config.IsFeatureEnabled(pkgConfig.FeatureConfiguration) {
cp.messagePipe.Process(newCtx, &bus.Message{Topic: bus.ConfigUploadRequestTopic, Data: message})
} else {
slog.WarnContext(
newCtx,
"Configuration feature disabled. Unable to process config upload request",
"request", message,
)
err := cp.commandService.SendDataPlaneResponse(newCtx, &mpi.DataPlaneResponse{
MessageMeta: message.GetMessageMeta(),
CommandResponse: &mpi.CommandResponse{
Status: mpi.CommandResponse_COMMAND_STATUS_FAILURE,
Message: "Config upload failed",
Error: "Configuration feature is disabled",
},
InstanceId: message.GetConfigUploadRequest().GetOverview().GetConfigVersion().GetInstanceId(),
})
if err != nil {
slog.ErrorContext(newCtx, "Unable to send data plane response", "error", err)
}
}
}
func (cp *CommandPlugin) handleHealthRequest(newCtx context.Context) {
cp.messagePipe.Process(newCtx, &bus.Message{Topic: bus.DataPlaneHealthRequestTopic})
}
func (cp *CommandPlugin) createDataPlaneResponse(correlationID string, status mpi.CommandResponse_CommandStatus,
message, err string,
) *mpi.DataPlaneResponse {
return &mpi.DataPlaneResponse{
MessageMeta: &mpi.MessageMeta{
MessageId: id.GenerateMessageID(),
CorrelationId: correlationID,
Timestamp: timestamppb.Now(),
},
CommandResponse: &mpi.CommandResponse{
Status: status,
Message: message,
Error: err,
},
}
}