-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathserver.go
591 lines (505 loc) · 17.4 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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.
package server
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
"slices"
"strings"
"sync"
"time"
"github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker"
"github.com/docker/cli/cli/config/configfile"
"github.com/gitpod-io/gitpod/common-go/baseserver"
"github.com/gitpod-io/gitpod/common-go/experiments"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/common-go/watch"
gitpodapi "github.com/gitpod-io/gitpod/gitpod-protocol"
api "github.com/gitpod-io/gitpod/ide-service-api"
"github.com/gitpod-io/gitpod/ide-service-api/config"
"github.com/heptiolabs/healthcheck"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health/grpc_health_v1"
)
// ResolverProvider provides new resolver
type ResolverProvider func() remotes.Resolver
type IDEServiceServer struct {
config *config.ServiceConfiguration
originIDEConfig []byte
parsedIDEConfigContent string
parsedCode1_85IDEConfigContent string
ideConfig *config.IDEConfig
code1_85IdeConfig *config.IDEConfig
ideConfigFileName string
experimentsClient experiments.Client
resolver ResolverProvider
api.UnimplementedIDEServiceServer
}
func Start(logger *logrus.Entry, cfg *config.ServiceConfiguration) error {
ctx := context.Background()
logger.WithField("config", cfg).Info("Starting ide-service.")
registry := prometheus.NewRegistry()
health := healthcheck.NewHandler()
srv, err := baseserver.New("ide-service",
baseserver.WithLogger(logger),
baseserver.WithConfig(cfg.Server),
baseserver.WithMetricsRegistry(registry),
baseserver.WithHealthHandler(health),
)
if err != nil {
return fmt.Errorf("failed to initialize ide-service: %w", err)
}
var (
dockerCfg *configfile.ConfigFile
dockerCfgMu sync.RWMutex
)
resolverProvider := func() remotes.Resolver {
var resolverOpts docker.ResolverOptions
dockerCfgMu.RLock()
defer dockerCfgMu.RUnlock()
if dockerCfg != nil {
resolverOpts.Hosts = docker.ConfigureDefaultRegistries(
docker.WithAuthorizer(authorizerFromDockerConfig(dockerCfg)),
)
}
return docker.NewResolver(resolverOpts)
}
if cfg.DockerCfg != "" {
dockerCfg = loadDockerCfg(cfg.DockerCfg)
err = watch.File(ctx, cfg.DockerCfg, func() {
dockerCfgMu.Lock()
defer dockerCfgMu.Unlock()
dockerCfg = loadDockerCfg(cfg.DockerCfg)
})
if err != nil {
log.WithError(err).Fatal("cannot start watch of Docker auth configuration file")
}
}
s := New(cfg, resolverProvider)
go s.watchIDEConfig(ctx)
go s.scheduleUpdate(ctx)
s.register(srv.GRPC())
health.AddReadinessCheck("ide-service", func() error {
if s.ideConfig == nil {
return fmt.Errorf("ide config is not ready")
}
return nil
})
health.AddReadinessCheck("grpc-server", grpcProbe(*cfg.Server.Services.GRPC))
if err := srv.ListenAndServe(); err != nil {
return fmt.Errorf("failed to serve ide-service: %w", err)
}
return nil
}
func loadDockerCfg(fn string) *configfile.ConfigFile {
if tproot := os.Getenv("TELEPRESENCE_ROOT"); tproot != "" {
fn = filepath.Join(tproot, fn)
}
fr, err := os.OpenFile(fn, os.O_RDONLY, 0)
if err != nil {
log.WithError(err).Fatal("cannot read docker auth config")
}
dockerCfg := configfile.New(fn)
err = dockerCfg.LoadFromReader(fr)
fr.Close()
if err != nil {
log.WithError(err).Fatal("cannot read docker config")
}
log.WithField("fn", fn).Info("using authentication for backing registries")
return dockerCfg
}
// FromDockerConfig turns docker client config into docker registry hosts
func authorizerFromDockerConfig(cfg *configfile.ConfigFile) docker.Authorizer {
return docker.NewDockerAuthorizer(docker.WithAuthCreds(func(host string) (user, pass string, err error) {
auth, err := cfg.GetAuthConfig(host)
if err != nil {
return
}
user = auth.Username
pass = auth.Password
return
}))
}
func New(cfg *config.ServiceConfiguration, resolver ResolverProvider) *IDEServiceServer {
fn, err := filepath.Abs(cfg.IDEConfigPath)
if err != nil {
log.WithField("path", cfg.IDEConfigPath).WithError(err).Fatal("cannot convert ide config path to abs path")
}
s := &IDEServiceServer{
config: cfg,
ideConfigFileName: fn,
experimentsClient: experiments.NewClient(),
resolver: resolver,
}
return s
}
func (s *IDEServiceServer) register(grpcServer *grpc.Server) {
api.RegisterIDEServiceServer(grpcServer, s)
}
func (s *IDEServiceServer) GetConfig(ctx context.Context, req *api.GetConfigRequest) (*api.GetConfigResponse, error) {
attributes := experiments.Attributes{
UserID: req.User.Id,
UserEmail: req.User.GetEmail(),
}
// Check flag to enable vscode for older linux distros (distros that don't support glibc 2.28)
enableVscodeForOlderLinuxDistros := s.experimentsClient.GetBoolValue(ctx, "enableVscodeForOlderLinuxDistros", false, attributes)
if enableVscodeForOlderLinuxDistros {
return &api.GetConfigResponse{
Content: s.parsedCode1_85IDEConfigContent,
}, nil
}
return &api.GetConfigResponse{
Content: s.parsedIDEConfigContent,
}, nil
}
func (s *IDEServiceServer) readIDEConfig(ctx context.Context, isInit bool) {
ideConfigbuffer, err := os.ReadFile(s.ideConfigFileName)
if err != nil {
log.WithError(err).Warn("cannot read original ide config file")
return
}
if originalIdeConfig, err := ParseConfig(ctx, s.resolver(), ideConfigbuffer); err != nil {
if !isInit {
log.WithError(err).Fatal("cannot parse original ide config")
}
log.WithError(err).Error("cannot parse original ide config")
return
} else {
parsedConfig, err := json.Marshal(originalIdeConfig)
if err != nil {
log.WithError(err).Error("cannot marshal original ide config")
return
}
// Precompute the config without code 1.85
code1_85IdeOptions := originalIdeConfig.IdeOptions.Options
ideOptions := make(map[string]config.IDEOption)
for key, ide := range code1_85IdeOptions {
if key != "code1_85" {
ideOptions[key] = ide
}
}
ideConfig := &config.IDEConfig{
SupervisorImage: originalIdeConfig.SupervisorImage,
IdeOptions: config.IDEOptions{
Options: ideOptions,
DefaultIde: originalIdeConfig.IdeOptions.DefaultIde,
DefaultDesktopIde: originalIdeConfig.IdeOptions.DefaultDesktopIde,
Clients: originalIdeConfig.IdeOptions.Clients,
},
}
parsedIdeConfig, err := json.Marshal(ideConfig)
if err != nil {
log.WithError(err).Error("cannot marshal ide config")
return
}
s.parsedCode1_85IDEConfigContent = string(parsedConfig)
s.code1_85IdeConfig = originalIdeConfig
s.ideConfig = ideConfig
s.parsedIDEConfigContent = string(parsedIdeConfig)
s.originIDEConfig = ideConfigbuffer
log.Info("ide config updated")
}
}
func (s *IDEServiceServer) watchIDEConfig(ctx context.Context) {
go s.readIDEConfig(ctx, true)
// `watch.File` only watch for create and remove event
// so with locally debugging, we cannot watch example ide config file change
// but in k8s, configmap change will create/remove file to replace it
if err := watch.File(ctx, s.ideConfigFileName, func() {
s.readIDEConfig(ctx, false)
}); err != nil {
log.WithError(err).Fatal("cannot start watch of ide config file")
}
}
func (s *IDEServiceServer) scheduleUpdate(ctx context.Context) {
t := time.NewTicker(time.Hour * 1)
for {
select {
case <-t.C:
log.Info("schedule update config")
s.readIDEConfig(ctx, false)
case <-ctx.Done():
t.Stop()
return
}
}
}
func grpcProbe(cfg baseserver.ServerConfiguration) func() error {
return func() error {
creds := insecure.NewCredentials()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, cfg.Address, grpc.WithTransportCredentials(creds))
if err != nil {
return err
}
defer conn.Close()
client := grpc_health_v1.NewHealthClient(conn)
check, err := client.Check(ctx, &grpc_health_v1.HealthCheckRequest{})
if err != nil {
return err
}
if check.Status == grpc_health_v1.HealthCheckResponse_SERVING {
return nil
}
return fmt.Errorf("grpc service not ready")
}
}
type IDESettings struct {
DefaultIde string `json:"defaultIde,omitempty"`
UseLatestVersion bool `json:"useLatestVersion,omitempty"`
PreferToolbox bool `json:"preferToolbox,omitempty"`
PinnedIDEversions map[string]string `json:"pinnedIDEversions,omitempty"`
}
type WorkspaceContext struct {
Referrer string `json:"referrer,omitempty"`
ReferrerIde string `json:"referrerIde,omitempty"`
}
func (s *IDEServiceServer) resolveReferrerIDE(ideConfig *config.IDEConfig, wsCtx *WorkspaceContext, chosenIDEName string) (ideName string, ideOption *config.IDEOption) {
if wsCtx == nil || wsCtx.Referrer == "" {
return
}
client, ok := ideConfig.IdeOptions.Clients[wsCtx.Referrer]
if !ok {
return
}
getValidIDEOption := func(ideName string) (*config.IDEOption, bool) {
optionToCheck, ok := ideConfig.IdeOptions.Options[ideName]
if !ok {
return nil, false
}
for _, ide := range client.DesktopIDEs {
if ide == ideName {
return &optionToCheck, true
}
}
return nil, false
}
ideOption, ok = getValidIDEOption(wsCtx.ReferrerIde)
if ok {
ideName = wsCtx.ReferrerIde
return
}
ideOption, ok = getValidIDEOption(chosenIDEName)
if ok {
ideName = chosenIDEName
return
}
ideOption, ok = getValidIDEOption(client.DefaultDesktopIDE)
if ok {
ideName = client.DefaultDesktopIDE
return
}
return
}
func (s *IDEServiceServer) ResolveWorkspaceConfig(ctx context.Context, req *api.ResolveWorkspaceConfigRequest) (resp *api.ResolveWorkspaceConfigResponse, err error) {
log.WithField("req", req).Debug("receive ResolveWorkspaceConfig request")
// make a copy for ref ideConfig, it's safe because we replace ref in update config
ideConfig := s.code1_85IdeConfig
defaultIdeOption, ok := ideConfig.IdeOptions.Options[ideConfig.IdeOptions.DefaultIde]
if !ok {
// I think it never happen, we have a check to make sure all DefaultIDE should be in Options
log.WithError(err).WithField("defaultIDE", ideConfig.IdeOptions.DefaultIde).Error("IDE configuration corrupt, cannot found defaultIDE")
return nil, fmt.Errorf("IDE configuration corrupt")
}
resp = &api.ResolveWorkspaceConfigResponse{
SupervisorImage: ideConfig.SupervisorImage,
WebImage: defaultIdeOption.Image,
IdeImageLayers: defaultIdeOption.ImageLayers,
}
if os.Getenv("CONFIGCAT_SDK_KEY") != "" {
resp.Envvars = append(resp.Envvars, &api.EnvironmentVariable{
Name: "GITPOD_CONFIGCAT_ENABLED",
Value: "true",
})
}
var wsConfig *gitpodapi.GitpodConfig
if req.WorkspaceConfig != "" {
if err := json.Unmarshal([]byte(req.WorkspaceConfig), &wsConfig); err != nil {
log.WithError(err).WithField("workspaceConfig", req.WorkspaceConfig).Error("failed to parse workspace config")
}
}
var ideSettings *IDESettings
if req.IdeSettings != "" {
if err := json.Unmarshal([]byte(req.IdeSettings), &ideSettings); err != nil {
log.WithError(err).WithField("ideSetting", req.IdeSettings).Error("failed to parse ide settings")
}
}
pinnedIDEversions := make(map[string]string)
if ideSettings != nil {
pinnedIDEversions = ideSettings.PinnedIDEversions
}
getUserIDEImage := func(ide string, useLatest bool) string {
ideOption := ideConfig.IdeOptions.Options[ide]
if useLatest && ideOption.LatestImage != "" {
return ideOption.LatestImage
}
if version, ok := pinnedIDEversions[ide]; ok {
if idx := slices.IndexFunc(ideOption.Versions, func(v config.IDEVersion) bool { return v.Version == version }); idx >= 0 {
return ideOption.Versions[idx].Image
}
}
return ideOption.Image
}
getUserImageLayers := func(ide string, useLatest bool) []string {
ideOption := ideConfig.IdeOptions.Options[ide]
if useLatest {
return ideOption.LatestImageLayers
}
if version, ok := pinnedIDEversions[ide]; ok {
if idx := slices.IndexFunc(ideOption.Versions, func(v config.IDEVersion) bool { return v.Version == version }); idx >= 0 {
return ideOption.Versions[idx].ImageLayers
}
}
return ideOption.ImageLayers
}
if req.Type == api.WorkspaceType_REGULAR {
var wsContext *WorkspaceContext
if req.Context != "" {
if err := json.Unmarshal([]byte(req.Context), &wsContext); err != nil {
log.WithError(err).WithField("context", req.Context).Error("failed to parse context")
}
}
userIdeName := ""
useLatest := false
preferToolbox := false
resultingIdeName := ideConfig.IdeOptions.DefaultIde
chosenIDE := ideConfig.IdeOptions.Options[resultingIdeName]
if ideSettings != nil {
userIdeName = ideSettings.DefaultIde
useLatest = ideSettings.UseLatestVersion
preferToolbox = ideSettings.PreferToolbox
}
if preferToolbox {
preferToolboxEnv := api.EnvironmentVariable{
Name: "GITPOD_PREFER_TOOLBOX",
Value: "true",
}
resp.Envvars = append(resp.Envvars, &preferToolboxEnv)
}
if userIdeName != "" {
if ide, ok := ideConfig.IdeOptions.Options[userIdeName]; ok {
resultingIdeName = userIdeName
chosenIDE = ide
// TODO: Currently this variable reflects the IDE selected in
// user's settings for backward compatibility but in the future
// we want to make it represent the actual IDE.
ideAlias := api.EnvironmentVariable{
Name: "GITPOD_IDE_ALIAS",
Value: userIdeName,
}
resp.Envvars = append(resp.Envvars, &ideAlias)
}
}
// we always need WebImage for when the user chooses a desktop ide
resp.WebImage = getUserIDEImage(ideConfig.IdeOptions.DefaultIde, useLatest)
resp.IdeImageLayers = getUserImageLayers(ideConfig.IdeOptions.DefaultIde, useLatest)
var desktopImageLayer string
var desktopUserImageLayers []string
if chosenIDE.Type == config.IDETypeDesktop {
desktopImageLayer = getUserIDEImage(resultingIdeName, useLatest)
desktopUserImageLayers = getUserImageLayers(resultingIdeName, useLatest)
} else {
resp.WebImage = getUserIDEImage(resultingIdeName, useLatest)
resp.IdeImageLayers = getUserImageLayers(resultingIdeName, useLatest)
}
// TODO (se) this should be handled on the surface (i.e. server or even dashboard) and not passed as a special workspace context down here.
ideName, _ := s.resolveReferrerIDE(ideConfig, wsContext, userIdeName)
if ideName != "" {
resp.RefererIde = ideName
resultingIdeName = ideName
desktopImageLayer = getUserIDEImage(ideName, useLatest)
desktopUserImageLayers = getUserImageLayers(ideName, useLatest)
}
if desktopImageLayer != "" {
resp.IdeImageLayers = append(resp.IdeImageLayers, desktopImageLayer)
resp.IdeImageLayers = append(resp.IdeImageLayers, desktopUserImageLayers...)
}
// we are returning the actually used ide name here, which might be different from the user's choice
ideSettingsEncoded := new(bytes.Buffer)
enc := json.NewEncoder(ideSettingsEncoded)
enc.SetEscapeHTML(false)
resultingIdeSettings := &IDESettings{
DefaultIde: resultingIdeName,
UseLatestVersion: useLatest,
PreferToolbox: preferToolbox,
}
err = enc.Encode(resultingIdeSettings)
if err != nil {
log.WithError(err).Error("cannot marshal ideSettings")
}
resp.IdeSettings = ideSettingsEncoded.String()
}
// TODO figure out how to make it configurable on IDE level, not hardcoded here
jbGW, ok := ideConfig.IdeOptions.Clients["jetbrains-gateway"]
if req.Type == api.WorkspaceType_PREBUILD && ok {
imageLayers := make(map[string]struct{})
for _, alias := range jbGW.DesktopIDEs {
if _, ok := ideConfig.IdeOptions.Options[alias]; !ok {
continue
}
prebuilds := getPrebuilds(wsConfig, alias)
if prebuilds != nil {
if prebuilds.Version != "latest" && prebuilds.Version != "stable" && prebuilds.Version != "both" {
continue
}
if prebuilds.Version != "latest" {
layers := getUserImageLayers(alias, false)
for _, layer := range layers {
if _, ok := imageLayers[layer]; !ok {
imageLayers[layer] = struct{}{}
resp.IdeImageLayers = append(resp.IdeImageLayers, layer)
}
}
resp.IdeImageLayers = append(resp.IdeImageLayers, getUserIDEImage(alias, false))
}
if prebuilds.Version != "stable" {
layers := getUserImageLayers(alias, true)
for _, layer := range layers {
if _, ok := imageLayers[layer]; !ok {
imageLayers[layer] = struct{}{}
resp.IdeImageLayers = append(resp.IdeImageLayers, layer)
}
}
resp.IdeImageLayers = append(resp.IdeImageLayers, getUserIDEImage(alias, true))
}
}
}
}
return
}
func getPrebuilds(config *gitpodapi.GitpodConfig, alias string) *gitpodapi.Prebuilds {
if config == nil || config.Jetbrains == nil {
return nil
}
productConfig := getProductConfig(config, alias)
if productConfig == nil {
return nil
}
return productConfig.Prebuilds
}
func getProductConfig(config *gitpodapi.GitpodConfig, alias string) *gitpodapi.JetbrainsProduct {
defer func() {
if err := recover(); err != nil {
log.WithField("error", err).WithField("alias", alias).Error("failed to extract JB product config")
}
}()
v := reflect.ValueOf(*config.Jetbrains).FieldByNameFunc(func(s string) bool {
return strings.ToLower(s) == alias
}).Interface()
productConfig, ok := v.(*gitpodapi.JetbrainsProduct)
if !ok {
return nil
}
return productConfig
}