Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions pkg/agent/delivery/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"

"github.com/devsy-org/devsy/pkg/driver"
"github.com/devsy-org/devsy/pkg/inject"
"github.com/devsy-org/devsy/pkg/log"
"github.com/devsy-org/devsy/pkg/provider"
Expand Down Expand Up @@ -99,19 +100,22 @@ func isLocalDockerHost(host string) bool {

// CommandFunc adapts a driver's command function to inject.ExecFunc.
func CommandFunc(
driverCmd func(
ctx context.Context,
workspaceID, user, command string,
stdin io.Reader, stdout io.Writer, stderr io.Writer,
) error,
driverCmd func(ctx context.Context, params *driver.CommandParams) error,
workspaceID string,
) inject.ExecFunc { //nolint:staticcheck // bridges driver command signature to legacy ExecFunc
return func(
ctx context.Context,
command string,
stdin io.Reader, stdout io.Writer, stderr io.Writer,
) error {
return driverCmd(ctx, workspaceID, "root", command, stdin, stdout, stderr)
return driverCmd(ctx, &driver.CommandParams{
WorkspaceID: workspaceID,
User: "root",
Command: command,
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
})
}
}

Expand Down
4 changes: 1 addition & 3 deletions pkg/devcontainer/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ func (m *mockDriver) DeleteDevContainer(_ context.Context, _ string) error {
}

//nolint:revive // interface implementation requires 7 args
func (m *mockDriver) CommandDevContainer(
_ context.Context, _, _, _ string, _ io.Reader, _ io.Writer, _ io.Writer,
) error {
func (m *mockDriver) CommandDevContainer(_ context.Context, _ *driver.CommandParams) error {
return nil
}

Expand Down
9 changes: 8 additions & 1 deletion pkg/devcontainer/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,14 @@ func (r *runner) Command(
stdout io.Writer,
stderr io.Writer,
) error {
return r.Driver.CommandDevContainer(ctx, r.ID, user, command, stdin, stdout, stderr)
return r.Driver.CommandDevContainer(ctx, &driver.CommandParams{
WorkspaceID: r.ID,
User: user,
Command: command,
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
})
}

func (r *runner) Find(ctx context.Context) (*config.ContainerDetails, error) {
Expand Down
34 changes: 16 additions & 18 deletions pkg/devcontainer/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,14 @@ func (r *runner) legacyInject(ctx context.Context, timeout time.Duration) error
err := agent.InjectAgent(&agent.InjectOptions{
Ctx: ctx,
Exec: func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
return r.Driver.CommandDevContainer(
ctx,
r.ID,
containerRootUser,
command,
stdin,
stdout,
stderr,
)
return r.Driver.CommandDevContainer(ctx, &driver.CommandParams{
WorkspaceID: r.ID,
User: containerRootUser,
Command: command,
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
})
},
IsLocal: false,
RemoteAgentPath: pkgconfig.ContainerDevsyHelperLocation,
Expand Down Expand Up @@ -368,15 +367,14 @@ func (r *runner) executeSetup(
sshTunnelStdinReader, sshTunnelStdoutWriter *os.File,
writer io.WriteCloser,
) error {
return r.Driver.CommandDevContainer(
cancelCtx,
r.ID,
containerRootUser,
sshCmd,
sshTunnelStdinReader,
sshTunnelStdoutWriter,
writer,
)
return r.Driver.CommandDevContainer(cancelCtx, &driver.CommandParams{
WorkspaceID: r.ID,
User: containerRootUser,
Command: sshCmd,
Stdin: sshTunnelStdinReader,
Stdout: sshTunnelStdoutWriter,
Stderr: writer,
})
}

return sshtunnel.ExecuteCommand(ctx, sshtunnel.ExecuteCommandOptions{
Expand Down
8 changes: 5 additions & 3 deletions pkg/docker/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,16 @@ func (r *DockerHelper) FindContainerJSON(ctx context.Context, labels []string) (
found := true

containers, err := r.InspectContainers(ctx, []string{id})
if err != nil {
if err != nil || len(containers) == 0 {
continue
}

for _, label := range labels {
key, value, _ := strings.Cut(label, "=")

found = containers[0].Config.Labels[key] == value
if containers[0].Config.Labels[key] != value {
found = false
break
}
}

if found {
Expand Down
27 changes: 27 additions & 0 deletions pkg/docker/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,33 @@ echo "$@" > `+argsFile+`
})
}

func TestFindContainerJSON_MatchesAllLabels(t *testing.T) {
tmp := t.TempDir()
// Fake docker: `ps -q -a` lists three containers; `inspect` returns each
// container's labels. c1 matches both query labels; c2 matches only the
// last label (an earlier label differs); c3 inspect returns an empty array.
bin := writeScript(t, tmp, "docker-fake", `#!/bin/sh
case "$1" in
ps) printf 'c1\nc2\nc3\n' ;;
inspect)
case "$4" in
c1) echo '[{"ID":"c1","Config":{"Labels":{"a":"x","b":"y"}}}]' ;;
c2) echo '[{"ID":"c2","Config":{"Labels":{"a":"zzz","b":"y"}}}]' ;;
c3) echo '[]' ;;
esac ;;
esac
`)

h := &DockerHelper{DockerCommand: bin}
got, err := h.FindContainerJSON(context.Background(), []string{"a=x", "b=y"})

require.NoError(t, err)
// Only c1 satisfies every label. c2 must be excluded (the AND-logic bug
// previously matched it on the last label alone), and c3's empty inspect
// result must not panic.
assert.Equal(t, []string{"c1"}, got)
}

func TestGPUSupportEnabled_CommandFailure(t *testing.T) {
tmp := t.TempDir()
bin := writeScript(t, tmp, "bad-runtime", `#!/bin/sh
Expand Down
17 changes: 7 additions & 10 deletions pkg/driver/custom/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,20 @@ func (c *customDriver) FindDevContainer(
// CommandDevContainer runs the given command inside the devcontainer.
func (c *customDriver) CommandDevContainer(
ctx context.Context,
workspaceId, user, command string,
stdin io.Reader,
stdout io.Writer,
stderr io.Writer,
params *driver.CommandParams,
) error {
// run command
err := c.runCommand(
ctx,
workspaceId,
params.WorkspaceID,
"commandDevContainer",
c.workspaceInfo.Agent.Custom.CommandDevContainer,
stdin,
stdout,
stderr,
params.Stdin,
params.Stdout,
params.Stderr,
[]string{
"DEVCONTAINER_USER=" + user,
"DEVCONTAINER_COMMAND=" + command,
"DEVCONTAINER_USER=" + params.User,
"DEVCONTAINER_COMMAND=" + params.Command,
},
)
if err != nil {
Expand Down
Loading
Loading