Skip to content

Commit 8432f8b

Browse files
akosyakovfelladrinAndrea Falzetti
committed
[supervisor] make it compatible with run-gp
Co-authored-by: Victor Nogueira <[email protected]> Co-authored-by: Andrea Falzetti <[email protected]>
1 parent 034543f commit 8432f8b

File tree

11 files changed

+131
-26
lines changed

11 files changed

+131
-26
lines changed

components/gitpod-cli/cmd/tasks-attach.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ var attachTaskCmd = &cobra.Command{
3030
Short: "Attach to a workspace task",
3131
Args: cobra.MaximumNArgs(1),
3232
Run: func(cmd *cobra.Command, args []string) {
33-
client, err := supervisor.New(context.Background())
33+
client, err := supervisor.New(context.Background(), &supervisor.SupervisorClientOption{
34+
Address: os.Getenv("SUPERVISOR_DEBUG_ADDR"),
35+
})
3436
if err != nil {
3537
log.Fatal(err)
3638
}

components/gitpod-cli/cmd/tasks-list.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ var listTasksCmd = &cobra.Command{
2727
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
2828
defer cancel()
2929

30-
client, err := supervisor.New(ctx)
30+
client, err := supervisor.New(ctx, &supervisor.SupervisorClientOption{
31+
Address: os.Getenv("SUPERVISOR_DEBUG_ADDR"),
32+
})
3133
if err != nil {
3234
log.Fatalf("cannot get task list: %s", err)
3335
}

components/gitpod-cli/cmd/tasks-stop.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"fmt"
1010
"log"
11+
"os"
1112
"time"
1213

1314
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
@@ -32,7 +33,9 @@ var stopTaskCmd = &cobra.Command{
3233
cancel context.CancelFunc
3334
)
3435

35-
client, err := supervisor.New(context.Background())
36+
client, err := supervisor.New(context.Background(), &supervisor.SupervisorClientOption{
37+
Address: os.Getenv("SUPERVISOR_DEBUG_ADDR"),
38+
})
3639
if err != nil {
3740
log.Fatalf("annot get task list: %s", err)
3841
}

components/gitpod-cli/pkg/supervisor/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type SupervisorClientOption struct {
3030
Address string
3131
}
3232

33-
func New(ctx context.Context, options ...SupervisorClientOption) (*SupervisorClient, error) {
33+
func New(ctx context.Context, options ...*SupervisorClientOption) (*SupervisorClient, error) {
3434
address := util.GetSupervisorAddress()
3535
for _, option := range options {
3636
if option.Address != "" {

components/supervisor/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
supervisor

components/supervisor/cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var runCmd = &cobra.Command{
2323
Short: "starts the supervisor",
2424

2525
Run: func(cmd *cobra.Command, args []string) {
26-
log.Init(ServiceName, Version, true, os.Getenv("SUPERVISOR_DEBUG_ENABLE") == "true")
26+
log.Init(ServiceName, Version, !runOpts.RunGP, os.Getenv("SUPERVISOR_DEBUG_ENABLE") == "true")
2727
common_grpc.SetupLogging()
2828
supervisor.Version = Version
2929
supervisor.Run(supervisor.WithRunGP(runOpts.RunGP))

components/supervisor/hot-swap.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3+
# Licensed under the GNU Affero General Public License (AGPL).
4+
# See License.AGPL.txt in the project root for license information.
5+
6+
set -Eeuo pipefail
7+
8+
# This script swaps the backend startup endpoint with a built one
9+
# in a workspace and restarts the JB backend.
10+
11+
component=${PWD##*/}
12+
workspaceUrl=$(echo "${1}" |sed -e "s/\/$//")
13+
echo "URL: $workspaceUrl"
14+
15+
workspaceDesc=$(gpctl workspaces describe "$workspaceUrl" -o=json)
16+
17+
podName=$(echo "$workspaceDesc" | jq .runtime.pod_name -r)
18+
echo "Pod: $podName"
19+
20+
workspaceId=$(echo "$workspaceDesc" | jq .metadata.meta_id -r)
21+
echo "ID: $workspaceId"
22+
23+
clusterHost=$(kubectl exec -it "$podName" -- printenv GITPOD_WORKSPACE_CLUSTER_HOST |sed -e "s/\s//g")
24+
echo "Cluster Host: $clusterHost"
25+
26+
# prepare ssh
27+
ownerToken=$(kubectl get pod "$podName" -o=json | jq ".metadata.annotations.\"gitpod\/ownerToken\"" -r)
28+
sshConfig="./ssh-config"
29+
echo "Host $workspaceId" > "$sshConfig"
30+
echo " Hostname \"$workspaceId.ssh.$clusterHost\"" >> "$sshConfig"
31+
echo " User \"$workspaceId#$ownerToken\"" >> "$sshConfig"
32+
33+
# build
34+
go build .
35+
echo "$component built"
36+
37+
# upload
38+
uploadDest="/.supervisor/$component"
39+
echo "Upload Dest: $uploadDest"
40+
ssh -F "$sshConfig" "$workspaceId" "sudo chmod 777 $uploadDest && sudo rm $uploadDest"
41+
scp -F "$sshConfig" -r "./supervisor" "$workspaceId":"$uploadDest"
42+
echo "Swap complete"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3+
# Licensed under the GNU Affero General Public License (AGPL).
4+
# See License.AGPL.txt in the project root for license information.
5+
6+
set -Eeuo pipefail
7+
8+
# This script swaps the backend startup endpoint with a built one
9+
# in a workspace and restarts the JB backend.
10+
11+
component=${PWD##*/}
12+
13+
# build
14+
go build .
15+
echo "$component built"
16+
17+
sudo rm /.supervisor/supervisor
18+
sudo mv ./"$component" /.supervisor
19+
echo "Local Swap complete"

components/supervisor/pkg/supervisor/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ type StaticConfig struct {
8383
// APIEndpointPort is the port where to serve the API endpoint on
8484
APIEndpointPort int `json:"apiEndpointPort"`
8585

86+
// HostAPIEndpointPort is the port where to the host API endpoint served
87+
HostAPIEndpointPort *int `json:"hostAPIEndpointPort,omitempty"`
88+
8689
// SSHPort is the port we run the SSH server on
8790
SSHPort int `json:"sshPort"`
8891
}

components/supervisor/pkg/supervisor/supervisor.go

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func Run(options ...RunOption) {
181181

182182
// BEWARE: we can only call buildChildProcEnv once, because it might download env vars from a one-time-secret
183183
// URL, which would fail if we tried another time.
184-
childProcEnvvars := buildChildProcEnv(cfg, nil)
184+
childProcEnvvars := buildChildProcEnv(cfg, nil, opts.RunGP)
185185

186186
err = AddGitpodUserIfNotExists()
187187
if err != nil {
@@ -191,15 +191,18 @@ func Run(options ...RunOption) {
191191
configureGit(cfg, childProcEnvvars)
192192

193193
tokenService := NewInMemoryTokenService()
194-
tkns, err := cfg.GetTokens(true)
195-
if err != nil {
196-
log.WithError(err).Warn("cannot prepare tokens")
197-
}
198-
for i := range tkns {
199-
_, err = tokenService.SetToken(context.Background(), &tkns[i].SetTokenRequest)
194+
195+
if !opts.RunGP {
196+
tkns, err := cfg.GetTokens(true)
200197
if err != nil {
201198
log.WithError(err).Warn("cannot prepare tokens")
202199
}
200+
for i := range tkns {
201+
_, err = tokenService.SetToken(context.Background(), &tkns[i].SetTokenRequest)
202+
if err != nil {
203+
log.WithError(err).Warn("cannot prepare tokens")
204+
}
205+
}
203206
}
204207

205208
tunneledPortsService := ports.NewTunneledPortsService(cfg.DebugEnable)
@@ -238,29 +241,41 @@ func Run(options ...RunOption) {
238241
desktopIdeReady *ideReadyState = nil
239242

240243
cstate = NewInMemoryContentState(cfg.RepoRoot)
244+
gitpodService serverapi.APIInterface
245+
246+
notificationService = NewNotificationService()
247+
)
248+
249+
if !opts.RunGP {
241250
gitpodService = serverapi.NewServerApiService(ctx, &serverapi.ServiceConfig{
242251
Host: host,
243252
Endpoint: endpoint,
244253
InstanceID: cfg.WorkspaceInstanceID,
245254
WorkspaceID: cfg.WorkspaceID,
246255
SupervisorVersion: Version,
247256
}, tokenService)
257+
}
248258

249-
notificationService = NewNotificationService()
250-
)
251259
if cfg.DesktopIDE != nil {
252260
desktopIdeReady = &ideReadyState{cond: sync.NewCond(&sync.Mutex{})}
253261
}
254-
if !cfg.isHeadless() {
262+
if !cfg.isHeadless() && !opts.RunGP {
255263
go trackReadiness(ctx, gitpodService, cfg, cstate, ideReady, desktopIdeReady)
256264
}
257265
tokenService.provider[KindGit] = []tokenProvider{NewGitTokenProvider(gitpodService, cfg.WorkspaceConfig, notificationService)}
258266

259267
gitpodConfigService := config.NewConfigService(cfg.RepoRoot+"/.gitpod.yml", cstate.ContentReady(), log.Log)
260268
go gitpodConfigService.Watch(ctx)
261269

270+
var exposedPorts ports.ExposedPortsInterface
271+
272+
if !opts.RunGP {
273+
exposedPorts = createExposedPortsImpl(cfg, gitpodService)
274+
}
275+
276+
// createExposedPortsImpl(cfg, gitpodService)
262277
portMgmt := ports.NewManager(
263-
createExposedPortsImpl(cfg, gitpodService),
278+
exposedPorts,
264279
&ports.PollingServedPortsObserver{
265280
RefreshInterval: 2 * time.Second,
266281
},
@@ -270,13 +285,14 @@ func Run(options ...RunOption) {
270285
)
271286

272287
topService := NewTopService()
273-
topService.Observe(ctx)
274288

275289
supervisorMetrics := metrics.NewMetrics()
276290
var metricsReporter *metrics.GrpcMetricsReporter
277291
if opts.RunGP {
278292
cstate.MarkContentReady(csapi.WorkspaceInitFromOther)
279293
} else {
294+
topService.Observe(ctx)
295+
280296
if !cfg.isHeadless() {
281297
go startAnalyze(ctx, cfg, gitpodConfigService, topService, gitpodService)
282298
}
@@ -355,17 +371,28 @@ func Run(options ...RunOption) {
355371
wg sync.WaitGroup
356372
shutdown = make(chan ShutdownReason, 1)
357373
)
358-
wg.Add(1)
359-
go startContentInit(ctx, cfg, &wg, cstate, supervisorMetrics)
374+
375+
if !opts.RunGP {
376+
wg.Add(1)
377+
go startContentInit(ctx, cfg, &wg, cstate, supervisorMetrics)
378+
}
379+
360380
wg.Add(1)
361381
go startAPIEndpoint(ctx, cfg, &wg, apiServices, tunneledPortsService, metricsReporter, apiEndpointOpts...)
362-
wg.Add(1)
363-
go startSSHServer(ctx, cfg, &wg, childProcEnvvars)
382+
383+
if !opts.RunGP {
384+
wg.Add(1)
385+
go startSSHServer(ctx, cfg, &wg, childProcEnvvars)
386+
}
387+
364388
wg.Add(1)
365389
tasksSuccessChan := make(chan taskSuccess, 1)
366390
go taskManager.Run(ctx, &wg, tasksSuccessChan)
367-
wg.Add(1)
368-
go socketActivationForDocker(ctx, &wg, termMux)
391+
392+
if !opts.RunGP {
393+
wg.Add(1)
394+
go socketActivationForDocker(ctx, &wg, termMux)
395+
}
369396

370397
if cfg.isHeadless() {
371398
wg.Add(1)
@@ -899,7 +926,7 @@ func prepareIDELaunch(cfg *Config, ideConfig *IDEConfig, childProcEnvvars []stri
899926
// of envvars. If envvars is nil, os.Environ() is used.
900927
//
901928
// Beware: if config contains an OTS URL the results may differ on subsequent calls.
902-
func buildChildProcEnv(cfg *Config, envvars []string) []string {
929+
func buildChildProcEnv(cfg *Config, envvars []string, runGP bool) []string {
903930
if envvars == nil {
904931
envvars = os.Environ()
905932
}
@@ -919,7 +946,13 @@ func buildChildProcEnv(cfg *Config, envvars []string) []string {
919946

920947
envs[nme] = val
921948
}
922-
envs["SUPERVISOR_ADDR"] = fmt.Sprintf("localhost:%d", cfg.APIEndpointPort)
949+
950+
if runGP {
951+
envs["SUPERVISOR_ADDR"] = fmt.Sprintf("localhost:%d", cfg.HostAPIEndpointPort)
952+
envs["SUPERVISOR_DEBUG_ADDR"] = fmt.Sprintf("localhost:%d", cfg.APIEndpointPort)
953+
} else {
954+
envs["SUPERVISOR_ADDR"] = fmt.Sprintf("localhost:%d", cfg.APIEndpointPort)
955+
}
923956

924957
if cfg.EnvvarOTS != "" {
925958
es, err := downloadEnvvarOTS(cfg.EnvvarOTS)

components/supervisor/pkg/supervisor/supervisor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestBuildChildProcEnv(t *testing.T) {
122122
cfg.EnvvarOTS = srv.URL
123123
}
124124

125-
act := buildChildProcEnv(cfg, test.Input)
125+
act := buildChildProcEnv(cfg, test.Input, false)
126126
assert(t, act)
127127
})
128128
}

0 commit comments

Comments
 (0)