-
Notifications
You must be signed in to change notification settings - Fork 40
/
api_container_gateway_service_server.go
434 lines (377 loc) · 19.8 KB
/
api_container_gateway_service_server.go
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package api_container_gateway
import (
"context"
"io"
"sync"
"github.com/kurtosis-tech/kurtosis/api/golang/core/kurtosis_core_rpc_api_bindings"
"github.com/kurtosis-tech/kurtosis/cli/cli/kurtosis_gateway/connection"
"github.com/kurtosis-tech/kurtosis/cli/cli/kurtosis_gateway/server/common"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/port_spec"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/emptypb"
)
const (
localHostIpStr = "127.0.0.1"
errorCallingRemoteApiContainerFromGateway = "Expected to be able to call the remote api container from the gateway, instead a non nil err was returned"
)
type ApiContainerGatewayServiceServer struct {
// ID of enclave the API container is running in
enclaveId string
// Client for the api container we'll be connecting too
remoteApiContainerClient kurtosis_core_rpc_api_bindings.ApiContainerServiceClient
// Provides connections to Kurtosis objectis in cluster
connectionProvider *connection.GatewayConnectionProvider
// ServiceMap and mutex to protect it
mutex *sync.Mutex
userServiceNameToLocalConnectionMap map[string]*runningLocalServiceConnection
// User services port forwarding
userServiceConnect kurtosis_core_rpc_api_bindings.Connect
}
type runningLocalServiceConnection struct {
localPublicServicePorts map[string]*kurtosis_core_rpc_api_bindings.Port
localPublicIp string
kurtosisConnection connection.GatewayConnectionToKurtosis
}
func NewEnclaveApiContainerGatewayServer(connectionProvider *connection.GatewayConnectionProvider, remoteApiContainerClient kurtosis_core_rpc_api_bindings.ApiContainerServiceClient, enclaveId string) (resultCoreGatewayServerService *ApiContainerGatewayServiceServer, resultGatewayCloseFunc func()) {
// Start out with 0 connections to user services
userServiceToLocalConnectionMap := map[string]*runningLocalServiceConnection{}
closeGatewayFunc := func() {
// Stop any port forwarding
for _, runningLocalServiceConnection := range resultCoreGatewayServerService.userServiceNameToLocalConnectionMap {
runningLocalServiceConnection.kurtosisConnection.Stop()
}
}
return &ApiContainerGatewayServiceServer{
remoteApiContainerClient: remoteApiContainerClient,
connectionProvider: connectionProvider,
mutex: &sync.Mutex{},
userServiceNameToLocalConnectionMap: userServiceToLocalConnectionMap,
enclaveId: enclaveId,
userServiceConnect: kurtosis_core_rpc_api_bindings.Connect_CONNECT,
}, closeGatewayFunc
}
func (service *ApiContainerGatewayServiceServer) RunStarlarkScript(args *kurtosis_core_rpc_api_bindings.RunStarlarkScriptArgs, streamToWriteTo kurtosis_core_rpc_api_bindings.ApiContainerService_RunStarlarkScriptServer) error {
logrus.Debug("Executing Starlark script")
streamToReadFrom, err := service.remoteApiContainerClient.RunStarlarkScript(streamToWriteTo.Context(), args)
if err != nil {
return stacktrace.Propagate(err, "An error occurred starting the execution of Kurtosis code")
}
if err := common.ForwardKurtosisExecutionStream[kurtosis_core_rpc_api_bindings.StarlarkRunResponseLine](streamToReadFrom, streamToWriteTo); err != nil {
return stacktrace.Propagate(err, "Error forwarding stream from Kurtosis core back to the user")
}
return nil
}
func (service *ApiContainerGatewayServiceServer) ListFilesArtifactNamesAndUuids(ctx context.Context, _ *emptypb.Empty) (*kurtosis_core_rpc_api_bindings.ListFilesArtifactNamesAndUuidsResponse, error) {
remoteApiContainerResponse, err := service.remoteApiContainerClient.ListFilesArtifactNamesAndUuids(ctx, &emptypb.Empty{})
if err != nil {
return nil, stacktrace.Propagate(err, errorCallingRemoteApiContainerFromGateway)
}
return remoteApiContainerResponse, nil
}
func (service *ApiContainerGatewayServiceServer) RunStarlarkPackage(args *kurtosis_core_rpc_api_bindings.RunStarlarkPackageArgs, streamToWriteTo kurtosis_core_rpc_api_bindings.ApiContainerService_RunStarlarkPackageServer) error {
logrus.Debugf("Executing Starlark package '%s'", args.GetPackageId())
streamToReadFrom, err := service.remoteApiContainerClient.RunStarlarkPackage(streamToWriteTo.Context(), args)
if err != nil {
return stacktrace.Propagate(err, "An error occurred starting the execution of Kurtosis code")
}
if err := common.ForwardKurtosisExecutionStream[kurtosis_core_rpc_api_bindings.StarlarkRunResponseLine](streamToReadFrom, streamToWriteTo); err != nil {
return stacktrace.Propagate(err, "Error forwarding stream from Kurtosis core back to the user while executing package '%s'", args.GetPackageId())
}
return nil
}
func (service *ApiContainerGatewayServiceServer) GetServices(ctx context.Context, args *kurtosis_core_rpc_api_bindings.GetServicesArgs) (*kurtosis_core_rpc_api_bindings.GetServicesResponse, error) {
service.mutex.Lock()
defer service.mutex.Unlock()
remoteApiContainerResponse, err := service.remoteApiContainerClient.GetServices(ctx, args)
if err != nil {
return nil, stacktrace.Propagate(err, errorCallingRemoteApiContainerFromGateway)
}
if service.userServiceConnect == kurtosis_core_rpc_api_bindings.Connect_CONNECT {
// Clean up the removed services when we have the full list of running services
cleanupRemovedServices := len(args.ServiceIdentifiers) == 0
if err := service.updateServicesLocalConnection(remoteApiContainerResponse.ServiceInfo, cleanupRemovedServices); err != nil {
return nil, stacktrace.Propagate(err, "Error updating the services local connection")
}
}
return remoteApiContainerResponse, nil
}
func (service *ApiContainerGatewayServiceServer) ConnectServices(ctx context.Context, args *kurtosis_core_rpc_api_bindings.ConnectServicesArgs) (*kurtosis_core_rpc_api_bindings.ConnectServicesResponse, error) {
service.mutex.Lock()
defer service.mutex.Unlock()
remoteApiContainerResponse, err := service.remoteApiContainerClient.ConnectServices(ctx, args)
if err != nil {
return nil, stacktrace.Propagate(err, errorCallingRemoteApiContainerFromGateway)
}
service.userServiceConnect = args.Connect
return remoteApiContainerResponse, nil
}
func (service *ApiContainerGatewayServiceServer) GetExistingAndHistoricalServiceIdentifiers(ctx context.Context, args *emptypb.Empty) (*kurtosis_core_rpc_api_bindings.GetExistingAndHistoricalServiceIdentifiersResponse, error) {
service.mutex.Lock()
defer service.mutex.Unlock()
remoteApiContainerResponse, err := service.remoteApiContainerClient.GetExistingAndHistoricalServiceIdentifiers(ctx, args)
if err != nil {
return nil, stacktrace.Propagate(err, errorCallingRemoteApiContainerFromGateway)
}
return remoteApiContainerResponse, nil
}
func (service *ApiContainerGatewayServiceServer) ExecCommand(ctx context.Context, args *kurtosis_core_rpc_api_bindings.ExecCommandArgs) (*kurtosis_core_rpc_api_bindings.ExecCommandResponse, error) {
remoteApiContainerResponse, err := service.remoteApiContainerClient.ExecCommand(ctx, args)
if err != nil {
return nil, stacktrace.Propagate(err, errorCallingRemoteApiContainerFromGateway)
}
return remoteApiContainerResponse, nil
}
func (service *ApiContainerGatewayServiceServer) WaitForHttpGetEndpointAvailability(ctx context.Context, args *kurtosis_core_rpc_api_bindings.WaitForHttpGetEndpointAvailabilityArgs) (*emptypb.Empty, error) {
remoteApiContainerResponse, err := service.remoteApiContainerClient.WaitForHttpGetEndpointAvailability(ctx, args)
if err != nil {
return nil, stacktrace.Propagate(err, errorCallingRemoteApiContainerFromGateway)
}
return remoteApiContainerResponse, nil
}
func (service *ApiContainerGatewayServiceServer) WaitForHttpPostEndpointAvailability(ctx context.Context, args *kurtosis_core_rpc_api_bindings.WaitForHttpPostEndpointAvailabilityArgs) (*emptypb.Empty, error) {
remoteApiContainerResponse, err := service.remoteApiContainerClient.WaitForHttpPostEndpointAvailability(ctx, args)
if err != nil {
return nil, stacktrace.Propagate(err, errorCallingRemoteApiContainerFromGateway)
}
return remoteApiContainerResponse, nil
}
func (service *ApiContainerGatewayServiceServer) UploadFilesArtifact(server kurtosis_core_rpc_api_bindings.ApiContainerService_UploadFilesArtifactServer) error {
client, err := service.remoteApiContainerClient.UploadFilesArtifact(server.Context())
if err != nil {
return stacktrace.Propagate(err, errorCallingRemoteApiContainerFromGateway)
}
if err := forwardDataChunkStreamWithClose[*kurtosis_core_rpc_api_bindings.UploadFilesArtifactResponse](server, client); err != nil {
return stacktrace.Propagate(err, "Error forwarding stream from UploadFilesArtifactV2 on gateway")
}
return nil
}
func (service *ApiContainerGatewayServiceServer) StoreWebFilesArtifact(ctx context.Context, args *kurtosis_core_rpc_api_bindings.StoreWebFilesArtifactArgs) (*kurtosis_core_rpc_api_bindings.StoreWebFilesArtifactResponse, error) {
remoteApiContainerResponse, err := service.remoteApiContainerClient.StoreWebFilesArtifact(ctx, args)
if err != nil {
return nil, stacktrace.Propagate(err, errorCallingRemoteApiContainerFromGateway)
}
return remoteApiContainerResponse, nil
}
func (service *ApiContainerGatewayServiceServer) StoreFilesArtifactFromService(ctx context.Context, args *kurtosis_core_rpc_api_bindings.StoreFilesArtifactFromServiceArgs) (*kurtosis_core_rpc_api_bindings.StoreFilesArtifactFromServiceResponse, error) {
remoteApiContainerResponse, err := service.remoteApiContainerClient.StoreFilesArtifactFromService(ctx, args)
if err != nil {
return nil, stacktrace.Propagate(err, errorCallingRemoteApiContainerFromGateway)
}
return remoteApiContainerResponse, nil
}
func (service *ApiContainerGatewayServiceServer) InspectFilesArtifactContents(ctx context.Context, args *kurtosis_core_rpc_api_bindings.InspectFilesArtifactContentsRequest) (*kurtosis_core_rpc_api_bindings.InspectFilesArtifactContentsResponse, error) {
remoteApiContainerResponse, err := service.remoteApiContainerClient.InspectFilesArtifactContents(ctx, args)
if err != nil {
return nil, stacktrace.Propagate(err, errorCallingRemoteApiContainerFromGateway)
}
return remoteApiContainerResponse, nil
}
func (service *ApiContainerGatewayServiceServer) DownloadFilesArtifact(args *kurtosis_core_rpc_api_bindings.DownloadFilesArtifactArgs, server kurtosis_core_rpc_api_bindings.ApiContainerService_DownloadFilesArtifactServer) error {
client, err := service.remoteApiContainerClient.DownloadFilesArtifact(server.Context(), args)
if err != nil {
return stacktrace.Propagate(err, errorCallingRemoteApiContainerFromGateway)
}
if err := forwardDataChunkStream(client, server); err != nil {
return stacktrace.Propagate(err, "Error forwarding stream from DownloadFilesArtifactV2 on gateway")
}
return nil
}
func (service *ApiContainerGatewayServiceServer) UploadStarlarkPackage(server kurtosis_core_rpc_api_bindings.ApiContainerService_UploadStarlarkPackageServer) error {
client, err := service.remoteApiContainerClient.UploadStarlarkPackage(server.Context())
if err != nil {
return stacktrace.Propagate(err, errorCallingRemoteApiContainerFromGateway)
}
if err := forwardDataChunkStreamWithClose[*emptypb.Empty](server, client); err != nil {
return stacktrace.Propagate(err, "Error forwarding stream from UploadStarlarkPackage on gateway")
}
return nil
}
func (service *ApiContainerGatewayServiceServer) GetStarlarkRun(ctx context.Context, args *emptypb.Empty) (*kurtosis_core_rpc_api_bindings.GetStarlarkRunResponse, error) {
remoteApiContainerResponse, err := service.remoteApiContainerClient.GetStarlarkRun(ctx, &emptypb.Empty{})
if err != nil {
return nil, stacktrace.Propagate(err, errorCallingRemoteApiContainerFromGateway)
}
return remoteApiContainerResponse, nil
}
// ====================================================================================================
//
// Private helper methods
//
// ====================================================================================================
// writeOverServiceInfoFieldsWithLocalConnectionInformationIfServiceRunning overwites the `MaybePublicPorts` and `MaybePublicIpAdrr` fields to connect to local ports forwarding requests to private ports in Kubernetes
// Only TCP Private Ports are forwarded
// Does nothing if the service is stopped (no pod running)
func (service *ApiContainerGatewayServiceServer) writeOverServiceInfoFieldsWithLocalConnectionInformationIfServiceRunning(serviceInfo *kurtosis_core_rpc_api_bindings.ServiceInfo) error {
// If the service has no private ports, then don't overwrite any of the service info fields
if len(serviceInfo.PrivatePorts) == 0 {
return nil
}
serviceName := serviceInfo.GetName()
var localConnErr error
var runningLocalConnection *runningLocalServiceConnection
cleanUpConnection := true
runningLocalConnection, isFound := service.userServiceNameToLocalConnectionMap[serviceName]
if !isFound {
runningLocalConnection, localConnErr = service.startRunningConnectionForKurtosisServiceIfRunning(serviceName, serviceInfo.PrivatePorts)
if localConnErr != nil {
return stacktrace.Propagate(localConnErr, "Expected to be able to start a local connection to Kurtosis service '%v', instead a non-nil error was returned", serviceName)
} else if runningLocalConnection == nil {
return nil
}
defer func() {
if cleanUpConnection {
service.idempotentKillRunningConnectionForServiceName(serviceName)
}
}()
}
serviceInfo.MaybePublicPorts = runningLocalConnection.localPublicServicePorts
serviceInfo.MaybePublicIpAddr = runningLocalConnection.localPublicIp
cleanUpConnection = false
return nil
}
// startRunningConnectionForKurtosisServiceIfRunning starts a port forwarding process from kernel assigned local ports to the remote service ports specified
// If privatePortsFromApi is empty, an error is thrown
func (service *ApiContainerGatewayServiceServer) startRunningConnectionForKurtosisServiceIfRunning(serviceName string, privatePortsFromApi map[string]*kurtosis_core_rpc_api_bindings.Port) (*runningLocalServiceConnection, error) {
if len(privatePortsFromApi) == 0 {
return nil, stacktrace.NewError("Expected Kurtosis service to have private ports specified for port forwarding, instead no ports were provided")
}
remotePrivatePortSpecs := map[string]*port_spec.PortSpec{}
for portSpecId, coreApiPort := range privatePortsFromApi {
if coreApiPort.GetTransportProtocol() != kurtosis_core_rpc_api_bindings.Port_TCP {
logrus.Warnf(
"Will not be able to forward service port with id '%v' for service with name '%v' in enclave '%v'. "+
"The protocol of this port is '%v', but only '%v' is supported",
portSpecId,
serviceName,
service.enclaveId,
coreApiPort.GetTransportProtocol(),
kurtosis_core_rpc_api_bindings.Port_TCP.String(),
)
continue
}
portNumberUint16 := uint16(coreApiPort.GetNumber())
remotePortSpec, err := port_spec.NewPortSpec(portNumberUint16, port_spec.TransportProtocol_TCP, coreApiPort.GetMaybeApplicationProtocol(), nil, "")
if err != nil {
return nil, stacktrace.Propagate(err, "Expected to be able to create port spec describing remote port '%v', instead a non-nil error was returned", portSpecId)
}
remotePrivatePortSpecs[portSpecId] = remotePortSpec
}
// Start listening
serviceConnection, err := service.connectionProvider.ForUserServiceIfRunning(service.enclaveId, serviceName, remotePrivatePortSpecs)
if err != nil {
logrus.Errorf("Tried forwarding ports for user service '%v' in enclave '%v' but failed with error:\n%v", serviceName, service.enclaveId, err)
return nil, nil
} else if serviceConnection == nil {
return nil, nil
}
cleanUpConnection := true
defer func() {
if cleanUpConnection {
serviceConnection.Stop()
}
}()
localPublicApiPorts := map[string]*kurtosis_core_rpc_api_bindings.Port{}
for portId, privateApiPort := range privatePortsFromApi {
localPortSpec, found := serviceConnection.GetLocalPorts()[portId]
// Skip the private remote port if no public local port is forwarding to it
if !found {
continue
}
localPublicApiPorts[portId] = &kurtosis_core_rpc_api_bindings.Port{
Number: uint32(localPortSpec.GetNumber()),
TransportProtocol: privateApiPort.GetTransportProtocol(),
MaybeApplicationProtocol: privateApiPort.GetMaybeApplicationProtocol(),
MaybeWaitTimeout: privateApiPort.GetMaybeWaitTimeout(),
}
}
runingLocalServiceConnection := &runningLocalServiceConnection{
kurtosisConnection: serviceConnection,
localPublicServicePorts: localPublicApiPorts,
localPublicIp: localHostIpStr,
}
// Store information about our running gateway
service.userServiceNameToLocalConnectionMap[serviceName] = runingLocalServiceConnection
cleanUpMapEntry := true
defer func() {
if cleanUpMapEntry {
delete(service.userServiceNameToLocalConnectionMap, serviceName)
}
}()
cleanUpMapEntry = false
cleanUpConnection = false
return runingLocalServiceConnection, nil
}
func (service *ApiContainerGatewayServiceServer) idempotentKillRunningConnectionForServiceName(serviceName string) {
runningLocalConnection, isRunning := service.userServiceNameToLocalConnectionMap[serviceName]
// Nothing running, nothing to kill
if !isRunning {
return
}
// Close up the connection
runningLocalConnection.kurtosisConnection.Stop()
// delete the entry for the serve
delete(service.userServiceNameToLocalConnectionMap, serviceName)
}
func (service *ApiContainerGatewayServiceServer) updateServicesLocalConnection(serviceInfos map[string]*kurtosis_core_rpc_api_bindings.ServiceInfo, cleanupRemovedServices bool) error {
serviceNames := map[string]bool{}
for _, serviceInfo := range serviceInfos {
if err := service.writeOverServiceInfoFieldsWithLocalConnectionInformationIfServiceRunning(serviceInfo); err != nil {
return stacktrace.Propagate(err, "Expected to be able to write over service info fields for service '%v', instead a non-nil error was returned", serviceInfo.Name)
}
serviceNames[serviceInfo.GetName()] = true
}
if cleanupRemovedServices {
// Clean up connection for removed services
for serviceName := range service.userServiceNameToLocalConnectionMap {
if _, found := serviceNames[serviceName]; !found {
service.idempotentKillRunningConnectionForServiceName(serviceName)
}
}
}
return nil
}
type dataChunkStreamReceiver interface {
Recv() (*kurtosis_core_rpc_api_bindings.StreamedDataChunk, error)
}
type dataChunkStreamSenderCloserAndReceiver[T any] interface {
dataChunkStreamSender
CloseAndRecv() (T, error)
}
type dataChunkStreamSender interface {
Send(*kurtosis_core_rpc_api_bindings.StreamedDataChunk) error
}
type dataChunkStreamReceiverSenderAndCloser[T any] interface {
dataChunkStreamReceiver
SendAndClose(T) error
}
func forwardDataChunkStream[T dataChunkStreamReceiver, U dataChunkStreamSender](streamToReadFrom T, streamToWriteTo U) error {
for {
dataChunk, readErr := streamToReadFrom.Recv()
if readErr == io.EOF {
logrus.Debug("Finished reading from the Kurtosis response line stream.")
return nil
}
if readErr != nil {
return stacktrace.Propagate(readErr, "Error reading Kurtosis execution lines from Kurtosis core stream")
}
if writeErr := streamToWriteTo.Send(dataChunk); writeErr != nil {
return stacktrace.Propagate(readErr, "Received a Kurtosis execution line but failed forwarding it back to the user")
}
}
}
func forwardDataChunkStreamWithClose[T any, R dataChunkStreamReceiverSenderAndCloser[T], W dataChunkStreamSenderCloserAndReceiver[T]](streamToReadFrom R, streamToWriteTo W) error {
err := forwardDataChunkStream(streamToReadFrom, streamToWriteTo)
if err != nil {
return err
}
uploadResponse, closeErr := streamToWriteTo.CloseAndRecv()
if closeErr != nil {
return stacktrace.Propagate(closeErr, "Error during Kurtosis closing upload client")
}
closeErr = streamToReadFrom.SendAndClose(uploadResponse)
if closeErr != nil {
return stacktrace.Propagate(closeErr, "Error during Kurtosis closing upload server")
}
return nil
}