Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature - container run #573

Merged
merged 1 commit into from
Mar 6, 2025
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
36 changes: 27 additions & 9 deletions pdcs/containers/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ var ErrInvalidCreateTimeout = errors.New("invalid container create timeout value
// CreateOptions container create options.
type CreateOptions struct {
Name string
Command string
Labels []string
Image string
Remove bool
Privileged bool
Timeout string
Interactive bool
TTY bool
Detach bool
Secret []string
WorkDir string
EnvVars []string
Expand Down Expand Up @@ -75,9 +79,10 @@ type CreateOptions struct {
}

// Create creates a new container.
func Create(opts CreateOptions) ([]string, error) { //nolint:cyclop,gocognit,gocyclo
func Create(opts CreateOptions, run bool) ([]string, string, error) { //nolint:cyclop,gocognit,gocyclo
var (
warningResponse []string
containerID string
createOptions entities.ContainerCreateOptions
)

Expand All @@ -86,7 +91,7 @@ func Create(opts CreateOptions) ([]string, error) { //nolint:cyclop,gocognit,goc

conn, err := registry.GetConnection()
if err != nil {
return warningResponse, err
return warningResponse, containerID, err
}

if len(opts.Labels) > 0 {
Expand All @@ -100,7 +105,7 @@ func Create(opts CreateOptions) ([]string, error) { //nolint:cyclop,gocognit,goc
if opts.Timeout != "" {
timeout, err := strconv.Atoi(opts.Timeout)
if err != nil {
return warningResponse, fmt.Errorf("%w: %s", ErrInvalidCreateTimeout, opts.Timeout)
return warningResponse, containerID, fmt.Errorf("%w: %s", ErrInvalidCreateTimeout, opts.Timeout)
}

createOptions.Timeout = uint(timeout) //nolint:gosec
Expand All @@ -116,7 +121,7 @@ func Create(opts CreateOptions) ([]string, error) { //nolint:cyclop,gocognit,goc

createOptions.Net, err = containerNetworkOptions(opts)
if err != nil {
return warningResponse, err
return warningResponse, containerID, err
}

if opts.Pod != "" {
Expand Down Expand Up @@ -217,31 +222,44 @@ func Create(opts CreateOptions) ([]string, error) { //nolint:cyclop,gocognit,goc
createOptions.Secrets = opts.Secret
}

if run {
createOptions.Interactive = opts.Interactive
createOptions.TTY = opts.TTY
}

// add healthcheck options
if err := containerHealthOptions(&createOptions, opts); err != nil {
return warningResponse, err
return warningResponse, containerID, err
}

s := specgen.NewSpecGenerator(opts.Name, false)
if err := specgenutil.FillOutSpecGen(s, &createOptions, nil); err != nil {
return warningResponse, err
return warningResponse, containerID, err
}

// container image
s.Image = opts.Image

// command
cmd := strings.TrimSpace(opts.Command)
if cmd != "" {
s.Command = strings.Split(cmd, " ")
}

// validate spec
if err := s.Validate(); err != nil {
return warningResponse, err
return warningResponse, containerID, err
}

response, err := containers.CreateWithSpec(conn, s, &containers.CreateOptions{})
if err != nil {
return warningResponse, err
return warningResponse, containerID, err
}

warningResponse = response.Warnings
containerID = response.ID

return warningResponse, nil
return warningResponse, containerID, nil
}

func containerHealthOptions(createOptions *entities.ContainerCreateOptions, opts CreateOptions) error { //nolint:cyclop
Expand Down
2 changes: 1 addition & 1 deletion pdcs/containers/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func ResizeExecTty(id string, height int, width int) {
}
}

// Exec returns the diff of the specified container ID.
// Exec executes command in a given sessionOD.
func Exec(sessionID string, opts ExecOption) { //nolint:cyclop
log.Debug().Msgf("pdcs: podman container session (%s) exec %v", sessionID, opts)

Expand Down
32 changes: 32 additions & 0 deletions pdcs/containers/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package containers

import (
"io"

"github.com/containers/podman-tui/pdcs/registry"
"github.com/containers/podman/v5/pkg/bindings/containers"
"github.com/rs/zerolog/log"
)

// RunInitAttach will init container for run and attach.
func RunInitAttach(cntID string, stdin io.Reader, stdout io.Writer, attachReady chan bool, detachKey string) error {
log.Debug().Msgf("pdcs: podman container run init attach %s", cntID)

conn, err := registry.GetConnection()
if err != nil {
return err
}

attachOptions := new(containers.AttachOptions)
attachOptions.WithDetachKeys(detachKey)

if err := containers.ContainerInit(conn, cntID, new(containers.InitOptions)); err != nil {
return err
}

if err := containers.Attach(conn, cntID, stdin, stdout, stdout, attachReady, attachOptions); err != nil {
return err
}

return nil
}
6 changes: 3 additions & 3 deletions test/005-container.bats
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ load helpers_tui
# select image from dropdown widget
# select privileged
# set timeout to 10
podman_tui_send_inputs $TEST_CONTAINER_NAME "Tab"
podman_tui_send_inputs $TEST_CONTAINER_NAME "Tab" "Tab"
podman_tui_send_inputs "Down"
podman_tui_select_item $image_index
podman_tui_send_inputs "Enter" "Tab" "Tab" "Tab"
Expand Down Expand Up @@ -62,7 +62,7 @@ load helpers_tui

# fillout name field
# select image from dropdown widget
podman_tui_send_inputs $TEST_CONTAINER_NAME "Tab"
podman_tui_send_inputs $TEST_CONTAINER_NAME "Tab" "Tab"
podman_tui_send_inputs "Down"
podman_tui_select_item $image_index
podman_tui_send_inputs "Enter" "Tab" "Tab" "Tab" "Tab" "Tab" "Tab" "Tab" "Tab" "Tab"
Expand Down Expand Up @@ -123,7 +123,7 @@ load helpers_tui
# select image from dropdown widget
# select pod from dropdown widget
# fillout label field
podman_tui_send_inputs $TEST_CONTAINER_NAME "Tab"
podman_tui_send_inputs $TEST_CONTAINER_NAME "Tab" "Tab"
podman_tui_send_inputs "Down"
podman_tui_select_item $image_index
podman_tui_send_inputs "Enter" "Tab"
Expand Down
Loading
Loading