Skip to content

Commit fbffe09

Browse files
authored
Merge pull request #1217 from bkneis/POD-763/expose-timeout-as-agent-config
POD-763: Expose inject timeout as contect config and add to agent workspace info
2 parents b48f066 + c15dd46 commit fbffe09

File tree

20 files changed

+190
-65
lines changed

20 files changed

+190
-65
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
/e2e/bin
55
devpod
66
devpod.exe
7+
devpod-cli
78
# Unit test targets
89
main
910
profile.out
1011
package-lock.json
11-
tags
12+
tags

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch Package",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "${workspaceRoot}",
13+
"args": "up examples/simple",
14+
}
15+
]
16+
}

cmd/agent/container_tunnel.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func (cmd *ContainerTunnelCmd) Run(ctx context.Context, log log.Logger) error {
9595
os.Stdout,
9696
os.Stderr,
9797
log,
98+
workspaceInfo.InjectTimeout,
9899
)
99100
if err != nil {
100101
return err
@@ -112,7 +113,7 @@ func startDevContainer(ctx context.Context, workspaceConfig *provider2.AgentWork
112113
// start container if necessary
113114
if containerDetails == nil || containerDetails.State.Status != "running" {
114115
// start container
115-
_, err = StartContainer(ctx, runner, log)
116+
_, err = StartContainer(ctx, runner, log, workspaceConfig)
116117
if err != nil {
117118
return err
118119
}
@@ -122,7 +123,7 @@ func startDevContainer(ctx context.Context, workspaceConfig *provider2.AgentWork
122123
err = runner.Command(ctx, "root", "cat "+setup.ResultLocation, nil, buf, buf)
123124
if err != nil {
124125
// start container
125-
_, err = StartContainer(ctx, runner, log)
126+
_, err = StartContainer(ctx, runner, log, workspaceConfig)
126127
if err != nil {
127128
return err
128129
}
@@ -132,9 +133,9 @@ func startDevContainer(ctx context.Context, workspaceConfig *provider2.AgentWork
132133
return nil
133134
}
134135

135-
func StartContainer(ctx context.Context, runner devcontainer.Runner, log log.Logger) (*config.Result, error) {
136+
func StartContainer(ctx context.Context, runner devcontainer.Runner, log log.Logger, workspaceConfig *provider2.AgentWorkspaceInfo) (*config.Result, error) {
136137
log.Debugf("Starting DevPod container...")
137-
result, err := runner.Up(ctx, devcontainer.UpOptions{NoBuild: true})
138+
result, err := runner.Up(ctx, devcontainer.UpOptions{NoBuild: true}, workspaceConfig.InjectTimeout)
138139
if err != nil {
139140
return result, err
140141
}

cmd/agent/workspace/up.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ func (cmd *UpCmd) devPodUp(ctx context.Context, workspaceInfo *provider2.AgentWo
405405
// start the devcontainer
406406
result, err := runner.Up(ctx, devcontainer.UpOptions{
407407
CLIOptions: workspaceInfo.CLIOptions,
408-
})
408+
}, workspaceInfo.InjectTimeout)
409409
if err != nil {
410410
return nil, err
411411
}

cmd/build.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (cmd *BuildCmd) build(ctx context.Context, workspaceClient client.Workspace
154154

155155
func (cmd *BuildCmd) buildAgentClient(ctx context.Context, workspaceClient client.WorkspaceClient, log log.Logger) error {
156156
// compress info
157-
workspaceInfo, _, err := workspaceClient.AgentInfo(cmd.CLIOptions)
157+
workspaceInfo, wInfo, err := workspaceClient.AgentInfo(cmd.CLIOptions)
158158
if err != nil {
159159
return err
160160
}
@@ -191,14 +191,26 @@ func (cmd *BuildCmd) buildAgentClient(ctx context.Context, workspaceClient clien
191191
writer := log.ErrorStreamOnly().Writer(logrus.InfoLevel, false)
192192
defer writer.Close()
193193

194-
errChan <- agent.InjectAgentAndExecute(cancelCtx, func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
195-
return workspaceClient.Command(ctx, client.CommandOptions{
196-
Command: command,
197-
Stdin: stdin,
198-
Stdout: stdout,
199-
Stderr: stderr,
200-
})
201-
}, workspaceClient.AgentLocal(), workspaceClient.AgentPath(), workspaceClient.AgentURL(), true, command, stdinReader, stdoutWriter, writer, log.ErrorStreamOnly())
194+
errChan <- agent.InjectAgentAndExecute(
195+
cancelCtx,
196+
func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
197+
return workspaceClient.Command(ctx, client.CommandOptions{
198+
Command: command,
199+
Stdin: stdin,
200+
Stdout: stdout,
201+
Stderr: stderr,
202+
})
203+
},
204+
workspaceClient.AgentLocal(),
205+
workspaceClient.AgentPath(),
206+
workspaceClient.AgentURL(),
207+
true,
208+
command,
209+
stdinReader,
210+
stdoutWriter,
211+
writer,
212+
log.ErrorStreamOnly(),
213+
wInfo.InjectTimeout)
202214
}()
203215

204216
// create container etc.

cmd/logs.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,34 @@ func (cmd *LogsCmd) Run(ctx context.Context, args []string) error {
7373
sshServerCmd += " --debug"
7474
}
7575

76+
// Get the timeout from the context options
77+
timeout := config.ParseTimeOption(devPodConfig, config.ContextOptionAgentInjectTimeout)
78+
7679
// start ssh server in background
7780
errChan := make(chan error, 1)
7881
go func() {
7982
stderr := log.ErrorStreamOnly().Writer(logrus.DebugLevel, false)
8083
defer stderr.Close()
8184

82-
errChan <- agent.InjectAgentAndExecute(ctx, func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
83-
return client.Command(ctx, clientpkg.CommandOptions{
84-
Command: command,
85-
Stdin: stdin,
86-
Stdout: stdout,
87-
Stderr: stderr,
88-
})
89-
}, client.AgentLocal(), client.AgentPath(), client.AgentURL(), true, sshServerCmd, stdinReader, stdoutWriter, stderr, log.ErrorStreamOnly())
85+
errChan <- agent.InjectAgentAndExecute(
86+
ctx,
87+
func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
88+
return client.Command(ctx, clientpkg.CommandOptions{
89+
Command: command,
90+
Stdin: stdin,
91+
Stdout: stdout,
92+
Stderr: stderr,
93+
})
94+
},
95+
client.AgentLocal(),
96+
client.AgentPath(),
97+
client.AgentURL(),
98+
true,
99+
sshServerCmd,
100+
stdinReader,
101+
stdoutWriter,
102+
stderr,
103+
log.ErrorStreamOnly(), timeout)
90104
}()
91105

92106
// create agent command

cmd/machine/ssh.go

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,39 @@ func (cmd *SSHCmd) Run(ctx context.Context, args []string) error {
6363
writer := log.Default.ErrorStreamOnly().Writer(logrus.InfoLevel, false)
6464
defer writer.Close()
6565

66+
// Get the timeout from the context options
67+
timeout := config.ParseTimeOption(devPodConfig, config.ContextOptionAgentInjectTimeout)
68+
6669
// start the ssh session
67-
return StartSSHSession(ctx, "", cmd.Command, cmd.AgentForwarding, func(ctx context.Context, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
68-
command := fmt.Sprintf("'%s' helper ssh-server --stdio", machineClient.AgentPath())
69-
if cmd.Debug {
70-
command += " --debug"
71-
}
72-
return devagent.InjectAgentAndExecute(ctx, func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
73-
return machineClient.Command(ctx, client.CommandOptions{
74-
Command: command,
75-
Stdin: stdin,
76-
Stdout: stdout,
77-
Stderr: stderr,
78-
})
79-
}, machineClient.AgentLocal(), machineClient.AgentPath(), machineClient.AgentURL(), true, command, stdin, stdout, stderr, log.Default.ErrorStreamOnly())
80-
}, writer)
70+
return StartSSHSession(
71+
ctx,
72+
"",
73+
cmd.Command,
74+
cmd.AgentForwarding,
75+
func(ctx context.Context, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
76+
command := fmt.Sprintf("'%s' helper ssh-server --stdio", machineClient.AgentPath())
77+
if cmd.Debug {
78+
command += " --debug"
79+
}
80+
return devagent.InjectAgentAndExecute(ctx, func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
81+
return machineClient.Command(ctx, client.CommandOptions{
82+
Command: command,
83+
Stdin: stdin,
84+
Stdout: stdout,
85+
Stderr: stderr,
86+
})
87+
},
88+
machineClient.AgentLocal(),
89+
machineClient.AgentPath(),
90+
machineClient.AgentURL(),
91+
true,
92+
command,
93+
stdin,
94+
stdout,
95+
stderr,
96+
log.Default.ErrorStreamOnly(),
97+
timeout)
98+
}, writer)
8199
}
82100

83101
type ExecFunc func(ctx context.Context, stdin io.Reader, stdout io.Writer, stderr io.Writer) error

cmd/ssh.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ func NewSSHCmd(flags *flags.GlobalFlags) *cobra.Command {
9999
}
100100

101101
// Run runs the command logic
102-
func (cmd *SSHCmd) Run(ctx context.Context, devPodConfig *config.Config, client client2.BaseWorkspaceClient, log log.Logger) error {
102+
func (cmd *SSHCmd) Run(
103+
ctx context.Context,
104+
devPodConfig *config.Config,
105+
client client2.BaseWorkspaceClient,
106+
log log.Logger) error {
103107
// add ssh keys to agent
104108
if !cmd.Proxy && devPodConfig.ContextOption(config.ContextOptionSSHAgentForwarding) == "true" && devPodConfig.ContextOption(config.ContextOptionSSHAddPrivateKeys) == "true" {
105109
log.Debug("Adding ssh keys to agent, disable via 'devpod context set-options -o SSH_ADD_PRIVATE_KEYS=false'")
@@ -234,7 +238,7 @@ func (cmd *SSHCmd) jumpContainer(
234238

235239
// start ssh tunnel
236240
return cmd.startTunnel(ctx, devPodConfig, containerClient, client.Workspace(), log)
237-
})
241+
}, devPodConfig)
238242
}
239243

240244
func (cmd *SSHCmd) forwardTimeout(log log.Logger) (time.Duration, error) {

cmd/up.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ func (cmd *UpCmd) devPodUpMachine(
452452
}
453453

454454
// compress info
455-
workspaceInfo, _, err := client.AgentInfo(cmd.CLIOptions)
455+
workspaceInfo, wInfo, err := client.AgentInfo(cmd.CLIOptions)
456456
if err != nil {
457457
return nil, err
458458
}
@@ -497,6 +497,7 @@ func (cmd *UpCmd) devPodUpMachine(
497497
sshTunnelStdoutWriter,
498498
writer,
499499
log.ErrorStreamOnly(),
500+
wInfo.InjectTimeout,
500501
)
501502
}
502503

pkg/agent/agent.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,12 @@ func Tunnel(
313313
stdout io.Writer,
314314
stderr io.Writer,
315315
log log.Logger,
316+
timeout time.Duration,
316317
) error {
317318
// inject agent
318319
err := InjectAgent(ctx, func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
319320
return exec(ctx, "root", command, stdin, stdout, stderr)
320-
}, false, ContainerDevPodHelperLocation, DefaultAgentDownloadURL(), false, log)
321+
}, false, ContainerDevPodHelperLocation, DefaultAgentDownloadURL(), false, log, timeout)
321322
if err != nil {
322323
return err
323324
}

