Skip to content

Commit 617cdc4

Browse files
Merge pull request #26466 from nbspsemicolon/quadlet-pod-exitpolicy
pkg/systemd: expose [Pod] ExitPolicy key for pod create --exit-policy
2 parents 5c89b64 + 5989370 commit 617cdc4

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

docs/source/markdown/podman-pod-create.1.md.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Set the exit policy of the pod when the last container exits. Supported policie
7676
| Exit Policy | Description |
7777
| ------------------ | -------------------------------------------------------------------------------------------------------------------------- |
7878
| *continue* | The pod continues running, by keeping its infra container alive, when the last container exits. Used by default. |
79-
| *stop* | The pod (including its infra container) is stopped when the last container exits. Used in `kube play`. |
79+
| *stop* | The pod (including its infra container) is stopped when the last container exits. Used in `kube play` and quadlets. |
8080

8181
@@option gidmap.pod
8282

docs/source/markdown/podman-systemd.unit.5.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@ Valid options for `[Pod]` are listed below:
10061006
| DNS=192.168.55.1 | --dns=192.168.55.1 |
10071007
| DNSOption=ndots:1 | --dns-option=ndots:1 |
10081008
| DNSSearch=example.com | --dns-search example.com |
1009+
| ExitPolicy=stop | --exit-policy stop |
10091010
| GIDMap=0:10000:10 | --gidmap=0:10000:10 |
10101011
| GlobalArgs=--log-level=debug | --log-level=debug |
10111012
| HostName=name | --hostname=name |
@@ -1059,6 +1060,12 @@ Set custom DNS search domains. Use **DNSSearch=.** to remove the search domain.
10591060

10601061
This key can be listed multiple times.
10611062

1063+
### `ExitPolicy=`
1064+
1065+
Set the exit policy of the pod when the last container exits. Default for quadlets is **stop**.
1066+
1067+
To keep the pod active, set `ExitPolicy=continue`.
1068+
10621069
### `GIDMap=`
10631070

10641071
Create the pod in a new user namespace using the supplied GID mapping.
@@ -2175,6 +2182,39 @@ Exec=sh -c "sleep inf"
21752182
Pod=test.pod
21762183
```
21772184

2185+
Example for a Pod with a oneshot Startup Task:
2186+
2187+
`test.pod`
2188+
```
2189+
[Pod]
2190+
PodName=test
2191+
ExitPolicy=continue
2192+
```
2193+
2194+
`startup-task.container`
2195+
```
2196+
[Service]
2197+
Type=oneshot
2198+
RemainAfterExit=yes
2199+
2200+
[Container]
2201+
Pod=test.pod
2202+
Image=quay.io/centos/centos:latest
2203+
Exec=sh -c "echo 'setup starting'; sleep 2; echo 'setup complete'"
2204+
```
2205+
2206+
`app.container`
2207+
```
2208+
[Unit]
2209+
Requires=startup-task.container
2210+
After=startup-task.container
2211+
2212+
[Container]
2213+
Pod=test.pod
2214+
Image=quay.io/centos/centos:latest
2215+
Exec=sh -c "echo 'app running.'; sleep 30"
2216+
```
2217+
21782218
Example `s3fs.volume`:
21792219

21802220
For further details, please see the [s3fs-fuse](https://github.com/s3fs-fuse/s3fs-fuse) project.

pkg/systemd/quadlet/quadlet.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const (
8686
KeyEnvironmentHost = "EnvironmentHost"
8787
KeyExec = "Exec"
8888
KeyExitCodePropagation = "ExitCodePropagation"
89+
KeyExitPolicy = "ExitPolicy"
8990
KeyExposeHostPort = "ExposeHostPort"
9091
KeyFile = "File"
9192
KeyForceRM = "ForceRM"
@@ -472,6 +473,7 @@ var (
472473
KeyDNS: true,
473474
KeyDNSOption: true,
474475
KeyDNSSearch: true,
476+
KeyExitPolicy: true,
475477
KeyGIDMap: true,
476478
KeyGlobalArgs: true,
477479
KeyHostName: true,
@@ -1538,10 +1540,11 @@ func ConvertPod(podUnit *parser.UnitFile, name string, unitsInfoMap map[string]*
15381540
execStartPre.add("pod", "create")
15391541
execStartPre.add(
15401542
"--infra-conmon-pidfile=%t/%N.pid",
1541-
"--exit-policy=stop",
15421543
"--replace",
15431544
)
15441545

1546+
handleExitPolicy(podUnit, PodGroup, execStartPre)
1547+
15451548
if err := handleUserMappings(podUnit, PodGroup, execStartPre, true); err != nil {
15461549
return nil, warnings, err
15471550
}
@@ -1800,6 +1803,17 @@ func getAbsolutePath(quadletUnitFile *parser.UnitFile, filePath string) (string,
18001803
return filePath, nil
18011804
}
18021805

1806+
func handleExitPolicy(unitFile *parser.UnitFile, groupName string, podman *PodmanCmdline) {
1807+
exitPolicy, found := unitFile.Lookup(groupName, KeyExitPolicy)
1808+
1809+
podman.add("--exit-policy")
1810+
if found {
1811+
podman.add(exitPolicy)
1812+
} else {
1813+
podman.add("stop")
1814+
}
1815+
}
1816+
18031817
func handlePublishPorts(unitFile *parser.UnitFile, groupName string, podman *PodmanCmdline) {
18041818
publishPorts := unitFile.LookupAll(groupName, KeyPublishPort)
18051819
for _, publishPort := range publishPorts {

test/e2e/quadlet/basic.pod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## assert-key-is Unit RequiresMountsFor "%t/containers"
22
## assert-key-is Service Type forking
33
## assert-key-is Service SyslogIdentifier "%N"
4-
## assert-key-is-regex Service ExecStartPre ".*/podman pod create --infra-conmon-pidfile=%t/%N.pid --exit-policy=stop --replace --infra-name systemd-basic-infra --name systemd-basic"
4+
## assert-key-is-regex Service ExecStartPre ".*/podman pod create --infra-conmon-pidfile=%t/%N.pid --replace --exit-policy stop --infra-name systemd-basic-infra --name systemd-basic"
55
## assert-key-is-regex Service ExecStart ".*/podman pod start systemd-basic"
66
## assert-key-is-regex Service ExecStop ".*/podman pod stop --ignore --time=10 systemd-basic"
77
## assert-key-is-regex Service ExecStopPost ".*/podman pod rm --ignore --force systemd-basic"

test/system/252-quadlet.bats

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,11 @@ EOF
16931693
# Pod should exist
16941694
run_podman pod exists ${test_pod_name}
16951695

1696+
# Pod exit policy should be stop by default
1697+
run_podman pod inspect --format "{{.ExitPolicy}}" ${test_pod_name}
1698+
assert "$output" = "stop" \
1699+
"quadlet - pod: default ExitPolicy should be stop"
1700+
16961701
# Wait for systemd to activate the container service
16971702
wait_for_command_output "systemctl show --property=ActiveState $container_service" "ActiveState=active"
16981703

0 commit comments

Comments
 (0)