/
server.go
421 lines (365 loc) · 13.3 KB
/
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
package main
import (
context "context"
"flag"
"fmt"
"log"
"net"
"os"
"strconv"
pb "github.com/microsoft/kalypso-observability-hub/storage/api/grpc/proto"
db "github.com/microsoft/kalypso-observability-hub/storage/postgres"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
)
var (
port *string
postgresHost *string
postgresPort *string
postgresUser *string
postgresPassword *string
postgresDbName *string
postgresSslmode *string
portInt int
postgresPortInt int
)
type storageApiServer struct {
pb.UnimplementedStorageApiServer
dbClient db.DBClient
}
// make sure that the server implements the interface
var _ pb.StorageApiServer = (*storageApiServer)(nil)
func (s *storageApiServer) UpdateWorkspace(ctx context.Context, workspace *pb.Workspace) (*pb.Workspace, error) {
log.Printf("Received Workspace: %v", workspace)
ws, err := s.dbClient.Update(ctx, &db.Workspace{
Name: workspace.Name,
Description: workspace.Description,
})
if err != nil {
return nil, err
}
workspace_entity := ws.(*db.Workspace)
log.Printf("Updated Workspace: %v", workspace_entity)
//return the workspace
return &pb.Workspace{
Id: int32(workspace_entity.Id),
Name: workspace_entity.Name,
Description: workspace_entity.Description,
}, nil
}
func (s *storageApiServer) UpdateApplication(ctx context.Context, application *pb.Application) (*pb.Application, error) {
log.Printf("Received Application: %v", application)
app, err := s.dbClient.Update(ctx, &db.Application{
Name: application.Name,
Description: application.Description,
WorkspaceId: int(application.WorkspaceId),
})
if err != nil {
return nil, err
}
application_entity := app.(*db.Application)
log.Printf("Updated Application: %v", application_entity)
//return the application
return &pb.Application{
Id: int32(application_entity.Id),
Name: application_entity.Name,
Description: application_entity.Description,
WorkspaceId: int32(application_entity.WorkspaceId),
}, nil
}
func (s *storageApiServer) UpdateWorkload(ctx context.Context, workload *pb.Workload) (*pb.Workload, error) {
log.Printf("Received Workload: %v", workload)
wl, err := s.dbClient.Update(ctx, &db.Workload{
Name: workload.Name,
Description: workload.Description,
SourceStorageType: workload.SourceStorageType,
SourceEndpoint: workload.SourceEndpoint,
ApplicationId: int(workload.ApplicationId),
})
if err != nil {
return nil, err
}
workload_entity := wl.(*db.Workload)
log.Printf("Updated Workload: %v", workload_entity)
//return the workload
return &pb.Workload{
Id: int32(workload_entity.Id),
Name: workload_entity.Name,
Description: workload_entity.Description,
SourceStorageType: workload_entity.SourceStorageType,
SourceEndpoint: workload_entity.SourceEndpoint,
ApplicationId: int32(workload_entity.ApplicationId),
}, nil
}
func (s *storageApiServer) UpdateEnvironment(ctx context.Context, environment *pb.Environment) (*pb.Environment, error) {
log.Printf("Received Environment: %v", environment)
env, err := s.dbClient.Update(ctx, &db.Environment{
Name: environment.Name,
Description: environment.Description,
})
if err != nil {
return nil, err
}
environment_entity := env.(*db.Environment)
log.Printf("Updated Environment: %v", environment_entity)
//return the environment
return &pb.Environment{
Id: int32(environment_entity.Id),
Name: environment_entity.Name,
Description: environment_entity.Description,
}, nil
}
// Update DeploymentTarget
func (s *storageApiServer) UpdateDeploymentTarget(ctx context.Context, deploymentTarget *pb.DeploymentTarget) (*pb.DeploymentTarget, error) {
log.Printf("Received DeploymentTarget: %v", deploymentTarget)
dt, err := s.dbClient.Update(ctx, &db.DeploymentTarget{
Name: deploymentTarget.Name,
Description: deploymentTarget.Description,
WorkloadId: int(deploymentTarget.WorkloadId),
EnvironmentId: int(deploymentTarget.EnvironmentId),
Labels: deploymentTarget.Labels,
ManifestsStorageType: deploymentTarget.ManifestsStorageType,
ManifestsEndpoint: deploymentTarget.ManifestsEndpoint,
})
if err != nil {
return nil, err
}
deploymentTarget_entity := dt.(*db.DeploymentTarget)
log.Printf("Updated DeploymentTarget: %v", deploymentTarget_entity)
//return the deploymentTarget
return &pb.DeploymentTarget{
Id: int32(deploymentTarget_entity.Id),
Name: deploymentTarget_entity.Name,
Description: deploymentTarget_entity.Description,
WorkloadId: int32(deploymentTarget_entity.WorkloadId),
EnvironmentId: int32(deploymentTarget_entity.EnvironmentId),
Labels: deploymentTarget_entity.Labels,
ManifestsStorageType: deploymentTarget_entity.ManifestsStorageType,
ManifestsEndpoint: deploymentTarget_entity.ManifestsEndpoint,
}, nil
}
// Update WorkloadVersion
func (s *storageApiServer) UpdateWorkloadVersion(ctx context.Context, workloadVersion *pb.WorkloadVersion) (*pb.WorkloadVersion, error) {
log.Printf("Received WorkloadVersion: %v", workloadVersion)
wv, err := s.dbClient.Update(ctx, &db.WorkloadVersion{
WorkloadId: int(workloadVersion.WorkloadId),
Version: workloadVersion.Version,
BuildId: workloadVersion.BuildId,
BuildCommitId: workloadVersion.BuildCommitId,
})
if err != nil {
return nil, err
}
workloadVersion_entity := wv.(*db.WorkloadVersion)
log.Printf("Updated WorkloadVersion: %v", workloadVersion_entity)
//return the workloadVersion
return &pb.WorkloadVersion{
Id: int32(workloadVersion_entity.Id),
WorkloadId: int32(workloadVersion_entity.WorkloadId),
Version: workloadVersion_entity.Version,
BuildId: workloadVersion_entity.BuildId,
BuildCommitId: workloadVersion_entity.BuildCommitId,
}, nil
}
// Update DeploymentAssignment
func (s *storageApiServer) UpdateDeploymentAssignment(ctx context.Context, deploymentAssignment *pb.DeploymentAssignment) (*pb.DeploymentAssignment, error) {
log.Printf("Received DeploymentAssignment: %v", deploymentAssignment)
da, err := s.dbClient.Update(ctx, &db.DeploymentAssignment{
DeploymentTargetId: int(deploymentAssignment.DeploymentTargetId),
WorkloadVersionId: int(deploymentAssignment.WorkloadVersionId),
GitopsCommitId: deploymentAssignment.GitopsCommitId,
})
if err != nil {
return nil, err
}
deploymentAssignment_entity := da.(*db.DeploymentAssignment)
log.Printf("Updated DeploymentAssignment: %v", deploymentAssignment_entity)
//return the deploymentAssignment
return &pb.DeploymentAssignment{
Id: int32(deploymentAssignment_entity.Id),
DeploymentTargetId: int32(deploymentAssignment_entity.DeploymentTargetId),
WorkloadVersionId: int32(deploymentAssignment_entity.WorkloadVersionId),
GitopsCommitId: deploymentAssignment_entity.GitopsCommitId,
}, nil
}
// Update Host
func (s *storageApiServer) UpdateHost(ctx context.Context, host *pb.Host) (*pb.Host, error) {
log.Printf("Received Host: %v", host)
h, err := s.dbClient.Update(ctx, &db.Host{
Name: host.Name,
Description: host.Description,
HostType: host.HostType,
Labels: host.Labels,
})
if err != nil {
return nil, err
}
host_entity := h.(*db.Host)
log.Printf("Updated Host: %v", host_entity)
//return the host
return &pb.Host{
Id: int32(host_entity.Id),
Name: host_entity.Name,
Description: host_entity.Description,
HostType: host_entity.HostType,
Labels: host_entity.Labels,
}, nil
}
// Update Reconciler
func (s *storageApiServer) UpdateReconciler(ctx context.Context, reconciler *pb.Reconciler) (*pb.Reconciler, error) {
log.Printf("Received Reconciler: %v", reconciler)
r, err := s.dbClient.Update(ctx, &db.Reconciler{
Name: reconciler.Name,
Description: reconciler.Description,
HostId: int(reconciler.HostId),
ReconcilerType: reconciler.ReconcilerType,
Labels: reconciler.Labels,
ManifestsStorageType: reconciler.ManifestsStorageType,
ManifestsEndpoint: reconciler.ManifestsEndpoint,
})
if err != nil {
return nil, err
}
reconciler_entity := r.(*db.Reconciler)
log.Printf("Updated Reconciler: %v", reconciler_entity)
//return the reconciler
return &pb.Reconciler{
Id: int32(reconciler_entity.Id),
Name: reconciler_entity.Name,
Description: reconciler_entity.Description,
HostId: int32(reconciler_entity.HostId),
ReconcilerType: reconciler_entity.ReconcilerType,
Labels: reconciler_entity.Labels,
ManifestsStorageType: reconciler_entity.ManifestsStorageType,
ManifestsEndpoint: reconciler_entity.ManifestsEndpoint,
}, nil
}
// Update Deployment
func (s *storageApiServer) UpdateDeployment(ctx context.Context, deployment *pb.Deployment) (*pb.Deployment, error) {
log.Printf("Received Deployment: %v", deployment)
d, err := s.dbClient.Update(ctx, &db.Deployment{
GitopsCommitId: deployment.GitopsCommitId,
ReconcilerId: int(deployment.ReconcilerId),
Status: deployment.Status,
StatusMessage: deployment.StatusMessage,
})
if err != nil {
return nil, err
}
deployment_entity := d.(*db.Deployment)
log.Printf("Updated Deployment: %v", deployment_entity)
//return the deployment
return &pb.Deployment{
Id: int32(deployment_entity.Id),
GitopsCommitId: deployment_entity.GitopsCommitId,
ReconcilerId: int32(deployment_entity.ReconcilerId),
Status: deployment_entity.Status,
StatusMessage: deployment_entity.StatusMessage,
}, nil
}
// Get Deployment Target
func (s *storageApiServer) GetDeploymentTarget(ctx context.Context, deploymentTargetSearch *pb.DeploymentTargetSearch) (*pb.DeploymentTarget, error) {
log.Printf("Received DeploymentTargetSearch: %v", deploymentTargetSearch)
//Get Environment by natural key
env, err := s.dbClient.GetByNaturalKey(ctx, &db.Environment{
Name: deploymentTargetSearch.EnvironmentName,
})
if err != nil {
return nil, err
}
//Get Workspace by natural key
ws, err := s.dbClient.GetByNaturalKey(ctx, &db.Workspace{
Name: deploymentTargetSearch.WorkspaceName,
})
if err != nil {
return nil, err
}
//Get Application by natural key
app, err := s.dbClient.GetByNaturalKey(ctx, &db.Application{
Name: deploymentTargetSearch.ApplicationName,
WorkspaceId: ws.(*db.Workspace).Id,
})
if err != nil {
return nil, err
}
//Get Workload by natural key
wl, err := s.dbClient.GetByNaturalKey(ctx, &db.Workload{
Name: deploymentTargetSearch.WorkloadName,
ApplicationId: app.(*db.Application).Id,
})
if err != nil {
return nil, err
}
//Get DeploymentTarget by natural key
dt, err := s.dbClient.GetByNaturalKey(ctx, &db.DeploymentTarget{
Name: deploymentTargetSearch.DeploymentTargetName,
EnvironmentId: env.(*db.Environment).Id,
WorkloadId: wl.(*db.Workload).Id,
})
if err != nil {
return nil, err
}
deploymentTarget_entity := dt.(*db.DeploymentTarget)
log.Printf("Got DeploymentTarget: %v", deploymentTarget_entity)
//return the deploymentTarget
return &pb.DeploymentTarget{
Id: int32(deploymentTarget_entity.Id),
Name: deploymentTarget_entity.Name,
Description: deploymentTarget_entity.Description,
WorkloadId: int32(deploymentTarget_entity.WorkloadId),
EnvironmentId: int32(deploymentTarget_entity.EnvironmentId),
Labels: deploymentTarget_entity.Labels,
ManifestsStorageType: deploymentTarget_entity.ManifestsStorageType,
ManifestsEndpoint: deploymentTarget_entity.ManifestsEndpoint,
}, nil
}
func newStorageApiServer(dbClient db.DBClient) *storageApiServer {
return &storageApiServer{dbClient: dbClient}
}
func getEnv(key string) *string {
// if ther is an env variable with the key, return it
if value, ok := os.LookupEnv(key); ok {
return &value
} else {
// throw an error if there is no env variable with the key
log.Fatalf("environment variable %s not set", key)
return nil
}
}
func readConfigValuesFromEnv() {
port = getEnv("PORT")
postgresHost = getEnv("POSTGRES_HOST")
postgresPort = getEnv("POSTGRES_PORT")
postgresUser = getEnv("POSTGRES_USER")
postgresPassword = getEnv("POSTGRES_PASSWORD")
postgresDbName = getEnv("POSTGRES_DBNAME")
postgresSslmode = getEnv("POSTGRES_SSL_MODE")
var err error
portInt, err = strconv.Atoi(*port)
if err != nil {
log.Fatalf("failed to convert port to int: %v", err)
}
postgresPortInt, err = strconv.Atoi(*postgresPort)
if err != nil {
log.Fatalf("failed to convert postgres port to int: %v", err)
}
}
func main() {
flag.Parse()
readConfigValuesFromEnv()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", portInt))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())
dbClient := db.NewPostgresClient(*postgresHost, postgresPortInt, *postgresUser, *postgresPassword, *postgresDbName, *postgresSslmode)
pb.RegisterStorageApiServer(grpcServer, newStorageApiServer(dbClient))
//log starting the server
log.Printf("Starting server on port %d", portInt)
err = grpcServer.Serve(lis)
if err != nil {
log.Fatalf("failed to serve: %v", err)
}
}