pkg/agent/inject.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func InjectAgent(
2828
downloadURL string,
2929
preferDownload bool,
3030
log log.Logger,
31+
timeout time.Duration,
3132
) error {
3233
return InjectAgentAndExecute(
3334
ctx,
@@ -41,6 +42,7 @@ func InjectAgent(
4142
nil,
4243
nil,
4344
log,
45+
timeout,
4446
)
4547
}
4648

@@ -56,6 +58,7 @@ func InjectAgentAndExecute(
5658
stdout io.Writer,
5759
stderr io.Writer,
5860
log log.Logger,
61+
timeout time.Duration,
5962
) error {
6063
// should execute locally?
6164
if local {
@@ -115,7 +118,7 @@ func InjectAgentAndExecute(
115118
stdin,
116119
stdout,
117120
stderr,
118-
time.Second*20,
121+
timeout,
119122
log,
120123
)
121124
if err != nil {

pkg/client/clientimplementation/workspace_client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ func (s *workspaceClient) agentInfo(cliOptions provider.CLIOptions) (string, *pr
200200
}
201201
}
202202

203+
// Get the timeout from the context options
204+
agentInfo.InjectTimeout = config.ParseTimeOption(s.devPodConfig, config.ContextOptionAgentInjectTimeout)
205+
203206
// marshal config
204207
out, err := json.Marshal(agentInfo)
205208
if err != nil {

pkg/config/config.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"strconv"
9+
"time"
810

911
"github.com/ghodss/yaml"
1012
"github.com/loft-sh/devpod/pkg/telemetry"
@@ -116,8 +118,12 @@ func (c *Config) IDEOptions(ide string) map[string]OptionValue {
116118
}
117119

118120
func (c *Config) ContextOption(option string) string {
119-
if c.Current().Options != nil && c.Current().Options[option].Value != "" {
120-
return c.Current().Options[option].Value
121+
if c.Contexts != nil {
122+
if _, ok := c.Contexts[c.DefaultContext]; ok && c.Current().Options != nil {
123+
if _, ok := c.Current().Options[option]; ok && c.Current().Options[option].Value != "" {
124+
return c.Current().Options[option].Value
125+
}
126+
}
121127
}
122128

123129
for _, contextOption := range ContextOptions {
@@ -306,3 +312,11 @@ func SaveConfig(config *Config) error {
306312

307313
return nil
308314
}
315+
316+
func ParseTimeOption(cfg *Config, opt string) time.Duration {
317+
timeout, err := strconv.ParseInt(cfg.ContextOption(opt), 10, 64)
318+
if err != nil {
319+
timeout = 20
320+
}
321+
return time.Duration(timeout) * time.Second
322+
}

pkg/config/context.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const (
1313
ContextOptionDotfilesScript = "DOTFILES_SCRIPT"
1414
ContextOptionSSHAgentForwarding = "SSH_AGENT_FORWARDING"
1515
ContextOptionSSHConfigPath = "SSH_CONFIG_PATH"
16+
ContextOptionAgentInjectTimeout = "AGENT_INJECT_TIMEOUT"
1617
)
1718

1819
var ContextOptions = []ContextOption{
@@ -80,4 +81,9 @@ var ContextOptions = []ContextOption{
8081
Name: ContextOptionSSHConfigPath,
8182
Description: "Specifies the path where the ssh config should be written to",
8283
},
84+
{
85+
Name: ContextOptionAgentInjectTimeout,
86+
Description: "Specifies the timeout to inject the agent",
87+
Default: "20",
88+
},
8389
}

pkg/devcontainer/compose.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func (r *runner) runDockerCompose(
115115
parsedConfig *config.SubstitutedConfig,
116116
substitutionContext *config.SubstitutionContext,
117117
options UpOptions,
118+
timeout time.Duration,
118119
) (*config.Result, error) {
119120
composeHelper, err := r.composeHelper()
120121
if err != nil {
@@ -202,7 +203,7 @@ func (r *runner) runDockerCompose(
202203
}
203204

204205
// setup container
205-
return r.setupContainer(ctx, parsedConfig.Raw, containerDetails, mergedConfig, substitutionContext)
206+
return r.setupContainer(ctx, parsedConfig.Raw, containerDetails, mergedConfig, substitutionContext, timeout)
206207
}
207208

208209
func (r *runner) getDockerComposeFilePaths(parsedConfig *config.SubstitutedConfig, envFiles []string) ([]string, error) {

0 commit comments

Comments
 (0)