Skip to content

Commit

Permalink
generate systemd: fix pod dependencies
Browse files Browse the repository at this point in the history
Change the dependencies from a pod unit to its associated container
units from `Requires` to `Wants` to prevent the entire pod from
transitioning to a failed state.  Restart policies for individual
containers can be configured separately.

Also make sure that the pod's RunRoot is always set.

Fixes: containers#14546
Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg committed Sep 13, 2022
1 parent 1b3e863 commit fe04c86
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
3 changes: 2 additions & 1 deletion pkg/systemd/generate/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type podInfo struct {
Requires []string
}

const podTemplate = headerTemplate + `Requires={{{{- range $index, $value := .RequiredServices -}}}}{{{{if $index}}}} {{{{end}}}}{{{{ $value }}}}.service{{{{end}}}}
const podTemplate = headerTemplate + `Wants={{{{- range $index, $value := .RequiredServices -}}}}{{{{if $index}}}} {{{{end}}}}{{{{ $value }}}}.service{{{{end}}}}
Before={{{{- range $index, $value := .RequiredServices -}}}}{{{{if $index}}}} {{{{end}}}}{{{{ $value }}}}.service{{{{end}}}}
{{{{- if or .Wants .After .Requires }}}}
Expand Down Expand Up @@ -252,6 +252,7 @@ func generatePodInfo(pod *libpod.Pod, options entities.GenerateSystemdOptions) (
StopTimeout: stopTimeout,
GenerateTimestamp: true,
CreateCommand: createCommand,
RunRoot: infraCtr.Runtime().RunRoot(),
}
return &info, nil
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/systemd/generate/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage
Requires=container-1.service container-2.service
Wants=container-1.service container-2.service
Before=container-1.service container-2.service
[Service]
Expand Down Expand Up @@ -98,7 +98,7 @@ Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage
Requires=container-1.service container-2.service
Wants=container-1.service container-2.service
Before=container-1.service container-2.service
[Service]
Expand All @@ -124,7 +124,7 @@ Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage
Requires=container-1.service container-2.service
Wants=container-1.service container-2.service
Before=container-1.service container-2.service
# User-defined dependencies
Expand Down Expand Up @@ -152,7 +152,7 @@ Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage
Requires=container-1.service container-2.service
Wants=container-1.service container-2.service
Before=container-1.service container-2.service
# User-defined dependencies
Expand Down Expand Up @@ -180,7 +180,7 @@ Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage
Requires=container-1.service container-2.service
Wants=container-1.service container-2.service
Before=container-1.service container-2.service
# User-defined dependencies
Expand Down Expand Up @@ -208,7 +208,7 @@ Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage
Requires=container-1.service container-2.service
Wants=container-1.service container-2.service
Before=container-1.service container-2.service
# User-defined dependencies
Expand Down Expand Up @@ -239,7 +239,7 @@ Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage
Requires=container-1.service container-2.service
Wants=container-1.service container-2.service
Before=container-1.service container-2.service
[Service]
Expand All @@ -266,7 +266,7 @@ Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage
Requires=container-1.service container-2.service
Wants=container-1.service container-2.service
Before=container-1.service container-2.service
[Service]
Expand Down Expand Up @@ -294,7 +294,7 @@ Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage
Requires=container-1.service container-2.service
Wants=container-1.service container-2.service
Before=container-1.service container-2.service
[Service]
Expand Down Expand Up @@ -322,7 +322,7 @@ Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage
Requires=container-1.service container-2.service
Wants=container-1.service container-2.service
Before=container-1.service container-2.service
[Service]
Expand Down Expand Up @@ -350,7 +350,7 @@ Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage
Requires=container-1.service container-2.service
Wants=container-1.service container-2.service
Before=container-1.service container-2.service
[Service]
Expand Down
36 changes: 19 additions & 17 deletions test/e2e/generate_systemd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package integration
import (
"io/ioutil"
"os"
"strings"

. "github.com/containers/podman/v4/test/utils"
. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -220,19 +221,20 @@ var _ = Describe("Podman generate systemd", func() {
Expect(session).Should(Exit(0))

// Grepping the output (in addition to unit tests)
Expect(session.OutputToString()).To(ContainSubstring("# pod-foo.service"))
Expect(session.OutputToString()).To(ContainSubstring("Requires=container-foo-1.service container-foo-2.service"))
Expect(session.OutputToString()).To(ContainSubstring("# container-foo-1.service"))
Expect(session.OutputToString()).To(ContainSubstring(" start foo-1"))
Expect(session.OutputToString()).To(ContainSubstring("-infra")) // infra container
Expect(session.OutputToString()).To(ContainSubstring("# container-foo-2.service"))
Expect(session.OutputToString()).To(ContainSubstring(" stop -t 42 foo-2"))
Expect(session.OutputToString()).To(ContainSubstring("BindsTo=pod-foo.service"))
Expect(session.OutputToString()).To(ContainSubstring("PIDFile="))
Expect(session.OutputToString()).To(ContainSubstring("/userdata/conmon.pid"))

output := session.OutputToString()
Expect(output).To(ContainSubstring("# pod-foo.service"))
Expect(output).To(ContainSubstring("Wants=container-foo-1.service container-foo-2.service"))
Expect(output).To(ContainSubstring("# container-foo-1.service"))
Expect(output).To(ContainSubstring(" start foo-1"))
Expect(output).To(ContainSubstring("-infra")) // infra container
Expect(output).To(ContainSubstring("# container-foo-2.service"))
Expect(output).To(ContainSubstring(" stop -t 42 foo-2"))
Expect(output).To(ContainSubstring("BindsTo=pod-foo.service"))
Expect(output).To(ContainSubstring("PIDFile="))
Expect(output).To(ContainSubstring("/userdata/conmon.pid"))
Expect(strings.Count(output, "RequiresMountsFor="+podmanTest.RunRoot)).To(Equal(3))
// The podman commands in the unit should not contain the root flags
Expect(session.OutputToString()).ToNot(ContainSubstring(" --runroot"))
Expect(output).ToNot(ContainSubstring(" --runroot"))
})

It("podman generate systemd pod --name --files", func() {
Expand Down Expand Up @@ -468,7 +470,7 @@ var _ = Describe("Podman generate systemd", func() {

// Grepping the output (in addition to unit tests)
Expect(session.OutputToString()).To(ContainSubstring("# p-foo.service"))
Expect(session.OutputToString()).To(ContainSubstring("Requires=container-foo-1.service container-foo-2.service"))
Expect(session.OutputToString()).To(ContainSubstring("Wants=container-foo-1.service container-foo-2.service"))
Expect(session.OutputToString()).To(ContainSubstring("# container-foo-1.service"))
Expect(session.OutputToString()).To(ContainSubstring("BindsTo=p-foo.service"))
})
Expand All @@ -492,7 +494,7 @@ var _ = Describe("Podman generate systemd", func() {

// Grepping the output (in addition to unit tests)
Expect(session.OutputToString()).To(ContainSubstring("# p_foo.service"))
Expect(session.OutputToString()).To(ContainSubstring("Requires=con_foo-1.service con_foo-2.service"))
Expect(session.OutputToString()).To(ContainSubstring("Wants=con_foo-1.service con_foo-2.service"))
Expect(session.OutputToString()).To(ContainSubstring("# con_foo-1.service"))
Expect(session.OutputToString()).To(ContainSubstring("# con_foo-2.service"))
Expect(session.OutputToString()).To(ContainSubstring("BindsTo=p_foo.service"))
Expand All @@ -518,7 +520,7 @@ var _ = Describe("Podman generate systemd", func() {

// Grepping the output (in addition to unit tests)
Expect(session1.OutputToString()).To(ContainSubstring("# foo.service"))
Expect(session1.OutputToString()).To(ContainSubstring("Requires=container-foo-1.service container-foo-2.service"))
Expect(session1.OutputToString()).To(ContainSubstring("Wants=container-foo-1.service container-foo-2.service"))
Expect(session1.OutputToString()).To(ContainSubstring("# container-foo-1.service"))
Expect(session1.OutputToString()).To(ContainSubstring("BindsTo=foo.service"))

Expand All @@ -529,7 +531,7 @@ var _ = Describe("Podman generate systemd", func() {

// Grepping the output (in addition to unit tests)
Expect(session2.OutputToString()).To(ContainSubstring("# foo.service"))
Expect(session2.OutputToString()).To(ContainSubstring("Requires=foo-1.service foo-2.service"))
Expect(session2.OutputToString()).To(ContainSubstring("Wants=foo-1.service foo-2.service"))
Expect(session2.OutputToString()).To(ContainSubstring("# foo-1.service"))
Expect(session2.OutputToString()).To(ContainSubstring("# foo-2.service"))
Expect(session2.OutputToString()).To(ContainSubstring("BindsTo=foo.service"))
Expand Down Expand Up @@ -560,7 +562,7 @@ var _ = Describe("Podman generate systemd", func() {

// Grepping the output (in addition to unit tests)
Expect(session.OutputToString()).To(ContainSubstring("# pod-foo.service"))
Expect(session.OutputToString()).To(ContainSubstring("Requires=container-foo-1.service container-foo-2.service"))
Expect(session.OutputToString()).To(ContainSubstring("Wants=container-foo-1.service container-foo-2.service"))
Expect(session.OutputToString()).To(ContainSubstring("BindsTo=pod-foo.service"))
Expect(session.OutputToString()).To(ContainSubstring("pod create --infra-conmon-pidfile %t/pod-foo.pid --pod-id-file %t/pod-foo.pod-id --name foo"))
Expect(session.OutputToString()).To(ContainSubstring("ExecStartPre=/bin/rm -f %t/pod-foo.pid %t/pod-foo.pod-id"))
Expand Down

0 comments on commit fe04c86

Please sign in to comment.