Skip to content

Commit

Permalink
Merge pull request #577 from navidys/container_create_resource_options
Browse files Browse the repository at this point in the history
New feature - container create resource settings category
  • Loading branch information
navidys authored Mar 7, 2025
2 parents 242982a + 0aeb2e5 commit 5022bef
Show file tree
Hide file tree
Showing 10 changed files with 729 additions and 99 deletions.
109 changes: 108 additions & 1 deletion pdcs/containers/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,24 @@ type CreateOptions struct {
HealthStartupRetries string
HealthStartupSuccess string
HealthStartupTimeout string
Memory string
MemoryReservation string
MemorySwap string
MemorySwappiness string
CPUs string
CPUShares string
CPUPeriod string
CPUQuota string
CPURtPeriod string
CPURtRuntime string
CPUSetCPUs string
CPUSetMems string
SHMSize string
SHMSizeSystemd string
}

// Create creates a new container.
func Create(opts CreateOptions, run bool) ([]string, string, error) { //nolint:cyclop,gocognit,gocyclo
func Create(opts CreateOptions, run bool) ([]string, string, error) { //nolint:cyclop,gocognit,gocyclo,maintidx
var (
warningResponse []string
containerID string
Expand Down Expand Up @@ -232,6 +246,99 @@ func Create(opts CreateOptions, run bool) ([]string, string, error) { //nolint:c
return warningResponse, containerID, err
}

// add resources
if opts.Memory != "" {
createOptions.Memory = opts.Memory
}

if opts.MemoryReservation != "" {
createOptions.MemoryReservation = opts.MemoryReservation
}

if opts.MemorySwap != "" {
createOptions.MemorySwap = opts.MemorySwap
}

if opts.MemorySwappiness != "" {
val, err := strconv.Atoi(opts.MemorySwappiness)
if err != nil {
return warningResponse, containerID, err
}

createOptions.MemorySwappiness = int64(val)
}

if opts.CPUs != "" {
val, err := strconv.ParseFloat(opts.CPUs, 64)
if err != nil {
return warningResponse, containerID, err
}

createOptions.CPUS = val
}

if opts.CPUShares != "" {
val, err := strconv.ParseUint(opts.CPUShares, 10, 64)
if err != nil {
return warningResponse, containerID, err
}

createOptions.CPUShares = val
}

if opts.CPUPeriod != "" {
val, err := strconv.ParseUint(opts.CPUPeriod, 10, 64)
if err != nil {
return warningResponse, containerID, err
}

createOptions.CPUPeriod = val
}

if opts.CPURtPeriod != "" {
val, err := strconv.ParseUint(opts.CPURtPeriod, 10, 64)
if err != nil {
return warningResponse, containerID, err
}

createOptions.CPURTPeriod = val
}

if opts.CPUQuota != "" {
val, err := strconv.Atoi(opts.CPUQuota)
if err != nil {
return warningResponse, containerID, err
}

createOptions.CPUQuota = int64(val)
}

if opts.CPURtRuntime != "" {
val, err := strconv.Atoi(opts.CPURtRuntime)
if err != nil {
return warningResponse, containerID, err
}

createOptions.CPURTRuntime = int64(val)
}

if opts.CPUSetCPUs != "" {
createOptions.CPUSetCPUs = opts.CPUSetCPUs
}

if opts.CPUSetMems != "" {
createOptions.CPUSetMems = opts.CPUSetMems
}

if opts.SHMSize != "" {
createOptions.ShmSize = opts.SHMSize
}

if opts.SHMSizeSystemd != "" {
createOptions.ShmSizeSystemd = opts.SHMSizeSystemd
}

// generate spec
s := specgen.NewSpecGenerator(opts.Name, false)
if err := specgenutil.FillOutSpecGen(s, &createOptions, nil); err != nil {
return warningResponse, containerID, err
Expand Down
26 changes: 13 additions & 13 deletions test/001-image.bats
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ load helpers_tui
podman_tui_set_view "images"
podman_tui_select_image_cmd "pull"
podman_tui_send_inputs "busybox" "Enter"
sleep 8
sleep $TEST_TIMEOUT_HIGH
podman_tui_send_inputs "Down" "Enter"
sleep 12
sleep $TEST_TIMEOUT_HIGH

run_helper podman image ls busybox --format "{{ .Repository }}"
assert "$output" =~ "docker.io/library/busybox" "expected image"
Expand All @@ -46,7 +46,7 @@ load helpers_tui
podman_tui_send_inputs $TEST_IMAGE_SAVE_PATH "Tab"
podman_tui_send_inputs "Space" "Tab" "Tab" "Tab" "Tab"
podman_tui_send_inputs "Enter"
sleep 6
sleep $TEST_TIMEOUT_MEDIUM

run_helper ls ${TEST_IMAGE_SAVE_PATH} 2> /dev/null
assert "$output" == "$TEST_IMAGE_SAVE_PATH" "expected $TEST_IMAGE_SAVE_PATH exists"
Expand All @@ -70,7 +70,7 @@ load helpers_tui
podman_tui_send_inputs "Tab"
podman_tui_send_inputs "localhost/${TEST_NAME}_image_imported:latest"
podman_tui_send_inputs "Tab" "Tab" "Enter"
sleep 6
sleep $TEST_TIMEOUT_MEDIUM

run_helper podman image ls ${TEST_NAME}_image_imported --format "{{ .Repository }}:{{ .Tag }}"
assert "$output" =~ "localhost/${TEST_NAME}_image_imported" "expected image"
Expand All @@ -96,7 +96,7 @@ load helpers_tui
podman_tui_send_inputs ${TEST_IMAGE_BUILD_REPOSITORY}
podman_tui_send_inputs "Tab" "Tab"
podman_tui_send_inputs "Enter"
sleep 8
sleep $TEST_TIMEOUT_MEDIUM
podman_tui_send_inputs "Tab" "Enter"

run_helper podman image ls ${TEST_IMAGE_BUILD_TAG} --format "{{ .Repository }}:{{ .Tag }}"
Expand All @@ -113,7 +113,7 @@ load helpers_tui
podman_tui_set_view "images"
podman_tui_select_item $image_index
podman_tui_select_image_cmd "diff"
sleep 2
sleep $TEST_TIMEOUT_LOW
podman_tui_send_inputs "Tab" "Enter"

run_helper grep -w 'A /var' $PODMAN_TUI_LOG
Expand All @@ -130,7 +130,7 @@ load helpers_tui
podman_tui_set_view "images"
podman_tui_select_item $image_index
podman_tui_select_image_cmd "history"
sleep 2
sleep $TEST_TIMEOUT_LOW
podman_tui_send_inputs "Tab" "Enter"

run_helper egrep -w "\[\[$image_id.*BusyBox.*" $PODMAN_TUI_LOG
Expand All @@ -148,7 +148,7 @@ load helpers_tui
podman_tui_set_view "images"
podman_tui_select_item $image_index
podman_tui_select_image_cmd "inspect"
sleep 2
sleep $TEST_TIMEOUT_LOW
podman_tui_send_inputs "Enter"

run_helper sed -n '/ "RepoTags": \[/, / \],/p' $PODMAN_TUI_LOG
Expand All @@ -165,7 +165,7 @@ load helpers_tui
podman_tui_select_item $busyboxIndex
podman_tui_select_image_cmd "tag"
podman_tui_send_inputs "$TEST_IMAGE_TAG_NAME" "Tab" "Tab" "Enter"
sleep 2
sleep $TEST_TIMEOUT_LOW

run_helper podman image ls $TEST_IMAGE_TAG_NAME --format "{{ .Repository }}"
assert "$output" =~ "$TEST_IMAGE_TAG_NAME" "expected tagged image $TEST_IMAGE_TAG_NAME"
Expand All @@ -180,10 +180,10 @@ load helpers_tui
# press "Tab" 2 times and "Enter" to untag busybox image
podman_tui_set_view "images"
podman_tui_select_item $busybox_tagindex
sleep 1
sleep $TEST_TIMEOUT_LOW
podman_tui_select_image_cmd "untag"
podman_tui_send_inputs "Tab" "Tab" "Enter"
sleep 2
sleep $TEST_TIMEOUT_LOW

untagged_umage=$(podman image ls --format '{{ .Repository }}')
assert "$untagged_umage" !~ "$TEST_IMAGE_TAG_NAME" "expected $TEST_IMAGE_TAG_NAME not to be in the list"
Expand All @@ -205,7 +205,7 @@ load helpers_tui
podman_tui_select_item $untagged_image
podman_tui_select_image_cmd "remove"
podman_tui_send_inputs "Enter"
sleep 2
sleep $TEST_TIMEOUT_LOW
podman_tui_send_inputs "Tab" "Enter"

# check if busybox image has been removed
Expand All @@ -222,7 +222,7 @@ load helpers_tui
podman_tui_set_view "images"
podman_tui_select_image_cmd "prune"
podman_tui_send_inputs "Enter"
sleep 2
sleep $TEST_TIMEOUT_LOW

# check if busybox image has been removed
run_helper podman image ls --format "{{ .Repository }}" --filter "reference=busybox"
Expand Down
12 changes: 6 additions & 6 deletions test/002-volume.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ load helpers_tui
podman_tui_set_view "volumes"
podman_tui_select_volume_cmd "create"
podman_tui_send_inputs "$TEST_VOLUME_NAME" "Tab" "$TEST_LABEL" "Tab" "Tab" "Tab" "Tab" "Enter"
sleep 1
sleep $TEST_TIMEOUT_LOW
podman_tui_send_inputs "Tab" "Enter"
sleep 2
sleep $TEST_TIMEOUT_LOW

run_helper podman volume ls --format "{{ .Name }}" --filter "name=${TEST_VOLUME_NAME}"
assert "$output" == "$TEST_VOLUME_NAME" "expected $TEST_VOLUME_NAME to be in the list"
Expand All @@ -34,9 +34,9 @@ load helpers_tui
podman_tui_set_view "volumes"
podman_tui_select_item $vol_index
podman_tui_select_volume_cmd "inspect"
sleep 1
sleep $TEST_TIMEOUT_LOW
podman_tui_send_inputs "Enter"
sleep 2
sleep $TEST_TIMEOUT_LOW

run_helper sed -n '/ "Labels": {/, / },/p' ${PODMAN_TUI_LOG}
assert "$output" =~ "\"$TEST_LABEL_NAME\": \"$TEST_LABEL_VALUE\"" "expected \"$TEST_LABEL_NAME\": \"$TEST_LABEL_VALUE\" in volume inspect"
Expand All @@ -52,7 +52,7 @@ load helpers_tui
podman_tui_select_item $vol_index
podman_tui_select_volume_cmd "remove"
podman_tui_send_inputs "Enter"
sleep 2
sleep $TEST_TIMEOUT_LOW

run_helper podman volume ls --format "{{ .Name }}" --filter "name=${TEST_VOLUME_NAME}"
assert "$output" == "" "expected $TEST_VOLUME_NAME removed"
Expand All @@ -68,7 +68,7 @@ load helpers_tui
podman_tui_set_view "volumes"
podman_tui_select_volume_cmd "prune"
podman_tui_send_inputs "Enter"
sleep 2
sleep $TEST_TIMEOUT_LOW

run_helper podman volume ls --format "{{ .Name }}" --filter "name=${TEST_NETWORK_NAME}"
assert "$output" =~ "" "expected at least $TEST_VOLUME_NAME image removal"
Expand Down
18 changes: 9 additions & 9 deletions test/003-network.bats
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ load helpers_tui

podman_tui_set_view "networks"
podman_tui_select_network_cmd "connect"
sleep 2
sleep $TEST_TIMEOUT_LOW
podman_tui_send_inputs "Tab" "Tab"
podman_tui_send_inputs $TEST_NETWORK_CONNECT_ALIAS
podman_tui_send_inputs "Tab" "Tab" "Tab" "Tab"
podman_tui_send_inputs "Tab" "Enter"

sleep 2
sleep $TEST_TIMEOUT_LOW

run_helper podman container inspect $TEST_CONTAINER_NAME --format "\"{{ .NetworkSettings.Networks.$TEST_NETWORK_CONNECT }}\""
assert "$output" =~ "$TEST_NETWORK_CONNECT_ALIAS" "expected $TEST_NETWORK_CONNECT_ALIAS to be in the list of aliases"
Expand All @@ -39,7 +39,7 @@ load helpers_tui

podman_tui_set_view "networks"
podman_tui_select_network_cmd "disconnect"
sleep 2
sleep $TEST_TIMEOUT_LOW
podman_tui_send_inputs "Tab" "Tab" "Tab" "Enter"

run_helper podman container inspect $TEST_CONTAINER_NAME --format "{{ .NetworkSettings.Networks.$TEST_NETWORK_CONNECT }}"
Expand All @@ -60,9 +60,9 @@ load helpers_tui
podman_tui_send_inputs "Tab"
podman_tui_send_inputs "$TEST_LABEL"
podman_tui_send_inputs "Tab" "Tab" "Tab" "Tab" "Tab" "Enter"
sleep 1
sleep $TEST_TIMEOUT_LOW
podman_tui_send_inputs "Tab" "Enter"
sleep 2
sleep $TEST_TIMEOUT_LOW
run_helper podman network ls --format "{{ .Name }}" --filter "name=${TEST_NETWORK_NAME}$"
assert "$output" == "$TEST_NETWORK_NAME" "expected $TEST_NETWORK_NAME to be in the list"
}
Expand All @@ -77,9 +77,9 @@ load helpers_tui
podman_tui_set_view "networks"
podman_tui_select_item $net_index
podman_tui_select_network_cmd "inspect"
sleep 1
sleep $TEST_TIMEOUT_LOW
podman_tui_send_inputs "Enter"
sleep 2
sleep $TEST_TIMEOUT_LOW

run_helper sed -n '/ "labels": {/, / }/p' $PODMAN_TUI_LOG
assert "$output" =~ "\"$TEST_LABEL_NAME\": \"$TEST_LABEL_VALUE\"" "expected \"$TEST_LABEL_NAME\": \"$TEST_LABEL_VALUE\" in network inspect"
Expand All @@ -95,7 +95,7 @@ load helpers_tui
podman_tui_select_item $net_index
podman_tui_select_network_cmd "remove"
podman_tui_send_inputs "Enter"
sleep 2
sleep $TEST_TIMEOUT_LOW

run_helper podman network ls --format "{{ .Name }}" --filter "name=${TEST_NETWORK_NAME}$"
assert "$output" == "" "expected $TEST_NETWORK_NAME removed"
Expand All @@ -111,7 +111,7 @@ load helpers_tui
podman_tui_set_view "networks"
podman_tui_select_network_cmd "prune"
podman_tui_send_inputs "Enter"
sleep 2
sleep $TEST_TIMEOUT_LOW

run_helper podman network ls --format "{{ .Name }}" --filter "name=${TEST_NETWORK_NAME}$"
assert "$output" == "" "expected at least $TEST_NETWORK_NAME network removal"
Expand Down
Loading

0 comments on commit 5022bef

Please sign in to comment.