Skip to content

Commit 5fb7024

Browse files
feat: implement attach flag for podman too (#72)
* feat: implement attach flag for podman too * fix: podman tests
1 parent 1465125 commit 5fb7024

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

cmd/schedctl/run.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,14 @@ func run(cmd *cobra.Command, args []string, attach bool) error {
8989
}
9090

9191
if driver == constants.PODMAN {
92-
err := podman.Run(result.ImageURI, schedulerID, containerArgs)
92+
err := podman.Run(result.ImageURI, schedulerID, attach, containerArgs)
9393
if err != nil {
9494
panic(err)
9595
}
9696

97-
_, _ = output.Out("Container %s started successfully\n", result.ImageURI)
97+
if !attach {
98+
_, _ = output.Out("Container %s started successfully\n", result.ImageURI)
99+
}
98100
}
99101

100102
return nil

internal/podman/podman.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func generateRandomSuffix() (string, error) {
2525
return hex.EncodeToString(bytes), nil
2626
}
2727

28-
func Run(image, id string, args []string) error {
28+
func Run(image, id string, attach bool, args []string) error {
2929
ctx := context.Background()
3030
privileged := true
3131

@@ -85,6 +85,35 @@ func Run(image, id string, args []string) error {
8585
return fmt.Errorf("failed to start container: %w", err)
8686
}
8787

88+
if attach {
89+
errChan := make(chan error, 1)
90+
go func() {
91+
err := podman_containers.Attach(
92+
client,
93+
createResponse.ID,
94+
os.Stdin,
95+
os.Stdout,
96+
os.Stderr,
97+
nil,
98+
new(podman_containers.AttachOptions).WithStream(true),
99+
)
100+
errChan <- err
101+
}()
102+
103+
exitCode, err := podman_containers.Wait(client, createResponse.ID, nil)
104+
if err != nil {
105+
return fmt.Errorf("failed to wait for container: %w", err)
106+
}
107+
108+
if attachErr := <-errChan; attachErr != nil {
109+
return fmt.Errorf("attach failed: %w", attachErr)
110+
}
111+
112+
if exitCode != 0 {
113+
return fmt.Errorf("container exited with status: %d", exitCode)
114+
}
115+
}
116+
88117
return nil
89118
}
90119

internal/podman/podman_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestPodmanSpawnStopProcesses(t *testing.T) {
2424

2525
assert.Equal(t, 0, len(containers))
2626

27-
err = podman.Run("ghcr.io/schedkit/scx_rusty:latest", "test-scheduler", []string{})
27+
err = podman.Run("ghcr.io/schedkit/scx_rusty:latest", "test-scheduler", false, []string{})
2828
assert.Nil(t, err)
2929

3030
containers, err = podman.List()

0 commit comments

Comments
 (0)