Skip to content

Commit

Permalink
Add restart-sec for container service files
Browse files Browse the repository at this point in the history
Attempts to fix containers#16419

podman generate systemd --restart-sec pod
^now generates RestartSec= both in pod service file and in container service file.

podman generate systemd --restart-sec container
^now generates RestartSec= in container service file.

Signed-off-by: Veronika Fuxova <[email protected]>
  • Loading branch information
Darth-Mera committed Nov 10, 2022
1 parent e86cef1 commit a2c43d4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/systemd/generate/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type containerInfo struct {
NotifyAccess string
StopTimeout uint
RestartPolicy string
RestartSec uint
StartLimitBurst string
PIDFile string
ContainerIDFile string
Expand Down Expand Up @@ -87,6 +88,9 @@ Environment={{{{- range $index, $value := .ExtraEnvs -}}}}{{{{if $index}}}} {{{{
Environment={{{{ $value }}}}{{{{end}}}}
{{{{- end}}}}
Restart={{{{.RestartPolicy}}}}
{{{{- if .RestartSec}}}}
RestartSec={{{{.RestartSec}}}}
{{{{- end}}}}
{{{{- if .StartLimitBurst}}}}
StartLimitBurst={{{{.StartLimitBurst}}}}
{{{{- end}}}}
Expand Down Expand Up @@ -282,6 +286,10 @@ func executeContainerTemplate(info *containerInfo, options entities.GenerateSyst
info.RestartPolicy = *options.RestartPolicy
}

if options.RestartSec != nil {
info.RestartSec = *options.RestartSec
}

// Make sure the executable is set.
if info.Executable == "" {
executable, err := os.Executable()
Expand Down
45 changes: 45 additions & 0 deletions pkg/systemd/generate/containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,32 @@ ExecStopPost=/usr/bin/podman rm \
Type=notify
NotifyAccess=all
[Install]
WantedBy=default.target
`
goodRestartSec := `# container-foobar.service
# autogenerated by Podman CI
[Unit]
Description=Podman container-foobar.service
Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage
[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure
RestartSec=15
TimeoutStopSec=70
ExecStart=/usr/bin/podman start foobar
ExecStop=/usr/bin/podman stop \
-t 10 foobar
ExecStopPost=/usr/bin/podman stop \
-t 10 foobar
PIDFile=/run/containers/storage/overlay-containers/639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401/userdata/conmon.pid
Type=forking
[Install]
WantedBy=default.target
`
Expand Down Expand Up @@ -1658,6 +1684,25 @@ WantedBy=default.target
false,
false,
},
{"good with RestartSec",
containerInfo{
Executable: "/usr/bin/podman",
ServiceName: "container-foobar",
ContainerNameOrID: "foobar",
PIDFile: "/run/containers/storage/overlay-containers/639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401/userdata/conmon.pid",
StopTimeout: 10,
PodmanVersion: "CI",
EnvVariable: define.EnvVariable,
GraphRoot: "/var/lib/containers/storage",
RunRoot: "/var/run/containers/storage",
RestartSec: 15,
},
goodRestartSec,
false,
false,
false,
false,
},
}
for _, tt := range tests {
test := tt
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/generate_systemd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,15 @@ var _ = Describe("Podman generate systemd", func() {

// Grepping the output (in addition to unit tests)
Expect(session.OutputToString()).To(ContainSubstring("RestartSec=15"))

n = podmanTest.Podman([]string{"create", "--name", "foocontainer", "alpine", "top"})
n.WaitWithDefaultTimeout()
Expect(n).Should(Exit(0))

session2 := podmanTest.Podman([]string{"generate", "systemd", "--restart-sec", "15", "--name", "foocontainer"})
session2.WaitWithDefaultTimeout()
Expect(session2).Should(Exit(0))
Expect(session2.OutputToString()).To(ContainSubstring("RestartSec=15"))
})

It("podman generate systemd --new=false pod", func() {
Expand Down

0 comments on commit a2c43d4

Please sign in to comment.