Skip to content

Commit d8d0913

Browse files
Merge pull request #26007 from ygalblum/quadlet-remove-cid
Quadlet - remove the usage of cid and podid for container and pod files
2 parents 4e3226c + 9b2fb40 commit d8d0913

File tree

7 files changed

+32
-31
lines changed

7 files changed

+32
-31
lines changed

cmd/quadlet/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,9 @@ func generateUnitsInfoMap(units []*parser.UnitFile) map[string]*quadlet.UnitInfo
604604
case strings.HasSuffix(unit.Filename, ".pod"):
605605
serviceName = quadlet.GetPodServiceName(unit)
606606
containers = make([]string, 0)
607+
// Prefill resouceNames for .pod files.
608+
// This is requires for referencing the pod from .container files
609+
resourceName = quadlet.GetPodResourceName(unit)
607610
default:
608611
Logf("Unsupported file type %q", unit.Filename)
609612
continue

pkg/systemd/quadlet/quadlet.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -608,16 +608,14 @@ func ConvertContainer(container *parser.UnitFile, isUser bool, unitsInfoMap map[
608608
// If conmon exited uncleanly it may not have removed the container, so
609609
// force it, -i makes it ignore non-existing files.
610610
serviceStopCmd := createBasePodmanCommand(container, ContainerGroup)
611-
serviceStopCmd.add("rm", "-v", "-f", "-i", "--cidfile=%t/%N.cid")
611+
serviceStopCmd.add("rm", "-v", "-f", "-i", containerName)
612612
service.AddCmdline(ServiceGroup, "ExecStop", serviceStopCmd.Args)
613613
// The ExecStopPost is needed when the main PID (i.e., conmon) gets killed.
614-
// In that case, ExecStop is not executed but *Post only. If both are
615-
// fired in sequence, *Post will exit when detecting that the --cidfile
616-
// has already been removed by the previous `rm`..
614+
// In that case, ExecStop is not executed but *Post only.
617615
serviceStopCmd.Args[0] = fmt.Sprintf("-%s", serviceStopCmd.Args[0])
618616
service.AddCmdline(ServiceGroup, "ExecStopPost", serviceStopCmd.Args)
619617

620-
if err := handleExecReload(container, service, ContainerGroup); err != nil {
618+
if err := handleExecReload(container, service, ContainerGroup, containerName); err != nil {
621619
return nil, warnings, err
622620
}
623621

@@ -628,9 +626,6 @@ func ConvertContainer(container *parser.UnitFile, isUser bool, unitsInfoMap map[
628626
podman.add("--name", containerName)
629627

630628
podman.add(
631-
// We store the container id so we can clean it up in case of failure
632-
"--cidfile=%t/%N.cid",
633-
634629
// And replace any previous container with the same name, not fail
635630
"--replace",
636631

@@ -1483,6 +1478,15 @@ func getServiceName(quadletUnitFile *parser.UnitFile, groupName string, defaultE
14831478
return removeExtension(quadletUnitFile.Filename, "", defaultExtraSuffix)
14841479
}
14851480

1481+
func GetPodResourceName(podUnit *parser.UnitFile) string {
1482+
// Derive pod name from unit name (with added prefix), or use user-provided name.
1483+
podName, ok := podUnit.Lookup(PodGroup, KeyPodName)
1484+
if !ok || len(podName) == 0 {
1485+
podName = removeExtension(podUnit.Filename, "systemd-", "")
1486+
}
1487+
return podName
1488+
}
1489+
14861490
func ConvertPod(podUnit *parser.UnitFile, name string, unitsInfoMap map[string]*UnitInfo, isUser bool) (*parser.UnitFile, error, error) {
14871491
var warn, warnings error
14881492

@@ -1491,11 +1495,7 @@ func ConvertPod(podUnit *parser.UnitFile, name string, unitsInfoMap map[string]*
14911495
return nil, warnings, err
14921496
}
14931497

1494-
// Derive pod name from unit name (with added prefix), or use user-provided name.
1495-
podName, ok := podUnit.Lookup(PodGroup, KeyPodName)
1496-
if !ok || len(podName) == 0 {
1497-
podName = removeExtension(name, "systemd-", "")
1498-
}
1498+
podName := GetPodResourceName(podUnit)
14991499

15001500
for _, containerService := range unitInfo.ContainersToStart {
15011501
service.Add(UnitGroup, "Wants", containerService)
@@ -1507,32 +1507,31 @@ func ConvertPod(podUnit *parser.UnitFile, name string, unitsInfoMap map[string]*
15071507
}
15081508

15091509
execStart := createBasePodmanCommand(podUnit, PodGroup)
1510-
execStart.add("pod", "start", "--pod-id-file=%t/%N.pod-id")
1510+
execStart.add("pod", "start", podName)
15111511
service.AddCmdline(ServiceGroup, "ExecStart", execStart.Args)
15121512

15131513
execStop := createBasePodmanCommand(podUnit, PodGroup)
15141514
execStop.add("pod", "stop")
15151515
execStop.add(
1516-
"--pod-id-file=%t/%N.pod-id",
15171516
"--ignore",
15181517
"--time=10",
1518+
podName,
15191519
)
15201520
service.AddCmdline(ServiceGroup, "ExecStop", execStop.Args)
15211521

15221522
execStopPost := createBasePodmanCommand(podUnit, PodGroup)
15231523
execStopPost.add("pod", "rm")
15241524
execStopPost.add(
1525-
"--pod-id-file=%t/%N.pod-id",
15261525
"--ignore",
15271526
"--force",
1527+
podName,
15281528
)
15291529
service.AddCmdline(ServiceGroup, "ExecStopPost", execStopPost.Args)
15301530

15311531
execStartPre := createBasePodmanCommand(podUnit, PodGroup)
15321532
execStartPre.add("pod", "create")
15331533
execStartPre.add(
15341534
"--infra-conmon-pidfile=%t/%N.pid",
1535-
"--pod-id-file=%t/%N.pod-id",
15361535
"--exit-policy=stop",
15371536
"--replace",
15381537
)
@@ -2075,7 +2074,7 @@ func handlePod(quadletUnitFile, serviceUnitFile *parser.UnitFile, groupName stri
20752074
return fmt.Errorf("quadlet pod unit %s does not exist", pod)
20762075
}
20772076

2078-
podman.add("--pod-id-file", fmt.Sprintf("%%t/%s.pod-id", podInfo.ServiceName))
2077+
podman.add("--pod", podInfo.ResourceName)
20792078

20802079
podServiceName := podInfo.ServiceFileName()
20812080
serviceUnitFile.Add(UnitGroup, "BindsTo", podServiceName)
@@ -2146,7 +2145,7 @@ func addDefaultDependencies(service *parser.UnitFile, isUser bool) {
21462145
}
21472146
}
21482147

2149-
func handleExecReload(quadletUnitFile, serviceUnitFile *parser.UnitFile, groupName string) error {
2148+
func handleExecReload(quadletUnitFile, serviceUnitFile *parser.UnitFile, groupName, containerName string) error {
21502149
reloadSignal, signalOk := quadletUnitFile.Lookup(groupName, KeyReloadSignal)
21512150
signalOk = signalOk && len(reloadSignal) > 0
21522151
reloadcmd, cmdOk := quadletUnitFile.LookupLastArgs(groupName, KeyReloadCmd)
@@ -2162,10 +2161,10 @@ func handleExecReload(quadletUnitFile, serviceUnitFile *parser.UnitFile, groupNa
21622161

21632162
serviceReloadCmd := createBasePodmanCommand(quadletUnitFile, groupName)
21642163
if cmdOk {
2165-
serviceReloadCmd.add("exec", "--cidfile=%t/%N.cid")
2164+
serviceReloadCmd.add("exec", containerName)
21662165
serviceReloadCmd.add(reloadcmd...)
21672166
} else {
2168-
serviceReloadCmd.add("kill", "--cidfile=%t/%N.cid", "--signal", reloadSignal)
2167+
serviceReloadCmd.add("kill", "--signal", reloadSignal, containerName)
21692168
}
21702169
serviceUnitFile.AddCmdline(ServiceGroup, "ExecReload", serviceReloadCmd.Args)
21712170

test/e2e/quadlet/basepodman.container

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## assert-podman-final-args run --name systemd-%N --cidfile=%t/%N.cid --replace --rm --cgroups=split --sdnotify=conmon -d localhost/imagename
1+
## assert-podman-final-args run --name systemd-%N --replace --rm --cgroups=split --sdnotify=conmon -d localhost/imagename
22

33
[Container]
44
Image=localhost/imagename

test/e2e/quadlet/basic.container

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
## assert-podman-final-args localhost/imagename
22
## assert-podman-args "--name" "systemd-%N"
3-
## assert-podman-args "--cidfile=%t/%N.cid"
43
## assert-podman-args "--rm"
54
## assert-podman-args "--replace"
65
## assert-podman-args "-d"
@@ -12,8 +11,8 @@
1211
## assert-key-is "Service" "Type" "notify"
1312
## assert-key-is "Service" "NotifyAccess" "all"
1413
## assert-key-is "Service" "SyslogIdentifier" "%N"
15-
## assert-key-is-regex "Service" "ExecStopPost" "-[/S].*/podman rm -v -f -i --cidfile=%t/%N.cid"
16-
## assert-key-is-regex "Service" "ExecStop" ".*/podman rm -v -f -i --cidfile=%t/%N.cid"
14+
## assert-key-is-regex "Service" "ExecStopPost" "-[/S].*/podman rm -v -f -i systemd-%N"
15+
## assert-key-is-regex "Service" "ExecStop" ".*/podman rm -v -f -i systemd-%N"
1716
## assert-key-is "Service" "Environment" "PODMAN_SYSTEMD_UNIT=%n"
1817
## assert-key-is-regex "Unit" "After" "network-online.target|podman-user-wait-network-online.service"
1918
## assert-key-is-regex "Unit" "Wants" "network-online.target|podman-user-wait-network-online.service"

test/e2e/quadlet/basic.pod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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 --pod-id-file=%t/%N.pod-id --exit-policy=stop --replace --infra-name systemd-basic-infra --name systemd-basic"
5-
## assert-key-is-regex Service ExecStart ".*/podman pod start --pod-id-file=%t/%N.pod-id"
6-
## assert-key-is-regex Service ExecStop ".*/podman pod stop --pod-id-file=%t/%N.pod-id --ignore --time=10"
7-
## assert-key-is-regex Service ExecStopPost ".*/podman pod rm --pod-id-file=%t/%N.pod-id --ignore --force"
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"
5+
## assert-key-is-regex Service ExecStart ".*/podman pod start systemd-basic"
6+
## assert-key-is-regex Service ExecStop ".*/podman pod stop --ignore --time=10 systemd-basic"
7+
## assert-key-is-regex Service ExecStopPost ".*/podman pod rm --ignore --force systemd-basic"
88

99
[Pod]

test/e2e/quadlet/reloadcmd.container

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## assert-podman-reload-args "exec"
2-
## assert-podman-reload-args "--cidfile=%t/%N.cid"
2+
## assert-podman-reload-args "systemd-%N"
33
## assert-podman-reload-final-args "/some/binary file" "--arg1" "arg 2"
44

55
[Container]

test/e2e/quadlet/reloadsignal.container

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## assert-podman-reload-args "kill"
2-
## assert-podman-reload-args "--cidfile=%t/%N.cid"
2+
## assert-podman-reload-args "systemd-%N"
33
## assert-podman-reload-args "--signal" "SIGHUP"
44

55
[Container]

0 commit comments

Comments
 (0)